-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlesson_spec.rb
More file actions
304 lines (235 loc) · 8.14 KB
/
lesson_spec.rb
File metadata and controls
304 lines (235 loc) · 8.14 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
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Lesson do
before do
stub_user_info_api_for(teacher)
end
let(:teacher) { create(:teacher, school:, name: 'School Teacher') }
let(:school) { create(:school) }
describe 'associations' do
it 'optionally belongs to a school (library)' do
lesson = create(:lesson, school:, user_id: teacher.id)
expect(lesson.school).to be_a(School)
end
it 'optionally belongs to a school class' do
school_class = create(:school_class, teacher_ids: [teacher.id], school:)
lesson = create(:lesson, school_class:, school: school_class.school, user_id: teacher.id)
expect(lesson.school_class).to be_a(SchoolClass)
end
it 'optionally belongs to a parent' do
lesson = create(:lesson, parent: build(:lesson))
expect(lesson.parent).to be_a(described_class)
end
it 'has many copies' do
lesson = create(:lesson, copies: [build(:lesson), build(:lesson)])
expect(lesson.copies.size).to eq(2)
end
it 'has one project' do
user_id = SecureRandom.uuid
lesson = create(:lesson, user_id:, project: build(:project, user_id:))
expect(lesson.project).to be_a(Project)
end
end
describe 'callbacks' do
it 'cannot be destroyed and should be archived instead' do
lesson = create(:lesson)
expect { lesson.destroy! }.to raise_error(ActiveRecord::RecordNotDestroyed)
end
end
describe 'validations' do
subject(:lesson) { build(:lesson, user_id: teacher.id) }
it 'has a valid default factory' do
expect(lesson).to be_valid
end
it 'can save the default factory' do
expect { lesson.save! }.not_to raise_error
end
it 'requires a user_id' do
lesson.user_id = ' '
expect(lesson).to be_invalid
end
it 'requires a UUID user_id' do
lesson.user_id = 'invalid'
expect(lesson).to be_invalid
end
context 'when the lesson has a school' do
before do
lesson.update!(school:)
end
let(:school) { create(:school) }
it 'requires that the user that has the school-owner or school-teacher role for the school' do
student = create(:student, school:)
lesson.user_id = student.id
expect(lesson).to be_invalid
end
end
context 'when the lesson has a school_class' do
before do
lesson.update!(school_class: create(:school_class, teacher_ids: [teacher.id], school:))
end
let(:school) { create(:school) }
it 'requires that the user that is a school-teacher for the school_class' do
owner = create(:owner, school:)
lesson.user_id = owner.id
expect(lesson).to be_invalid
end
end
it 'requires a name' do
lesson.name = ' '
expect(lesson).to be_invalid
end
it 'requires a visibility' do
lesson.visibility = ' '
expect(lesson).to be_invalid
end
it "requires a visibility that is either 'private', 'teachers', 'students' or 'public'" do
lesson.visibility = 'invalid'
expect(lesson).to be_invalid
end
it 'requires original project to be public if project is a remix' do
original_project = create(:project, user_id: SecureRandom.uuid)
lesson.project.remixed_from_id = original_project.id
expect(lesson).to be_invalid
end
it 'is valid with a remixed project from a public project' do
original_project = create(:project, user_id: nil)
lesson.project.remixed_from_id = original_project.id
expect(lesson).to be_valid
end
end
describe '.archived' do
let!(:archived_lesson) { create(:lesson, archived_at: Time.now.utc) }
let!(:unarchived_lesson) { create(:lesson) }
it 'includes archived lessons' do
expect(described_class.archived).to include(archived_lesson)
end
it 'excludes unarchived lessons' do
expect(described_class.archived).not_to include(unarchived_lesson)
end
end
describe '.unarchived' do
let!(:archived_lesson) { create(:lesson, archived_at: Time.now.utc) }
let!(:unarchived_lesson) { create(:lesson) }
it 'includes unarchived lessons' do
expect(described_class.unarchived).to include(unarchived_lesson)
end
it 'excludes archived lessons' do
expect(described_class.unarchived).not_to include(archived_lesson)
end
end
describe '#school' do
let(:school) { create(:school) }
it 'is set from the school_class' do
school_class = create(:school_class, teacher_ids: [teacher.id], school:)
lesson = create(:lesson, school_class:, user_id: teacher.id)
expect(lesson.school).to eq(lesson.school_class.school)
end
it 'is not nullified when there is no school_class' do
lesson = create(:lesson, school:, user_id: teacher.id)
expect(lesson.school).not_to eq(lesson.school_class&.school)
end
end
describe '.users' do
it 'returns User instances for the current scope' do
create(:lesson, user_id: teacher.id)
user = described_class.all.users.first
expect(user.name).to eq('School Teacher')
end
it 'ignores members where no profile account exists' do
user_id = SecureRandom.uuid
stub_user_info_api_for_unknown_users(user_id:)
create(:lesson, user_id:)
user = described_class.all.users.first
expect(user).to be_nil
end
it 'ignores members not included in the current scope' do
create(:lesson)
user = described_class.none.users.first
expect(user).to be_nil
end
end
describe '.with_users' do
it 'returns an array of class members paired with their User instance' do
lesson = create(:lesson, user_id: teacher.id)
pair = described_class.all.with_users.first
user = described_class.all.users.first
expect(pair).to eq([lesson, user])
end
it 'returns nil values for members where no profile account exists' do
user_id = SecureRandom.uuid
stub_user_info_api_for_unknown_users(user_id:)
lesson = create(:lesson, user_id:)
pair = described_class.all.with_users.first
expect(pair).to eq([lesson, nil])
end
it 'ignores members not included in the current scope' do
create(:lesson)
pair = described_class.none.with_users.first
expect(pair).to be_nil
end
end
describe '#with_user' do
it 'returns the class member paired with their User instance' do
lesson = create(:lesson, user_id: teacher.id)
pair = lesson.with_user
user = described_class.all.users.first
expect(pair).to eq([lesson, user])
end
it 'returns a nil value if the member has no profile account' do
user_id = SecureRandom.uuid
stub_user_info_api_for_unknown_users(user_id:)
lesson = create(:lesson, user_id:)
pair = lesson.with_user
expect(pair).to eq([lesson, nil])
end
end
describe '#archive!' do
let(:lesson) { build(:lesson) }
it 'archives the lesson' do
lesson.archive!
expect(lesson.archived?).to be(true)
end
it 'sets archived_at' do
lesson.archive!
expect(lesson.archived_at).to be_present
end
it 'does not set archived_at if it was already set' do
lesson.update!(archived_at: 1.day.ago)
lesson.archive!
expect(lesson.archived_at).to be < 23.hours.ago
end
it 'saves the record' do
lesson.archive!
expect(lesson).to be_persisted
end
it 'is infallible to other validation errors' do
lesson.save!
lesson.name = ' '
lesson.save!(validate: false)
lesson.archive!
expect(lesson.archived?).to be(true)
end
end
describe '#unarchive!' do
let(:lesson) { build(:lesson, archived_at: Time.now.utc) }
it 'unarchives the lesson' do
lesson.unarchive!
expect(lesson.archived?).to be(false)
end
it 'clears archived_at' do
lesson.unarchive!
expect(lesson.archived_at).to be_nil
end
it 'saves the record' do
lesson.unarchive!
expect(lesson).to be_persisted
end
it 'is infallible to other validation errors' do
lesson.archive!
lesson.name = ' '
lesson.save!(validate: false)
lesson.unarchive!
expect(lesson.archived?).to be(false)
end
end
end