Skip to content

Commit 072b68d

Browse files
committed
Add omniauth tests and fix identifier_scheme bug
Here we have a first pass of working integration tests for omniauth SSO. The tests still need to check for the messages we are setting as notifications. We can think about splitting the tests into integration and controller tests.
1 parent d645bb6 commit 072b68d

File tree

5 files changed

+110
-6
lines changed

5 files changed

+110
-6
lines changed

app/models/identifier_scheme.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class IdentifierScheme < ApplicationRecord
6262
# { "ror": "12345" }
6363
# so we cannot allow spaces or non alpha characters!
6464
def name=(value)
65-
super(value&.downcase&.gsub(/[^a-z]/, ''))
65+
super(value&.downcase&.gsub(/[^a-z|_]/, ''))
6666
end
6767

6868
# ===========================

app/models/user.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def self.create_from_provider_data(provider_data)
189189

190190
return user if user
191191

192-
user = User.new(
192+
User.create!(
193193
firstname: provider_data.info.first_name,
194194
surname: provider_data.info.last_name,
195195
email: provider_data.info.email,
@@ -198,8 +198,6 @@ def self.create_from_provider_data(provider_data)
198198
accept_terms: true,
199199
password: Devise.friendly_token[0, 20]
200200
)
201-
202-
user.save!
203201
end
204202

205203
def self.to_csv(users)
@@ -239,7 +237,6 @@ def locale
239237
# Returns String
240238
# rubocop:disable Style/OptionalBooleanParameter
241239
def name(use_email = true)
242-
# byebug
243240
if (firstname.blank? && surname.blank?) || use_email
244241
email
245242
else

db/seeds/test.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
logo_url: 'http://newsite.shibboleth.net/wp-content/uploads/2017/01/Shibboleth-logo_2000x1200-1.png',
6969
identifier_prefix: "https://example.com"
7070
},
71+
{
72+
name: "openid_connect",
73+
description: "CILogon",
74+
active: true,
75+
identifier_prefix: "https://www.cilogon.org/",
76+
},
7177
]
7278
identifier_schemes.each { |is| IdentifierScheme.create!(is) }
7379

@@ -249,7 +255,13 @@
249255
abbreviation: 'UOS',
250256
org_type: 1, links: {"org":[]},
251257
language: default_language, region: region,
252-
is_other: false, managed: true}
258+
is_other: false, managed: true},
259+
{name: 'Other organisation',
260+
abbreviation: 'OO',
261+
org_type: 1, links: {"org":[]},
262+
language: default_language, region: region,
263+
is_other: true, managed: true}
264+
253265
]
254266
orgs.each { |o| Org.create!(o) }
255267

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'Openid_connection SSO', type: :feature do
4+
context 'with correct credentials' do
5+
before do
6+
create(:org, managed: false, is_other: true)
7+
@org = create(:org, managed: true)
8+
@identifier_scheme = create(:identifier_scheme,
9+
name: 'openid_connect',
10+
description: 'CILogon',
11+
active: true,
12+
identifier_prefix: 'https://www.cilogon.org/')
13+
# OmniAuth.config.add_mock(openid_connect: {
14+
# provider: 'openid_connect',
15+
# uid: '12345',
16+
# info:
17+
# { email: 'user@organization.ca',
18+
# first_name: 'John',
19+
# last_name: 'Doe' }
20+
# })
21+
22+
# OmniAuth.config.add_mock(openid_connect: {
23+
# 'provider' => 'openid_connect',
24+
# 'uid' => '12345',
25+
# 'info' =>
26+
# { 'email' => 'user@organization.ca',
27+
# 'first_name' => 'John',
28+
# 'last_name' => 'Doe' }
29+
# })
30+
31+
# OmniAuth.config.add_mock(:openid_connect, {
32+
# provider: 'openid_connect',
33+
# uid: '12345',
34+
# info: {
35+
# email: 'user@organization.ca',
36+
# first_name: 'John',
37+
# last_name: 'Doe'
38+
# }
39+
# })
40+
# OmniAuth.config.mock_auth[:openid_connect] = OmniAuth::AuthHash.new({
41+
# provider: 'openid_connect',
42+
# uid: '12345',
43+
# info:
44+
# { email: 'user@organization.ca',
45+
# first_name: 'John',
46+
# last_name: 'Doe' }
47+
Rails.application.env_config['devise.mapping'] = Devise.mappings[:user]
48+
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect]
49+
end
50+
51+
# it 'links external credentials with existing account' do
52+
# end
53+
54+
it 'creates account from external credentials' do
55+
visit root_path
56+
click_link 'Sign in with CILogon'
57+
58+
identifier = Identifier.last
59+
expect(identifier.value).to eql('https://www.cilogon.org/12345')
60+
identifiable = identifier.identifiable
61+
expect(identifiable.email).to eql('user@organization.ca')
62+
expect(identifiable.firstname).to eql('John')
63+
expect(identifiable.surname).to eql('Doe')
64+
65+
# XXX Check notice message
66+
end
67+
68+
it 'links account from external credentails' do
69+
# Create existing user
70+
create(:user, :org_admin, org: @org, email: 'user@organization.ca')
71+
visit root_path
72+
click_link 'Sign in with CILogon'
73+
identifier = Identifier.last
74+
expect(identifier.value).to eql('https://www.cilogon.org/12345')
75+
identifiable = identifier.identifiable
76+
# We will find the new user with the email specified above
77+
expect(identifiable.email).to eql('user@organization.ca')
78+
end
79+
end
80+
end

spec/spec_helper.rb

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'capybara/rspec'
44
require 'mocha'
5+
require 'omniauth'
56

67
$LOAD_PATH.unshift(File.expand_path(__dir__))
78

@@ -119,3 +120,17 @@
119120
# Capybara::Webmock.stop if example.metadata[:type] == :feature
120121
end
121122
end
123+
124+
OmniAuth.config.test_mode = true
125+
126+
OmniAuth.config.mock_auth[:openid_connect] = OmniAuth::AuthHash.new(
127+
{
128+
provider: 'openid_connect',
129+
uid: '12345',
130+
info: {
131+
email: 'user@organization.ca',
132+
first_name: 'John',
133+
last_name: 'Doe'
134+
}
135+
}
136+
)

0 commit comments

Comments
 (0)