This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathquestionnaires_controller_test.rb
More file actions
255 lines (217 loc) · 11.4 KB
/
questionnaires_controller_test.rb
File metadata and controls
255 lines (217 loc) · 11.4 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
require 'test_helper'
require 'minitest/mock'
class QuestionnairesControllerTest < ActionController::TestCase
include ActiveJob::TestHelper
setup do
@school = create(:school, name: "Another School")
@questionnaire = create(:questionnaire, school_id: @school.id)
end
context "while not authenticated" do
should "redirect to sign up page on questionnaire#new" do
get :new
assert_redirected_to new_user_session_path
end
should "redirect to sign up page on questionnaire#edit" do
get :edit
assert_redirected_to new_user_session_path
end
should "redirect to sign up page on questionnaire#update" do
patch :update, params: { questionnaire: { major: "different" } }
assert_redirected_to new_user_session_path
end
should "redirect to sign up page on questionnaire#destroy" do
assert_difference('Questionnaire.count', 0) do
delete :destroy
end
assert_redirected_to new_user_session_path
end
end
context "while authenticated without a completed questionnaire" do
setup do
@request.env["devise.mapping"] = Devise.mappings[:director]
@user = create(:user)
sign_in @user
end
should "get new" do
get :new
assert_response :success
end
should "create questionnaire" do
assert_difference('Questionnaire.count', 1) do
post :create, params: { questionnaire: { experience: @questionnaire.experience, interest: @questionnaire.interest, country: @questionnaire.country, phone: @questionnaire.phone, level_of_study: @questionnaire.level_of_study, date_of_birth: @questionnaire.date_of_birth, shirt_size: @questionnaire.shirt_size, school_id: @school.id, major: @questionnaire.major, gender: @questionnaire.gender, why_attend: @questionnaire.why_attend, graduation_year: @questionnaire.graduation_year, race_ethnicity: @questionnaire.race_ethnicity, agreement_ids: @questionnaire.agreements.map(&:id) } }
end
assert_redirected_to questionnaires_path
if HackathonConfig.auto_late_waitlist
assert_equal "late_waitlist", assigns(:questionnaire).acc_status
else
assert_equal "pending", assigns(:questionnaire).acc_status
end
end
should "not allow multiple questionnaires" do
assert_difference('Questionnaire.count', 1) do
post :create, params: { questionnaire: { experience: @questionnaire.experience, interest: @questionnaire.interest, country: @questionnaire.country, phone: @questionnaire.phone, level_of_study: @questionnaire.level_of_study, date_of_birth: @questionnaire.date_of_birth, shirt_size: @questionnaire.shirt_size, school_id: @school.id, major: @questionnaire.major, gender: @questionnaire.gender, why_attend: @questionnaire.why_attend, graduation_year: @questionnaire.graduation_year, race_ethnicity: @questionnaire.race_ethnicity, agreement_ids: @questionnaire.agreements.map(&:id) } }
post :create, params: { questionnaire: { experience: @questionnaire.experience, interest: @questionnaire.interest, country: @questionnaire.country, phone: @questionnaire.phone, level_of_study: @questionnaire.level_of_study, date_of_birth: @questionnaire.date_of_birth, shirt_size: @questionnaire.shirt_size, school_id: @school.id, major: @questionnaire.major, gender: @questionnaire.gender, why_attend: @questionnaire.why_attend, graduation_year: @questionnaire.graduation_year, race_ethnicity: @questionnaire.race_ethnicity, agreement_ids: @questionnaire.agreements.map(&:id) } }
end
assert_redirected_to questionnaires_path
end
context "with an invalid questionnaire" do
should "not allow creation" do
@questionnaire.delete
assert_difference('Questionnaire.count', 0) do
post :create, params: { questionnaire: { major: "a great major" } }
end
end
end
context "with block questionnaires set" do
should "not allow creation" do
HackathonConfig.accepting_questionnaires = false
assert_difference('Questionnaire.count', 0) do
post :create, params: { questionnaire: { experience: @questionnaire.experience, interest: @questionnaire.interest, country: @questionnaire.country, phone: @questionnaire.phone, level_of_study: @questionnaire.level_of_study, date_of_birth: @questionnaire.date_of_birth, shirt_size: @questionnaire.shirt_size, school_id: @school.id, major: @questionnaire.major, gender: @questionnaire.gender, why_attend: @questionnaire.why_attend, graduation_year: @questionnaire.graduation_year, race_ethnicity: @questionnaire.race_ethnicity, agreement_ids: @questionnaire.agreements.map(&:id) } }
end
end
end
context "#school_name" do
context "on create" do
should "save existing school name" do
post :create, params: { questionnaire: { experience: @questionnaire.experience, interest: @questionnaire.interest, country: @questionnaire.country, phone: @questionnaire.phone, level_of_study: @questionnaire.level_of_study, date_of_birth: @questionnaire.date_of_birth, shirt_size: @questionnaire.shirt_size, school_name: @school.name, major: @questionnaire.major, gender: @questionnaire.gender, why_attend: @questionnaire.why_attend, graduation_year: @questionnaire.graduation_year, race_ethnicity: @questionnaire.race_ethnicity, agreement_ids: @questionnaire.agreements.map(&:id) } }
assert_redirected_to questionnaires_path
assert_equal 1, School.all.count
end
should "create a new school when unknown" do
post :create, params: { questionnaire: { experience: @questionnaire.experience, interest: @questionnaire.interest, country: @questionnaire.country, phone: @questionnaire.phone, level_of_study: @questionnaire.level_of_study, date_of_birth: @questionnaire.date_of_birth, shirt_size: @questionnaire.shirt_size, school_name: "New School", major: @questionnaire.major, gender: @questionnaire.gender, why_attend: @questionnaire.why_attend, graduation_year: @questionnaire.graduation_year, race_ethnicity: @questionnaire.race_ethnicity, agreement_ids: @questionnaire.agreements.map(&:id) } }
assert_redirected_to questionnaires_path
assert_equal 2, School.all.count
end
should "send confirmation email to questionnaire" do
message = create(:message, type: 'automated', trigger: "questionnaire.pending")
assert_difference 'enqueued_jobs.size', 1 do
assert_difference 'Questionnaire.count', 1 do
post :create, params: { questionnaire: { experience: @questionnaire.experience, interest: @questionnaire.interest, country: @questionnaire.country, phone: @questionnaire.phone, level_of_study: @questionnaire.level_of_study, date_of_birth: @questionnaire.date_of_birth, shirt_size: @questionnaire.shirt_size, school_name: @school.name, major: @questionnaire.major, gender: @questionnaire.gender, why_attend: @questionnaire.why_attend, graduation_year: @questionnaire.graduation_year, race_ethnicity: @questionnaire.race_ethnicity, agreement_ids: @questionnaire.agreements.map(&:id) } }
end
end
questionnaire = Questionnaire.last
job = enqueued_jobs.last
args = job[:args]
job_name_arg = args[1]
message_id_arg = args[3]["args"][0]
user_id_arg = args[3]["args"][1]
assert_equal "bulk_message_email", job_name_arg, "expected correct job name in job args"
assert_equal message.id, message_id_arg, "expected correct message ID in job args"
assert_equal questionnaire.user_id, user_id_arg, "expected correct user ID in job args"
end
end
end
context "disabled fields are enabled" do
should "display why_attend field" do
get :new
assert_select '#questionnaire_why_attend', 1
end
end
context "disabled fields are disabled" do
should "not display why_attend field when disabled" do
HackathonManager.stub :field_enabled?, false do
get :new
end
assert_select '#questionnaire_why_attend', 0
end
end
end
context "while authenticated with a completed questionnaire" do
setup do
@request.env["devise.mapping"] = Devise.mappings[:director]
sign_in @questionnaire.user
end
should "show questionnaire" do
get :show
assert_response :success
end
should "get edit" do
get :edit
assert_response :success
end
should "get edit with questionnaire resume" do
@questionnaire.resume.attach(io: sample_file, filename: "sample_pdf.pdf")
@questionnaire.save
get :edit
assert_response :success
end
should "update questionnaire" do
patch :update, params: { questionnaire: { major: "Computer Science" } }
assert_redirected_to questionnaires_path
end
context "destroy questionnaire" do
should "if bus captain, notify directors that bus captain has been removed" do
@director = create(:director)
@questionnaire.update_attribute(:is_bus_captain, true)
assert_difference('enqueued_jobs.size', User.where(role: :director).size) do
delete :destroy
end
end
should "user destroy questionnaire" do
assert_difference('Questionnaire.count', -1) do
delete :destroy
end
assert_redirected_to questionnaires_path
end
end
context "with invalid questionnaire params" do
should "not allow updates" do
saved_major = @questionnaire.major
patch :update, params: { questionnaire: { major: "" } }
assert_equal saved_major, @questionnaire.reload.major
end
end
context "accessing #new after already submitting a questionnaire" do
should "redirect to existing questionnaire" do
get :new
assert_response :redirect
assert_redirected_to questionnaires_path
end
end
context "#school_name" do
context "on update" do
should "save existing school name" do
patch :update, params: { questionnaire: { school_name: @school.name } }
assert_redirected_to questionnaires_path
assert_equal 1, School.all.count
end
should "create a new school when unknown" do
patch :update, params: { questionnaire: { school_name: "New School" } }
assert_redirected_to questionnaires_path
assert_equal 2, School.all.count
end
end
end
context "#schools" do
should "not respond to search with no query" do
get :schools
assert_response 400
assert @response.body.blank?
end
should "not respond to search with short query" do
get :schools, params: { school: "Al" }
assert_response 400
assert @response.body.blank?
end
should "respond to school search" do
create(:school, name: "Alpha University")
create(:school, name: "Pheta College")
get :schools, params: { name: "Alph" }
assert_response :success
assert_equal 1, json_response.count
assert_equal "Alpha University", json_response[0]
end
should "prioritize schools with more applicants" do
create(:school, name: "Alpha College", questionnaire_count: 0)
create(:school, name: "Pheta College", questionnaire_count: 5)
get :schools, params: { name: "Coll" }
assert_response :success
assert_equal "Pheta College", json_response[0]
end
end
end
private
def json_response
ActiveSupport::JSON.decode @response.body
end
end