|
109 | 109 | expect(copied_component.content).to eq(original_component.content) |
110 | 110 | end |
111 | 111 |
|
| 112 | + context 'when the project is a Scratch project' do |
| 113 | + let(:copied_teacher_id) { SecureRandom.uuid } |
| 114 | + let(:lesson_params) { { user_id: copied_teacher_id } } |
| 115 | + let(:scratch_content) do |
| 116 | + { |
| 117 | + targets: [ |
| 118 | + { |
| 119 | + isStage: true, |
| 120 | + costumes: [ |
| 121 | + { |
| 122 | + assetId: 'teacher_asset', |
| 123 | + md5ext: 'teacher_asset.png', |
| 124 | + dataFormat: 'png' |
| 125 | + } |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + monitors: [], |
| 130 | + extensions: [], |
| 131 | + meta: {} |
| 132 | + } |
| 133 | + end |
| 134 | + |
| 135 | + before do |
| 136 | + lesson.project.update!(project_type: Project::Types::CODE_EDITOR_SCRATCH, locale: nil) |
| 137 | + create(:scratch_component, project: lesson.project, content: scratch_content) |
| 138 | + end |
| 139 | + |
| 140 | + it 'copies the Scratch component content' do |
| 141 | + response = described_class.call(lesson:, lesson_params:) |
| 142 | + copied_project = response[:lesson].reload.project |
| 143 | + |
| 144 | + expect(copied_project.scratch_component.content.to_h) |
| 145 | + .to eq(scratch_content.deep_stringify_keys) |
| 146 | + end |
| 147 | + |
| 148 | + it 'copies only teacher-owned Scratch assets to the new project owner' do |
| 149 | + create_scratch_asset( |
| 150 | + filename: 'teacher_asset.png', |
| 151 | + project: lesson.project, |
| 152 | + uploaded_user_id: teacher_id, |
| 153 | + body: 'teacher-body' |
| 154 | + ) |
| 155 | + create_scratch_asset( |
| 156 | + filename: 'student_asset.png', |
| 157 | + project: lesson.project, |
| 158 | + uploaded_user_id: SecureRandom.uuid, |
| 159 | + body: 'student-body' |
| 160 | + ) |
| 161 | + |
| 162 | + response = described_class.call(lesson:, lesson_params:) |
| 163 | + copied_project = response[:lesson].reload.project |
| 164 | + copied_asset = ScratchAsset.find_by!( |
| 165 | + filename: 'teacher_asset.png', |
| 166 | + project: copied_project, |
| 167 | + uploaded_user_id: copied_teacher_id |
| 168 | + ) |
| 169 | + |
| 170 | + visible_asset = ScratchAsset.find_visible_to_project( |
| 171 | + project: copied_project, |
| 172 | + user: User.new(id: SecureRandom.uuid), |
| 173 | + filename: 'teacher_asset.png' |
| 174 | + ) |
| 175 | + |
| 176 | + expect(copied_asset.file.download).to eq('teacher-body') |
| 177 | + expect(visible_asset).to eq(copied_asset) |
| 178 | + expect( |
| 179 | + ScratchAsset.find_by(filename: 'student_asset.png', project: copied_project) |
| 180 | + ).to be_nil |
| 181 | + end |
| 182 | + end |
| 183 | + |
112 | 184 | context 'when creating a copy fails' do |
113 | 185 | let(:lesson_params) { { name: ' ' } } |
114 | 186 |
|
|
135 | 207 | expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError)) |
136 | 208 | end |
137 | 209 | end |
| 210 | + |
| 211 | + def create_scratch_asset(filename:, project:, uploaded_user_id:, body:) |
| 212 | + ScratchAsset.create!(filename:, project:, uploaded_user_id:).tap do |asset| |
| 213 | + asset.file.attach(io: StringIO.new(body), filename:, content_type: 'image/png') |
| 214 | + end |
| 215 | + end |
138 | 216 | end |
0 commit comments