forked from rubyforgood/human-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_request_spec.rb
More file actions
291 lines (239 loc) · 10.9 KB
/
account_request_spec.rb
File metadata and controls
291 lines (239 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
require "rails_helper"
RSpec.describe AccountRequest do
describe '#get_by_identity_token', :phoenix do
let(:account_request) { create(:account_request) }
let(:identity_token) { JWT.encode({ account_request_id: account_request.id }, Rails.application.secret_key_base, 'HS256') }
it 'returns the account request when given a valid identity token' do
allow(AccountRequest).to receive(:find_by).with(id: account_request.id).and_return(account_request)
expect(AccountRequest.get_by_identity_token(identity_token)).to eq(account_request)
end
it 'returns nil when no account request is found for a valid identity token' do
allow(AccountRequest).to receive(:find_by).with(id: account_request.id).and_return(nil)
expect(AccountRequest.get_by_identity_token(identity_token)).to be_nil
end
it 'returns nil for an invalid identity token due to decoding error' do
invalid_token = 'invalid.token.string'
expect(AccountRequest.get_by_identity_token(invalid_token)).to be_nil
end
it 'returns nil when a StandardError is raised during decoding' do
allow(JWT).to receive(:decode).and_raise(StandardError)
expect(AccountRequest.get_by_identity_token(identity_token)).to be_nil
end
end
describe '#identity_token', :phoenix do
let(:account_request) { create(:account_request) }
context 'when the account request is persisted' do
it 'encodes a JWT token with the correct account_request_id' do
token = account_request.identity_token
decoded_token = JWT.decode(token, Rails.application.secret_key_base, true, { algorithm: 'HS256' })
expect(decoded_token.first['account_request_id']).to eq(account_request.id)
end
end
context 'when the account request is not persisted' do
let(:account_request) { build(:account_request) }
it 'raises an error indicating the id is missing' do
expect { account_request.identity_token }.to raise_error('must have an id')
end
end
end
describe '#confirmed?', :phoenix do
let(:account_request) { build(:account_request, status: status) }
context 'when user is confirmed and admin is not approved' do
let(:status) { 'user_confirmed' }
it 'returns true when user is confirmed' do
expect(account_request.confirmed?).to eq(true)
end
end
context 'when user is not confirmed and admin is approved' do
let(:status) { 'admin_approved' }
it 'returns true when admin is approved' do
expect(account_request.confirmed?).to eq(true)
end
end
context 'when both user is confirmed and admin is approved' do
let(:status) { 'user_confirmed_and_admin_approved' }
it 'returns true when both user is confirmed and admin is approved' do
expect(account_request.confirmed?).to eq(true)
end
end
context 'when neither user is confirmed nor admin is approved' do
let(:status) { 'pending' }
it 'returns false when neither user is confirmed nor admin is approved' do
expect(account_request.confirmed?).to eq(false)
end
end
end
describe '#processed?', :phoenix do
let(:organization) { build(:organization) }
let(:account_request_with_org) { build(:account_request, organization: organization) }
let(:account_request_without_org) { build(:account_request, organization: nil) }
it 'returns true when organization is present' do
expect(account_request_with_org.processed?).to be true
end
it 'returns false when organization is not present' do
expect(account_request_without_org.processed?).to be false
end
end
describe '#can_be_closed?', :phoenix do
let(:account_request) { build(:account_request, status: status) }
context 'when status is started' do
let(:status) { 'started' }
it 'returns true when the status is started' do
expect(account_request.can_be_closed?).to eq(true)
end
end
context 'when status is user_confirmed' do
let(:status) { 'user_confirmed' }
it 'returns true when the status is user_confirmed' do
expect(account_request.can_be_closed?).to eq(true)
end
end
context 'when status is neither started nor user_confirmed' do
let(:status) { 'pending' }
it 'returns false when the status is neither started nor user_confirmed' do
expect(account_request.can_be_closed?).to eq(false)
end
end
end
describe '#confirm!', :phoenix do
let(:account_request) { create(:account_request) }
it 'updates confirmed_at to current time' do
account_request.confirm!
expect(account_request.confirmed_at).to be_within(1.second).of(Time.current)
end
it 'updates status to user_confirmed' do
account_request.confirm!
expect(account_request.status).to eq('user_confirmed')
end
it 'enqueues an approval request email' do
expect { account_request.confirm! }.to have_enqueued_job.on_queue('mailers')
end
describe 'when update fails' do
before do
allow(account_request).to receive(:update!).and_raise(ActiveRecord::RecordInvalid)
end
it 'raises ActiveRecord::RecordInvalid error' do
expect { account_request.confirm! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe 'when email delivery fails' do
before do
allow(AccountRequestMailer).to receive_message_chain(:approval_request, :deliver_later).and_raise(StandardError)
end
it 'raises StandardError' do
expect { account_request.confirm! }.to raise_error(StandardError)
end
end
end
describe '#reject!', :phoenix do
let(:account_request) { create(:account_request) }
let(:rejection_reason) { 'Insufficient information provided' }
it 'updates the status to rejected' do
account_request.reject!(rejection_reason)
expect(account_request.status).to eq('rejected')
end
it 'sets the rejection reason' do
account_request.reject!(rejection_reason)
expect(account_request.rejection_reason).to eq(rejection_reason)
end
it 'sends a rejection email' do
mailer_double = instance_double(AccountRequestMailer)
allow(AccountRequestMailer).to receive(:rejection).with(account_request_id: account_request.id).and_return(mailer_double)
expect(mailer_double).to receive(:deliver_later)
account_request.reject!(rejection_reason)
end
describe 'when update fails' do
before do
allow(account_request).to receive(:update!).and_raise(ActiveRecord::RecordInvalid)
end
it 'does not send a rejection email' do
expect(AccountRequestMailer).not_to receive(:rejection)
expect { account_request.reject!(rejection_reason) }.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe 'when email delivery fails' do
before do
allow(AccountRequestMailer).to receive(:rejection).and_raise(Net::SMTPFatalError)
end
it 'raises an email delivery failure error' do
expect { account_request.reject!(rejection_reason) }.to raise_error(Net::SMTPFatalError)
end
end
end
describe '#close!', :phoenix do
let(:account_request) { build(:account_request, status: account_status) }
let(:account_status) { 'open' }
it 'raises an error if the account cannot be closed' do
allow(account_request).to receive(:can_be_closed?).and_return(false)
expect { account_request.close!('Some reason') }.to raise_error('Cannot be closed from this state')
end
context 'when the account can be closed' do
before do
allow(account_request).to receive(:can_be_closed?).and_return(true)
end
it 'updates the status to admin_closed' do
account_request.close!('Some reason')
expect(account_request.status).to eq('admin_closed')
end
it 'sets the rejection reason' do
account_request.close!('Some reason')
expect(account_request.rejection_reason).to eq('Some reason')
end
end
end
describe '#email_not_already_used_by_organization', :phoenix do
let(:organization) { build(:organization, email: 'unique@example.com') }
let(:existing_organization) { create(:organization, email: 'existing@example.com') }
it 'does not add an error if the email is not associated with any organization' do
allow(Organization).to receive(:find_by).with(email: 'unique@example.com').and_return(nil)
organization.email_not_already_used_by_organization
expect(organization.errors[:email]).to be_empty
end
it 'does not add an error if the email is associated with the current organization' do
allow(Organization).to receive(:find_by).with(email: 'unique@example.com').and_return(organization)
organization.email_not_already_used_by_organization
expect(organization.errors[:email]).to be_empty
end
it 'adds an error if the email is associated with a different organization' do
allow(Organization).to receive(:find_by).with(email: 'existing@example.com').and_return(existing_organization)
organization.email = 'existing@example.com'
organization.email_not_already_used_by_organization
expect(organization.errors[:email]).to include('already used by an existing Organization')
end
end
describe '#email_not_already_used_by_user', :phoenix do
let(:organization) { create(:organization) }
let(:user_with_same_email) { build(:user, email: 'test@example.com', organization: user_organization) }
let(:user_organization) { nil }
it 'adds an error when a user with the same email exists and no organization is provided' do
allow(User).to receive(:find_by).with(email: 'test@example.com').and_return(user_with_same_email)
account_request = build(:account_request, email: 'test@example.com', organization: nil)
account_request.email_not_already_used_by_user
expect(account_request.errors[:email]).to include('already used by an existing User')
end
context 'when a different organization is provided' do
let(:user_organization) { create(:organization) }
it 'adds an error when a user with the same email exists and a different organization is provided' do
allow(User).to receive(:find_by).with(email: 'test@example.com').and_return(user_with_same_email)
account_request = build(:account_request, email: 'test@example.com', organization: organization)
account_request.email_not_already_used_by_user
expect(account_request.errors[:email]).to include('already used by an existing User')
end
end
context 'when the same organization is provided' do
let(:user_organization) { organization }
it 'does not add an error when a user with the same email exists and the same organization is provided' do
allow(User).to receive(:find_by).with(email: 'test@example.com').and_return(user_with_same_email)
account_request = build(:account_request, email: 'test@example.com', organization: organization)
account_request.email_not_already_used_by_user
expect(account_request.errors[:email]).to be_empty
end
end
it 'does not add an error when no user with the same email exists' do
allow(User).to receive(:find_by).with(email: 'test@example.com').and_return(nil)
account_request = build(:account_request, email: 'test@example.com', organization: organization)
account_request.email_not_already_used_by_user
expect(account_request.errors[:email]).to be_empty
end
end
end