|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Set read_at on feedback 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, visibility: 'students') } |
| 13 | + let(:teacher_project) { create(:project, user_id: teacher.id, school:, lesson:) } |
| 14 | + let(:student_project) { create(:project, user_id: class_student.student_id, school:, parent: teacher_project) } |
| 15 | + let!(:feedback) { create(:feedback, school_project: student_project.school_project, user_id: teacher.id, content: 'Excellent work!') } |
| 16 | + let(:feedback_json) do |
| 17 | + { |
| 18 | + id: feedback.id, |
| 19 | + school_project_id: feedback.school_project_id, |
| 20 | + user_id: feedback.user_id, |
| 21 | + content: feedback.content, |
| 22 | + created_at: feedback.created_at, |
| 23 | + updated_at: feedback.updated_at, |
| 24 | + read_at: feedback.read_at |
| 25 | + }.to_json |
| 26 | + end |
| 27 | + |
| 28 | + context 'when logged in as the class teacher' do |
| 29 | + before do |
| 30 | + authenticated_in_hydra_as(teacher) |
| 31 | + put("/api/projects/#{student_project.identifier}/feedback/#{feedback.id}/read", headers: headers) |
| 32 | + feedback.reload |
| 33 | + end |
| 34 | + |
| 35 | + it 'returns forbidden response' do |
| 36 | + expect(response).to have_http_status(:forbidden) |
| 37 | + end |
| 38 | + |
| 39 | + it 'does not set the read_at on feedback' do |
| 40 | + expect(feedback.read_at).to be_nil |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context 'when logged in as the student' do |
| 45 | + before do |
| 46 | + authenticated_in_hydra_as(student) |
| 47 | + end |
| 48 | + |
| 49 | + context 'when feedback exists' do |
| 50 | + before do |
| 51 | + put("/api/projects/#{student_project.identifier}/feedback/#{feedback.id}/read", headers: headers) |
| 52 | + feedback.reload |
| 53 | + end |
| 54 | + |
| 55 | + it 'returns ok response' do |
| 56 | + expect(response).to have_http_status(:ok) |
| 57 | + end |
| 58 | + |
| 59 | + it 'returns the feedback json' do |
| 60 | + expect(response.body).to eq(feedback_json) |
| 61 | + end |
| 62 | + |
| 63 | + it 'does set the read_at on feedback' do |
| 64 | + expect(feedback.read_at).to be_present |
| 65 | + end |
| 66 | + |
| 67 | + it 'sets read_at to be a time' do |
| 68 | + expect(feedback.read_at).to be_a(ActiveSupport::TimeWithZone) |
| 69 | + end |
| 70 | + |
| 71 | + it 'sets read_at to a recent time' do |
| 72 | + expect(feedback.read_at).to be_within(5.seconds).of(Time.current) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context 'when feedback does not exist' do |
| 77 | + before do |
| 78 | + put("/api/projects/#{student_project.identifier}/feedback/does-not-exist/read", headers: headers) |
| 79 | + feedback.reload |
| 80 | + end |
| 81 | + |
| 82 | + it 'returns not found response' do |
| 83 | + expect(response).to have_http_status(:not_found) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments