-
Notifications
You must be signed in to change notification settings - Fork 482
/
transfers_controller_test.rb
202 lines (166 loc) · 7.18 KB
/
transfers_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
require 'test_helper'
class TransfersControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
# As we no longer generate codes with vowels, this code should not already
# exist in the test DB.
NONEXISTENT_SECTION_CODE = 'AEIOUY'.freeze
setup do
@teacher = create(:teacher)
sign_in(@teacher)
@word_section = create(:section, user: @teacher, login_type: 'word')
@word_student = create(:follower, section: @word_section).student_user
@picture_section = create(:section, user: @teacher, login_type: 'picture')
@picture_student = create(:follower, section: @picture_section).student_user
@params = {
student_ids: @word_student.id.to_s,
current_section_code: @word_section.code,
new_section_code: @picture_section.code,
stay_enrolled_in_current_section: true
}
@other_teacher = create :teacher
@other_teacher_section = create :section, user: @other_teacher, login_type: 'word'
end
test "returns an error when student ids are not provided" do
@params.delete(:student_ids)
post :create, params: @params
assert_response :bad_request
assert_equal "Please provide student_ids.", json_response["error"]
end
test "returns an error when stay_enrolled_in_current_section is not provided" do
@params.delete(:stay_enrolled_in_current_section)
post :create, params: @params
assert_response :bad_request
end
test "returns an error when new_section_code is not provided" do
@params.delete(:new_section_code)
post :create, params: @params
assert_response :bad_request
end
test "returns an error when current_section_code is not provided" do
@params.delete(:current_section_code)
post :create, params: @params
assert_response :bad_request
end
test "returns an error when new_section_code does not exist" do
@params[:new_section_code] = NONEXISTENT_SECTION_CODE
post :create, params: @params
assert_response :not_found
assert_equal(
"Sorry, but section #{NONEXISTENT_SECTION_CODE} does not exist. Please "\
"enter a different section code.",
json_response["error"]
)
end
test "returns an error when the current_section_code does not exist" do
@params[:current_section_code] = NONEXISTENT_SECTION_CODE
post :create, params: @params
assert_response :not_found
assert_equal(
"Sorry, but section #{NONEXISTENT_SECTION_CODE} does not exist. Please "\
"enter a different section code.",
json_response["error"]
)
end
test "returns an error when one of the student_ids does not exist" do
student_ids_with_invalid = [@word_student.id, User.last.id + 1].join(',')
@params[:student_ids] = student_ids_with_invalid
post :create, params: @params
assert_response :not_found
end
test "transferring without logging in fails" do
# TODO(asher): Make this test confirm that the transfer did not occur. Alternately, fix the
# controller to do something saner.
sign_out(@teacher)
post :create, params: @params
# Ergh, Devise will return a :found instead of a 401 because of the way
# this route is set up
assert_response :found
end
test "when the new section belongs to the same teacher, students should no longer be in the current section if stay_enrolled_in_current_section is false" do
@params[:stay_enrolled_in_current_section] = false
post :create, params: @params
assert_response :no_content
refute Follower.exists?(student_user: @word_student, section: @word_section)
end
test "when the new section belongs to a different teacher, students should stay enrolled in the current section if stay_enrolled_in_current_section is true" do
@params[:new_section_code] = @other_teacher_section.code
@params[:stay_enrolled_in_current_section] = true
post :create, params: @params
assert_response :no_content
assert Follower.exists?(student_user: @word_student, section: @word_section)
end
test "when the new section belongs to a different teacher, students should no longer be in the current section if stay_enrolled_in_current_section is false" do
@params[:new_section_code] = @other_teacher_section.code
@params[:stay_enrolled_in_current_section] = false
post :create, params: @params
assert_response :no_content
refute Follower.exists?(student_user: @word_student, section: @word_section)
end
test "transferring to the same section does nothing" do
@params[:new_section_code] = @params[:current_section_code]
post :create, params: @params
assert_response :bad_request
assert Follower.exists?(student_user: @word_student, section: @word_section)
end
test "transferring a student with a messed up email succeeds" do
@word_student.update_attribute(:email, '')
@word_student.update_attribute(:hashed_email, '')
post :create, params: @params
assert_response :no_content
assert Follower.exists?(student_user: @word_student, section: @picture_section)
end
test "multiple students can be transferred" do
new_student = create(:student)
Follower.create!(
user: @teacher,
student_user: new_student,
section: @word_section
)
@params[:student_ids] = "#{new_student.id},#{@word_student.id}"
post :create, params: @params
assert_response :no_content
assert Follower.exists?(student_user: new_student, section: @picture_section)
assert Follower.exists?(student_user: @word_student, section: @picture_section)
end
test "students can be transferred to other teachers if they already belong to a section belonging to the other teacher" do
already_enrolled_section = create(:section, user: @other_teacher, login_type: 'word')
Follower.create!(
user: @other_teacher,
student_user: @word_student,
section: already_enrolled_section
)
@params[:new_section_code] = @other_teacher_section.code
assert_creates(Follower) do
post :create, params: @params
assert_response :no_content
end
assert Follower.find_by(section: @other_teacher_section, student_user: @word_student)
end
test "student cannot be transferred to another section if already in the section" do
Follower.create! section: @picture_section, student_user: @word_student
assert_does_not_create(Follower) do
post :create, params: @params
assert_response :bad_request
assert_equal "You cannot move these students because they are already in the new section.",
json_response["error"]
end
end
test "students cannot be transferred to other soft-deleted teachers" do
@other_teacher.update!(deleted_at: DateTime.now)
@params[:new_section_code] = @other_teacher_section.code
@params[:stay_enrolled_in_current_section] = false
post :create, params: @params
assert_response :not_found
assert_equal(
"Sorry, but section #{@other_teacher_section.code} does not exist. "\
"Please enter a different section code.",
json_response['error']
)
assert Follower.exists?(student_user: @word_student, section: @word_section)
end
test "current section must belong to current user" do
@word_section.update!(user: @other_teacher)
post :create, params: @params
assert_response :forbidden
end
end