Skip to content

Commit 03c1442

Browse files
committed
Refactor bypass spec into shared examples
1 parent 79f3392 commit 03c1442

File tree

1 file changed

+77
-110
lines changed

1 file changed

+77
-110
lines changed

spec/rpi_auth_bypass_spec.rb

Lines changed: 77 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -28,156 +28,123 @@
2828
end
2929
end
3030

31-
describe 'OmniAuth::Configuration#add_rpi_mock' do
32-
subject(:mock_auth) { OmniAuth.config.mock_auth[:rpi] }
33-
34-
let(:args) { {} }
31+
shared_examples 'a mocked auth object' do
32+
let(:raw_info) { extra[:raw_info] }
3533

36-
before do
37-
OmniAuth.config.add_rpi_mock(**args)
34+
it 'has the uid' do
35+
expect(mock_auth.uid).to eq(uid)
3836
end
3937

40-
it 'has the default uid' do
41-
expect(mock_auth.uid).to eq(RpiAuthBypass::DEFAULT_UID)
38+
it 'has the email from info' do
39+
expect(mock_auth.info.email).to eq(info[:email])
4240
end
4341

44-
it 'has the default email' do
45-
expect(mock_auth.info.email).to eq(RpiAuthBypass::DEFAULT_EMAIL)
42+
it 'has the username from info' do
43+
expect(mock_auth.info.username).to eq(info[:username])
4644
end
4745

48-
it 'has the default username' do
49-
expect(mock_auth.info.username).to eq(RpiAuthBypass::DEFAULT_USERNAME)
46+
it 'has the name from info' do
47+
expect(mock_auth.info.name).to eq info[:name]
5048
end
5149

52-
it 'has the default name' do
53-
expect(mock_auth.info.name).to eq(RpiAuthBypass::DEFAULT_NAME)
50+
it 'has the nickname from info' do
51+
expect(mock_auth.info.nickname).to eq info[:nickname]
5452
end
5553

56-
it 'has the default nickname' do
57-
expect(mock_auth.info.nickname).to eq(RpiAuthBypass::DEFAULT_NICKNAME)
54+
it 'has the image from info' do
55+
expect(mock_auth.info.image).to eq info[:image]
5856
end
5957

60-
it 'has the default image' do
61-
expect(mock_auth.info.image).to eq(RpiAuthBypass::DEFAULT_IMAGE)
58+
it 'has the email from raw_info' do
59+
expect(mock_auth.extra.raw_info.email).to eq raw_info[:email]
6260
end
6361

64-
it 'has the default roles' do
65-
expect(mock_auth.extra.raw_info.roles).to eq(RpiAuthBypass::DEFAULT_ROLES)
62+
it 'has the username from raw_info' do
63+
expect(mock_auth.extra.raw_info.username).to eq raw_info[:username]
6664
end
6765

68-
it 'has the default avatar' do
69-
expect(mock_auth.extra.raw_info.avatar).to eq(RpiAuthBypass::DEFAULT_IMAGE)
66+
it 'has the name from raw_info' do
67+
expect(mock_auth.extra.raw_info.name).to eq raw_info[:name]
7068
end
7169

72-
it 'has the default profile' do
73-
expect(mock_auth.extra.raw_info.profile).to eq(RpiAuthBypass::DEFAULT_PROFILE)
70+
it 'has the nickname from raw_info' do
71+
expect(mock_auth.extra.raw_info.nickname).to eq raw_info[:nickname]
7472
end
7573

76-
it 'has the default country' do
77-
expect(mock_auth.extra.raw_info.country).to eq(RpiAuthBypass::DEFAULT_COUNTRY)
74+
it 'has the roles from raw_info' do
75+
expect(mock_auth.extra.raw_info.roles).to eq raw_info[:roles]
7876
end
7977

80-
it 'has the default country code' do
81-
expect(mock_auth.extra.raw_info.country_code).to eq(RpiAuthBypass::DEFAULT_COUNTRY_CODE)
78+
it 'has the sub from raw_info' do
79+
expect(mock_auth.extra.raw_info.sub).to eq raw_info[:sub]
8280
end
8381

84-
it 'has the default postcode' do
85-
expect(mock_auth.extra.raw_info.postcode).to eq(RpiAuthBypass::DEFAULT_POSTCODE)
82+
it 'has the user from raw_info' do
83+
expect(mock_auth.extra.raw_info.user).to eq raw_info[:user]
8684
end
8785

