Skip to content

Commit fccca1b

Browse files
committed
Limit cohort imports by team's schools
This ensures that when importing a cohort, you can only provide school URNs for schools managed by the team, avoiding the possibility of editing the cohort of a different team.
1 parent fcb9e41 commit fccca1b

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

app/models/cohort_import_row.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def school
2020
@school ||=
2121
if (urn = school_urn&.to_s).present? &&
2222
![SCHOOL_URN_HOME_EDUCATED, SCHOOL_URN_UNKNOWN].include?(urn)
23-
Location.school.find_by_urn_and_site(urn) ||
24-
Location.school.find_by(systm_one_code: urn)
23+
schools.find_by_urn_and_site(urn) ||
24+
schools.find_by(systm_one_code: urn)
2525
end
2626
end
2727

@@ -35,6 +35,10 @@ def home_educated
3535

3636
private
3737

38+
def schools
39+
Location.school.eager_load(:team).where(subteam: { team: })
40+
end
41+
3842
def stage_registration?
3943
true
4044
end
@@ -56,8 +60,8 @@ def validate_school_urn
5660
errors.add(school_urn.header, "is required but missing")
5761
elsif !school_urn.to_s.in?(
5862
[SCHOOL_URN_HOME_EDUCATED, SCHOOL_URN_UNKNOWN]
59-
) && !Location.school.where_urn_and_site(school_urn.to_s).exists? &&
60-
!Location.school.exists?(systm_one_code: school_urn.to_s)
63+
) && !schools.where_urn_and_site(school_urn.to_s).exists? &&
64+
!schools.exists?(systm_one_code: school_urn.to_s)
6165
errors.add(
6266
school_urn.header,
6367
"The school URN is not recognised. If you’ve checked the URN, " \

spec/models/cohort_import_row_spec.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
}
5959
end
6060

61-
before { create(:school, urn: "123456", team:) }
61+
let!(:school) { create(:school, urn: "123456", team:) }
6262

6363
describe "validations" do
6464
let(:data) { valid_data }
@@ -112,6 +112,19 @@
112112
end
113113
end
114114

115+
context "with a school for a different team" do
116+
let(:data) { valid_data }
117+
118+
before { school.update!(subteam: nil) }
119+
120+
it "is invalid" do
121+
expect(cohort_import_row).to be_invalid
122+
expect(cohort_import_row.errors["CHILD_SCHOOL_URN"].first).to include(
123+
"The school URN is not recognised."
124+
)
125+
end
126+
end
127+
115128
context "with an invalid year group" do
116129
let(:data) { valid_data.merge("CHILD_YEAR_GROUP" => "abc") }
117130

@@ -661,12 +674,12 @@
661674
end
662675

663676
describe "#school" do
664-
subject(:school) { school_move.school }
677+
subject { school_move.school }
665678

666679
context "with a school location" do
667680
let(:school_urn) { "123456" }
668681

669-
it { should eq(Location.first) }
682+
it { should eq(school) }
670683
end
671684

672685
context "with an unknown school" do
@@ -683,7 +696,7 @@
683696
end
684697

685698
describe "#home_educated" do
686-
subject(:home_educated) { school_move.home_educated }
699+
subject { school_move.home_educated }
687700

688701
context "with a school location" do
689702
let(:school_urn) { "123456" }

0 commit comments

Comments
 (0)