Skip to content

Commit b118883

Browse files
committed
initial tests and fixing
1 parent 41aacda commit b118883

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

app/controllers/api/lessons_controller.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ def create_copy
4242
end
4343

4444
def create_from_project
45-
remix_origin = request.origin || request.referer
46-
47-
# authorize the project if it exists
45+
# authorize the project
4846
if lesson_params[:project_identifier].present?
4947
project = Project.find_by(identifier: lesson_params[:project_identifier])
5048
authorize! :update, project if project
5149
end
5250

53-
result = Lesson::CreateFromProject.call(lesson_params:, remix_origin:)
51+
result = Lesson::CreateFromProject.call(lesson_params:)
5452

5553
if result.success?
5654
@lesson_with_user = result[:lesson].with_user

app/models/lesson.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def user_is_the_school_teacher_for_the_school_class
7878
end
7979

8080
def project_belongs_to_the_same_school
81-
return unless project && school && project.school_id != school.id
81+
return unless (project && (school || project&.school) && project&.school_id != school&.id)
8282

83-
errors.add(:project, "must belong to the same school (#{school.id}) as the lesson (#{id})")
83+
errors.add(:project, "must belong to the same school (#{school&.id}) as the lesson (#{id})")
8484
end
8585

8686
def project_belongs_to_the_same_user

lib/concepts/lesson/operations/create_from_project.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
class Lesson
44
class CreateFromProject
55
class << self
6-
def call(lesson_params:, remix_origin:)
6+
def call(lesson_params:)
77
response = OperationResponse.new
8-
response[:lesson] = build_lesson_from_project(lesson_params, remix_origin)
8+
response[:lesson] = build_lesson_from_project(lesson_params)
99
response[:lesson].save!
1010
response
1111
rescue StandardError => e
@@ -17,7 +17,7 @@ def call(lesson_params:, remix_origin:)
1717

1818
private
1919

20-
def build_lesson_from_project(lesson_params, _remix_origin)
20+
def build_lesson_from_project(lesson_params)
2121
project = Project.find_by(identifier: lesson_params[:project_identifier])
2222
lesson = Lesson.new(
2323
name: project.name

spec/factories/lesson.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
sequence(:name) { |n| "Lesson #{n}" }
77
description { 'Description' }
88
visibility { 'teachers' }
9-
project { create(:project, user_id:, name:) }
9+
project { create(:project, user_id:, name:, school: school || school_class&.school) }
1010

1111
trait :with_project_components do
1212
transient do

spec/models/lesson_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,19 @@
6868
expect(lesson).to be_invalid
6969
end
7070

71+
it 'requires the user id to match the user_id on the project' do
72+
lesson.project = build(:project, user_id: SecureRandom.uuid)
73+
expect(lesson).to be_invalid
74+
end
75+
76+
it 'requires the project to belong to the same school as the lesson' do
77+
lesson.project = build(:project, school: create(:school))
78+
expect(lesson).to be_invalid
79+
end
80+
7181
context 'when the lesson has a school' do
7282
before do
83+
lesson.project.update!(school:)
7384
lesson.update!(school:)
7485
end
7586

@@ -84,6 +95,7 @@
8495

8596
context 'when the lesson has a school_class' do
8697
before do
98+
lesson.project.update!(school:)
8799
lesson.update!(school_class: create(:school_class, teacher_ids: [teacher.id], school:))
88100
end
89101

0 commit comments

Comments
 (0)