Skip to content

Commit a54f922

Browse files
committed
Handle loading and creating assets
For now, the show always returns a static asset and the create always succeeds but doesn't do any saving. Note that the /internalapi/asset/.../get/ route is dictated by Scratch.
1 parent 39f4ebb commit a54f922

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module Api
4+
module Scratch
5+
class AssetsController < ScratchController
6+
skip_before_action :authorize_user, only: [:show]
7+
skip_before_action :check_scratch_feature, only: [:show]
8+
9+
def show
10+
render :show, formats: [:svg]
11+
end
12+
13+
def create
14+
render json: { status: 'ok', 'content-name': params[:id] }, status: :created
15+
end
16+
end
17+
end
18+
end
Lines changed: 20 additions & 0 deletions
Loading

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
namespace :api do
3636
namespace :scratch do
3737
resources :projects, only: %i[show update]
38+
get '/assets/internalapi/asset/:id(.:format)/get/' => 'assets#show'
39+
post '/assets/:id(.:format)' => 'assets#create'
3840
end
3941

4042
resource :default_project, only: %i[show] do
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Creating a Scratch asset', type: :request do
6+
let(:school) { create(:school) }
7+
let(:teacher) { create(:teacher, school:) }
8+
let(:cookie_headers) { { 'Cookie' => "scratch_auth=#{UserProfileMock::TOKEN}" } }
9+
10+
before do
11+
Flipper.disable :cat_mode
12+
Flipper.disable_actor :cat_mode, school
13+
end
14+
15+
it 'responds 401 Unauthorized when no cookie is provided' do
16+
post '/api/scratch/assets/example.svg'
17+
18+
expect(response).to have_http_status(:unauthorized)
19+
end
20+
21+
it 'responds 404 Not Found when cat_mode is not enabled' do
22+
authenticated_in_hydra_as(teacher)
23+
24+
post '/api/scratch/assets/example.svg', headers: cookie_headers
25+
26+
expect(response).to have_http_status(:not_found)
27+
end
28+
29+
it 'creates an asset when cat_mode is enabled and a cookie is provided' do
30+
authenticated_in_hydra_as(teacher)
31+
Flipper.enable_actor :cat_mode, school
32+
33+
post '/api/scratch/assets/example.svg', headers: cookie_headers
34+
35+
expect(response).to have_http_status(:created)
36+
37+
data = JSON.parse(response.body, symbolize_names: true)
38+
expect(data[:status]).to eq('ok')
39+
expect(data[:'content-name']).to eq('example')
40+
end
41+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Showing a Scratch asset', type: :request do
6+
it 'returns scratch asset SVG' do
7+
get '/api/scratch/assets/internalapi/asset/example.svg/get/'
8+
9+
expect(response).to have_http_status(:ok)
10+
expect(response.body).to include('<svg')
11+
end
12+
end

0 commit comments

Comments
 (0)