-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathproject_spec.rb
More file actions
401 lines (311 loc) · 13.2 KB
/
project_spec.rb
File metadata and controls
401 lines (311 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Project, versioning: true do
let(:school) { create(:school) }
describe 'associations' do
it { is_expected.to belong_to(:school).optional(true) }
it { is_expected.to belong_to(:lesson).optional(true) }
it { is_expected.to belong_to(:parent).optional(true) }
it { is_expected.to have_many(:remixes).dependent(:nullify) }
it { is_expected.to have_many(:components) }
it { is_expected.to have_many(:project_errors).dependent(:nullify) }
it { is_expected.to have_many_attached(:images) }
it { is_expected.to have_many_attached(:videos) }
it { is_expected.to have_many_attached(:audio) }
it { is_expected.to have_one(:school_project).dependent(:destroy) }
it 'purges attached images' do
expect(described_class.reflect_on_attachment(:images).options[:dependent]).to eq(:purge_later)
end
it 'purges attached videos' do
expect(described_class.reflect_on_attachment(:videos).options[:dependent]).to eq(:purge_later)
end
it 'purges attached audio' do
expect(described_class.reflect_on_attachment(:audio).options[:dependent]).to eq(:purge_later)
end
end
describe 'validations' do
let(:project) { create(:project) }
let(:identifier) { project.identifier }
it 'has a valid default factory' do
expect(build(:project)).to be_valid
end
it 'can save the default factory' do
expect { build(:project).save! }.not_to raise_error
end
it 'is invalid if no user or locale' do
invalid_project = build(:project, locale: nil, user_id: nil)
expect(invalid_project).to be_invalid
end
it 'is valid if user but no locale' do
valid_project = build(:project, locale: nil)
expect(valid_project).to be_valid
end
it 'is invalid if school_id but no school project' do
invalid_project = build(:project, school_id: SecureRandom.uuid)
expect(invalid_project).to be_invalid
end
it 'is invalid if school_id and school project with different school_id' do
invalid_project = build(:project, school_id: SecureRandom.uuid, school_project: build(:school_project, school_id: SecureRandom.uuid))
expect(invalid_project).to be_invalid
end
it 'is valid if school_id and school project with matching school_id' do
school_id = SecureRandom.uuid
valid_project = build(:project, school_id:, school_project: build(:school_project, school_id:))
expect(valid_project).to be_valid
end
it 'is invalid if a school project with lesson and class but user is not class member' do
school = create(:school)
teacher = create(:teacher, school:)
school_class = create(:school_class, school:, teacher_ids: [teacher.id])
lesson = create(:lesson, school:, school_class:, user_id: teacher.id)
invalid_project = build(:project, school:, lesson:, user_id: SecureRandom.uuid)
expect(invalid_project).to be_invalid
end
context 'with same identifier and same user as existing project' do
let(:user_id) { project.user_id }
it 'is invalid if identifier in use by same user in the same locale' do
new_project = build(:project, identifier:, user_id:, locale: project.locale)
expect(new_project).to be_invalid
end
it 'is valid if identifier only in use by the user in the another locale' do
new_project = build(:project, identifier:, user_id:, locale: 'another_locale')
expect(new_project).to be_valid
end
end
context 'with same identifier but different user as existing project' do
let(:user_id) { 'another_user' }
it 'is invalid if identifier in use by another user in same locale' do
new_project = build(:project, identifier:, user_id:, locale: project.locale)
expect(new_project).to be_invalid
end
it 'is invalid if identifier in use in another locale by another user' do
new_project = build(:project, identifier:, user_id:, locale: 'another_locale')
expect(new_project).to be_invalid
end
end
context 'when the project has a school' do
before do
project.update!(school: create(:school))
end
it 'requires that the user that has a role within the school' do
project.user_id = SecureRandom.uuid
expect(project).to be_invalid
end
end
context 'when the project has a lesson' do
let(:school) { create(:school) }
let(:teacher) { create(:teacher, school:) }
let(:student) { create(:student, school:) }
let(:school_class) { create(:school_class, school:, teacher_ids: [teacher.id]) }
before do
lesson = create(:lesson, school:, school_class:, user_id: teacher.id)
project.update!(lesson:, school:, user_id: lesson.user_id, identifier: 'something')
end
it 'fails if the user is the owner of the lesson' do
project.user_id = SecureRandom.uuid
expect(project).to be_invalid
end
it 'succeeds if the user is the owner of the lesson' do
expect(project).to be_valid
end
it 'fails if the user is not a member of the lesson' do
create(:class_student, school_class:, student_id: teacher.id)
project.user_id = student.id
expect(project).to be_invalid
end
it 'suceeds if the user is a member of the lesson' do
create(:class_student, school_class:, student_id: student.id)
project.user_id = student.id
expect(project).to be_invalid
end
end
end
describe 'generate_identifier' do
it 'generates an identifier if nil' do
project = build(:project, identifier: nil)
project.valid?
expect(project.identifier).not_to be_nil
end
context 'when skip_identifier_generation is true' do
it 'does not generate an identifier if nil' do
project = build(:project, identifier: nil, skip_identifier_generation: true)
project.valid?
expect(project.identifier).to be_nil
end
end
end
describe 'create_school_project_if_needed' do
let(:teacher) { create(:teacher, school:) }
let(:teacher_project) { create(:project, school_id: school.id, user_id: teacher.id) }
let(:project) { create(:project) }
it 'creates a school project if the project belongs to a school' do
expect(teacher_project.school_project).to be_present
end
it 'gives the school project the same school_id as the project' do
expect(teacher_project.school_project.school_id).to eq(school.id)
end
it 'does not create a school project if the project does not belong to a school' do
expect(project.school_project).to be_nil
end
end
describe '.users' do
let(:student) { create(:student, school:, name: 'School Student') }
let(:teacher) { create(:teacher, school:) }
let(:student_attributes) do
[{ id: student.id, name: student.name, username: student.username }]
end
before do
stub_profile_api_list_school_students(school:, student_attributes:)
end
it 'returns User instances for the current scope' do
create(:project, user_id: student.id, school_id: school.id)
user = described_class.all.users(teacher).first
expect(user.name).to eq('School Student')
end
it 'ignores members where no profile account exists' do
user_id = SecureRandom.uuid
create(:project, user_id:)
user = described_class.all.users(teacher).first
expect(user).to be_nil
end
it 'ignores members not included in the current scope' do
create(:project)
user = described_class.none.users(teacher).first
expect(user).to be_nil
end
end
describe '.with_users' do
let(:student) { create(:student, school:) }
let(:teacher) { create(:teacher, school:) }
let(:student_attributes) do
[{ id: student.id, name: student.name, username: student.username }]
end
before do
stub_profile_api_list_school_students(school:, student_attributes:)
end
it 'returns an array of class members paired with their User instance' do
project = create(:project, user_id: student.id)
pair = described_class.all.with_users(teacher).first
user = described_class.all.users(teacher).first
expect(pair).to eq([project, user])
end
it 'returns nil values for members where no profile account exists' do
user_id = SecureRandom.uuid
project = create(:project, user_id:)
pair = described_class.all.with_users(teacher).first
expect(pair).to eq([project, nil])
end
it 'ignores members not included in the current scope' do
create(:project)
pair = described_class.none.with_users(teacher).first
expect(pair).to be_nil
end
end
describe '#with_user' do
let(:student) { create(:student, school:) }
let(:teacher) { create(:teacher, school:) }
let(:student_attributes) do
[{ id: student.id, name: student.name, username: student.username }]
end
before do
stub_profile_api_list_school_students(school:, student_attributes:)
end
it 'returns the class member paired with their User instance' do
project = create(:project, user_id: student.id)
pair = project.with_user(teacher)
user = described_class.all.users(teacher).first
expect(pair).to eq([project, user])
end
it 'returns a nil value if the member has no profile account' do
user_id = SecureRandom.uuid
project = create(:project, user_id:)
pair = project.with_user(teacher)
expect(pair).to eq([project, nil])
end
end
describe '#last_edited_at' do
let(:project) { create(:project, updated_at: 1.day.ago) }
let(:component) { create(:component, project:, updated_at: 2.days.ago) }
it 'returns the project updated_at if most recent' do
expect(project.last_edited_at).to eq(project.updated_at)
end
it 'returns the latest component updated_at if most recent' do
latest_component = create(:component, project:, updated_at: 1.hour.ago)
expect(project.last_edited_at).to eq(latest_component.updated_at)
end
end
describe '#media' do
let(:project) { create(:project, :with_attached_image, :with_attached_video, :with_attached_audio) }
it 'returns all media files' do
expect(project.media).to eq(project.images + project.videos + project.audio)
end
end
describe 'auditing' do
let(:school) { create(:school) }
let(:teacher) { create(:teacher, school:) }
let(:student) { create(:student, school:) }
it 'enables auditing for projects with a school_id' do
project_with_school = create(:project, user_id: student.id, school_id: school.id)
expect(project_with_school.versions.length).to(eq(1))
end
it 'does not enable auditing for projects without a school_id' do
project_without_school = create(:project, school_id: nil)
expect(project_without_school.versions.length).to(eq(0))
end
end
describe 'default scope' do
let!(:python_project) { create(:project, project_type: Project::Types::PYTHON) }
let!(:html_project) { create(:project, project_type: Project::Types::HTML) }
let!(:project_with_unknown_type) { create(:project, project_type: 'unknown') }
let!(:scratch_project) { create(:project, project_type: Project::Types::SCRATCH) }
it 'includes python projects' do
expect(described_class.all).to include(python_project)
end
it 'includes html projects' do
expect(described_class.all).to include(html_project)
end
it 'includes projects with unknown type' do
expect(described_class.all).to include(project_with_unknown_type)
end
it 'does not include scratch projects' do
expect(described_class.all).not_to include(scratch_project)
end
end
describe 'only_scratch scope' do
let!(:python_project) { create(:project, project_type: Project::Types::PYTHON) }
let!(:html_project) { create(:project, project_type: Project::Types::HTML) }
let!(:project_with_unknown_type) { create(:project, project_type: 'unknown') }
let!(:scratch_project) { create(:project, project_type: Project::Types::SCRATCH) }
let(:projects_in_scope) { described_class.only_scratch(only_scratch) }
context 'when only_scratch is false' do
let(:only_scratch) { false }
it 'includes python projects' do
expect(projects_in_scope).to include(python_project)
end
it 'includes html projects' do
expect(projects_in_scope).to include(html_project)
end
it 'includes projects with unknown type' do
expect(projects_in_scope).to include(project_with_unknown_type)
end
it 'does not include scratch projects' do
expect(projects_in_scope).not_to include(scratch_project)
end
end
context 'when only_scratch is true' do
let(:only_scratch) { true }
it 'does not include python projects' do
expect(projects_in_scope).not_to include(python_project)
end
it 'does not include html projects' do
expect(projects_in_scope).not_to include(html_project)
end
it 'does not include projects with unknown type' do
expect(projects_in_scope).not_to include(project_with_unknown_type)
end
it 'includes scratch projects' do
expect(projects_in_scope).to include(scratch_project)
end
end
end
end