diff --git a/app/controllers/artefacts_controller.rb b/app/controllers/artefacts_controller.rb index 6f59a083..48e1eb8f 100644 --- a/app/controllers/artefacts_controller.rb +++ b/app/controllers/artefacts_controller.rb @@ -37,7 +37,7 @@ def edit end def create - @artefact.save_as current_user + @artefact.save_as action_user respond_with @artefact, location: @artefact.admin_url(params.slice(:return_to)) end @@ -62,7 +62,7 @@ def update return end - saved = @artefact.update_attributes_as(current_user, parameters_to_use) + saved = @artefact.update_attributes_as(action_user, parameters_to_use) flash[:notice] = saved ? 'Panopticon item updated' : 'Failed to save item' if saved && params[:commit] == 'Save and continue editing' @@ -111,4 +111,11 @@ def extract_parameters(params) parameters_to_use end + def action_user + # The user to associate with actions + # Currently this returns nil for the API user: this should go away once + # we have real user authentication for API requests + action_user = current_user.is_a?(User) ? current_user : nil + end + end diff --git a/test/functional/artefacts_controller_test.rb b/test/functional/artefacts_controller_test.rb index 2a71bb38..a7d0c242 100644 --- a/test/functional/artefacts_controller_test.rb +++ b/test/functional/artefacts_controller_test.rb @@ -158,6 +158,25 @@ class ArtefactsControllerTest < ActionController::TestCase assert_equal stub_user, artefact.actions.last.user end + should "Not record the user for API requests" do + login_as GDS::SSO::ApiUser.new + artefact = Artefact.create!( + :slug => 'whatever', + :kind => 'guide', + :owning_app => 'publisher', + :rendering_app => 'frontend', + :name => 'Whatever', + :need_id => 1 + ) + + put :update, id: artefact.id, format: :json, name: "Changed" + assert_response :success + + artefact.reload + assert_equal nil, artefact.actions.last.user + end + + should "Update our primary section and ensure it persists into sections" do @tags = FactoryGirl.create_list(:tag, 3) artefact = Artefact.create!(:slug => 'whatever', :kind => 'guide', diff --git a/test/test_helper.rb b/test/test_helper.rb index 9e0e27d9..0057d9e0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -41,10 +41,14 @@ def stub_user end def login_as_stub_user + login_as stub_user + end + + def login_as(user) request.env['warden'] = stub( :authenticate! => true, :authenticated? => true, - :user => stub_user + :user => user ) end