-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_embargo_spec.rb
73 lines (63 loc) · 2.85 KB
/
update_embargo_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Update embargo' do
let(:item) { Dor::Item.new(pid: 'druid:1234') }
let(:mock_embargo_service) { instance_double(Dor::EmbargoService) }
let(:events_datastream) { instance_double(Dor::EventsDS, add_event: true) }
before do
allow(Dor).to receive(:find).and_return(item)
allow(item).to receive(:events).and_return(events_datastream)
end
context 'without the :embargo_date param' do
it 'returns HTTP 400' do
patch '/v1/objects/druid:mk420bs7601/embargo',
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(response.status).to eq(400)
expect(response.body).to eq('{"errors":[{"title":"bad request","detail":"param is missing or the value is empty: embargo_date"}]}')
end
end
context 'without the :requesting_user param' do
it 'returns HTTP 400' do
patch '/v1/objects/druid:mk420bs7601/embargo',
params: { embargo_date: '2100-01-01' },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(events_datastream).not_to have_received(:add_event)
expect(response.status).to eq(400)
expect(response.body).to eq('{"errors":[{"title":"bad request","detail":"param is missing or the value is empty: requesting_user"}]}')
end
end
context 'when Dor::EmbargoService raises an ArgumentError' do
let(:error_message) { 'You cannot change the embargo date of an item that is not embargoed.' }
before do
allow(Dor::EmbargoService).to receive(:new).and_return(mock_embargo_service)
allow(mock_embargo_service).to receive(:update).and_raise(ArgumentError, error_message)
end
it 'hits the Dor::EmbargoService and returns HTTP 422' do
patch '/v1/objects/druid:mk420bs7601/embargo',
params: { embargo_date: '2100-01-01', requesting_user: 'mjg' },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(mock_embargo_service).to have_received(:update).once
expect(events_datastream).not_to have_received(:add_event)
expect(response.status).to eq(422)
expect(response.body).to eq(error_message)
end
end
context 'when Dor::EmbargoService succeeds' do
before do
allow(Dor::EmbargoService).to receive(:new).and_return(mock_embargo_service)
allow(mock_embargo_service).to receive(:update)
end
it 'hits the Dor::EmbargoService and returns HTTP 204' do
patch '/v1/objects/druid:mk420bs7601/embargo',
params: { embargo_date: '2100-01-01', requesting_user: 'mjg' },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(mock_embargo_service).to have_received(:update).with(Date.parse('2100-01-01'))
expect(events_datastream).to have_received(:add_event).with(
'Embargo',
'mjg',
'Embargo date modified'
)
expect(response.status).to eq(204)
end
end
end