|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Creating a Scratch asset', type: :request do |
| 6 | + let(:basename) { 'test_image_1' } |
| 7 | + let(:format) { 'png' } |
| 8 | + let(:filename) { "#{basename}.#{format}" } |
| 9 | + let(:school) { create(:school) } |
| 10 | + let(:teacher) { create(:teacher, school:) } |
| 11 | + let(:cookie_headers) { { 'Cookie' => "scratch_auth=#{UserProfileMock::TOKEN}" } } |
| 12 | + |
| 13 | + describe 'GET #show' do |
| 14 | + let(:make_request) { get '/api/scratch/assets/internalapi/asset/test_image_1.png/get/' } |
| 15 | + |
| 16 | + context 'when the asset exists' do |
| 17 | + let!(:scratch_asset) { create(:scratch_asset, :with_file, filename:, asset_path: file_fixture(filename)) } |
| 18 | + |
| 19 | + it 'redirects to the asset file URL' do |
| 20 | + make_request |
| 21 | + |
| 22 | + expect(response).to redirect_to(rails_blob_url(scratch_asset.file, only_path: true)) |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe 'POST #create' do |
| 28 | + let(:upload) { File.binread(file_fixture(filename)) } |
| 29 | + let(:make_request) do |
| 30 | + post '/api/scratch/assets/test_image_1.png', headers: { 'Content-Type' => 'application/octet-stream' }.merge(cookie_headers), params: upload |
| 31 | + end |
| 32 | + |
| 33 | + context 'when user is logged in and cat_mode is enabled' do |
| 34 | + before do |
| 35 | + authenticated_in_hydra_as(teacher) |
| 36 | + Flipper.disable :cat_mode |
| 37 | + Flipper.disable_actor :cat_mode, school |
| 38 | + end |
| 39 | + |
| 40 | + it 'creates a new asset' do |
| 41 | + # Arrange |
| 42 | + Flipper.enable_actor :cat_mode, school |
| 43 | + |
| 44 | + # Act & Assert |
| 45 | + expect { make_request }.to change(ScratchAsset, :count).by(1) |
| 46 | + end |
| 47 | + |
| 48 | + it 'sets the filename on the asset' do |
| 49 | + # Arrange |
| 50 | + Flipper.enable_actor :cat_mode, school |
| 51 | + |
| 52 | + # Act & Assert |
| 53 | + make_request |
| 54 | + expect(ScratchAsset.last.filename).to eq(filename) |
| 55 | + end |
| 56 | + |
| 57 | + it 'attaches the uploaded file to the asset' do |
| 58 | + # Arrange |
| 59 | + Flipper.enable_actor :cat_mode, school |
| 60 | + |
| 61 | + # Act & Assert |
| 62 | + make_request |
| 63 | + expect(ScratchAsset.last.file).to be_attached |
| 64 | + end |
| 65 | + |
| 66 | + it 'stores the content of the file in the attachment' do |
| 67 | + # Arrange |
| 68 | + Flipper.enable_actor :cat_mode, school |
| 69 | + |
| 70 | + # Act & Assert |
| 71 | + make_request |
| 72 | + expect(ScratchAsset.last.file.download).to eq(upload) |
| 73 | + end |
| 74 | + |
| 75 | + it 'responds with 201 Created' do |
| 76 | + # Arrange |
| 77 | + Flipper.enable_actor :cat_mode, school |
| 78 | + |
| 79 | + # Act & Assert |
| 80 | + make_request |
| 81 | + expect(response).to have_http_status(:created) |
| 82 | + end |
| 83 | + |
| 84 | + it 'includes the status and filename (without extension) in the response' do |
| 85 | + # Arrange |
| 86 | + Flipper.enable_actor :cat_mode, school |
| 87 | + |
| 88 | + # Act & Assert |
| 89 | + make_request |
| 90 | + expect(response.parsed_body).to include( |
| 91 | + 'status' => 'ok', |
| 92 | + 'content-name' => basename |
| 93 | + ) |
| 94 | + end |
| 95 | + |
| 96 | + context 'when the asset already exists' do |
| 97 | + let(:another_upload_path) { file_fixture('test_image_2.jpeg') } |
| 98 | + let(:upload) { File.binread(another_upload_path) } |
| 99 | + let(:original_upload) { File.binread(file_fixture(filename)) } |
| 100 | + |
| 101 | + before do |
| 102 | + create(:scratch_asset, :with_file, filename:, asset_path: file_fixture(filename)) |
| 103 | + end |
| 104 | + |
| 105 | + it 'does not update the content of the file in the attachment' do |
| 106 | + # Arrange |
| 107 | + Flipper.enable_actor :cat_mode, school |
| 108 | + |
| 109 | + # Act & Assert |
| 110 | + make_request |
| 111 | + expect(ScratchAsset.last.file.download).to eq(original_upload) |
| 112 | + end |
| 113 | + |
| 114 | + it 'responds with 201 Created' do |
| 115 | + # Arrange |
| 116 | + Flipper.enable_actor :cat_mode, school |
| 117 | + |
| 118 | + # Act & Assert |
| 119 | + make_request |
| 120 | + expect(response).to have_http_status(:created) |
| 121 | + end |
| 122 | + |
| 123 | + it 'includes the status and filename (without extension) in the response' do |
| 124 | + # Arrange |
| 125 | + Flipper.enable_actor :cat_mode, school |
| 126 | + |
| 127 | + # Act & Assert |
| 128 | + make_request |
| 129 | + expect(response.parsed_body).to include( |
| 130 | + 'status' => 'ok', |
| 131 | + 'content-name' => basename |
| 132 | + ) |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + context 'when user is logged in a and cat_mode is disabled' do |
| 138 | + before do |
| 139 | + authenticated_in_hydra_as(teacher) |
| 140 | + Flipper.disable :cat_mode |
| 141 | + Flipper.disable_actor :cat_mode, school |
| 142 | + end |
| 143 | + |
| 144 | + it 'responds 404 Not Found when cat_mode is not enabled' do |
| 145 | + # Act |
| 146 | + post '/api/scratch/assets/example.svg', headers: cookie_headers |
| 147 | + |
| 148 | + # Assert |
| 149 | + expect(response).to have_http_status(:not_found) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments