/
registration_service_spec.rb
400 lines (360 loc) · 14.7 KB
/
registration_service_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe RegistrationService do
before do
@pid = 'druid:ab123cd4567'
@mock_repo = instance_double(Rubydora::Repository)
@apo = instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject)
allow(@apo).to receive(:new_record?).and_return false
allow(Dor).to receive(:find).with('druid:fg890hi1234').and_return(@apo)
end
context '#register_object' do
subject(:register) { described_class.send(:register_object, request) }
before do
allow(Dor::SuriService).to receive(:mint_id).and_return(@pid)
allow(Dor::SearchService).to receive(:query_by_id).and_return([])
allow(ActiveFedora::Base).to receive(:connection_for_pid).and_return(@mock_repo)
# allow_any_instance_of(Dor::Item).to receive(:save).and_return(true)
allow_any_instance_of(Dor::Collection).to receive(:save).and_return(true)
allow_any_instance_of(Dor::Item).to receive(:create).and_return(true)
@params = {
object_type: 'item',
content_model: 'googleScannedBook',
admin_policy: 'druid:fg890hi1234',
label: 'Google : Scanned Book 12345',
source_id: { barcode: 9_191_919_191 },
other_ids: { catkey: '000', uuid: '111' },
tags: ['Google : Google Tag!', 'Google : Other Google Tag!']
}
end
let(:request) { RegistrationRequest.new(@params) }
let(:mock_collection) do
coll = Dor::Collection.new
allow(coll).to receive(:new?).and_return false
allow(coll).to receive(:new_record?).and_return false
allow(coll).to receive(:pid).and_return 'druid:something'
allow(coll).to receive(:save)
coll
end
let(:world_xml) do
<<-XML
<?xml version="1.0"?>
<rightsMetadata>
<copyright>
<human type="copyright">This work is in the Public Domain.</human>
</copyright>
<access type="discover">
<machine>
<world/>
</machine>
</access>
<access type="read">
<machine>
<world/>
</machine>
</access>
<use>
<human type="creativecommons">Attribution Share Alike license</human>
<machine type="creativecommons">by-sa</machine>
</use>
</rightsMetadata>
XML
end
let(:stanford_xml) do
<<-XML
<?xml version="1.0"?>
<rightsMetadata>
<copyright>
<human type="copyright">This work is in the Public Domain.</human>
</copyright>
<access type="discover">
<machine>
<world/>
</machine>
</access>
<access type="read">
<machine>
<group>stanford</group>
</machine>
</access>
<use>
<human type="creativecommons">Attribution Share Alike license</human>
<machine type="creativecommons">by-sa</machine>
</use>
</rightsMetadata>
XML
end
let(:stanford_no_download_xml) do
<<-XML
<?xml version="1.0"?>
<rightsMetadata>
<copyright>
<human type="copyright">This work is in the Public Domain.</human>
</copyright>
<access type="discover">
<machine>
<world/>
</machine>
</access>
<access type="read">
<machine>
<group rule="no-download">stanford</group>
</machine>
</access>
<use>
<human type="creativecommons">Attribution Share Alike license</human>
<machine type="creativecommons">by-sa</machine>
</use>
</rightsMetadata>
XML
end
let(:location_music_xml) do
<<-XML
<?xml version="1.0"?>
<rightsMetadata>
<copyright>
<human type="copyright">This work is in the Public Domain.</human>
</copyright>
<access type="discover">
<machine>
<world/>
</machine>
</access>
<access type="read">
<machine>
<location>music</location>
</machine>
</access>
<use>
<human type="creativecommons">Attribution Share Alike license</human>
<machine type="creativecommons">by-sa</machine>
</use>
</rightsMetadata>
XML
end
context 'exception should be raised for' do
it 'registering a duplicate PID' do
@params[:pid] = @pid
expect(Dor::SearchService).to receive(:query_by_id).with('druid:ab123cd4567').and_return([@pid])
expect { register }.to raise_error(Dor::DuplicateIdError)
end
it 'registering a duplicate source ID' do
expect(Dor::SearchService).to receive(:query_by_id).with('barcode:9191919191').and_return([@pid])
expect { register }.to raise_error(Dor::DuplicateIdError)
end
it 'missing a required parameter' do
@params.delete(:object_type)
expect { register }.to raise_error(Dor::ParameterError)
end
context 'when seed_datastream is present and something other than descMetadata' do
it 'raises an error' do
@params[:seed_datastream] = ['invalid']
expect { register }.to raise_error(Dor::ParameterError)
end
end
context 'empty label' do
before do
@params[:label] = ''
end
it 'and metadata_source is label or none' do
@params[:metadata_source] = 'label'
expect { register }.to raise_error(Dor::ParameterError)
@params[:metadata_source] = 'none'
expect { register }.to raise_error(Dor::ParameterError)
end
end
end
RSpec.shared_examples 'common registration' do
it 'produces a registered object' do
expect(@obj.pid).to eq(@pid)
expect(@obj.label).to eq(@params[:label])
expect(@obj.identityMetadata.sourceId).to eq('barcode:9191919191')
expect(@obj.identityMetadata.otherId).to match_array(@params[:other_ids].collect { |*e| e.join(':') })
end
end
describe 'should set rightsMetadata based on the APO default (but replace read rights) even if it is a collection' do
before do
@coll = Dor::Collection.new(pid: @pid)
expect(Dor::Collection).to receive(:new).with(pid: @pid).and_return(@coll)
@params[:rights] = 'stanford'
@params[:object_type] = 'collection'
@obj = register
end
it_behaves_like 'common registration'
it 'produces rightsMetadata XML' do
expect(@obj.datastreams['rightsMetadata'].ng_xml).to be_equivalent_to stanford_xml
end
end
context 'when seed_datastream is provided' do
before do
@params[:seed_datastream] = ['descMetadata']
allow(RefreshMetadataAction).to receive(:run)
end
it 'creates the datastream' do
@obj = register
expect(RefreshMetadataAction).to have_received(:run)
end
end
context 'common cases' do
before do
expect_any_instance_of(Dor::Item).to receive(:save).and_return(true)
end
describe 'object registration' do
before do
@obj = register
end
it_behaves_like 'common registration'
it 'produces correct rels_ext' do
expect(@obj.rels_ext.to_rels_ext).to be_equivalent_to <<-XML
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#"
xmlns:fedora-model="info:fedora/fedora-system:def/model#" xmlns:hydra="http://projecthydra.org/ns/relations#">
<rdf:Description rdf:about="info:fedora/druid:ab123cd4567">
<hydra:isGovernedBy rdf:resource="info:fedora/druid:fg890hi1234"/>
<fedora-model:hasModel rdf:resource="info:fedora/afmodel:Dor_Item"/>
<fedora-model:hasModel rdf:resource='info:fedora/afmodel:Dor_Abstract' />
<fedora:isMemberOf rdf:resource="info:fedora/druid:zb871zd0767"/>
<fedora:isMemberOfCollection rdf:resource="info:fedora/druid:zb871zd0767"/>
</rdf:Description>
</rdf:RDF>
XML
end
end
describe 'collection registration' do
before do
@params[:collection] = 'druid:something'
expect(Dor::Collection).to receive(:find).with('druid:something').and_return(mock_collection)
@obj = register
end
it_behaves_like 'common registration'
it 'produces correct RELS-EXT' do
expect(@obj.datastreams['RELS-EXT'].to_rels_ext).to be_equivalent_to <<-XML
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#"
xmlns:fedora-model="info:fedora/fedora-system:def/model#" xmlns:hydra="http://projecthydra.org/ns/relations#">
<rdf:Description rdf:about="info:fedora/druid:ab123cd4567">
<hydra:isGovernedBy rdf:resource="info:fedora/druid:fg890hi1234"/>
<fedora-model:hasModel rdf:resource="info:fedora/afmodel:Dor_Item"/>
<fedora-model:hasModel rdf:resource='info:fedora/afmodel:Dor_Abstract' />
<fedora:isMemberOf rdf:resource="info:fedora/druid:something"/>
<fedora:isMemberOf rdf:resource="info:fedora/druid:zb871zd0767"/>
<fedora:isMemberOfCollection rdf:resource="info:fedora/druid:something"/>
<fedora:isMemberOfCollection rdf:resource="info:fedora/druid:zb871zd0767"/>
</rdf:Description>
</rdf:RDF>
XML
end
end
context 'when passed rights=' do
describe 'default' do
before do
@params[:rights] = 'default'
@obj = register
end
it_behaves_like 'common registration'
it 'sets rightsMetadata based on the APO default' do
expect(@obj.datastreams['rightsMetadata'].ng_xml).to be_equivalent_to stanford_xml
end
end
describe 'world' do
before do
@params[:rights] = 'world'
@obj = register
end
it_behaves_like 'common registration'
it 'sets rightsMetadata based on the APO default but replace read rights to be world' do
expect(@obj.datastreams['rightsMetadata'].ng_xml).to be_equivalent_to world_xml
end
end
describe 'loc:music' do
before do
@params[:rights] = 'loc:music'
@obj = register
end
it_behaves_like 'common registration'
it 'sets rightsMetadata based on the APO default but replace read rights to be loc:music' do
expect(@obj.datastreams['rightsMetadata'].ng_xml).to be_equivalent_to location_music_xml
end
end
describe 'stanford no-download' do
before do
@params[:rights] = 'stanford-nd'
@obj = register
end
it_behaves_like 'common registration'
it 'sets rightsMetadata based on the APO default but replace read rights to be group stanford with the no-download rule' do
expect(@obj.datastreams['rightsMetadata'].ng_xml).to be_equivalent_to stanford_no_download_xml
end
end
end
describe 'when passed metadata_source=label' do
before do
@params[:metadata_source] = 'label'
@obj = register
end
it_behaves_like 'common registration'
it 'sets the descriptive metadata to basic mods using the label as title' do
expect(@obj.datastreams['descMetadata'].ng_xml).to be_equivalent_to <<-XML
<?xml version="1.0"?>
<mods xmlns="http://www.loc.gov/mods/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.6" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-6.xsd">
<titleInfo>
<title>Google : Scanned Book 12345</title>
</titleInfo>
</mods>
XML
end
end
it 'truncates label if >= 255 chars' do
# expect(Dor.logger).to receive(:warn).at_least(:once)
@params[:label] = 'a' * 256
obj = register
expect(obj.label).to eq('a' * 254)
end
context 'when initiate workflow is passed in' do
let(:workflow_client) { instance_double(Dor::Workflow::Client, create_workflow_by_name: true) }
before do
allow(Dor::Config.workflow).to receive(:client).and_return(workflow_client)
end
it 'sets priority' do
@params[:workflow_priority] = 50
@params[:initiate_workflow] = 'digitizationWF'
register
expect(workflow_client).to have_received(:create_workflow_by_name).with(String, 'digitizationWF', priority: 50, version: '1')
end
end
end # context common cases
end
context '#create_from_request' do
before do
allow(Dor::SuriService).to receive(:mint_id).and_return(@pid)
allow(Dor::SearchService).to receive(:query_by_id).and_return([])
allow(ActiveFedora::Base).to receive(:connection_for_pid).and_return(@mock_repo)
allow_any_instance_of(Dor::Item).to receive(:save).and_return(true)
# allow_any_instance_of(Dor::Collection).to receive(:save).and_return(true)
allow_any_instance_of(Dor::Item).to receive(:create).and_return(true)
@params = {
object_type: 'item',
admin_policy: 'druid:fg890hi1234',
label: 'web-archived-crawl for http://www.example.org',
source_id: 'sul:SOMETHING-www.example.org'
}
end
it 'source_id may have one or more colons' do
expect { described_class.create_from_request(@params) }.not_to raise_error
@params[:source_id] = 'sul:SOMETHING-http://www.example.org'
expect { described_class.create_from_request(@params) }.not_to raise_error
end
it 'source_id must have at least one colon' do
# Execution gets into IdentityMetadataDS code for specific error
@params[:source_id] = 'no-colon'
exp_regex = /Source ID must follow the format 'namespace:value'/
expect { described_class.create_from_request(@params) }.to raise_error(ArgumentError, exp_regex)
end
it 'other_id may have any number of colons' do
@params[:other_id] = 'no-colon'
expect { described_class.create_from_request(@params) }.not_to raise_error
@params[:other_id] = 'catkey:000'
expect { described_class.create_from_request(@params) }.not_to raise_error
@params[:other_id] = 'catkey:oop:sie'
expect { described_class.create_from_request(@params) }.not_to raise_error
end
end # create_from_request
end