cirandas.net

ref: master

plugins/stoa/test/functional/stoa_plugin_controller_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'test_helper'
require_relative '../../controllers/stoa_plugin_controller'

class StoaPluginControllerTest < ActionController::TestCase

  SALT=YAML::load(File.open(StoaPlugin.root_path + 'config.yml'))['salt']

  def setup
    @controller = StoaPluginController.new
    ApplicationRecord.configurations['stoa'] = {:adapter => 'sqlite3', :database => ':memory:', :verbosity => 'quiet'}
    env = Environment.default
    env.enable_plugin(StoaPlugin.name)
    env.enable('skip_new_user_email_confirmation')
    env.save!
    @user = create_user_full('real_user', {:password => '123456', :password_confirmation => '123456'}, {:usp_id => 9999999})
    @user.activate
  end

  attr_accessor :user

  should 'not authenticate if method not post' do
    get :authenticate, :login => user.login, :password => '123456'

    assert_not_nil json_response['error']
    assert_match /post method/,json_response['error']
  end

  should 'not authenticate if method password is wrong' do
    post :authenticate, :login => user.login, :password => 'wrong_password'

    assert_not_nil json_response['error']
    assert_match /password/,json_response['error']
  end

  should 'authenticate if everything is right' do
    post :authenticate, :login => user.login, :password => '123456'

    assert_nil json_response['error']
    assert_equal user.login, json_response['username']
  end

  should 'authenticate with usp_id' do
    post :authenticate, :usp_id => user.person.usp_id.to_s, :password => '123456'

    assert_nil json_response['error']
    assert_equal user.login, json_response['username']
  end

  should 'return no fields if fields requested was none' do
    post :authenticate, :login => user.login, :password => '123456', :fields => 'none'

    expected_response = {'ok' => true}

    assert_nil json_response['error']
    assert_equal expected_response, json_response
  end

  should 'return only the essential fields if no fields requested' do
    post :authenticate, :login => user.login, :password => '123456'
    response = json_response.clone

    assert_nil response['error']
    assert_equal true, response.delete('ok')
    assert_equal user.login, response.delete('username')
    assert_equal user.email, response.delete('email')
    assert_equal user.person.usp_id.to_s, response.delete('nusp')
    assert response.blank?
  end

  should 'return only selected fields' do
    Person.any_instance.stubs(:f1).returns('field1')
    Person.any_instance.stubs(:f2).returns('field2')
    Person.any_instance.stubs(:f3).returns('field3')
    @controller.stubs(:selected_fields).returns(%w[f1 f2 f3])

    post :authenticate, :login => user.login, :password => '123456', :fields => 'special'
    response = json_response.clone

    assert_equal true, response.delete('ok')
    assert_equal 'field1', response.delete('f1')
    assert_equal 'field2', response.delete('f2')
    assert_equal 'field3', response.delete('f3')
    assert response.blank?
  end

  should 'not return private fields' do
    Person.any_instance.stubs(:f1).returns('field1')
    Person.any_instance.stubs(:f2).returns('field2')
    Person.any_instance.stubs(:f3).returns('field3')
    StoaPluginController::FIELDS['special'] = %w[f1 f2 f3]
    person = user.person
    person.fields_privacy = {:f1 => 'private', :f2 => 'public', :f3 => 'public'}
    person.save!

    post :authenticate, :login => user.login, :password => '123456', :fields => 'special'

    refute json_response.keys.include?('f1')
    assert json_response.keys.include?('f2')
    assert json_response.keys.include?('f3')
  end

  should 'return essential fields even if they are private' do
    person = user.person
    person.fields_privacy = {:email => 'private'}
    person.save!

    post :authenticate, :login => user.login, :password => '123456'

    assert json_response.keys.include?('email')
  end

  should 'return only essential fields when profile is private' do
    Person.any_instance.stubs(:f1).returns('field1')
    Person.any_instance.stubs(:f2).returns('field2')
    Person.any_instance.stubs(:f3).returns('field3')
    StoaPluginController::FIELDS['special'] = %w[f1 f2 f3] + StoaPluginController::FIELDS['essential']
    person = user.person
    person.public_profile = false
    person.save!

    post :authenticate, :login => user.login, :password => '123456', :fields => 'special'
    response = json_response.clone

    assert_nil response['error']
    assert_equal true, response.delete('ok')
    assert_equal user.login, response.delete('username')
    assert_equal user.email, response.delete('email')
    assert_equal user.person.usp_id.to_s, response.delete('nusp')
    assert response.blank?
  end

  should 'not crash if usp_id is invalid' do
    assert_nothing_raised do
      post :authenticate, :usp_id => 12321123, :password => '123456'
    end
    assert_not_nil json_response['error']
    assert_match /user/,json_response['error']
  end

  should 'check valid usp id' do
    usp_id = '12345678'
    StoaPlugin::UspUser.stubs(:exists?).with(usp_id).returns(true)
    get :check_usp_id, :usp_id => usp_id
    assert json_response['exists']
  end

  should 'check invalid usp id' do
    usp_id = '87654321'
    StoaPlugin::UspUser.stubs(:exists?).with(usp_id).returns(false)
    get :check_usp_id, :usp_id => usp_id
    refute json_response['exists']
  end

  should 'check existent cpf' do
    usp_id = '12345678'
    user = mock
    user.stubs(:cpf).returns('12345678')
    StoaPlugin::UspUser.stubs(:find_by).with(codpes: usp_id).returns(user)
    get :check_cpf, :usp_id => usp_id
    assert json_response['exists']
  end

  should 'check not existent cpf' do
    usp_id_with_cpf = '12345678'
    user_with_cpf = mock
    user_with_cpf.stubs(:cpf).returns('12345678')
    StoaPlugin::UspUser.stubs(:find_by).with(codpes: usp_id_with_cpf).returns(user_with_cpf)
    get :check_cpf, :usp_id => usp_id_with_cpf
    usp_id_without_cpf = '87654321'
    user_without_cpf = mock
    user_with_cpf.stubs(:cpf).returns(nil)
    StoaPlugin::UspUser.stubs(:find_by).with(codpes: usp_id_without_cpf).returns(user_without_cpf)
    get :check_cpf, :usp_id => usp_id_without_cpf
    refute json_response['exists']
  end

  private

  def json_response
    ActiveSupport::JSON.decode @response.body
  end

end