-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update specs to use Discourse patterns, and avoid stubs
This means that the specs are a much closer match to production, and should pick up any breaking changes to Discourse core
- Loading branch information
1 parent
83c463f
commit dbef9bd
Showing
5 changed files
with
37 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
- :name: Example User | ||
:email: [email protected] | ||
:username: example_user | ||
:groups: ['staff', 'engineering'] | ||
:groups: ['team', 'engineering'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,98 @@ | ||
require 'spec_helper' | ||
require 'lib/auth' | ||
require 'ostruct' | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
def enabled_site_setting(setting=nil) | ||
end | ||
|
||
def gem(name, version=nil, opts = {}) | ||
end | ||
|
||
def auth_provider(opts = {}) | ||
end | ||
|
||
def register_css(css='') | ||
end | ||
describe LDAPAuthenticator do | ||
|
||
load 'plugin.rb' | ||
let(:authenticator) { LDAPAuthenticator.new } | ||
let(:auth_hash) { OmniAuth::AuthHash.new( | ||
info: { | ||
email: '[email protected]', | ||
nickname: 'tester', | ||
name: 'Testy McTesterson' | ||
} | ||
)} | ||
|
||
describe LDAPAuthenticator do | ||
before(:all) do | ||
SiteSetting = OpenStruct.new(ldap_user_create_mode: 'auto') | ||
end | ||
before(:each) do | ||
@user = stub_const('LDAPUser::User', Class.new) | ||
@auth = LDAPAuthenticator.new | ||
@entry = OpenStruct.new({ | ||
info: { | ||
email: '[email protected]', | ||
nickname: 'tester', | ||
name: 'Testy McTesterson' | ||
} | ||
}) | ||
end | ||
context 'when SiteSettings.ldap_user_create_mode is auto' do | ||
it 'will create auth result with ldap entry data and nil user if user with email does not exist' do | ||
allow(@user).to receive(:find_by_email).and_return(nil) | ||
expect(@user).to receive(:find_by_email).with('[email protected]') | ||
result = @auth.after_authenticate(@entry) | ||
expect(result.email).to eq(@entry.info[:email]) | ||
expect(result.name).to eq(@entry.info[:name]) | ||
expect(result.username).to eq(@entry.info[:nickname]) | ||
result = authenticator.after_authenticate(auth_hash) | ||
expect(result.email).to eq(auth_hash.info[:email]) | ||
expect(result.name).to eq(auth_hash.info[:name]) | ||
expect(result.username).to eq(auth_hash.info[:nickname]) | ||
expect(result.failed?).to eq(false) | ||
expect(result.user).to be_nil | ||
end | ||
end | ||
|
||
context 'when SiteSettings.ldap_user_create_mode is none' do | ||
before(:all) do | ||
before do | ||
SiteSetting.ldap_user_create_mode = 'none' | ||
end | ||
it 'will fail auth if user account does not exist' do | ||
allow(@user).to receive(:find_by_email).and_return(nil) | ||
result = @auth.after_authenticate(@entry) | ||
result = authenticator.after_authenticate(auth_hash) | ||
expect(result.failed?).to eq(true) | ||
expect(result.failed_reason).to eq('User account does not exist.') | ||
end | ||
it 'will pass auth if user account exists' do | ||
allow(@user).to receive(:find_by_email).and_return(@user) | ||
result = @auth.after_authenticate(@entry) | ||
user = Fabricate(:user, email: auth_hash.info[:email]) | ||
result = authenticator.after_authenticate(auth_hash) | ||
expect(result.failed?).to eq(false) | ||
expect(result.user).to eq(user) | ||
end | ||
end | ||
|
||
context 'when SiteSettings.ldap_user_create_mode is list' do | ||
before(:each) do | ||
before do | ||
SiteSetting.ldap_user_create_mode = 'list' | ||
@group = stub_const('LDAPUser::Group', Class.new) | ||
allow(@group).to receive(:find_by).with(name: 'staff').and_return('staff_group') | ||
allow(@group).to receive(:find_by).with(name: 'engineering').and_return('engineering_group') | ||
Fabricate(:group, name: 'team') | ||
Fabricate(:group, name: 'engineering') | ||
end | ||
it 'will fail auth if list does not contain user with email' do | ||
allow(@user).to receive(:find_by_email).and_return(nil) | ||
result = @auth.after_authenticate(@entry) | ||
result = authenticator.after_authenticate(auth_hash) | ||
expect(result.failed?).to eq(true) | ||
expect(result.failed_reason).to eq('User with email is not listed in LDAP user list.') | ||
end | ||
it 'will pass auth if list contains user with email' do | ||
#user account exists | ||
allow(@user).to receive(:find_by_email).and_return(OpenStruct.new(activate: true, groups: [])) | ||
entry = OpenStruct.new({ | ||
Fabricate(:user, email: '[email protected]') | ||
entry = OmniAuth::AuthHash.new({ | ||
info: { | ||
email: '[email protected]', | ||
nickname: 'ldap_user', | ||
name: 'LDAP User' | ||
} | ||
}) | ||
result = @auth.after_authenticate(entry) | ||
result = authenticator.after_authenticate(entry) | ||
expect(result.failed?).to eq(false) | ||
end | ||
it 'will create user groups when creating new user account' do | ||
#user account does not exist | ||
allow(@user).to receive(:find_by_email).and_return(nil) | ||
allow(@user).to receive(:create!).and_return(OpenStruct.new(activate: true, groups: [])) | ||
entry = OpenStruct.new({ | ||
entry = OmniAuth::AuthHash.new({ | ||
info: { | ||
email: '[email protected]', | ||
nickname: 'ldap_user', | ||
name: 'LDAP User' | ||
} | ||
}) | ||
result = @auth.after_authenticate(entry) | ||
result = authenticator.after_authenticate(entry) | ||
expect(result.failed?).to eq(false) | ||
expect(result.email).to eq(entry.info[:email]) | ||
#username and name from ldap_user.yml | ||
expect(result.username).to eq('example_user') | ||
expect(result.name).to eq('Example User') | ||
expect(result.user.groups[0]).to eq('staff_group') | ||
expect(result.user.groups[1]).to eq('engineering_group') | ||
expect(result.user.groups[0].name).to eq('team') | ||
expect(result.user.groups[1].name).to eq('engineering') | ||
end | ||
end | ||
|
||
context 'when SiteSettings.ldap_lookup_users_by is username' do | ||
before(:all) do | ||
before do | ||
SiteSetting.ldap_user_create_mode = 'auto' | ||
SiteSetting.ldap_lookup_users_by = 'username' | ||
end | ||
it 'will lookup user by username' do | ||
expect(@user).to_not receive(:find_by_email) | ||
expect(@user).to receive(:find_by_username).with('tester') | ||
@auth.after_authenticate(@entry) | ||
user = Fabricate(:user, username: "tester") | ||
result = authenticator.after_authenticate(auth_hash) | ||
expect(result.user).to eq(user) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.