File tree Expand file tree Collapse file tree 5 files changed +35
-8
lines changed Expand file tree Collapse file tree 5 files changed +35
-8
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ class Project < ApplicationRecord
16
16
validates :identifier , presence : true , uniqueness : { scope : :locale }
17
17
validate :identifier_cannot_be_taken_by_another_user
18
18
validates :locale , presence : true , unless : :user_id
19
+ validate :user_has_a_role_within_the_school
19
20
20
21
scope :internal_projects , -> { where ( user_id : nil ) }
21
22
@@ -49,4 +50,16 @@ def identifier_cannot_be_taken_by_another_user
49
50
50
51
errors . add ( :identifier , "can't be taken by another user" )
51
52
end
53
+
54
+ def user_has_a_role_within_the_school
55
+ return unless user_id_changed? && errors . blank? && school
56
+
57
+ _ , user = with_user
58
+
59
+ return if user . blank?
60
+ return if user . org_roles ( organisation_id : school . id ) . any?
61
+
62
+ msg = "'#{ user_id } ' does not have any roles for for organisation '#{ school . id } '"
63
+ errors . add ( :user , msg )
64
+ end
52
65
end
Original file line number Diff line number Diff line change @@ -27,12 +27,6 @@ def attributes
27
27
ATTRIBUTES . index_with { |_k | nil }
28
28
end
29
29
30
- def role? ( role :)
31
- return false if roles . nil?
32
-
33
- roles . to_s . split ( ',' ) . map ( &:strip ) . include? role . to_s
34
- end
35
-
36
30
def organisation_ids
37
31
organisations &.keys || [ ]
38
32
end
@@ -58,7 +52,7 @@ def school_student?(organisation_id:)
58
52
end
59
53
60
54
def admin?
61
- role? ( role : 'editor-admin' )
55
+ organisation_ids . any? { | organisation_id | org_role? ( organisation_id : , role : 'editor-admin' ) }
62
56
end
63
57
64
58
def ==( other )
Original file line number Diff line number Diff line change 8
8
organisations { { } }
9
9
10
10
factory :admin_user do
11
- roles { ' editor-admin' }
11
+ organisations { { '12345678-1234-1234-1234-123456789abc' => ' editor-admin' } }
12
12
end
13
13
14
14
skip_create
Original file line number Diff line number Diff line change 5
5
RSpec . describe 'Creating a project' , type : :request do
6
6
before do
7
7
stub_hydra_public_api
8
+ stub_user_info_api
8
9
mock_phrase_generation
9
10
end
10
11
Original file line number Diff line number Diff line change 25
25
let ( :project ) { create ( :project ) }
26
26
let ( :identifier ) { project . identifier }
27
27
28
+ it 'has a valid default factory' do
29
+ expect ( build ( :project ) ) . to be_valid
30
+ end
31
+
32
+ it 'can save the default factory' do
33
+ expect { build ( :project ) . save! } . not_to raise_error
34
+ end
35
+
28
36
it 'is invalid if no user or locale' do
29
37
invalid_project = build ( :project , locale : nil , user_id : nil )
30
38
expect ( invalid_project ) . to be_invalid
62
70
expect ( new_project ) . to be_invalid
63
71
end
64
72
end
73
+
74
+ context 'when the project has a school' do
75
+ before do
76
+ project . update! ( school : create ( :school ) )
77
+ end
78
+
79
+ it 'requires a user that has a role within the school' do
80
+ project . user_id = SecureRandom . uuid
81
+ expect ( project ) . to be_invalid
82
+ end
83
+ end
65
84
end
66
85
67
86
describe 'check_unique_not_null' , :sample_words do
You can’t perform that action at this time.
0 commit comments