-
Notifications
You must be signed in to change notification settings - Fork 5
Add asset save/loading #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7215427
Add asset save/loading
mwtrew 8958dd1
Fix RuboCop violations
mwtrew da54733
Add non-null constraint and index to `filename`
mwtrew a966aa8
Fix typo
mwtrew 88465c9
Add uniqueness and presence validation to Scratch asset model
mwtrew ca51402
Expect file extension in routes
mwtrew be5a91e
Resolve merge conflict
mwtrew 9f989c0
Use Active Storage redirect mode
mwtrew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ScratchAsset < ApplicationRecord | ||
| validates :filename, presence: true, uniqueness: true | ||
|
|
||
| has_one_attached :file | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class CreateScratchAssets < ActiveRecord::Migration[7.2] | ||
| def change | ||
| create_table :scratch_assets, id: :uuid do |t| | ||
| t.string :filename, null: false | ||
| t.index :filename, unique: true | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| FactoryBot.define do | ||
| factory :scratch_asset do | ||
| sequence(:filename) { Random.hex } | ||
|
|
||
| trait :with_file do | ||
| transient { asset_path { file_fixture('test_image_1.png') } } | ||
|
|
||
| after(:build) do |asset, evaluator| | ||
| io = Rails.root.join(evaluator.asset_path).open | ||
| filename = File.basename(evaluator.asset_path) | ||
| content_type = Mime::Type.lookup_by_extension(filename) | ||
| asset.file.attach(io:, filename:, content_type:) | ||
|
mwtrew marked this conversation as resolved.
|
||
| end | ||
| end | ||
| end | ||
| end | ||
153 changes: 153 additions & 0 deletions
153
spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Creating a Scratch asset', type: :request do | ||
| let(:basename) { 'test_image_1' } | ||
| let(:format) { 'png' } | ||
| let(:filename) { "#{basename}.#{format}" } | ||
| let(:school) { create(:school) } | ||
| let(:teacher) { create(:teacher, school:) } | ||
| let(:cookie_headers) { { 'Cookie' => "scratch_auth=#{UserProfileMock::TOKEN}" } } | ||
|
|
||
| describe 'GET #show' do | ||
| let(:make_request) { get '/api/scratch/assets/internalapi/asset/test_image_1.png/get/' } | ||
|
|
||
| context 'when the asset exists' do | ||
| let!(:scratch_asset) { create(:scratch_asset, :with_file, filename:, asset_path: file_fixture(filename)) } | ||
|
|
||
| it 'redirects to the asset file URL' do | ||
| make_request | ||
|
|
||
| expect(response).to redirect_to(rails_storage_redirect_url(scratch_asset.file, only_path: true)) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'POST #create' do | ||
| let(:upload) { File.binread(file_fixture(filename)) } | ||
| let(:make_request) do | ||
| post '/api/scratch/assets/test_image_1.png', headers: { 'Content-Type' => 'application/octet-stream' }.merge(cookie_headers), params: upload | ||
| end | ||
|
|
||
| context 'when user is logged in and cat_mode is enabled' do | ||
| before do | ||
| authenticated_in_hydra_as(teacher) | ||
| Flipper.disable :cat_mode | ||
| Flipper.disable_actor :cat_mode, school | ||
| end | ||
|
|
||
| it 'creates a new asset' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| expect { make_request }.to change(ScratchAsset, :count).by(1) | ||
| end | ||
|
|
||
| it 'sets the filename on the asset' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(ScratchAsset.last.filename).to eq(filename) | ||
| end | ||
|
|
||
| it 'attaches the uploaded file to the asset' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(ScratchAsset.last.file).to be_attached | ||
| end | ||
|
|
||
| it 'stores the content of the file in the attachment' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(ScratchAsset.last.file.download).to eq(upload) | ||
| end | ||
|
|
||
| it 'responds with 201 Created' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(response).to have_http_status(:created) | ||
| end | ||
|
|
||
| it 'includes the status and filename (without extension) in the response' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(response.parsed_body).to include( | ||
| 'status' => 'ok', | ||
| 'content-name' => basename | ||
| ) | ||
| end | ||
|
|
||
| context 'when the asset already exists' do | ||
| let(:another_upload_path) { file_fixture('test_image_2.jpeg') } | ||
| let(:upload) { File.binread(another_upload_path) } | ||
| let(:original_upload) { File.binread(file_fixture(filename)) } | ||
|
|
||
| before do | ||
| create(:scratch_asset, :with_file, filename:, asset_path: file_fixture(filename)) | ||
| end | ||
|
|
||
| it 'does not update the content of the file in the attachment' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(ScratchAsset.last.file.download).to eq(original_upload) | ||
| end | ||
|
|
||
| it 'responds with 201 Created' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(response).to have_http_status(:created) | ||
| end | ||
|
|
||
| it 'includes the status and filename (without extension) in the response' do | ||
| # Arrange | ||
| Flipper.enable_actor :cat_mode, school | ||
|
|
||
| # Act & Assert | ||
| make_request | ||
| expect(response.parsed_body).to include( | ||
| 'status' => 'ok', | ||
| 'content-name' => basename | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when user is logged in and cat_mode is disabled' do | ||
| before do | ||
| authenticated_in_hydra_as(teacher) | ||
| Flipper.disable :cat_mode | ||
| Flipper.disable_actor :cat_mode, school | ||
| end | ||
|
|
||
| it 'responds 404 Not Found when cat_mode is not enabled' do | ||
| # Act | ||
| post '/api/scratch/assets/example.svg', headers: cookie_headers | ||
|
|
||
| # Assert | ||
| expect(response).to have_http_status(:not_found) | ||
| end | ||
| end | ||
| end | ||
| end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.