stoa_plugin_controller_test.rb
2.3 KB
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
require File.dirname(__FILE__) + '/../../../../test/test_helper'
require File.dirname(__FILE__) + '/../../controllers/stoa_plugin_controller'
# Re-raise errors caught by the controller.
class StoaPluginController; def rescue_action(e) raise e end; end
class StoaPluginControllerTest < ActionController::TestCase
def setup
@controller = StoaPluginController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@user = create_user('real_user', :password => '123456', :password_confirmation => '123456')
environment = Environment.default
environment.enabled_plugins = ['StoaPlugin']
environment.save!
@db = Tempfile.new('stoa-test')
configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path}
end
attr_accessor :user
should 'not authenticate if method not post' do
@request.stubs(:ssl?).returns(true)
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 request is not using ssl' do
@request.stubs(:ssl?).returns(false)
post :authenticate, :login => user.login, :password => '123456'
assert_not_nil json_response['error']
assert_match /SSL/,json_response['error']
end
should 'not authenticate if method password is wrong' do
@request.stubs(:ssl?).returns(true)
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
@request.stubs(:ssl?).returns(true)
post :authenticate, :login => user.login, :password => '123456'
assert_nil json_response['error']
assert_equal user.login, json_response['username']
end
should 'check invalid usp id' do
StoaPlugin::UspUser.stubs(:exists?).returns(false)
get :check_usp_id, :usp_id => '987654321'
assert !json_response['exists']
end
should 'check valid usp id' do
StoaPlugin::UspUser.stubs(:exists?).returns(true)
get :check_usp_id, :usp_id => '987654321'
assert json_response['exists']
end
private
def json_response
ActiveSupport::JSON.decode @response.body
end
end