Skip to content

Commit

Permalink
Add testing for Datastreams#edit
Browse files Browse the repository at this point in the history
And fix problems the test finds
  • Loading branch information
jcoyne committed Jul 6, 2020
1 parent 4e46c4b commit e64e15f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 125 deletions.
86 changes: 21 additions & 65 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-07-01 21:29:33 UTC using RuboCop version 0.86.0.
# on 2020-07-06 16:19:06 UTC using RuboCop version 0.86.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -47,52 +47,29 @@ Metrics/AbcSize:
Max: 82

# Offense count: 5
# Configuration parameters: CountComments, Max.
# Configuration parameters: CountComments.
Metrics/ClassLength:
Exclude:
- 'app/controllers/apo_controller.rb'
- 'app/controllers/catalog_controller.rb'
- 'app/forms/apo_form.rb'
- 'app/models/user_log.rb'
- 'app/presenters/buttons_presenter.rb'
Max: 173

# Offense count: 11
# Configuration parameters: IgnoredMethods, Max.
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Exclude:
- 'app/controllers/tags_controller.rb'
- 'app/forms/registration_form.rb'
- 'app/helpers/value_helper.rb'
- 'app/models/ability.rb'
- 'app/models/report.rb'
- 'app/models/track_sheet.rb'
- 'app/models/user.rb'
- 'app/models/user_log.rb'
- 'app/presenters/buttons_presenter.rb'
- 'app/services/apply_mods_metadata.rb'
- 'spec/support/fixtures.rb'
Max: 14

# Offense count: 74
# Offense count: 80
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 50

# Offense count: 3
# Configuration parameters: Max, CountKeywordArgs.
# Configuration parameters: CountKeywordArgs.
Metrics/ParameterLists:
Exclude:
- 'app/components/action_button.rb'
- 'app/jobs/modsulator_job.rb'
- 'app/services/apply_mods_metadata.rb'
Max: 7

# Offense count: 4
# Configuration parameters: IgnoredMethods, Max.
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Exclude:
- 'app/models/ability.rb'
- 'app/models/track_sheet.rb'
- 'app/presenters/buttons_presenter.rb'
- 'app/services/apply_mods_metadata.rb'
Max: 12

# Offense count: 1
Naming/AccessorMethodName:
Expand All @@ -106,14 +83,7 @@ Naming/MemoizedInstanceVariableName:
Exclude:
- 'config/initializers/okcomputer.rb'

# Offense count: 1
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: io, id, to, by, on, in, at, ip, db, os, pp
Naming/MethodParameterName:
Exclude:
- 'app/helpers/dor_object_helper.rb'

# Offense count: 16
# Offense count: 17
RSpec/AnyInstance:
Exclude:
- 'spec/controllers/bulk_actions_controller_spec.rb'
Expand All @@ -124,20 +94,21 @@ RSpec/AnyInstance:
- 'spec/features/enable_buttons_spec.rb'
- 'spec/features/item_registration_spec.rb'
- 'spec/features/set_governing_apo_spec.rb'
- 'spec/requests/edit_datastream_spec.rb'
- 'spec/requests/item_files_download_spec.rb'
- 'spec/requests/manage_release_spec.rb'

# Offense count: 83
# Offense count: 85
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Enabled: false

# Offense count: 74
# Offense count: 75
RSpec/DescribeClass:
Enabled: false

# Offense count: 182
# Offense count: 183
# Configuration parameters: Max.
RSpec/ExampleLength:
Enabled: false
Expand Down Expand Up @@ -189,31 +160,16 @@ RSpec/MessageChain:
RSpec/MessageSpies:
Enabled: false

# Offense count: 188
# Offense count: 189
# Configuration parameters: IgnoreSharedExamples.
RSpec/NamedSubject:
Enabled: false

# Offense count: 53
# Configuration parameters: Max.
RSpec/NestedGroups:
Exclude:
- 'spec/components/workflow_table_process_component_spec.rb'
- 'spec/controllers/bulk_actions_controller_spec.rb'
- 'spec/controllers/catalog_controller_spec.rb'
- 'spec/controllers/content_types_controller_spec.rb'
- 'spec/controllers/files_controller_spec.rb'
- 'spec/controllers/items_controller_spec.rb'
- 'spec/controllers/report_controller_spec.rb'
- 'spec/controllers/workflow_service_controller_spec.rb'
- 'spec/controllers/workflows_controller_spec.rb'
- 'spec/forms/apo_form_spec.rb'
- 'spec/jobs/checksum_report_job_spec.rb'
- 'spec/jobs/create_virtual_objects_job_spec.rb'
- 'spec/models/user_spec.rb'
- 'spec/services/state_service_spec.rb'
Max: 6

# Offense count: 3
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: Strict, EnforcedStyle.
# SupportedStyles: inflected, explicit
Expand Down Expand Up @@ -253,7 +209,7 @@ Rails/ApplicationJob:
- 'app/jobs/generic_job.rb'
- 'app/jobs/modsulator_job.rb'

