-
Notifications
You must be signed in to change notification settings - Fork 480
/
maker_controller_test.rb
491 lines (389 loc) · 16.6 KB
/
maker_controller_test.rb
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
require 'test_helper'
class MakerControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
setup do
@student = create :student
@teacher = create :teacher
@admin = create :admin
@school = create :school
@school_maker_high_needs = create :school, :is_maker_high_needs_school
@csd_2017 = ensure_course 'csd-2017', '2017'
@csd_2018 = ensure_course 'csd-2018', '2018'
@csd_2019 = ensure_course 'csd-2019', '2019'
@csd_2020_unstable = ensure_course 'csd-2020-unstable', '2020'
@csd6_2017 = ensure_script Script::CSD6_NAME, '2017'
@csd6_2018 = ensure_script Script::CSD6_2018_NAME, '2018'
@csd6_2019 = ensure_script Script::CSD6_2019_NAME, '2019'
@csd6_2020_unstable = ensure_script 'csd6-2020-unstable', '2020', false
end
test_redirect_to_sign_in_for :home
test "home loads for student" do
sign_in @student
get :home
assert_response :success
assert_select '#maker-home'
end
test "home loads for teacher" do
sign_in @teacher
get :home
assert_response :success
assert_select '#maker-home'
end
test "shows CSD6-2019 when there are no relevant assignments" do
assert_empty @student.scripts
assert_empty @student.section_courses
assert_nil @student.user_script_with_most_recent_progress
assert_equal @csd6_2019, MakerController.maker_script(@student)
end
test "assignment should take precedence over progress in a script" do
create :user_script, user: @student, script: @csd6_2019
create :follower, section: create(:section, unit_group: @csd_2017), student_user: @student
assert_equal @csd6_2017, MakerController.maker_script(@student)
end
test "shows CSD6-2019 if CSD6-2019 is assigned" do
create :user_script, user: @student, script: @csd6_2019, assigned_at: Time.now
assert_includes @student.scripts, @csd6_2019
assert_equal @csd6_2019, MakerController.maker_script(@student)
end
test "shows CSD6-2018 if CSD6-2018 is assigned" do
create :user_script, user: @student, script: @csd6_2018, assigned_at: Time.now
refute_includes @student.scripts, @csd6_2017
assert_includes @student.scripts, @csd6_2018
assert_equal @csd6_2018, MakerController.maker_script(@student)
end
test "shows CSD6-2017 if CSD6-2017 is assigned" do
create :user_script, user: @student, script: @csd6_2017, assigned_at: Time.now
assert_includes @student.scripts, @csd6_2017
refute_includes @student.scripts, @csd6_2018
assert_equal @csd6_2017, MakerController.maker_script(@student)
end
test "shows CSD6-2018 if both CSD6-2017 and CSD6-2018 are assigned" do
create :user_script, user: @student, script: @csd6_2017, assigned_at: Time.now
create :user_script, user: @student, script: @csd6_2018, assigned_at: Time.now
assert_includes @student.scripts, @csd6_2017
assert_includes @student.scripts, @csd6_2018
assert_equal @csd6_2018, MakerController.maker_script(@student)
end
test "shows CSD6-2019 if both CSD6-2018 and CSD6-2019 are assigned" do
create :user_script, user: @student, script: @csd6_2018, assigned_at: Time.now
create :user_script, user: @student, script: @csd6_2019, assigned_at: Time.now
assert_includes @student.scripts, @csd6_2018
assert_includes @student.scripts, @csd6_2019
assert_equal @csd6_2019, MakerController.maker_script(@student)
end
test "shows CSD6-2019 if CSD-2019 is assigned" do
create :follower, section: create(:section, unit_group: @csd_2019), student_user: @student
assert_includes @student.section_courses, @csd_2019
assert_equal @csd6_2019, MakerController.maker_script(@student)
end
test "shows CSD6-2018 if CSD-2018 is assigned" do
create :follower, section: create(:section, unit_group: @csd_2018), student_user: @student
refute_includes @student.section_courses, @csd_2017
assert_includes @student.section_courses, @csd_2018
assert_equal @csd6_2018, MakerController.maker_script(@student)
end
test "shows CSD6-2017 if CSD-2017 is assigned" do
create :follower, section: create(:section, unit_group: @csd_2017), student_user: @student
assert_includes @student.section_courses, @csd_2017
refute_includes @student.section_courses, @csd_2018
assert_equal @csd6_2017, MakerController.maker_script(@student)
end
test "shows CSD6-2018 if both CSD-2017 and CSD-2018 are assigned" do
create :follower, section: create(:section, unit_group: @csd_2017), student_user: @student
create :follower, section: create(:section, unit_group: @csd_2018), student_user: @student
@student.reload
assert_includes @student.section_courses, @csd_2017
assert_includes @student.section_courses, @csd_2018
assert_equal @csd6_2018, MakerController.maker_script(@student)
end
test "shows CSD6-2018 if both CSD6-2017 and CSD-2018 are assigned" do
create :follower, section: create(:section, script: @csd6_2017), student_user: @student
create :follower, section: create(:section, unit_group: @csd_2018), student_user: @student
@student.reload
assert_includes @student.scripts, @csd6_2017
refute_includes @student.section_courses, @csd_2017
refute_includes @student.scripts, @csd6_2018
assert_includes @student.section_courses, @csd_2018
assert_equal @csd6_2018, MakerController.maker_script(@student)
end
test "shows CSD6-2018 if both CSD-2017 and CSD6-2018 are assigned" do
create :follower, section: create(:section, unit_group: @csd_2017), student_user: @student
create :follower, section: create(:section, script: @csd6_2018), student_user: @student
assert_includes @student.section_scripts, @csd6_2018
refute_includes @student.scripts, @csd6_2017
assert_includes @student.section_courses, @csd_2017
assert_includes @student.scripts, @csd6_2018
refute_includes @student.section_courses, @csd_2018
assert_equal @csd6_2018, MakerController.maker_script(@student)
end
test "apply: fails if unit_6_intention not provided" do
sign_in @teacher
assert_raises ActionController::ParameterMissing do
post :apply
end
end
test "apply: fails if user doesn't have an application" do
sign_in @teacher
post :apply, params: {unit_6_intention: 'no'}
assert_response :not_found
end
test "apply: fails if school doesn't meet eligibility requirements" do
sign_in @teacher
create :circuit_playground_discount_application,
user: @teacher,
school: @school,
full_discount: false # This should be true before submitting a unit 6 intention
CircuitPlaygroundDiscountApplication.stubs(:studio_person_pd_eligible?).returns(true)
CircuitPlaygroundDiscountApplication.stubs(:student_progress_eligible?).returns(true)
post :apply, params: {unit_6_intention: 'no'}
assert_response :forbidden
end
test "apply: fails if teacher doesn't meet eligibility requirements" do
sign_in @teacher
create :circuit_playground_discount_application,
user: @teacher,
school: @school,
full_discount: true
# These should be true before submitting a unit 6 intention
CircuitPlaygroundDiscountApplication.stubs(:studio_person_pd_eligible?).returns(false)
CircuitPlaygroundDiscountApplication.stubs(:student_progress_eligible?).returns(false)
post :apply, params: {unit_6_intention: 'no'}
assert_response :forbidden
end
test "apply: fails if nonsense unit_6_intention provided" do
sign_in @teacher
create :circuit_playground_discount_application,
user: @teacher,
school: @school_maker_high_needs,
full_discount: true
CircuitPlaygroundDiscountApplication.stubs(:studio_person_pd_eligible?).returns(true)
CircuitPlaygroundDiscountApplication.stubs(:student_progress_eligible?).returns(true)
assert_raises ArgumentError do
post :apply, params: {unit_6_intention: 'not an answer'}
end
end
test "apply: updates the application with the intention" do
sign_in @teacher
application = create :circuit_playground_discount_application,
user: @teacher,
school: @school_maker_high_needs,
full_discount: true
assert_nil application.unit_6_intention
CircuitPlaygroundDiscountApplication.stubs(:studio_person_pd_eligible?).returns(true)
CircuitPlaygroundDiscountApplication.stubs(:student_progress_eligible?).returns(true)
post :apply, params: {unit_6_intention: 'no'}
assert_response :success
application.reload
assert_equal 'no', application.unit_6_intention
end
test "schoolchoice: fails if no school id provided" do
sign_in @teacher
assert_raises ActionController::ParameterMissing do
post :schoolchoice
end
end
test "schoolchoice: fails if given a bad school_id" do
sign_in @teacher
assert_raises ActiveRecord::RecordNotFound do
post :schoolchoice, params: {nces: 'asdf'}
end
end
test "schoolchoice: fails if user already has an application with a selected school" do
sign_in @teacher
create :circuit_playground_discount_application,
user: @teacher,
school: @school,
full_discount: true
post :schoolchoice, params: {nces: @school.id}
assert_response :forbidden
end
test "schoolchoice: Creates an application" do
sign_in @teacher
assert_creates CircuitPlaygroundDiscountApplication do
post :schoolchoice, params: {nces: @school.id}
assert_response :success
expected = {"school_high_needs_eligible" => false}
assert_equal expected, JSON.parse(@response.body)
end
end
test "schoolchoice: Returns full discount when applicant from Alaska" do
sign_in @teacher
school_alaska = create(:school, state: 'AK')
post :schoolchoice, params: {nces: school_alaska.id}
application = CircuitPlaygroundDiscountApplication.find_by(user_id: @teacher)
assert application.full_discount
end
test "schoolchoice: Returns full discount when applicant from Hawaii" do
sign_in @teacher
school_hawaii = create(:school, state: 'HI')
post :schoolchoice, params: {nces: school_hawaii.id}
application = CircuitPlaygroundDiscountApplication.find_by(user_id: @teacher)
assert application.full_discount
end
test "schoolchoice: Returns no discount when school not in Alaska or Hawaii" do
sign_in @teacher
school_washington = create(:school, state: 'WA')
post :schoolchoice, params: {nces: school_washington.id}
application = CircuitPlaygroundDiscountApplication.find_by(user_id: @teacher)
refute application.full_discount
end
test "complete: fails if not given a signature" do
DCDO.stubs(:get).with('currently_distributing_discount_codes', false).returns(true)
sign_in @teacher
assert_raises ActionController::ParameterMissing do
post :complete, params: {nces: 'asdf'}
end
end
test "complete: fails if user doesnt have application" do
DCDO.stubs(:get).with('currently_distributing_discount_codes', false).returns(true)
sign_in @teacher
post :complete, params: {signature: "My Name"}
assert_response :not_found
end
test "complete: fails if application not in the right state" do
DCDO.stubs(:get).with('currently_distributing_discount_codes', false).returns(true)
sign_in @teacher
# no intention to teach unit 6
application = create :circuit_playground_discount_application,
user_id: @teacher.id,
unit_6_intention: 'no'
post :complete, params: {signature: "My Name"}
assert_response :forbidden
# intend to teach unit 6, but has not confirmed school
application.update!(unit_6_intention: 'yesSpring2020')
post :complete, params: {signature: "My Name"}
assert_response :forbidden
# has confirmed school, but already has a code
application.update!(school_id: @school.id, circuit_playground_discount_code_id: 123)
post :complete, params: {signature: "My Name"}
assert_response :forbidden
end
test "complete: returns a new code" do
DCDO.stubs(:get).with('currently_distributing_discount_codes', false).returns(true)
sign_in @teacher
create :circuit_playground_discount_application,
user: @teacher,
school: @school,
unit_6_intention: 'yesSpring2020',
full_discount: true
code = create :circuit_playground_discount_code
post :complete, params: {signature: "My name"}
assert_response :success
expected = {code: code.code, expiration: code.expiration}.to_json
assert_equal expected, @response.body
end
test "complete: works after admin override" do
DCDO.stubs(:get).with('facilitator_ids_eligible_for_maker_discount', []).returns([])
DCDO.stubs(:get).with('currently_distributing_discount_codes', false).returns(true)
sign_in @admin
post :override, params: {user: @teacher.id, full_discount: true}
assert_response :success
sign_out @admin
create :circuit_playground_discount_code
sign_in @teacher
post :complete, params: {signature: "My name"}
assert_response :success
end
test "application_status: fails if not admin" do
sign_in @teacher
get :application_status, params: {user: @teacher.id}
assert_response :forbidden
end
test "application_status: works for user without in progress application" do
sign_in @admin
get :application_status, params: {user: @teacher.id}
assert_response :success
refute_nil JSON.parse(@response.body)['application']
# actual contents tested in CircuitPlaygroundDiscountApplication tests
end
test "application_status: works for user with in progress application" do
sign_in @admin
create :circuit_playground_discount_application,
user_id: @teacher.id,
unit_6_intention: 'yesSpring2020',
school_id: @school.id,
full_discount: true
get :application_status, params: {user: @teacher.id}
assert_response :success
refute_nil JSON.parse(@response.body)['application']
# actual contents tested in CircuitPlaygroundDiscountApplication tests
end
test "override: fails if not admin" do
sign_in @teacher
post :override, params: {user: @teacher.id, full_discount: true}
assert_response :forbidden
end
test "override: can override discount for a user that has not started an application" do
sign_in @admin
assert_equal 0, CircuitPlaygroundDiscountApplication.where(user_id: @teacher.id).length
post :override, params: {user: @teacher.id, full_discount: true}
assert_response :success
expected = {
"application" => {
"is_pd_eligible" => false,
"is_progress_eligible" => false,
"user_school" => {
"id" => nil,
"name" => nil,
"high_needs" => nil,
},
"application_school" => {
"id" => nil,
"name" => nil,
"high_needs" => nil,
},
"unit_6_intention" => nil,
"full_discount" => true,
"admin_set_status" => true,
"discount_code" => nil,
}
}
assert_equal expected, JSON.parse(@response.body)
assert_equal 1, CircuitPlaygroundDiscountApplication.where(user_id: @teacher.id).length
end
test "override: can update an existing application that does not yet have a code" do
sign_in @admin
# Application in which user has answered question about unit6 intentions, but
# has not yet confirmed school
create :circuit_playground_discount_application,
user_id: @teacher.id,
unit_6_intention: 'yesSpring2020'
post :override, params: {user: @teacher.id, full_discount: true}
assert_response :success
expected = {
"application" => {
"is_pd_eligible" => false,
"is_progress_eligible" => false,
"user_school" => {
"id" => nil,
"name" => nil,
"high_needs" => nil,
},
"application_school" => {
"id" => nil,
"name" => nil,
"high_needs" => nil,
},
"unit_6_intention" => "yesSpring2020",
"full_discount" => true,
"admin_set_status" => true,
"discount_code" => nil,
}
}
assert_equal expected, JSON.parse(@response.body)
assert_equal 1, CircuitPlaygroundDiscountApplication.where(user_id: @teacher.id).length
end
private
def ensure_script(script_name, version_year, is_stable=true)
Script.find_by_name(script_name) ||
create(:script, name: script_name, family_name: 'csd6', version_year: version_year, is_stable: is_stable).tap do |script|
lesson_group = create :lesson_group, script: script
lesson = create :lesson, script: script, lesson_group: lesson_group
create :script_level, script: script, lesson: lesson
end
end
def ensure_course(course_name, version_year)
UnitGroup.find_by_name(course_name) ||
create(:unit_group, name: course_name, version_year: version_year, family_name: UnitGroup::CSD)
end
end