Skip to content

Commit 5a0abcc

Browse files
committed
Allow ExCS admin to create starter projects
This gives users with the "experience-cs-admin" role permission to create starter (or "public") projects (i.e. projects with no `user_id` set). I've added examples to the "Creating a project" feature spec to check this works as intended. Note that I've had to add support for `User#roles` to `UserProfileMock#user_to_hash` for the new examples that I've added to the feature spec.
1 parent 64db06f commit 5a0abcc

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

app/models/ability.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def initialize(user)
1414
define_school_teacher_abilities(user:, school:) if user.school_teacher?(school)
1515
define_school_owner_abilities(school:) if user.school_owner?(school)
1616
end
17+
18+
define_experience_cs_admin_abilities(user)
1719
end
1820

1921
private
@@ -100,6 +102,12 @@ def define_school_student_abilities(user:, school:)
100102
can(%i[show_finished set_finished], SchoolProject, project: { user_id: user.id, lesson_id: nil }, school_id: school.id)
101103
end
102104

105+
def define_experience_cs_admin_abilities(user)
106+
return unless user&.experience_cs_admin?
107+
108+
can :create, Project
109+
end
110+
103111
def school_teacher_can_manage_lesson?(user:, school:, lesson:)
104112
is_my_lesson = lesson.school_id == school.id && lesson.user_id == user.id
105113
is_my_class = lesson.school_class&.teacher_ids&.include?(user.id)

spec/features/project/creating_a_project_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,48 @@
207207
expect(response).to have_http_status(:forbidden)
208208
end
209209
end
210+
211+
context 'when the user is an Experience CS admin' do
212+
let(:experience_cs_admin) { create(:experience_cs_admin_user) }
213+
let(:params) do
214+
{
215+
project: {
216+
name: 'Test Project',
217+
locale: 'fr',
218+
project_type: Project::Types::SCRATCH,
219+
components: []
220+
}
221+
}
222+
end
223+
224+
before do
225+
authenticated_in_hydra_as(experience_cs_admin)
226+
end
227+
228+
it 'responds 201 Created' do
229+
post('/api/projects', headers:, params:)
230+
expect(response).to have_http_status(:created)
231+
end
232+
233+
it 'sets the project name to the specified value' do
234+
post('/api/projects', headers:, params:)
235+
data = JSON.parse(response.body, symbolize_names: true)
236+
237+
expect(data[:name]).to eq('Test Project')
238+
end
239+
240+
it 'sets the project locale to the specified value' do
241+
post('/api/projects', headers:, params:)
242+
data = JSON.parse(response.body, symbolize_names: true)
243+
244+
expect(data[:locale]).to eq('fr')
245+
end
246+
247+
it 'sets the project type to the specified value' do
248+
post('/api/projects', headers:, params:)
249+
data = JSON.parse(response.body, symbolize_names: true)
250+
251+
expect(data[:project_type]).to eq(Project::Types::SCRATCH)
252+
end
253+
end
210254
end

spec/models/ability_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@
139139
end
140140
end
141141

142+
context 'with an experience-cs admin' do
143+
let(:user) { build(:experience_cs_admin_user) }
144+
145+
it { is_expected.to be_able_to(:create, starter_project) }
146+
end
147+
142148
# rubocop:disable RSpec/MultipleMemoizedHelpers
143149
context "with a teacher's project where the lesson is visible to students" do
144150
let(:user) { create(:user) }

spec/support/user_profile_mock.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def user_to_hash(user, user_type, id_field = :id)
3131
id_field => user_type ? "#{user_type}:#{user.id}" : user.id,
3232
name: user.name,
3333
email: user.email,
34-
username: user.username
34+
username: user.username,
35+
roles: user.roles
3536
}
3637
end
3738

0 commit comments

Comments
 (0)