Skip to content
Browse files

Merge pull request #51 from alphagov/fix_artefact_registration

Fix artefact registration
  • Loading branch information...
2 parents ca9b009 + 0f8747b commit d5b95f5467a9a2a1e90fc195081594eebc3aa124 @mnowster mnowster committed Jan 22, 2013
View
2 Gemfile
@@ -34,7 +34,7 @@ gem 'lograge', '~> 0.1.0'
if ENV['CONTENT_MODELS_DEV']
gem "govuk_content_models", path: '../govuk_content_models'
else
- gem "govuk_content_models", "4.0.0"
+ gem "govuk_content_models", "4.3.0"
end
if ENV['BUNDLE_DEV']
View
4 Gemfile.lock
@@ -122,7 +122,7 @@ GEM
htmlentities (~> 4)
kramdown (~> 0.13.3)
sanitize (= 2.0.3)
- govuk_content_models (4.0.0)
+ govuk_content_models (4.3.0)
bson_ext
differ
gds-api-adapters
@@ -324,7 +324,7 @@ DEPENDENCIES
gds-sso (= 3.0.0)
gds-warmup-controller (= 0.1.0)
gelf
- govuk_content_models (= 4.0.0)
+ govuk_content_models (= 4.3.0)
launchy
less-rails-bootstrap
lograge (~> 0.1.0)
View
8 app/controllers/artefacts_controller.rb
@@ -68,7 +68,13 @@ def update
else
@actions = build_actions
respond_with @artefact, status: status_to_use do |format|
- format.json { render json: @artefact.to_json, status: status_to_use }
+ format.json do
+ if saved
+ render json: @artefact.to_json, status: status_to_use
+ else
+ render json: {"errors" => @artefact.errors.full_messages}, status: 422
+ end
+ end
end
end
end
View
128 test/integration/artefacts_api_test.rb
@@ -0,0 +1,128 @@
+require_relative '../test_helper'
+
+class ArtefactsAPITest < ActiveSupport::TestCase
+
+ setup do
+ create_test_user
+ end
+
+ context "registering an artefact in panopticon" do
+ context "for a new artefact" do
+ should "create a new artefact, and return its details" do
+ artefact_data = {
+ 'slug' => 'wibble',
+ 'name' => 'Wibble',
+ 'kind' => 'answer',
+ 'description' => 'Wibble description',
+ 'owning_app' => 'publisher',
+ 'rendering_app' => 'frontend',
+ 'state' => 'draft',
+ }
+
+ # Rack::Test put method calls to_json on whatever body you pass.
+ # This is different to the post method. Go figure.
+ put "/artefacts/wibble.json", artefact_data
+
+ assert_equal 201, last_response.status
+
+ artefact = Artefact.find_by_slug('wibble')
+ assert artefact
+ assert_equal 'wibble', artefact.slug
+ assert_equal 'Wibble', artefact.name
+ assert_equal 'answer', artefact.kind
+ assert_equal 'Wibble description', artefact.description
+ assert_equal 'publisher', artefact.owning_app
+ assert_equal 'frontend', artefact.rendering_app
+ assert_equal 'draft', artefact.state
+ end
+
+ should "return an error if creating an artefact fails" do
+ artefact_data = {
+ 'slug' => 'wibble',
+ 'name' => 'Wibble',
+ 'kind' => 'wibble', # invalid kind
+ 'description' => 'Wibble description',
+ 'owning_app' => 'publisher',
+ 'state' => 'draft',
+ }
+
+ put "/artefacts/wibble.json", artefact_data
+
+ assert_equal 422, last_response.status
+ error_details = JSON.parse(last_response.body)
+ assert_equal({"errors" => ["Kind is not included in the list"]}, error_details)
+ end
+
+ should "support travel-advice artefacts with a travel-advice/foo style slug" do
+ artefact_data = {
+ 'slug' => 'travel-advice/aruba',
+ 'name' => 'Aruba travel advice',
+ 'kind' => 'travel-advice',
+ 'description' => 'Travel advice for people travelling to Aruba',
+ 'owning_app' => 'travel-advice-publisher',
+ 'rendering_app' => 'frontend',
+ 'state' => 'draft',
+ }
+
+ put "/artefacts/travel-advice/aruba.json", artefact_data
+
+ assert_equal 201, last_response.status
+
+ artefact = Artefact.find_by_slug('travel-advice/aruba')
+ assert artefact
+ end
+ end # for a new artefact
+
+ context "for an existing artefact" do
+ setup do
+ @artefact = FactoryGirl.create(:artefact, :slug => 'wibble', :name => "Wibble")
+ end
+
+ should "update the artefact" do
+ artefact_data = {
+ 'slug' => 'wibble',
+ 'name' => 'Wibble 2 - the return',
+ 'kind' => 'answer',
+ 'description' => 'Wibble description',
+ 'owning_app' => 'publisher',
+ 'rendering_app' => 'frontend',
+ 'state' => 'draft',
+ }
+
+ put "/artefacts/wibble.json", artefact_data
+
+ assert_equal 200, last_response.status
+
+ @artefact.reload
+ assert_equal 'wibble', @artefact.slug
+ assert_equal 'Wibble 2 - the return', @artefact.name
+ assert_equal 'answer', @artefact.kind
+ assert_equal 'Wibble description', @artefact.description
+ assert_equal 'publisher', @artefact.owning_app
+ assert_equal 'frontend', @artefact.rendering_app
+ assert_equal 'draft', @artefact.state
+ end
+
+ should "return an error if updating the artefact fails" do
+ artefact_data = {
+ 'slug' => 'wibble',
+ 'name' => 'Wibble 2 - the return',
+ 'kind' => 'wibble', # invalid kind
+ 'description' => 'Wibble description',
+ 'owning_app' => 'publisher',
+ 'state' => 'draft',
+ }
+
+ put "/artefacts/wibble.json", artefact_data
+
+ assert_equal 422, last_response.status
+ error_details = JSON.parse(last_response.body)
+ assert_equal({"errors" => ["Kind is not included in the list"]}, error_details)
+
+ @artefact.reload
+ assert_equal "Wibble", @artefact.name
+ end
+
+ end
+ end
+end
View
14 test/integration_test_helper.rb
@@ -1,25 +1,11 @@
require_relative 'test_helper'
require 'capybara/rails'
-require 'webmock'
-
-DatabaseCleaner.strategy = :truncation
class ActionDispatch::IntegrationTest
include Capybara::DSL
- include WebMock::API
-
- setup do
- DatabaseCleaner.clean
- end
teardown do
- DatabaseCleaner.clean
- WebMock.reset! # Not entirely sure whether this happens anyway
Capybara.use_default_driver
end
-
- def create_test_user
- FactoryGirl.create(:user)
- end
end
View
10 test/test_helper.rb
@@ -13,7 +13,7 @@
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'mocha'
-require 'webmock'
+require 'webmock/minitest'
require 'govuk_content_models/test_helpers/factories'
DatabaseCleaner.strategy = :truncation
@@ -28,10 +28,6 @@ def clean_db
end
set_callback :teardown, :before, :clean_db
- def setup
- WebMock.disable_net_connect!
- end
-
def app
Panopticon::Application
end
@@ -52,7 +48,7 @@ def login_as(user)
)
end
- def teardown
- WebMock.reset!
+ def create_test_user
+ FactoryGirl.create(:user)
end
end

0 comments on commit d5b95f5

Please sign in to comment.
Something went wrong with that request. Please try again.