88-
context 'with info and extra specified' do # rubocop:disable RSpec/MultipleMemoizedHelpers
89-
let(:uid) { '1d27cca2-fef3-4f79-bc64-b76e93db84a2' }
90-
let(:name) { 'Robert Flemming' }
91-
let(:nickname) { 'Bob' }
92-
let(:email) { 'bob.flemming@example.com' }
93-
let(:username) { 'bob.flemming' }
94-
let(:roles) { 'gardener' }
95-
let(:image) { 'https://my.avatar.com/image/1' }
96-
let(:profile) { 'https://my.user.com/profile/1' }
97-
let(:country) { 'United States' }
98-
let(:country_code) { 'US' }
99-
let(:postcode) { '123456' }
100-
101-
let(:info) { { name: name, email: email, username: username, nickname: nickname, image: image } }
102-
let(:extra) do
103-
{ raw_info: {
104-
name: name,
105-
email: email,
106-
username: username,
107-
nickname: nickname,
108-
roles: roles,
109-
avatar: image,
110-
profile: profile,
111-
country: country,
112-
country_code: country_code,
113-
postcode: postcode
114-
} }
115-
end
116-
let(:args) { { uid: uid, info: info, extra: extra } }
117-
118-
it 'has the uid' do
119-
expect(mock_auth.uid).to eq(uid)
120-
end
121-
122-
it 'has the email from info' do
123-
expect(mock_auth.info.email).to eq(email)
124-
end
125-
126-
it 'has the username from info' do
127-
expect(mock_auth.info.username).to eq(username)
128-
end
129-
130-
it 'has the name from info' do
131-
expect(mock_auth.info.name).to eq(name)
132-
end
133-
134-
it 'has the nickname from info' do
135-
expect(mock_auth.info.nickname).to eq(nickname)
136-
end
86+
it 'has the profile from raw_info' do
87+
expect(mock_auth.extra.raw_info.profile).to eq raw_info[:profile]
88+
end
13789

138-
it 'has the image from info' do
139-
expect(mock_auth.info.image).to eq(image)
140-
end
90+
it 'has the country from raw_info' do
91+
expect(mock_auth.extra.raw_info.country).to eq raw_info[:country]
92+
end
14193

142-
it 'has the email from extra' do
143-
expect(mock_auth.extra.raw_info.email).to eq(email)
144-
end
94+
it 'has the country_code from raw_info' do
95+
expect(mock_auth.extra.raw_info.country_code).to eq raw_info[:country_code]
96+
end
14597

146-
it 'has the username from extra' do
147-
expect(mock_auth.extra.raw_info.username).to eq(username)
148-
end
98+
it 'has the postcode from raw_info' do
99+
expect(mock_auth.extra.raw_info.postcode).to eq raw_info[:postcode]
100+
end
101+
end
149102

150-
it 'has the name from extra' do
151-
expect(mock_auth.extra.raw_info.name).to eq(name)
152-
end
103+
describe 'OmniAuth::Configuration#add_rpi_mock' do
104+
subject(:mock_auth) { OmniAuth.config.mock_auth[:rpi] }
153105

154-
it 'has the nickname from extra' do
155-
expect(mock_auth.extra.raw_info.nickname).to eq(nickname)
156-
end
106+
let(:args) { {} }
107+
let(:uid) { RpiAuthBypass::DEFAULT_UID }
108+
let(:info) { RpiAuthBypass::DEFAULT_INFO }
109+
let(:extra) { RpiAuthBypass::DEFAULT_EXTRA }
157110

158-
it 'has the roles from extra' do
159-
expect(mock_auth.extra.raw_info.roles).to eq(roles)
160-
end
111+
before do
112+
OmniAuth.config.add_rpi_mock(**args)
113+
end
161114

162-
it 'has the avatar from extra' do
163-
expect(mock_auth.extra.raw_info.avatar).to eq(image)
164-
end
115+
it_behaves_like 'a mocked auth object'
165116

166-
it 'has the profile from extra' do
167-
expect(mock_auth.extra.raw_info.profile).to eq(profile)
168-
end
117+
context 'with info and extra specified' do
118+
let(:uid) { '1d27cca2-fef3-4f79-bc64-b76e93db84a2' }
169119

170-
it 'has the country from extra' do
171-
expect(mock_auth.extra.raw_info.country).to eq(country)
120+
let(:info) do
121+
{
122+
name: 'Robert Flemming',
123+
email: 'bob.flemming@example.com',
124+
username: 'bob.flemming',
125+
nickname: 'Bob',
126+
image: 'https://my.avatar.com/image/1'
127+
}
172128
end
173129

174-
it 'has the country_code from extra' do
175-
expect(mock_auth.extra.raw_info.country_code).to eq(country_code)
130+
let(:extra) do
131+
{ raw_info: {
132+
country: 'US',
133+
country_code: 'United States',
134+
email: info[:email],
135+
name: info[:name],
136+
nickname: info[:nickname],
137+
postcode: '90210',
138+
profile: 'https://my.user.com/profile/1',
139+
roles: 'gardener',
140+
sub: uid,
141+
user: uid,
142+
username: info[:username]
143+
} }
176144
end
145+
let(:args) { { uid: uid, info: info, extra: extra } }
177146

178-
it 'has the postcode from extra' do
179-
expect(mock_auth.extra.raw_info.postcode).to eq(postcode)
180-
end
147+
it_behaves_like 'a mocked auth object'
181148
end
182149
end
183150
end

0 commit comments

Comments
 (0)