Permalink
Browse files

Merge pull request #16 from alphagov/support_archiving_artefacts

Support archiving artefacts
  • Loading branch information...
2 parents 7859c97 + 3f7c7f4 commit 493cfeddb081f41b85a2c79fadad5c6fe8c9ad37 @jystewart jystewart committed Aug 16, 2012
View
@@ -33,7 +33,7 @@ gem 'lograge'
if ENV['CONTENT_MODELS_DEV']
gem "govuk_content_models", path: '../govuk_content_models'
else
- gem "govuk_content_models", "0.7.0"
+ gem "govuk_content_models", "1.0.0"
end
if ENV['BUNDLE_DEV']
View
@@ -100,7 +100,7 @@ GEM
factory_girl_rails (3.3.0)
factory_girl (~> 3.3.0)
railties (>= 3.0.0)
- faraday (0.8.1)
+ faraday (0.8.2)
multipart-post (~> 1.1)
ffi (1.1.4)
gds-api-adapters (0.2.2)
@@ -118,7 +118,7 @@ GEM
json
gherkin (2.11.1)
json (>= 1.4.6)
- govuk_content_models (0.7.0)
+ govuk_content_models (1.0.0)
bson_ext
differ
gds-api-adapters
@@ -253,7 +253,7 @@ GEM
stomp (1.2.5)
therubyracer (0.9.10)
libv8 (~> 3.3.10)
- thor (0.15.4)
+ thor (0.16.0)
tilt (1.3.3)
treetop (1.4.10)
polyglot
@@ -302,7 +302,7 @@ DEPENDENCIES
gds-sso (~> 1.2.0)
gds-warmup-controller (= 0.1.0)
gelf
- govuk_content_models (= 0.7.0)
+ govuk_content_models (= 1.0.0)
launchy
lograge
minitest
@@ -74,6 +74,14 @@ def update
end
end
+ def destroy
+ @artefact = Artefact.from_param(params[:id])
+ @artefact.update_attributes_as(action_user, state: "archived")
+ respond_with(@artefact) do |format|
+ format.html { head 200 }
+ end
+ end
+
private
def tag_collection
@@ -116,6 +124,13 @@ def extract_parameters(params)
# TODO: Remove this variance
parameters_to_use = params[:artefact] || params.slice(*fields_to_update)
+ # Partly for legacy reasons, the API can receive live=true
+ if live_param = parameters_to_use[:live]
+ if ["true", true, "1"].include?(live_param)
+ parameters_to_use[:state] = "live"
+ end
+ end
+
# Strip out the empty submit option for sections
['sections'].each do |param|
param_value = parameters_to_use[param]
@@ -4,4 +4,8 @@ class Artefact
# Add a non-field attribute so we can pass indexable content over to Rummager
# without persisting it
attr_accessor :indexable_content
+
+ def archived?
+ state == 'archived'
+ end
end
@@ -38,6 +38,12 @@ def submit
end
end
+ def delete
+ ([@artefact.slug] + @artefact.paths + @artefact.prefixes).each do |path|
+ router.delete_route(path)
+ end
+ end
+
private
def rendering_app
@artefact.rendering_app || @artefact.owning_app
@@ -22,6 +22,10 @@ def submit
end
end
+ def delete
+ Rummageable.delete(artefact_link)
+ end
+
def should_amend
@artefact.indexable_content.nil?
end
@@ -3,5 +3,8 @@ class UpdateRouterObserver < Mongoid::Observer
def after_save(artefact)
RoutableArtefact.new(artefact).submit if artefact.live?
+ # Relying on current behaviour where this does not raise errors
+ # if done more than once, or done on artefacts never put live
+ RoutableArtefact.new(artefact).delete if artefact.archived?
end
end
@@ -3,5 +3,8 @@ class UpdateSearchObserver < Mongoid::Observer
def after_save(artefact)
RummageableArtefact.new(artefact).submit if artefact.live?
+ # Relying on current behaviour where this does not raise errors
+ # if done more than once, or done on artefacts never put live
+ RummageableArtefact.new(artefact).delete if artefact.archived?
end
end
@@ -26,3 +26,9 @@ Feature: Registering resources
When I put a new item into panopticon whose slug is already taken
Then I should receive an HTTP 409 response
And the relevant artefact should not be updated
+
+ Scenario: Deleting an item
+ When I delete an artefact
+ Then the artefact state should be archived
+ And rummager should be notified of the delete
+ And the router should be notified of the delete
@@ -3,11 +3,15 @@
end
Given /^the first artefact is in draft$/ do
- Artefact.first.update_attribute 'live', false
+ Artefact.observers.disable :update_search_observer, :update_router_observer do
+ Artefact.first.update_attributes!('state' => 'draft')
+ end
end
Given /^the first artefact is live$/ do
- Artefact.first.update_attribute 'live', true
+ Artefact.observers.disable :update_search_observer, :update_router_observer do
+ Artefact.first.update_attributes!('state' => 'live')
+ end
end
Given /^two non-publisher artefacts exist$/ do
@@ -30,7 +30,7 @@
prepare_registration_environment
details = example_smart_answer
- details['live'] = false
+ details['state'] = 'draft'
put "/artefacts/#{example_smart_answer['slug']}.json", artefact: details
end
@@ -45,6 +45,15 @@
artefact: artefact_basics
end
+When /^I delete an artefact$/ do
+ prepare_registration_environment
+ setup_existing_artefact
+ stub_search_delete
+ stub_router_delete
+
+ delete "/artefacts/#{@artefact.slug}.json"
+end
+
Then /^a new artefact should be created$/ do
assert_equal 201, last_response.status, "Expected 201, got #{last_response.status}"
end
@@ -97,3 +106,18 @@
Then /^rummager should not be notified$/ do
assert_not_requested @fake_search
end
+
+Then /^the artefact state should be archived$/ do
+ assert_equal 'archived', Artefact.last.state
+end
+
+Then /^rummager should be notified of the delete$/ do
+ assert_requested @fake_search_delete, times: 1 # The default, but let's be explicit
+end
+
+Then /^the router should be notified of the delete$/ do
+ assert ! @fake_router_deletes.blank?, "No router requests registered to assert on"
+ @fake_router_deletes.each do |fake_router_delete|
+ assert_requested fake_router_delete, times: 1
+ end
+end
@@ -15,7 +15,7 @@ def example_smart_answer
"link" => "/calculate-married-couples-allowance",
"indexable_content" => "You can use this calculator to work out if you qualify for Married Couple's Allowance, and how much you might get. You need to be married or in a civil partnership to claim. Were you or your partner born on or before 6 April 1935? You must be married or in a civil partnership to qualify. Did you marry before 5 December 2005? Before this date the husband's income is used to work out your allowance, after this date it's the income of the highest earner. What's the husband's date of birth? We need your date of birth to work out your personal allowance (how much of your income is tax-free). What's the highest earner's date of birth? We need your date of birth to work out your personal allowance (how much of your income is tax-free). What's the husband's yearly income? Add up your taxable income, eg earnings, pensions and any taxable benefits, eg Employment and Support Allowance. What's the highest earner's yearly income? Add up your taxable income, eg earnings, pensions and any taxable benefits, eg Employment and Support Allowance. Contact HM Revenue & Customs to claim. HM Revenue & Customs Telephone 0845 300 0627 Textphone 0845 302 1408 This result is an estimate based on your answers. Contact HM Revenue & Customs to claim. HM Revenue & Customs Telephone 0845 300 0627 Textphone 0845 302 1408 This result is an estimate based on your answers. Sorry, you don't qualify for Married Couple's Allowance.",
"owning_app" => 'smart-answers',
- "live" => true
+ "state" => "live"
}
end
@@ -56,7 +56,17 @@ def stub_router
end
end
+ def stub_search_delete
+ @fake_search_delete = WebMock.stub_request(:delete, artefact_search_url(@artefact)).to_return(status: 200)
+ end
+ def stub_router_delete
+ # so that we can assert on them later
+ @fake_router_deletes = [@artefact].map do |artefact|
+ WebMock.stub_request(:delete, "#{ROUTER_ROOT}/router/routes/#{artefact.slug}").
+ to_return(:status => 200, :body => "{}", :headers => {})
+ end
+ end
def artefact_search_url(artefact)
# The search URL to which amendment requests should be POSTed
View
@@ -20,4 +20,16 @@ namespace :migrate do
puts "Added description to tag: #{tag.tag_id}"
end
end
+
+ desc "Move Artefacts from the 'live' column to having a 'state' column"
+ task :move_artefacts_to_state_column => :environment do
+ Artefact.all.each do |artefact|
+ if artefact[:live]
+ artefact.state = "published"
+ else
+ artefact.state = "draft"
+ end
+ artefact.save!
+ end
+ end
end
@@ -226,5 +226,31 @@ class ArtefactsControllerTest < ActionController::TestCase
end
end
+ context "DELETE /artefacts/:id" do
+ setup do
+ WebMock.stub_request(:delete, "http://router.cluster:8080/router/routes/whatever").
+ to_return(:status => 200)
+ WebMock.stub_request(:delete, "http://search.test.gov.uk/documents/%2Fwhatever").
+ to_return(:status => 200)
+ end
+
+ should "mark an artefact as archived" do
+ artefact = Artefact.create!(
+ slug: "whatever",
+ kind: "guide",
+ owning_app: "publisher",
+ name: "Whatever",
+ need_id: 1
+ )
+ delete :destroy, id: artefact.id, "CONTENT_TYPE" => "application/json"
+ assert_equal 200, response.status
+ assert_equal "archived", artefact.reload.state
+ end
+
+ should "return a 404" do
+ delete :destroy, id: "4567", "CONTENT_TYPE" => "application/json"
+ assert_equal 404, response.status
+ end
+ end
end
end

0 comments on commit 493cfed

Please sign in to comment.