|
70 | 70 | expect(data[:status]).to eq('wrong_school') |
71 | 71 | end |
72 | 72 |
|
73 | | - it 'returns status: not_a_student when the user is a teacher of this school' do |
| 73 | + it 'returns status: joinable_as_teacher when the user is a teacher of this school not yet in the class' do |
74 | 74 | create(:teacher_role, school:, user_id: student.id) |
75 | 75 |
|
76 | 76 | get "/api/join/#{school_class.join_code}", headers: headers |
77 | 77 |
|
78 | 78 | data = JSON.parse(response.body, symbolize_names: true) |
79 | | - expect(data[:status]).to eq('not_a_student') |
| 79 | + expect(data[:status]).to eq('joinable_as_teacher') |
| 80 | + end |
| 81 | + |
| 82 | + it 'returns status: already_member when the user is already a teacher in the class' do |
| 83 | + create(:teacher_role, school:, user_id: student.id) |
| 84 | + ClassTeacher.create!(school_class:, teacher_id: student.id) |
| 85 | + |
| 86 | + get "/api/join/#{school_class.join_code}", headers: headers |
| 87 | + |
| 88 | + data = JSON.parse(response.body, symbolize_names: true) |
| 89 | + expect(data[:status]).to eq('already_member') |
80 | 90 | end |
81 | 91 |
|
82 | | - it 'returns status: not_a_student when the user is an owner of this school' do |
| 92 | + it 'returns status: owner when the user is an owner of this school' do |
83 | 93 | create(:owner_role, school:, user_id: student.id) |
84 | 94 |
|
85 | 95 | get "/api/join/#{school_class.join_code}", headers: headers |
86 | 96 |
|
87 | 97 | data = JSON.parse(response.body, symbolize_names: true) |
88 | | - expect(data[:status]).to eq('not_a_student') |
| 98 | + expect(data[:status]).to eq('owner') |
89 | 99 | end |
90 | 100 |
|
91 | 101 | it 'returns status: not_a_student for a teacher of a different school (not wrong_school)' do |
|
204 | 214 | end |
205 | 215 | end |
206 | 216 |
|
207 | | - it 'responds with 403 not_a_student when the user is a teacher of the school' do |
| 217 | + it 'adds the user to the class as a teacher and returns a redirect URL' do |
208 | 218 | create(:teacher_role, school:, user_id: student.id) |
| 219 | + school_class # force creation before the request |
209 | 220 |
|
210 | 221 | expect do |
211 | 222 | post "/api/join/#{school_class.join_code}", headers: headers |
212 | | - end.not_to change(ClassStudent, :count) |
| 223 | + end.to change(ClassTeacher, :count).by(1) |
213 | 224 |
|
214 | | - expect(response).to have_http_status(:forbidden) |
| 225 | + expect(response).to have_http_status(:ok) |
215 | 226 | data = JSON.parse(response.body, symbolize_names: true) |
216 | | - expect(data[:error]).to eq('not_a_student') |
| 227 | + expect(data[:redirect_url]).to eq("/school/#{school.code}/class/#{school_class.code}") |
| 228 | + expect(ClassTeacher.exists?(school_class:, teacher_id: student.id)).to be(true) |
| 229 | + expect(ClassStudent.exists?(school_class:, student_id: student.id)).to be(false) |
| 230 | + expect(Role.where(user_id: student.id, school:).pluck(:role)).to eq(['teacher']) |
217 | 231 | end |
218 | 232 |
|
219 | | - it 'responds with 403 not_a_student when the user is an owner of the school' do |
| 233 | + it 'is idempotent when the user is already a teacher in the class' do |
| 234 | + create(:teacher_role, school:, user_id: student.id) |
| 235 | + ClassTeacher.create!(school_class:, teacher_id: student.id) |
| 236 | + |
| 237 | + expect do |
| 238 | + post "/api/join/#{school_class.join_code}", headers: headers |
| 239 | + end.not_to change(ClassTeacher, :count) |
| 240 | + |
| 241 | + expect(response).to have_http_status(:ok) |
| 242 | + data = JSON.parse(response.body, symbolize_names: true) |
| 243 | + expect(data[:redirect_url]).to eq("/school/#{school.code}/class/#{school_class.code}") |
| 244 | + end |
| 245 | + |
| 246 | + it 'redirects an owner into the class without adding them to it' do |
220 | 247 | create(:owner_role, school:, user_id: student.id) |
221 | 248 |
|
222 | | - post "/api/join/#{school_class.join_code}", headers: headers |
| 249 | + expect do |
| 250 | + post "/api/join/#{school_class.join_code}", headers: headers |
| 251 | + end.not_to change(ClassStudent, :count) |
223 | 252 |
|
224 | | - expect(response).to have_http_status(:forbidden) |
| 253 | + expect(response).to have_http_status(:ok) |
225 | 254 | data = JSON.parse(response.body, symbolize_names: true) |
226 | | - expect(data[:error]).to eq('not_a_student') |
| 255 | + expect(data[:redirect_url]).to eq("/school/#{school.code}/class/#{school_class.code}") |
| 256 | + expect(ClassTeacher.exists?(school_class:, teacher_id: student.id)).to be(false) |
| 257 | + expect(Role.where(user_id: student.id, school:).pluck(:role)).to eq(['owner']) |
227 | 258 | end |
228 | 259 |
|
229 | 260 | it 'responds with 404 when the join code does not exist' do |
|
0 commit comments