diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index eead83a5..e5694cfe 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -55,6 +55,6 @@ def restrict_request_format end def asset_params - params.require(:asset).permit(:file) + params.require(:asset).permit(:file, :draft) end end diff --git a/app/controllers/whitehall_assets_controller.rb b/app/controllers/whitehall_assets_controller.rb index 3e74b72e..037b82d6 100644 --- a/app/controllers/whitehall_assets_controller.rb +++ b/app/controllers/whitehall_assets_controller.rb @@ -27,7 +27,7 @@ def show def asset_params params .require(:asset) - .permit(:file, :legacy_url_path, :legacy_etag, :legacy_last_modified) + .permit(:file, :draft, :legacy_url_path, :legacy_etag, :legacy_last_modified) end def existing_asset_with_this_legacy_url_path diff --git a/app/models/asset.rb b/app/models/asset.rb index 27baf751..1233fe75 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -13,6 +13,8 @@ class Asset field :uuid, type: String, default: -> { SecureRandom.uuid } attr_readonly :uuid + field :draft, type: Boolean, default: false + field :etag, type: String protected :etag= diff --git a/app/presenters/asset_presenter.rb b/app/presenters/asset_presenter.rb index 0c7fffe0..fb113006 100644 --- a/app/presenters/asset_presenter.rb +++ b/app/presenters/asset_presenter.rb @@ -14,6 +14,7 @@ def as_json(options = {}) content_type: @asset.content_type, file_url: URI.join(Plek.new.asset_root, Addressable::URI.encode(@asset.public_url_path)).to_s, state: @asset.state, + draft: @asset.draft? } end end diff --git a/spec/controllers/assets_controller_spec.rb b/spec/controllers/assets_controller_spec.rb index 78cdae88..eae02f42 100644 --- a/spec/controllers/assets_controller_spec.rb +++ b/spec/controllers/assets_controller_spec.rb @@ -34,6 +34,7 @@ expect(body['id']).to eq("http://test.host/assets/#{asset.id}") expect(body['name']).to eq("asset.png") expect(body['content_type']).to eq("image/png") + expect(body['draft']).to be_falsey end end @@ -53,6 +54,24 @@ expect(response).to have_http_status(:unprocessable_entity) end end + + context "a draft asset" do + let(:attributes) { { draft: true, file: load_fixture_file("asset.png") } } + + it "is persisted" do + post :create, params: { asset: attributes } + + expect(assigns(:asset)).to be_draft + end + + it "returns the draft status of the new asset" do + post :create, params: { asset: attributes } + + body = JSON.parse(response.body) + + expect(body['draft']).to be_truthy + end + end end describe "PUT update" do @@ -83,6 +102,7 @@ expect(body['id']).to eq("http://test.host/assets/#{asset.id}") expect(body['name']).to eq("asset2.jpg") expect(body['content_type']).to eq("image/jpeg") + expect(body['draft']).to be_falsey end end @@ -102,6 +122,25 @@ expect(response).to have_http_status(:unprocessable_entity) end end + + context "a draft asset" do + let(:attributes) { { draft: true, file: load_fixture_file("asset2.jpg") } } + let(:asset) { FactoryBot.create(:asset) } + + it "updates attributes" do + put :update, params: { id: asset.id, asset: attributes } + + expect(assigns(:asset)).to be_draft + end + + it "returns the draft status of the updated asset" do + put :update, params: { id: asset.id, asset: attributes } + + body = JSON.parse(response.body) + + expect(body['draft']).to be_truthy + end + end end describe "DELETE destroy" do @@ -165,6 +204,14 @@ expect(assigns(:asset)).to be_a(Asset) expect(assigns(:asset).id).to eq(asset.id) end + + it "returns the draft status of the asset" do + get :show, params: { id: asset.id } + + body = JSON.parse(response.body) + + expect(body['draft']).to be_falsey + end end context "an asset which does not exist" do diff --git a/spec/controllers/whitehall_assets_controller_spec.rb b/spec/controllers/whitehall_assets_controller_spec.rb index 3f6d33bb..a9a12843 100644 --- a/spec/controllers/whitehall_assets_controller_spec.rb +++ b/spec/controllers/whitehall_assets_controller_spec.rb @@ -104,6 +104,23 @@ expect(existing_asset.reload).to be_deleted end end + + context "a draft asset" do + let(:attributes) { FactoryBot.attributes_for(:whitehall_asset, :with_legacy_metadata, draft: true) } + + it "stores draft status on asset" do + post :create, params: { asset: attributes } + + expect(assigns(:asset)).to be_draft + end + + it "returns JSON response including draft status of new asset" do + post :create, params: { asset: attributes } + + body = JSON.parse(response.body) + expect(body['draft']).to be_truthy + end + end end describe 'GET show' do diff --git a/spec/models/asset_spec.rb b/spec/models/asset_spec.rb index 7586d692..5c04d3a7 100644 --- a/spec/models/asset_spec.rb +++ b/spec/models/asset_spec.rb @@ -77,6 +77,30 @@ end end + describe '#draft?' do + subject(:asset) { Asset.new } + + it 'returns false-y by default' do + expect(asset).not_to be_draft + end + + context 'when draft attribute is set to false' do + subject(:asset) { Asset.new(draft: false) } + + it 'returns false-y by default' do + expect(asset).not_to be_draft + end + end + + context 'when draft attribute is set to true' do + subject(:asset) { Asset.new(draft: true) } + + it 'returns truth-y by default' do + expect(asset).to be_draft + end + end + end + describe '#public_url_path' do subject(:asset) { Asset.new } diff --git a/spec/presenters/asset_presenter_spec.rb b/spec/presenters/asset_presenter_spec.rb index 5df51b53..8b4d9a7b 100644 --- a/spec/presenters/asset_presenter_spec.rb +++ b/spec/presenters/asset_presenter_spec.rb @@ -55,6 +55,20 @@ expect(json).to include(state: 'unscanned') end + it 'returns hash including asset draft status as false' do + expect(json).to include(draft: false) + end + + context 'when asset is draft' do + before do + asset.draft = true + end + + it 'returns hash including asset draft status as true' do + expect(json).to include(draft: true) + end + end + context 'when public url path contains non-ascii characters' do let(:public_url_path) { '/public-ürl-path' }