Skip to content

Commit 194ef55

Browse files
committed
test(nbp): Add request specs
Part of XI-6523
1 parent 0a791a5 commit 194ef55

File tree

5 files changed

+402
-0
lines changed

5 files changed

+402
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Users: NBPWallet: Connect', type: :request do
6+
subject(:connect_request) { get '/users/nbp_wallet/connect' }
7+
8+
let(:uid) { 'example-uid' }
9+
let(:session_params) { {saml_uid: uid, omniauth_provider: 'nbp'} }
10+
11+
before do
12+
set_session(session_params)
13+
end
14+
15+
context 'without errors' do
16+
context 'with a pending Relationship' do
17+
before do
18+
allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return(Enmeshed::Relationship)
19+
end
20+
21+
it 'redirects to #finalize' do
22+
connect_request
23+
expect(response).to redirect_to nbp_wallet_finalize_users_path
24+
end
25+
end
26+
27+
context 'without a pending Relationship' do
28+
let(:truncated_reference) { 'RelationshipTemplateExampleTruncatedReferenceA==' }
29+
let(:relationship_template) do
30+
instance_double(Enmeshed::RelationshipTemplate,
31+
expires_at: 12.hours.from_now,
32+
nbp_uid: uid,
33+
truncated_reference:,
34+
url: "nmshd://tr##{truncated_reference}",
35+
qr_code_path: nbp_wallet_qr_code_users_path(truncated_reference:),
36+
remaining_validity: 12.hours.from_now - Time.zone.now,
37+
app_store_link: Settings.dig(:omniauth, :nbp, :enmeshed, :app_store_link),
38+
play_store_link: Settings.dig(:omniauth, :nbp, :enmeshed, :play_store_link))
39+
end
40+
41+
before do
42+
allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return([])
43+
allow(Enmeshed::RelationshipTemplate).to receive(:create!).with(nbp_uid: uid).and_return(relationship_template)
44+
end
45+
46+
it 'sets the correct template' do
47+
connect_request
48+
expect(response.body).to include 'RelationshipTemplateExampleTruncatedReferenceA=='
49+
end
50+
end
51+
end
52+
53+
context 'with errors' do
54+
# `Enmeshed::ConnectorError` is unknown until 'lib/enmeshed/connector.rb' is loaded, because it's defined there
55+
require 'enmeshed/connector'
56+
57+
shared_examples 'an erroneous request' do |error_type|
58+
it 'passes the error reason to Sentry' do
59+
expect(Sentry).to receive(:capture_exception) do |e|
60+
expect(e).to be_a error_type
61+
end
62+
connect_request
63+
end
64+
65+
it 'redirects to #new_user_registration' do
66+
expect(connect_request).to redirect_to new_user_registration_path
67+
end
68+
69+
it 'displays an error message' do
70+
connect_request
71+
expect(flash[:alert]).to include I18n.t('common.errors.generic_try_later')
72+
end
73+
end
74+
75+
context 'without the session' do
76+
let(:session_params) { {} }
77+
78+
before do
79+
connect_request
80+
end
81+
82+
it_behaves_like 'an unauthorized request'
83+
end
84+
85+
context 'when the connector is down' do
86+
before { allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_raise(Faraday::ConnectionFailed) }
87+
88+
it_behaves_like 'an erroneous request', Faraday::Error
89+
end
90+
91+
context 'with an error when parsing the connector response' do
92+
before { allow(Enmeshed::Connector).to receive(:pending_relationships).and_raise(Enmeshed::ConnectorError) }
93+
94+
it_behaves_like 'an erroneous request', Enmeshed::ConnectorError
95+
end
96+
end
97+
end
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Account: NBPWallet: Finalize', type: :request do
6+
subject(:finalize_request) { get '/users/nbp_wallet/finalize' }
7+
8+
let(:uid) { 'example-uid' }
9+
let(:session_params) { {saml_uid: uid, omniauth_provider: 'nbp'} }
10+
11+
before do
12+
set_session(session_params)
13+
end
14+
15+
context 'without any errors' do
16+
let(:relationship) do
17+
instance_double(Enmeshed::Relationship,
18+
accept!: true,
19+
userdata: {
20+
21+
first_name: 'john',
22+
last_name: 'oliver',
23+
status_group: 'learner',
24+
})
25+
end
26+
27+
before do
28+
allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return(relationship)
29+
allow(relationship).to receive(:peer).and_return('id1EvvJ68x6wdHBwYrFTR31XtALHko9fnbyp')
30+
end
31+
32+
it 'creates a user' do
33+
expect { finalize_request }.to change(User, :count).by(1)
34+
end
35+
36+
it 'creates two UserIdentities' do
37+
expect { finalize_request }.to change(UserIdentity, :count).by(2)
38+
end
39+
40+
it 'accepts the Relationship' do
41+
expect(relationship).to receive(:accept!)
42+
finalize_request
43+
end
44+
45+
it 'sends a confirmation mail' do
46+
expect { finalize_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
47+
end
48+
49+
it 'does not create a confirmed user' do
50+
finalize_request
51+
expect(User.order(:created_at).last).not_to be_confirmed
52+
end
53+
54+
it 'asks the user to verify the email address' do
55+
finalize_request
56+
expect(response).to redirect_to home_index_path
57+
expect(flash[:notice]).to include I18n.t('devise.registrations.signed_up_but_unconfirmed')
58+
end
59+
end
60+
61+
context 'with errors' do
62+
shared_examples 'a handled erroneous request' do |error_message|
63+
it 'redirects to the connect page' do
64+
finalize_request
65+
expect(response).to redirect_to nbp_wallet_connect_users_path
66+
end
67+
68+
it 'displays an error message' do
69+
finalize_request
70+
expect(flash[:alert]).to eq error_message
71+
end
72+
73+
it 'does not send a confirmation mail' do
74+
expect { finalize_request }.not_to change(ActionMailer::Base, :deliveries)
75+
end
76+
end
77+
78+
shared_examples 'a documented erroneous request' do |error|
79+
it 'passes the error reason to Sentry' do
80+
expect(Sentry).to receive(:capture_exception) do |e|
81+
expect(e).to be_a error
82+
end
83+
finalize_request
84+
end
85+
end
86+
87+
context 'without the session' do
88+
let(:session_params) { {} }
89+
90+
before do
91+
finalize_request
92+
end
93+
94+
it_behaves_like 'an unauthorized request'
95+
end
96+
97+
context 'when the connector is down' do
98+
before { allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_raise(Faraday::ConnectionFailed) }
99+
100+
it 'redirects to the connect page' do
101+
finalize_request
102+
expect(response).to redirect_to(nbp_wallet_connect_users_path)
103+
end
104+
end
105+
106+
context 'when an attribute is missing' do
107+
# `Enmeshed::ConnectorError` is unknown until 'lib/enmeshed/connector.rb' is loaded, because it's defined there
108+
require 'enmeshed/connector'
109+
110+
let(:relationship) do
111+
instance_double(Enmeshed::Relationship, accept!: false, reject!: true)
112+
end
113+
114+
before do
115+
allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return(relationship)
116+
allow(relationship).to receive(:userdata).and_raise(Enmeshed::ConnectorError, 'EMailAddress must not be empty')
117+
end
118+
119+
it_behaves_like 'a handled erroneous request', I18n.t('common.errors.generic')
120+
it_behaves_like 'a documented erroneous request', Enmeshed::ConnectorError
121+
end
122+
123+
context 'when the RelationshipChange cannot be accepted' do
124+
let(:relationship) do
125+
instance_double(Enmeshed::Relationship,
126+
accept!: false,
127+
reject!: true,
128+
userdata: {
129+
130+
first_name: 'john',
131+
last_name: 'oliver',
132+
status_group: 'learner',
133+
})
134+
end
135+
136+
before do
137+
allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return(relationship)
138+
allow(relationship).to receive(:peer).and_return('id1EvvJ68x6wdHBwYrFTR31XtALHko9fnbyp')
139+
end
140+
141+
it_behaves_like 'a handled erroneous request', I18n.t('common.errors.generic')
142+
143+
it 'does not create a user' do
144+
expect { finalize_request }.not_to change(User, :count)
145+
end
146+
147+
it 'creates no UserIdentities' do
148+
expect { finalize_request }.not_to change(UserIdentity, :count)
149+
end
150+
151+
it 'rejects the Relationship' do
152+
expect(relationship).to receive(:reject!)
153+
finalize_request
154+
end
155+
156+
context 'when the RelationshipChange cannot be rejected either' do
157+
before { allow(relationship).to receive(:reject!).and_raise(Faraday::ConnectionFailed) }
158+
159+
it_behaves_like 'a handled erroneous request', I18n.t('common.errors.generic')
160+
it_behaves_like 'a documented erroneous request', Faraday::ConnectionFailed
161+
162+
it 'does not create a user' do
163+
expect { finalize_request }.not_to change(User, :count)
164+
end
165+
166+
it 'creates no UserIdentities' do
167+
expect { finalize_request }.not_to change(UserIdentity, :count)
168+
end
169+
170+
it 'does not try to reject the Relationship again' do
171+
expect(relationship).to receive(:reject!).once
172+
finalize_request
173+
end
174+
end
175+
end
176+
end
177+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Account: NBPWallet: QRCode', type: :request do
6+
subject(:qr_code_request) { get '/users/nbp_wallet/qr_code', params: {truncated_reference:} }
7+
8+
let(:uid) { 'example-uid' }
9+
let(:session_params) { {saml_uid: uid, omniauth_provider: 'nbp'} }
10+
11+
let(:truncated_reference) { 'example_truncated_reference' }
12+
let(:qr_code) { RQRCode::QRCode.new("nmshd://tr##{truncated_reference}").as_png(border_modules: 0) }
13+
let(:relationship_template) do
14+
instance_double(Enmeshed::RelationshipTemplate, qr_code:)
15+
end
16+
17+
before do
18+
set_session(session_params)
19+
allow(Enmeshed::RelationshipTemplate).to receive(:new).with(truncated_reference:).and_return(relationship_template)
20+
end
21+
22+
it 'returns a png image' do
23+
qr_code_request
24+
expect(response.content_type).to eq 'image/png'
25+
end
26+
27+
it 'initializes a RelationshipTemplate' do
28+
expect(Enmeshed::RelationshipTemplate).to receive(:new)
29+
qr_code_request
30+
end
31+
32+
it 'sends the qr code' do
33+
qr_code_request
34+
expect(response.body).to eq qr_code.to_s
35+
end
36+
37+
context 'without the session' do
38+
let(:session_params) { {} }
39+
40+
before do
41+
qr_code_request
42+
end
43+
44+
it_behaves_like 'an unauthorized request'
45+
end
46+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Account: NBPWallet: RelationshipStatus', type: :request do
6+
subject(:relationship_status_request) { get '/users/nbp_wallet/relationship_status' }
7+
8+
let(:uid) { 'example_uid' }
9+
let(:session_params) { {saml_uid: uid, omniauth_provider: 'nbp'} }
10+
11+
before do
12+
set_session(session_params)
13+
end
14+
15+
context 'without errors' do
16+
context 'with a Relationship' do
17+
before { allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return(Enmeshed::Relationship) }
18+
19+
it 'returns a json with the ready status' do
20+
relationship_status_request
21+
expect(response.parsed_body['status']).to eq 'ready'
22+
end
23+
end
24+
25+
context 'without a Relationship' do
26+
before do
27+
allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return([])
28+
end
29+
30+
it 'returns a json with the waiting status' do
31+
relationship_status_request
32+
expect(response.parsed_body['status']).to eq 'waiting'
33+
end
34+
end
35+
end
36+
37+
context 'with errors' do
38+
# `Enmeshed::ConnectorError` is unknown until 'lib/enmeshed/connector.rb' is loaded, because it's defined there
39+
require 'enmeshed/connector'
40+
41+
context 'without the session' do
42+
let(:session_params) { {} }
43+
44+
before do
45+
relationship_status_request
46+
end
47+
48+
it_behaves_like 'an unauthorized request'
49+
end
50+
51+
context 'when the connector is down' do
52+
before { allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_raise(Faraday::ConnectionFailed) }
53+
54+
it 'redirects to the connect page' do
55+
relationship_status_request
56+
expect(response).to redirect_to(nbp_wallet_connect_users_path)
57+
end
58+
end
59+
60+
context 'with an error when parsing the connector response' do
61+
before { allow(Enmeshed::Connector).to receive(:pending_relationships).and_raise(Enmeshed::ConnectorError) }
62+
63+
it 'redirects to the connect page' do
64+
relationship_status_request
65+
expect(response).to redirect_to(nbp_wallet_connect_users_path)
66+
end
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)