Skip to content

Commit 144f517

Browse files
committed
Add tests for Admin::SchoolsController
I want to make sure that verifying and rejecting schools continues to work as I make changes to the functionality. Unfortunately we now see the following deprecation warning in our tests: > DEPRECATION WARNING: The option :class_name is deprecated. > Administrate should detect it automatically. This is caused by our use of `Field::BelongsTo` to associate the school and user in the `SchoolDashboard` configuration[1]. The `School#creator` method isn't actually a `belongs_to` association so we have to pass the `class_name` to let Administrate know that the method returns a user. I can't see a way of avoiding this deprecation warning at the moment. [1]: https://github.com/RaspberryPiFoundation/editor-api/blob/6a8f7903112de73b018d4b50f7701f0e0bc40924/app/dashboards/school_dashboard.rb#L14
1 parent afc6d34 commit 144f517

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

spec/features/admin/schools_spec.rb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Schools', type: :request do
6+
let(:admin_user) { create(:admin_user) }
7+
8+
before do
9+
sign_in_as(admin_user)
10+
end
11+
12+
describe 'GET #index' do
13+
it 'responds 200' do
14+
get admin_schools_path
15+
expect(response).to have_http_status(:success)
16+
end
17+
end
18+
19+
describe 'GET #show' do
20+
let(:creator) { create(:user) }
21+
let(:verified_at) { nil }
22+
let(:rejected_at) { nil }
23+
let(:school) { create(:school, creator_id: creator.id, verified_at:, rejected_at:) }
24+
25+
before do
26+
stub_user_info_api_for(creator)
27+
get admin_school_path(school)
28+
end
29+
30+
it 'responds 200' do
31+
expect(response).to have_http_status(:success)
32+
end
33+
34+
it 'includes link to verify school' do
35+
expect(response.body).to include(I18n.t('administrate.actions.verify_school'))
36+
end
37+
38+
it 'includes link to reject school' do
39+
expect(response.body).to include(I18n.t('administrate.actions.reject_school'))
40+
end
41+
42+
describe 'when the school is verified' do
43+
let(:verified_at) { Time.zone.now }
44+
45+
it 'does not include a link to verify school' do
46+
expect(response.body).not_to include(I18n.t('administrate.actions.verify_school'))
47+
end
48+
49+
it 'includes link to reject school' do
50+
expect(response.body).to include(I18n.t('administrate.actions.reject_school'))
51+
end
52+
end
53+
54+
describe 'when the school is rejected' do
55+
let(:rejected_at) { Time.zone.now }
56+
57+
it 'includes a link to verify school' do
58+
expect(response.body).to include(I18n.t('administrate.actions.verify_school'))
59+
end
60+
61+
it 'does not include a link to reject school' do
62+
expect(response.body).not_to include(I18n.t('administrate.actions.reject_school'))
63+
end
64+
end
65+
end
66+
67+
describe 'POST #verify_school' do
68+
let(:creator) { create(:user) }
69+
let(:verified_at) { nil }
70+
let(:school) { create(:school, creator_id: creator.id, verified_at:) }
71+
let(:verification_result) { nil }
72+
let(:verification_service) { instance_double(SchoolVerificationService, verify: verification_result) }
73+
74+
before do
75+
stub_user_info_api_for(creator)
76+
allow(SchoolVerificationService).to receive(:new).with(school.id).and_return(verification_service)
77+
78+
post admin_school_verify_school_path(school)
79+
end
80+
81+
it 'redirects to school path' do
82+
expect(response).to redirect_to(admin_school_path(school))
83+
end
84+
85+
describe 'when verification was successful' do
86+
let(:verification_result) { true }
87+
88+
before do
89+
follow_redirect!
90+
end
91+
92+
it 'displays success message' do
93+
expect(response.body).to include(I18n.t('administrate.controller.verify_school.success'))
94+
end
95+
end
96+
97+
describe 'when verification was unsuccessful' do
98+
let(:verification_result) { false }
99+
100+
before do
101+
follow_redirect!
102+
end
103+
104+
it 'displays failure message' do
105+
expect(response.body).to include(I18n.t('administrate.controller.verify_school.error'))
106+
end
107+
end
108+
end
109+
110+
describe 'PUT #reject_school' do
111+
let(:creator) { create(:user) }
112+
let(:school) { create(:school, creator_id: creator.id) }
113+
let(:rejection_result) { nil }
114+
let(:verification_service) { instance_double(SchoolVerificationService, reject: rejection_result) }
115+
116+
before do
117+
stub_user_info_api_for(creator)
118+
allow(SchoolVerificationService).to receive(:new).with(school.id).and_return(verification_service)
119+
120+
patch admin_school_reject_school_path(school)
121+
end
122+
123+
it 'redirects to school path' do
124+
expect(response).to redirect_to(admin_school_path(school))
125+
end
126+
127+
describe 'when rejection was successful' do
128+
let(:rejection_result) { true }
129+
130+
before do
131+
follow_redirect!
132+
end
133+
134+
it 'displays success message' do
135+
expect(response.body).to include(I18n.t('administrate.controller.reject_school.success'))
136+
end
137+
end
138+
139+
describe 'when rejection was unsuccessful' do
140+
let(:rejection_result) { false }
141+
142+
before do
143+
follow_redirect!
144+
end
145+
146+
it 'displays failure message' do
147+
expect(response.body).to include(I18n.t('administrate.controller.reject_school.error'))
148+
end
149+
end
150+
end
151+
152+
private
153+
154+
def sign_in_as(user)
155+
allow(User).to receive(:from_omniauth).and_return(user)
156+
get '/auth/callback'
157+
end
158+
end

0 commit comments

Comments
 (0)