|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'School project return requests', type: :request do |
| 6 | + let(:headers) { { Authorization: UserProfileMock::TOKEN } } |
| 7 | + let(:school) { create(:school) } |
| 8 | + let(:student) { create(:student, school:) } |
| 9 | + let(:teacher) { create(:teacher, school:) } |
| 10 | + let(:school_class) { create(:school_class, teacher_ids: [teacher.id], school:) } |
| 11 | + let(:class_student) { create(:class_student, school_class:, student_id: student.id) } |
| 12 | + let(:lesson) { create(:lesson, school:, school_class:, user_id: teacher.id) } |
| 13 | + let(:teacher_project) { create(:project, user_id: teacher.id, school:, lesson:) } |
| 14 | + let!(:student_project) { create(:project, school_id: school.id, lesson_id: nil, user_id: student.id, remixed_from_id: teacher_project.id, locale: nil, finished: false) } |
| 15 | + let(:school_project_json) do |
| 16 | + { |
| 17 | + id: student_project.school_project.id, |
| 18 | + school_id: student_project.school_project.school_id, |
| 19 | + project_id: student_project.school_project.project_id, |
| 20 | + status: student_project.school_project.status, |
| 21 | + identifier: student_project.identifier |
| 22 | + }.to_json |
| 23 | + end |
| 24 | + |
| 25 | + context 'when logged in as student' do |
| 26 | + before do |
| 27 | + authenticated_in_hydra_as(student) |
| 28 | + post("/api/projects/#{student_project.identifier}/return", headers:) |
| 29 | + end |
| 30 | + |
| 31 | + it 'returns forbidden response' do |
| 32 | + expect(response).to have_http_status(:forbidden) |
| 33 | + end |
| 34 | + |
| 35 | + it 'does not change the status' do |
| 36 | + expect(student_project.school_project).to be_unsubmitted |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + context 'when logged in as teacher' do |
| 41 | + before do |
| 42 | + authenticated_in_hydra_as(teacher) |
| 43 | + end |
| 44 | + |
| 45 | + context('when transition is valid') do |
| 46 | + before do |
| 47 | + student_project.school_project.transition_status_to!(:submitted, student.id) |
| 48 | + post("/api/projects/#{student_project.identifier}/return", headers:) |
| 49 | + end |
| 50 | + |
| 51 | + it 'returns success response' do |
| 52 | + expect(response).to have_http_status(:ok) |
| 53 | + end |
| 54 | + |
| 55 | + it 'returns the school project json' do |
| 56 | + expect(response.body).to eq(school_project_json) |
| 57 | + end |
| 58 | + |
| 59 | + it 'sets the status to returned' do |
| 60 | + expect(student_project.school_project).to be_returned |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + context 'when attempting an invalid status transition' do |
| 65 | + before do |
| 66 | + post("/api/projects/#{student_project.identifier}/return", headers:) |
| 67 | + end |
| 68 | + |
| 69 | + it 'returns unauthorized response' do |
| 70 | + expect(response).to have_http_status(:unprocessable_entity) |
| 71 | + end |
| 72 | + |
| 73 | + it 'returns error message' do |
| 74 | + expect(JSON.parse(response.body)['error']).to eq("Cannot transition from 'unsubmitted' to 'returned'") |
| 75 | + end |
| 76 | + |
| 77 | + it 'does not change the status' do |
| 78 | + expect(student_project.school_project).to be_unsubmitted |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + context 'when user does not own the project and is not the class teacher' do |
| 84 | + let(:another_teacher) { create(:teacher, school:) } |
| 85 | + |
| 86 | + before do |
| 87 | + authenticated_in_hydra_as(another_teacher) |
| 88 | + post("/api/projects/#{student_project.identifier}/return", headers:) |
| 89 | + end |
| 90 | + |
| 91 | + it 'returns forbidden response' do |
| 92 | + expect(response).to have_http_status(:forbidden) |
| 93 | + end |
| 94 | + |
| 95 | + it 'does not change the status' do |
| 96 | + expect(student_project.school_project).to be_unsubmitted |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context 'when project does not exist' do |
| 101 | + before do |
| 102 | + authenticated_in_hydra_as(student) |
| 103 | + post('/api/projects/does-not-exist/return', headers:) |
| 104 | + end |
| 105 | + |
| 106 | + it 'returns not found response' do |
| 107 | + expect(response).to have_http_status(:not_found) |
| 108 | + end |
| 109 | + end |
| 110 | +end |
0 commit comments