-
Notifications
You must be signed in to change notification settings - Fork 481
/
sections_controller_test.rb
281 lines (234 loc) · 8.53 KB
/
sections_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
# -*- coding: utf-8 -*-
require 'test_helper'
class SectionsControllerTest < ActionController::TestCase
self.use_transactional_test_case = true
setup_all do
@teacher = create(:teacher)
@word_section = create(:section, user: @teacher, login_type: 'word')
@word_user_1 = create(:follower, section: @word_section).student_user
@picture_section = create(:section, user: @teacher, login_type: 'picture')
@picture_user_1 = create(:follower, section: @picture_section).student_user
@regular_section = create(:section, user: @teacher, login_type: 'email')
@flappy_section = create(:section, user: @teacher, login_type: 'word', script_id: Script.flappy_script.id)
@flappy_user_1 = create(:follower, section: @flappy_section).student_user
end
setup do
# place in setup instead of setup_all otherwise course ends up being serialized
# to a file if levelbuilder_mode is true
@course = create(:course)
@script_in_course = create(:script)
create(:course_script, script: @script_in_course, course: @course, position: 1)
@section_with_course = create(:section, user: @teacher, login_type: 'word', course_id: @course.id)
@section_with_course_user_1 = create(:follower, section: @section_with_course).student_user
end
test "do not show login screen for invalid section code" do
assert_raises(ActiveRecord::RecordNotFound) do
get :show, params: {id: @word_section.id} # we use code not id
end
end
test "do not show login screen for non-picture/word sections" do
assert_raises(ActiveRecord::RecordNotFound) do
get :show, params: {id: @regular_section.code}
end
end
test "show login screen for picture section" do
get :show, params: {id: @picture_section.code}
assert_response :success
end
test "show login screen for word section" do
get :show, params: {id: @word_section.code}
assert_response :success
end
test "valid log_in wih picture" do
assert_difference '@picture_user_1.reload.sign_in_count' do # devise Trackable fields are updated
post :log_in, params: {
id: @picture_section.code,
user_id: @picture_user_1.id,
secret_picture_id: @picture_user_1.secret_picture_id
}
end
assert_redirected_to '/'
end
test "invalid log_in wih picture" do
assert_no_difference '@picture_user_1.reload.sign_in_count' do # devise Trackable fields are not updated
post :log_in, params: {
id: @picture_section.code,
user_id: @picture_user_1.id,
secret_picture_id: @picture_user_1.secret_picture_id + 1
}
end
assert_redirected_to section_path(id: @picture_section.code)
end
test "valid log_in wih word" do
assert_difference '@word_user_1.reload.sign_in_count' do # devise Trackable fields are updated
post :log_in, params: {
id: @word_section.code,
user_id: @word_user_1.id,
secret_words: @word_user_1.secret_words
}
end
assert_redirected_to '/'
end
test "invalid log_in wih word" do
assert_no_difference '@word_user_1.reload.sign_in_count' do # devise Trackable fields are not updated
post :log_in, params: {
id: @word_section.code,
user_id: @word_user_1.id,
secret_words: "not correct"
}
end
assert_redirected_to section_path(id: @word_section.code)
end
test "login to section with a script redirects to script" do
post :log_in, params: {
id: @flappy_section.code,
user_id: @flappy_user_1.id,
secret_words: @flappy_user_1.secret_words
}
assert_redirected_to '/s/flappy'
end
test "login to section with a course redirects to course" do
post :log_in, params: {
id: @section_with_course.code,
user_id: @section_with_course_user_1.id,
secret_words: @section_with_course_user_1.secret_words
}
assert_redirected_to "/courses/#{@section_with_course.course.name}"
end
test "login with show_pairing_dialog shows pairing dialog" do
post :log_in, params: {
id: @flappy_section.code,
user_id: @flappy_user_1.id,
secret_words: @flappy_user_1.secret_words,
show_pairing_dialog: '1'
}
assert_redirected_to '/s/flappy'
assert session[:show_pairing_dialog]
end
test "login without show_pairing_dialog shows pairing dialog" do
post :log_in, params: {
id: @flappy_section.code,
user_id: @flappy_user_1.id,
secret_words: @flappy_user_1.secret_words
}
assert_redirected_to '/s/flappy'
refute session[:show_pairing_dialog]
end
test "cannot log in to section if you are not in the section" do
assert_no_difference '@picture_user_1.reload.sign_in_count' do # devise Trackable fields are not updated
post :log_in, params: {
id: @picture_section.code,
user_id: @word_user_1.id,
secret_picture_id: @word_user_1.secret_picture_id
}
end
assert_redirected_to section_path(id: @picture_section.code)
end
test "update: can update section you own" do
sign_in @teacher
section_with_script = create(
:section,
user: @teacher,
script_id: Script.flappy_script.id,
login_type: Section::LOGIN_TYPE_WORD,
grade: "1",
stage_extras: true,
pairing_allowed: false,
hidden: true
)
post :update, params: {
id: section_with_script.id,
course_id: @course.id,
name: "My Section",
login_type: Section::LOGIN_TYPE_PICTURE,
grade: "K",
stage_extras: false,
pairing_allowed: true,
hidden: false
}
assert_response :success
# Cannot use section_with_script.reload because login_type has changed
section_with_script = Section.find(section_with_script.id)
assert_equal(@course.id, section_with_script.course_id)
assert_nil(section_with_script.script_id)
assert_equal("My Section", section_with_script.name)
assert_equal(Section::LOGIN_TYPE_PICTURE, section_with_script.login_type)
assert_equal("K", section_with_script.grade)
assert_equal(false, section_with_script.stage_extras)
assert_equal(true, section_with_script.pairing_allowed)
assert_equal(false, section_with_script.hidden)
end
test "update: cannot update section you dont own" do
other_teacher = create(:teacher)
sign_in other_teacher
post :update, params: {
id: @regular_section.id,
course_id: @course.id,
}
assert_response :forbidden
end
test "update: cannot update section if not logged in " do
post :update, params: {
id: @regular_section.id,
course_id: @course.id,
}
assert_response :redirect
end
test "update: can set course and script" do
sign_in @teacher
section = create(:section, user: @teacher, script_id: Script.flappy_script.id)
post :update, as: :json, params: {
id: section.id,
course_id: @course.id,
script_id: @script_in_course.id
}
assert_response :success
section.reload
assert_equal(@course.id, section.course_id)
assert_equal(@script_in_course.id, section.script_id)
end
test "update: non-matching course/script rejected" do
sign_in @teacher
section = create(:section, user: @teacher, script_id: Script.flappy_script.id)
post :update, params: {
id: section.id,
course_id: @course.id,
script_id: Script.artist_script.id
}
assert_response 400
end
test "update: can set course-less script" do
sign_in @teacher
section = create(:section, user: @teacher, script_id: Script.flappy_script.id)
post :update, params: {
id: section.id,
script_id: Script.artist_script.id
}
assert_response :success
section.reload
assert_nil section.course_id
assert_equal(Script.artist_script.id, section.script_id)
end
test "upate: setting a script results in UserScripts for students" do
sign_in @teacher
section = create(:section, user: @teacher, script_id: Script.flappy_script.id)
student = create(:follower, section: section).student_user
assert_nil UserScript.find_by(script: Script.artist_script, user: student)
post :update, params: {
id: section.id,
script_id: Script.artist_script.id
}
assert_not_nil UserScript.find_by(script: Script.artist_script, user: student)
end
test "update: can set script from nested script param" do
sign_in @teacher
section = create(:section, user: @teacher, script_id: Script.flappy_script.id)
post :update, as: :json, params: {
id: section.id,
script: {id: @script_in_course.id}
}
assert_response :success
section.reload
assert_equal(@script_in_course.id, section.script_id)
end
end