-
Notifications
You must be signed in to change notification settings - Fork 2
/
batch_create_virtual_objects_spec.rb
165 lines (147 loc) · 6.44 KB
/
batch_create_virtual_objects_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Batch creation of virtual objects' do
let(:parent_id) { 'druid:mk420bs7601' }
let(:child1_id) { 'druid:child1' }
let(:child2_id) { 'druid:child2' }
let(:object) { Dor::Item.new(pid: parent_id) }
let(:service) { instance_double(ConstituentService, add: nil) }
before do
allow(Dor).to receive(:find).and_return(object)
allow(ConstituentService).to receive(:new).with(parent_druid: parent_id).and_return(service)
end
context 'when virtual_objects param is provided' do
it 'creates a virtual object out of the parent object and all child objects' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: parent_id, child_ids: [child1_id, child2_id] }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).to have_received(:add).with(child_druids: [child1_id, child2_id])
expect(response).to be_successful
end
end
context 'when virtual_objects contain objects that raise Dor::Exception' do
before do
allow(service).to receive(:add).and_raise(Dor::Exception, 'versioning is messed up')
end
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: parent_id, child_ids: [child1_id, child2_id] }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).to have_received(:add).with(child_druids: [child1_id, child2_id])
expect(response).to be_unprocessable
expect(response.body).to eq '{"errors":[{"druid:mk420bs7601":["versioning is messed up"]}]}'
end
end
context 'when virtual_objects contain objects that are not combinable' do
before do
allow(service).to receive(:add).and_return(parent_id => ["Item #{child2_id} is not open for modification"])
end
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: parent_id, child_ids: [child1_id, child2_id] }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).to have_received(:add).with(child_druids: [child1_id, child2_id])
expect(response).to be_unprocessable
expect(response.body).to eq '{"errors":[{"druid:mk420bs7601":["Item druid:child2 is not open for modification"]}]}'
end
end
context 'when virtual_objects param is not provided' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { title: 'New name' },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq 'virtual_objects is missing'
end
end
context 'when virtual_objects is not an array' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: child1_id },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq 'virtual_objects must be an array'
end
end
context 'when virtual_objects is empty array' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq 'parent_id is missing'
end
end
context 'when virtual_objects array lacks hashes defining parent_id' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ child_ids: ['foo'] }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq 'parent_id is missing'
end
end
context 'when virtual_objects array has a hash w/ an empty parent_id' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: '' }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq 'parent_id must be filled'
end
end
context 'when virtual_objects array lacks hashes defining child_ids' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: 'foo' }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq 'child_ids is missing'
end
end
context 'when virtual_objects array has a hash w/ child_ids not an array' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: 'foo', child_ids: '' }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq 'child_ids must be an array'
end
end
context 'when virtual_objects array has a hash w/ child_ids empty' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: 'foo', child_ids: [] }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq '0 must be filled'
end
end
context 'when virtual_objects array has a hash w/ child_ids containing empties' do
it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: 'foo', child_ids: ['foo', 'bar', ''] }] },
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(service).not_to have_received(:add)
expect(response).to be_bad_request
json = JSON.parse(response.body)
expect(json['errors'][0]['text']).to eq '2 must be filled'
end
end
end