# Offense count: 6
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: NilOrEmpty, NotPresent, UnlessPresent.
Rails/Blank:
Expand Down Expand Up @@ -345,7 +301,7 @@ Style/CommentedKeyword:
- 'lib/tasks/argo.rake'
- 'lib/tasks/argo_testing.rake'

# Offense count: 62
# Offense count: 63
Style/Documentation:
Enabled: false

Expand All @@ -372,7 +328,7 @@ Style/NumericPredicate:
- 'spec/**/*'
- 'app/jobs/descmetadata_download_job.rb'

# Offense count: 290
# Offense count: 291
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/datastreams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ class DatastreamsController < ApplicationController
include Blacklight::Catalog
copy_blacklight_config_from CatalogController

before_action :show_aspect, only: %i[dc show]
before_action :show_aspect, only: %i[dc show edit]

def dc
respond_to do |format|
format.html { render layout: !request.xhr? }
end
end

def edit
@ds = @obj.datastreams[params[:id]]
render layout: !request.xhr?
end

def show
if params[:dsid] == 'full_dc'
@content = Nokogiri::XML(Dor::Services::Client.object(@obj.pid).metadata.dublin_core).prettify
Expand Down
2 changes: 1 addition & 1 deletion app/views/datastreams/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<% component.with(:header, params[:dsid]) %>
<% component.with(:body) do %>
<% if can?(:manage_item, @obj) %>
<%= form_tag url_for(controller: :items, action: :datastream_update), id: 'xmlEditForm', method: :POST do %>
<%= form_tag item_datastream_path(@obj.pid, @ds.dsid), id: 'xmlEditForm', method: :patch do %>
<input type="hidden" name="id" value="<%= @obj.pid %>">
<input name="dsid" id="datastream" type="hidden" value="<%= params[:dsid] %>">
<div class="form-group">
Expand Down
58 changes: 0 additions & 58 deletions spec/controllers/datastreams_controller_spec.rb

This file was deleted.

58 changes: 58 additions & 0 deletions spec/requests/edit_datastream_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Draw the edit datastream form' do
let(:item) do
instance_double(Dor::Item, pid: 'druid:bc123df4567', datastreams: { 'descMetadata' => datastream })
end
let(:datastream) do
instance_double(Dor::DescMetadataDS, dsid: 'descMetadata', content: xml)
end

let(:xml) do
<<~XML
<descMetadata></descMetadata>
XML
end

let(:document) do
instance_double(SolrDocument,
id: 'druid:bc123df4567',
object_type: 'item',
title: 'My item',
released_to: ['Searchworks'])
end

let(:user) { create(:user) }

before do
allow(Dor).to receive(:find).with('druid:bc123df4567').and_return(item)
end

context 'for content managers' do
before do
sign_in create(:user), groups: ['sdr:administrator-role']

allow_any_instance_of(Blacklight::Solr::Repository).to receive(:find)
.with('druid:bc123df4567', {})
.and_return(instance_double(Blacklight::Solr::Response, documents: [document]))
end

it 'authorizes the view' do
get '/items/druid:bc123df4567/datastreams/descMetadata/edit', xhr: true
expect(response).to have_http_status(:success)
end
end

context 'for unauthorized_user' do
before do
sign_in user
end

it 'returns not found' do
expect { get '/items/druid:bc123df4567/datastreams/descMetadata/edit' }
.to raise_error Blacklight::Exceptions::RecordNotFound
end
end
end
53 changes: 53 additions & 0 deletions spec/requests/update_datastream_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Update a datastream' do
before do
allow(Dor).to receive(:find).with(pid).and_return(item)
allow(item).to receive_messages(save: nil)
allow(Argo::Indexer).to receive(:reindex_pid_remotely)
end

let(:pid) { 'druid:bc123df4567' }
let(:item) { Dor::Item.new pid: pid }
let(:user) { create(:user) }
let(:state_service) { instance_double(StateService, allows_modification?: true) }
let(:xml) { '<contentMetadata/>' }
let(:invalid_apo_xml) { '<hydra:isGovernedBy rdf:resource="info:fedora/druid:not_exist"/>' }

context 'without management access' do
before do
sign_in user
end

it 'prevents access' do
expect(item).not_to receive(:save)
patch "/items/#{pid}/datastreams/contentMetadata", params: { content: xml }
expect(response).to have_http_status(:forbidden)
end
end

context 'when they have manage access' do
before do
sign_in user, groups: ['sdr:administrator-role']
end

it 'updates the datastream' do
expect(item).to receive(:datastreams).and_return(
'contentMetadata' => double(Dor::ContentMetadataDS, 'content=': xml)
)
expect(item).to receive(:save)
patch "/items/#{pid}/datastreams/contentMetadata", params: { content: xml }
expect(response).to have_http_status(:found)
end

it 'errors on empty xml' do
expect { patch "/items/#{pid}/datastreams/contentMetadata", params: { content: ' ' } }.to raise_error(ArgumentError)
end

it 'errors on malformed xml' do
expect { patch "/items/#{pid}/datastreams/contentMetadata", params: { content: '<this>isnt well formed.' } }.to raise_error(ArgumentError)
end
end
end

0 comments on commit e64e15f

Please sign in to comment.