Skip to content

Commit

Permalink
Merge pull request #435 from alphagov/allow-assets-to-be-marked-as-draft
Browse files Browse the repository at this point in the history
Allow assets to be marked as draft
  • Loading branch information
floehopper authored Jan 29, 2018
2 parents b497ecb + b811b14 commit 2eca322
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/controllers/whitehall_assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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=

Expand Down
1 change: 1 addition & 0 deletions app/presenters/asset_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 47 additions & 0 deletions spec/controllers/assets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/controllers/whitehall_assets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions spec/models/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
14 changes: 14 additions & 0 deletions spec/presenters/asset_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand Down

0 comments on commit 2eca322

Please sign in to comment.