|
155 | 155 | expect(this_class[:unread_feedback_count]).to eq(1) |
156 | 156 | end |
157 | 157 |
|
| 158 | + it 'returns correct unread_feedback_count across multiple classes with different amounts of unread feedback' do |
| 159 | + authenticated_in_hydra_as(student) |
| 160 | + stub_user_info_api_for(teacher) |
| 161 | + |
| 162 | + # Create a second class the student is a member of |
| 163 | + school_class_2 = create(:school_class, name: 'Second Class', teacher_ids: [teacher.id], school:) |
| 164 | + create(:class_student, school_class: school_class_2, student_id: student.id) |
| 165 | + |
| 166 | + # Class 1: Create 2 remixes with unread feedback |
| 167 | + lesson_one = create(:lesson, school:, school_class:, visibility: 'students', user_id: teacher.id) |
| 168 | + remix_one = create(:project, school:, lesson: lesson_one, parent: lesson_one.project, remixed_from_id: lesson_one.project.id, user_id: student.id) |
| 169 | + create(:feedback, school_project: remix_one.school_project, user_id: teacher.id, content: 'Unread 1', read_at: nil) |
| 170 | + |
| 171 | + lesson_two = create(:lesson, school:, school_class:, visibility: 'students', user_id: teacher.id) |
| 172 | + remix_two = create(:project, school:, lesson: lesson_two, parent: lesson_two.project, remixed_from_id: lesson_two.project.id, user_id: student.id) |
| 173 | + create(:feedback, school_project: remix_two.school_project, user_id: teacher.id, content: 'Unread 2', read_at: nil) |
| 174 | + |
| 175 | + # Class 2: Create 1 remix with unread feedback |
| 176 | + lesson_three = create(:lesson, school:, school_class: school_class_2, visibility: 'students', user_id: teacher.id) |
| 177 | + remix_three = create(:project, school:, lesson: lesson_three, parent: lesson_three.project, remixed_from_id: lesson_three.project.id, user_id: student.id) |
| 178 | + create(:feedback, school_project: remix_three.school_project, user_id: teacher.id, content: 'Unread 3', read_at: nil) |
| 179 | + |
| 180 | + get("/api/schools/#{school.id}/classes", headers:) |
| 181 | + data = JSON.parse(response.body, symbolize_names: true) |
| 182 | + |
| 183 | + class_1 = data.find { |c| c[:name] == 'Test School Class' } |
| 184 | + class_2 = data.find { |c| c[:name] == 'Second Class' } |
| 185 | + |
| 186 | + expect(class_1[:unread_feedback_count]).to eq(2) |
| 187 | + expect(class_2[:unread_feedback_count]).to eq(1) |
| 188 | + end |
| 189 | + |
| 190 | + it 'returns 0 unread_feedback_count when class has no unread feedback' do |
| 191 | + authenticated_in_hydra_as(student) |
| 192 | + stub_user_info_api_for(teacher) |
| 193 | + |
| 194 | + lesson = create(:lesson, school:, school_class:, visibility: 'students', user_id: teacher.id) |
| 195 | + remix = create(:project, school:, lesson:, parent: lesson.project, remixed_from_id: lesson.project.id, user_id: student.id) |
| 196 | + |
| 197 | + create(:feedback, school_project: remix.school_project, user_id: teacher.id, content: 'Already read', read_at: Time.current) |
| 198 | + |
| 199 | + get("/api/schools/#{school.id}/classes", headers:) |
| 200 | + data = JSON.parse(response.body, symbolize_names: true) |
| 201 | + |
| 202 | + this_class = data.find { |c| c[:name] == 'Test School Class' } |
| 203 | + expect(this_class[:unread_feedback_count]).to eq(0) |
| 204 | + end |
| 205 | + |
| 206 | + it 'returns 0 unread_feedback_count when class has no feedback' do |
| 207 | + authenticated_in_hydra_as(student) |
| 208 | + stub_user_info_api_for(teacher) |
| 209 | + |
| 210 | + get("/api/schools/#{school.id}/classes", headers:) |
| 211 | + data = JSON.parse(response.body, symbolize_names: true) |
| 212 | + |
| 213 | + this_class = data.find { |c| c[:name] == 'Test School Class' } |
| 214 | + expect(this_class[:unread_feedback_count]).to eq(0) |
| 215 | + end |
| 216 | + |
| 217 | + it 'counts projects with unread feedback, not individual feedback items' do |
| 218 | + authenticated_in_hydra_as(student) |
| 219 | + stub_user_info_api_for(teacher) |
| 220 | + |
| 221 | + lesson = create(:lesson, school:, school_class:, visibility: 'students', user_id: teacher.id) |
| 222 | + remix = create(:project, school:, lesson:, parent: lesson.project, remixed_from_id: lesson.project.id, user_id: student.id) |
| 223 | + |
| 224 | + # Multiple unread feedback on the same project should count as 1 |
| 225 | + create(:feedback, school_project: remix.school_project, user_id: teacher.id, content: 'Unread 1', read_at: nil) |
| 226 | + create(:feedback, school_project: remix.school_project, user_id: teacher.id, content: 'Unread 2', read_at: nil) |
| 227 | + create(:feedback, school_project: remix.school_project, user_id: teacher.id, content: 'Unread 3', read_at: nil) |
| 228 | + |
| 229 | + get("/api/schools/#{school.id}/classes", headers:) |
| 230 | + data = JSON.parse(response.body, symbolize_names: true) |
| 231 | + |
| 232 | + this_class = data.find { |c| c[:name] == 'Test School Class' } |
| 233 | + expect(this_class[:unread_feedback_count]).to eq(1) |
| 234 | + end |
| 235 | + |
| 236 | + it 'only counts unread feedback on the current students remixes' do |
| 237 | + authenticated_in_hydra_as(student) |
| 238 | + stub_user_info_api_for(teacher) |
| 239 | + |
| 240 | + other_student = create(:student, school:) |
| 241 | + create(:class_student, school_class:, student_id: other_student.id) |
| 242 | + |
| 243 | + lesson = create(:lesson, school:, school_class:, visibility: 'students', user_id: teacher.id) |
| 244 | + |
| 245 | + # Current student's remix with unread feedback |
| 246 | + my_remix = create(:project, school:, lesson:, parent: lesson.project, remixed_from_id: lesson.project.id, user_id: student.id) |
| 247 | + create(:feedback, school_project: my_remix.school_project, user_id: teacher.id, content: 'My unread', read_at: nil) |
| 248 | + |
| 249 | + # Other student's remix with unread feedback (should not count) |
| 250 | + other_remix = create(:project, school:, lesson:, parent: lesson.project, remixed_from_id: lesson.project.id, user_id: other_student.id) |
| 251 | + create(:feedback, school_project: other_remix.school_project, user_id: teacher.id, content: 'Other unread', read_at: nil) |
| 252 | + |
| 253 | + get("/api/schools/#{school.id}/classes", headers:) |
| 254 | + data = JSON.parse(response.body, symbolize_names: true) |
| 255 | + |
| 256 | + this_class = data.find { |c| c[:name] == 'Test School Class' } |
| 257 | + expect(this_class[:unread_feedback_count]).to eq(1) |
| 258 | + end |
| 259 | + |
| 260 | + it 'does not count unread feedback on lessons the student cannot access' do |
| 261 | + authenticated_in_hydra_as(student) |
| 262 | + stub_user_info_api_for(teacher) |
| 263 | + |
| 264 | + # Visible lesson |
| 265 | + visible_lesson = create(:lesson, school:, school_class:, visibility: 'students', user_id: teacher.id) |
| 266 | + visible_remix = create(:project, school:, lesson: visible_lesson, parent: visible_lesson.project, remixed_from_id: visible_lesson.project.id, user_id: student.id) |
| 267 | + create(:feedback, school_project: visible_remix.school_project, user_id: teacher.id, content: 'Visible', read_at: nil) |
| 268 | + |
| 269 | + # Hidden lesson (visibility: 'teachers') |
| 270 | + hidden_lesson = create(:lesson, school:, school_class:, visibility: 'teachers', user_id: teacher.id) |
| 271 | + hidden_remix = create(:project, school:, lesson: hidden_lesson, parent: hidden_lesson.project, remixed_from_id: hidden_lesson.project.id, user_id: student.id) |
| 272 | + create(:feedback, school_project: hidden_remix.school_project, user_id: teacher.id, content: 'Hidden', read_at: nil) |
| 273 | + |
| 274 | + get("/api/schools/#{school.id}/classes", headers:) |
| 275 | + data = JSON.parse(response.body, symbolize_names: true) |
| 276 | + |
| 277 | + this_class = data.find { |c| c[:name] == 'Test School Class' } |
| 278 | + expect(this_class[:unread_feedback_count]).to eq(1) |
| 279 | + end |
| 280 | + |
158 | 281 | it 'does not include unread_feedback_count if user is a school-teacher' do |
159 | 282 | authenticated_in_hydra_as(teacher) |
160 | 283 |
|
|
0 commit comments