Skip to content

Commit

Permalink
Update specs to use Discourse patterns, and avoid stubs
Browse files Browse the repository at this point in the history
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
davidtaylorhq committed Dec 29, 2021
1 parent 83c463f commit dbef9bd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 191 deletions.
2 changes: 1 addition & 1 deletion ldap_users.yml
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']
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
require 'yaml'
require_relative 'lib/ldap_user'

class LDAPAuthenticator < ::Auth::Authenticator
class ::LDAPAuthenticator < ::Auth::Authenticator
def name
'ldap'
end
Expand Down
95 changes: 35 additions & 60 deletions spec/ldap_authenticator_spec.rb
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
33 changes: 0 additions & 33 deletions spec/lib/auth.rb

This file was deleted.

96 changes: 0 additions & 96 deletions spec/spec_helper.rb

This file was deleted.

0 comments on commit dbef9bd

Please sign in to comment.