|
118 | 118 | end |
119 | 119 | end |
120 | 120 |
|
| 121 | + context 'when bulk creating lessons via lesson_projects' do |
| 122 | + let(:lesson_project_params) do |
| 123 | + [ |
| 124 | + { |
| 125 | + name: 'Lesson 1', |
| 126 | + school_id: school.id, |
| 127 | + project_attributes: { name: 'Project 1', project_type: Project::Types::CODE_EDITOR_SCRATCH } |
| 128 | + }, |
| 129 | + { |
| 130 | + name: 'Lesson 2', |
| 131 | + school_id: school.id, |
| 132 | + project_attributes: { name: 'Project 2', project_type: Project::Types::CODE_EDITOR_SCRATCH } |
| 133 | + } |
| 134 | + ] |
| 135 | + end |
| 136 | + |
| 137 | + it 'responds 201 Created' do |
| 138 | + post('/api/lessons', headers:, params: { lesson_projects: lesson_project_params }) |
| 139 | + expect(response).to have_http_status(:created) |
| 140 | + end |
| 141 | + |
| 142 | + it 'creates one lesson per entry' do |
| 143 | + expect do |
| 144 | + post('/api/lessons', headers:, params: { lesson_projects: lesson_project_params }) |
| 145 | + end.to change(Lesson, :count).by(2) |
| 146 | + end |
| 147 | + |
| 148 | + it 'responds with the same lesson JSON shape as a single create' do |
| 149 | + post('/api/lessons', headers:, params: { lesson_projects: lesson_project_params }) |
| 150 | + data = JSON.parse(response.body, symbolize_names: true) |
| 151 | + |
| 152 | + expect(data).to all(include(:id, :name, :user_name)) |
| 153 | + expect(data.pluck(:name)).to contain_exactly('Lesson 1', 'Lesson 2') |
| 154 | + end |
| 155 | + |
| 156 | + it 'omits origin_identifier when not supplied' do |
| 157 | + post('/api/lessons', headers:, params: { lesson_projects: lesson_project_params }) |
| 158 | + data = JSON.parse(response.body, symbolize_names: true) |
| 159 | + |
| 160 | + expect(data).to all(satisfy { |entry| !entry.key?(:origin_identifier) }) |
| 161 | + end |
| 162 | + |
| 163 | + context 'when origin_identifier is supplied' do |
| 164 | + let(:lesson_project_params) do |
| 165 | + [ |
| 166 | + { |
| 167 | + name: 'Lesson 1', |
| 168 | + school_id: school.id, |
| 169 | + origin_identifier: 'curriculum-project-one', |
| 170 | + project_attributes: { name: 'Project 1', project_type: Project::Types::CODE_EDITOR_SCRATCH } |
| 171 | + }, |
| 172 | + { |
| 173 | + name: 'Lesson 2', |
| 174 | + school_id: school.id, |
| 175 | + origin_identifier: 'curriculum-project-two', |
| 176 | + project_attributes: { name: 'Project 2', project_type: Project::Types::CODE_EDITOR_SCRATCH } |
| 177 | + } |
| 178 | + ] |
| 179 | + end |
| 180 | + |
| 181 | + it 'echoes origin_identifier on each successful entry' do |
| 182 | + post('/api/lessons', headers:, params: { lesson_projects: lesson_project_params }) |
| 183 | + data = JSON.parse(response.body, symbolize_names: true) |
| 184 | + expect(data.pluck(:origin_identifier)).to contain_exactly('curriculum-project-one', 'curriculum-project-two') |
| 185 | + end |
| 186 | + end |
| 187 | + |
| 188 | + context 'when some entries are invalid' do |
| 189 | + let(:invalid_lesson_project_params) do |
| 190 | + lesson_project_params + [{ |
| 191 | + name: ' ', |
| 192 | + school_id: school.id, |
| 193 | + origin_identifier: 'curriculum-project-three', |
| 194 | + project_attributes: { name: 'Project 3', project_type: Project::Types::CODE_EDITOR_SCRATCH } |
| 195 | + }] |
| 196 | + end |
| 197 | + |
| 198 | + it 'responds 201 Created' do |
| 199 | + post('/api/lessons', headers:, params: { lesson_projects: invalid_lesson_project_params }) |
| 200 | + expect(response).to have_http_status(:created) |
| 201 | + end |
| 202 | + |
| 203 | + it 'includes an error entry for the failed lesson' do |
| 204 | + post('/api/lessons', headers:, params: { lesson_projects: invalid_lesson_project_params }) |
| 205 | + expect(response.parsed_body.any? { |entry| entry['error'].present? }).to be true |
| 206 | + end |
| 207 | + |
| 208 | + it 'still creates the valid lessons' do |
| 209 | + expect do |
| 210 | + post('/api/lessons', headers:, params: { lesson_projects: invalid_lesson_project_params }) |
| 211 | + end.to change(Lesson, :count).by(2) |
| 212 | + end |
| 213 | + |
| 214 | + it 'echoes origin_identifier on failed entries' do |
| 215 | + post('/api/lessons', headers:, params: { lesson_projects: invalid_lesson_project_params }) |
| 216 | + error_entry = response.parsed_body.find { |entry| entry['error'].present? } |
| 217 | + |
| 218 | + expect(error_entry['origin_identifier']).to eq('curriculum-project-three') |
| 219 | + end |
| 220 | + end |
| 221 | + |
| 222 | + context 'when entries are associated with a school class' do |
| 223 | + let(:school_class) { create(:school_class, teacher_ids: [teacher.id], school:) } |
| 224 | + let(:lesson_project_params) do |
| 225 | + [ |
| 226 | + { |
| 227 | + name: 'Lesson 1', |
| 228 | + school_id: school.id, |
| 229 | + school_class_id: school_class.id, |
| 230 | + project_attributes: { name: 'Project 1', project_type: Project::Types::CODE_EDITOR_SCRATCH } |
| 231 | + }, |
| 232 | + { |
| 233 | + name: 'Lesson 2', |
| 234 | + school_id: school.id, |
| 235 | + school_class_id: school_class.id, |
| 236 | + project_attributes: { name: 'Project 2', project_type: Project::Types::CODE_EDITOR_SCRATCH } |
| 237 | + } |
| 238 | + ] |
| 239 | + end |
| 240 | + |
| 241 | + before do |
| 242 | + authenticated_in_hydra_as(teacher) |
| 243 | + school_class.update!(teachers: [ClassTeacher.new({ teacher_id: teacher.id })]) |
| 244 | + end |
| 245 | + |
| 246 | + it 'responds 201 Created' do |
| 247 | + post('/api/lessons', headers:, params: { lesson_projects: lesson_project_params }) |
| 248 | + |
| 249 | + expect(response).to have_http_status(:created) |
| 250 | + end |
| 251 | + |
| 252 | + it 'responds 422 Unprocessable if school_class_id does not correspond to school_id' do |
| 253 | + mismatched_params = lesson_project_params.map { |entry| entry.merge(school_id: SecureRandom.uuid) } |
| 254 | + |
| 255 | + post('/api/lessons', headers:, params: { lesson_projects: mismatched_params }) |
| 256 | + |
| 257 | + expect(response).to have_http_status(:unprocessable_content) |
| 258 | + end |
| 259 | + |
| 260 | + it 'does not create any lessons when school_class_id does not correspond to school_id' do |
| 261 | + mismatched_params = lesson_project_params.map { |entry| entry.merge(school_id: SecureRandom.uuid) } |
| 262 | + |
| 263 | + expect do |
| 264 | + post('/api/lessons', headers:, params: { lesson_projects: mismatched_params }) |
| 265 | + end.not_to change(Lesson, :count) |
| 266 | + end |
| 267 | + |
| 268 | + it 'rejects the request when only one entry has a mismatched school_id' do |
| 269 | + mismatched_params = [ |
| 270 | + lesson_project_params.first, |
| 271 | + lesson_project_params.last.merge(school_id: SecureRandom.uuid) |
| 272 | + ] |
| 273 | + |
| 274 | + expect do |
| 275 | + post('/api/lessons', headers:, params: { lesson_projects: mismatched_params }) |
| 276 | + end.not_to change(Lesson, :count) |
| 277 | + |
| 278 | + expect(response).to have_http_status(:unprocessable_content) |
| 279 | + end |
| 280 | + end |
| 281 | + end |
| 282 | + |
121 | 283 | context 'when the lesson is associated with a school class' do |
122 | 284 | let(:school_class) { create(:school_class, teacher_ids: [teacher.id], school:) } |
123 | 285 | let(:school) { create(:school) } |
|
0 commit comments