-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreating_a_project_spec.rb
More file actions
271 lines (215 loc) · 8.54 KB
/
creating_a_project_spec.rb
File metadata and controls
271 lines (215 loc) · 8.54 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
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Creating a project', type: :request do
let(:generated_identifier) { 'word1-word2-word3' }
let(:headers) { { Authorization: UserProfileMock::TOKEN } }
let(:teacher) { create(:teacher, school:) }
let(:school) { create(:school) }
let(:owner) { create(:owner, school:) }
let(:params) do
{
project: {
name: 'Test Project',
components: [
{ name: 'main', extension: 'py', content: 'print("hi")' }
]
}
}
end
before do
authenticated_in_hydra_as(teacher)
mock_phrase_generation(generated_identifier)
end
it 'responds 201 Created' do
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:created)
end
it 'generates an identifier for the project even if another identifier is specified' do
params_with_identifier = { project: { identifier: 'test-identifier', components: [] } }
post('/api/projects', headers:, params: params_with_identifier)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:identifier]).to eq(generated_identifier)
end
it 'responds with the project JSON' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:name]).to eq('Test Project')
end
it 'responds with the components JSON' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:components].first[:content]).to eq('print("hi")')
end
it 'responds 422 Unprocessable Entity when params are invalid' do
post('/api/projects', headers:, params: { project: { components: [{ name: ' ' }] } })
expect(response).to have_http_status(:unprocessable_entity)
end
it 'responds 401 Unauthorized when no token is given' do
post('/api/projects', params:)
expect(response).to have_http_status(:unauthorized)
end
context 'when the project is associated with a school (library)' do
let(:school) { create(:school) }
let(:teacher) { create(:teacher, school:) }
let(:params) do
{
project: {
name: 'Test Project',
components: [],
school_id: school.id,
user_id: teacher.id
}
}
end
it 'responds 201 Created' do
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:created)
end
it 'responds 201 Created when the user is a school-teacher for the school' do
authenticated_in_hydra_as(teacher)
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:created)
end
it 'responds 403 Forbidden when the user is a school-student for the school' do
student = create(:student, school:)
authenticated_in_hydra_as(student)
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:forbidden)
end
it 'sets the lesson user to the specified user for school-owner users' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:user_id]).to eq(teacher.id)
end
it 'sets the project user to the current user for school-teacher users' do
authenticated_in_hydra_as(teacher)
new_params = { project: params[:project].merge(user_id: 'ignored') }
post('/api/projects', headers:, params: new_params)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:user_id]).to eq(teacher.id)
end
it 'responds 403 Forbidden when the user is a school-owner for a different school' do
Role.teacher.find_by(user_id: teacher.id, school:).delete
Role.owner.find_by(user_id: owner.id, school:).delete
school.update!(id: SecureRandom.uuid)
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:forbidden)
end
end
context 'when the project is associated with a lesson' do
let(:school) { create(:school) }
let(:lesson) { create(:lesson, school:, user_id: teacher.id) }
let(:lesson_created_by_owner) { create(:lesson, school:, user_id: owner.id) }
let(:teacher) { create(:teacher, school:) }
let(:params) do
{
project: {
name: 'Test Project',
components: [],
school_id: school.id,
lesson_id: lesson.id,
user_id: teacher.id
}
}
end
it 'responds 201 Created' do
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:created)
end
it 'responds 201 Created when the current user is the owner of the lesson' do
authenticated_in_hydra_as(teacher)
lesson.update!(user_id: teacher.id)
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:created)
end
it 'responds 422 Unprocessable when when the user_id is not the owner of the lesson' do
user_id = SecureRandom.uuid
project = {
project: {
name: 'Test Project',
components: [],
school_id: school.id,
lesson_id: lesson_created_by_owner.id,
user_id: teacher.id
}
}
new_params = { project: project.merge(user_id:) }
post('/api/projects', headers:, params: new_params)
expect(response).to have_http_status(:unprocessable_entity)
end
it 'responds 422 Unprocessable when lesson_id is provided but school_id is missing' do
new_params = { project: params[:project].without(:school_id) }
post('/api/projects', headers:, params: new_params)
expect(response).to have_http_status(:unprocessable_entity)
end
it 'responds 422 Unprocessable when lesson_id does not correspond to school_id' do
new_params = { project: params[:project].merge(lesson_id: SecureRandom.uuid) }
post('/api/projects', headers:, params: new_params)
expect(response).to have_http_status(:unprocessable_entity)
end
it 'responds 403 Forbidden when the user is a school-owner for a different school' do
new_params = { project: params[:project].without(:lesson_id).merge(school_id: SecureRandom.uuid) }
post('/api/projects', headers:, params: new_params)
expect(response).to have_http_status(:forbidden)
end
it 'responds 403 Forbidden when the current user is not the owner of the lesson' do
teacher = create(:teacher, school:)
authenticated_in_hydra_as(teacher)
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:forbidden)
end
it 'responds 403 Forbidden when the user is a school-student' do
student = create(:student, school:)
authenticated_in_hydra_as(student)
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:forbidden)
end
end
context 'when an Experience CS admin creates a starter Scratch project' do
let(:experience_cs_admin) { create(:experience_cs_admin_user) }
let(:params) do
{
project: {
identifier: 'test-project',
name: 'Test Project',
locale: 'fr',
project_type: Project::Types::SCRATCH,
user_id: nil,
components: []
}
}
end
before do
authenticated_in_hydra_as(experience_cs_admin)
end
it 'responds 201 Created' do
post('/api/projects', headers:, params:)
expect(response).to have_http_status(:created)
end
it 'sets the project identifier to the specified (not the generated) value' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:identifier]).to eq('test-project')
end
it 'sets the project name to the specified value' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:name]).to eq('Test Project')
end
it 'sets the project locale to the specified value' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:locale]).to eq('fr')
end
it 'sets the project type to the specified value' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:project_type]).to eq(Project::Types::SCRATCH)
end
it 'sets the project user_id to the specified value (i.e. nil to represent a public project)' do
post('/api/projects', headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:user_id]).to be_nil
end
end
end