|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe CASServer::Authenticators::SQL do |
| 4 | + let(:options) do |
| 5 | + { |
| 6 | + auth_index: 0, |
| 7 | + user_table: 'users', |
| 8 | + username_column: 'username', |
| 9 | + password_column: 'password', |
| 10 | + database: { |
| 11 | + adapter: 'mysql2', |
| 12 | + database: 'casserver', |
| 13 | + username: 'root', |
| 14 | + password: 'password', |
| 15 | + host: 'localhost' |
| 16 | + } |
| 17 | + } |
| 18 | + end |
| 19 | + let(:connection) { double('Connection', run_callbacks: nil) } |
| 20 | + let(:connection_pool) { double('ConnectionPool', |
| 21 | + connections: [connection], |
| 22 | + checkin: nil) } |
| 23 | + |
| 24 | + before do |
| 25 | + load_server('default_config') if $LOG.nil? # ensure logger is present |
| 26 | + ActiveRecord::Base.stub(:establish_connection) |
| 27 | + ActiveRecord::Base.stub(:connection).and_return(connection) |
| 28 | + ActiveRecord::Base.stub(:connection_pool).and_return(connection_pool) |
| 29 | + CASServer::Authenticators::SQL.setup(options) |
| 30 | + end |
| 31 | + |
| 32 | + describe '#validate' do |
| 33 | + let(:auth) { CASServer::Authenticators::SQL.new } |
| 34 | + let(:username) { 'dave' } |
| 35 | + let(:password) { 'secret' } |
| 36 | + let(:user_model) { CASServer::Authenticators::SQL.user_models[0] } |
| 37 | + |
| 38 | + before do |
| 39 | + auth.configure(HashWithIndifferentAccess.new(options)) |
| 40 | + end |
| 41 | + |
| 42 | + context 'when credentials match a user in the database' do |
| 43 | + it 'returns true' do |
| 44 | + conditions = ['username = ? AND password = ?', username, password] |
| 45 | + user_model.should_receive(:find).with(:all, conditions: conditions) |
| 46 | + .and_return([:user]) |
| 47 | + credentials = { |
| 48 | + username: username, |
| 49 | + password: password |
| 50 | + } |
| 51 | + expect(auth.validate(credentials)).to be true |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when credentials do not match a user in the database' do |
| 56 | + it 'returns false' do |
| 57 | + conditions = ['username = ? AND password = ?', username, password] |
| 58 | + user_model.should_receive(:find).with(:all, conditions: conditions) |
| 59 | + .and_return([]) |
| 60 | + credentials = { |
| 61 | + username: username, |
| 62 | + password: password |
| 63 | + } |
| 64 | + expect(auth.validate(credentials)).to be false |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'when many SQL authenticators have been setup' do |
| 69 | + let(:alt_options) do |
| 70 | + { |
| 71 | + auth_index: 1, |
| 72 | + user_table: 'users', |
| 73 | + username_column: 'username', |
| 74 | + password_column: 'password', |
| 75 | + database: { |
| 76 | + adapter: 'mysql2', |
| 77 | + database: 'casserver', |
| 78 | + username: 'root', |
| 79 | + password: 'password', |
| 80 | + host: 'localhost' |
| 81 | + } |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + before do |
| 86 | + CASServer::Authenticators::SQL.setup(alt_options) |
| 87 | + end |
| 88 | + |
| 89 | + it 'chooses the correct user model based upon auth_index' do |
| 90 | + # Original authenticator |
| 91 | + conditions = ['username = ? AND password = ?', username, password] |
| 92 | + user_model.should_receive(:find).with(:all, conditions: conditions) |
| 93 | + .and_return([:user]) |
| 94 | + credentials = { |
| 95 | + username: username, |
| 96 | + password: password |
| 97 | + } |
| 98 | + expect(auth.validate(credentials)).to be true |
| 99 | + |
| 100 | + # Alternate authenticator, different credentials, different user model |
| 101 | + alt_auth = CASServer::Authenticators::SQL.new |
| 102 | + alt_user_model = CASServer::Authenticators::SQL.user_models[1] |
| 103 | + alt_username = 'dan' |
| 104 | + conditions = ['username = ? AND password = ?', alt_username, password] |
| 105 | + alt_user_model.should_receive(:find).with(:all, conditions: conditions) |
| 106 | + .and_return([:user]) |
| 107 | + alt_credentials = { |
| 108 | + username: alt_username, |
| 109 | + password: password |
| 110 | + } |
| 111 | + alt_auth.configure(HashWithIndifferentAccess.new(alt_options)) |
| 112 | + expect(alt_auth.validate(alt_credentials)).to be true |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments