Skip to content

Commit 195923c

Browse files
committed
Allow experience-cs admin to create projects
This gives users with the "experience-cs-admin" role permission to create projects. 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 e4597f4 commit 195923c

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
it { is_expected.not_to be_able_to(:update, project) }
3030
it { is_expected.not_to be_able_to(:destroy, project) }
3131
end
32+
33+
it { is_expected.not_to be_able_to(:create, :Project) }
3234
end
3335

3436
context 'with a standard user' do
@@ -56,6 +58,8 @@
5658
it { is_expected.not_to be_able_to(:update, another_project) }
5759
it { is_expected.not_to be_able_to(:destroy, another_project) }
5860
end
61+
62+
it { is_expected.not_to be_able_to(:create, :Project) }
5963
end
6064

6165
context 'with a teacher' do
@@ -83,6 +87,8 @@
8387
it { is_expected.not_to be_able_to(:update, another_project) }
8488
it { is_expected.not_to be_able_to(:destroy, another_project) }
8589
end
90+
91+
it { is_expected.not_to be_able_to(:create, :Project) }
8692
end
8793

8894
context 'with an owner' do
@@ -110,6 +116,8 @@
110116
it { is_expected.not_to be_able_to(:update, another_project) }
111117
it { is_expected.not_to be_able_to(:destroy, another_project) }
112118
end
119+
120+
it { is_expected.not_to be_able_to(:create, :Project) }
113121
end
114122

115123
context 'with a student' do
@@ -137,6 +145,14 @@
137145
it { is_expected.not_to be_able_to(:update, another_project) }
138146
it { is_expected.not_to be_able_to(:destroy, another_project) }
139147
end
148+
149+
it { is_expected.not_to be_able_to(:create, :Project) }
150+
end
151+
152+
context 'with an experience-cs admin' do
153+
let(:user) { build(:experience_cs_admin_user) }
154+
155+
it { is_expected.to be_able_to(:create, Project) }
140156
end
141157

142158
# rubocop:disable RSpec/MultipleMemoizedHelpers

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)