/
work_actor_spec.rb
308 lines (271 loc) · 10.9 KB
/
work_actor_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
require 'spec_helper'
require 'redlock'
describe CurationConcerns::Actors::GenericWorkActor do
include ActionDispatch::TestProcess
let(:user) { create(:user) }
let(:file) { curation_concerns_fixture_file_upload('files/image.png', 'image/png') }
# stub out redis connection
let(:redlock_client_stub) do
client = double('redlock client')
allow(client).to receive(:lock).and_yield(true)
allow(Redlock::Client).to receive(:new).and_return(client)
client
end
subject do
CurationConcerns::CurationConcern.actor(curation_concern, user)
end
describe '#create' do
let(:curation_concern) { GenericWork.new }
let(:xmas) { DateTime.parse('2014-12-25 11:30') }
context 'failure' do
let(:attributes) { {} }
it 'returns false' do
expect_any_instance_of(described_class).to receive(:save).and_return(false)
allow(subject).to receive(:attach_files).and_return(true)
expect(subject.create(attributes)).to be false
end
end
context 'success' do
before do
redlock_client_stub
create(:workflow_action)
end
it "invokes the after_create_concern callback" do
allow(CharacterizeJob).to receive(:perform_later).and_return(true)
expect(CurationConcerns.config.callback).to receive(:run)
.with(:after_create_concern, curation_concern, user)
subject.create(title: ['Foo Bar'])
end
end
context 'valid attributes' do
let(:visibility) { Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_AUTHENTICATED }
before do
redlock_client_stub
create(:workflow_action)
end
context 'with embargo' do
context "with attached files" do
let(:date) { Date.today + 2 }
let(:attributes) do
{ title: ['New embargo'], visibility: Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_EMBARGO,
visibility_during_embargo: 'authenticated', embargo_release_date: date.to_s,
visibility_after_embargo: 'open', visibility_during_lease: 'open',
lease_expiration_date: '2014-06-12', visibility_after_lease: 'restricted',
files: [
file
],
rights: ['http://creativecommons.org/licenses/by/3.0/us/'] }
end
it "applies embargo to attached files" do
allow(CharacterizeJob).to receive(:perform_later).and_return(true)
subject.create(attributes)
file = curation_concern.file_sets.first
expect(file).to be_persisted
expect(file.visibility_during_embargo).to eq 'authenticated'
expect(file.visibility_after_embargo).to eq 'open'
expect(file.visibility).to eq 'authenticated'
end
end
end
context 'with in_work_ids' do
let(:parent) { FactoryGirl.create(:generic_work) }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, visibility: visibility).merge(
in_works_ids: [parent.id]
)
end
it "attaches the parent" do
expect(subject.create(attributes)).to be true
expect(curation_concern.in_works).to eq [parent]
end
end
context 'with a file' do
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, visibility: visibility).tap do |a|
a[:files] = file
end
end
context 'authenticated visibility' do
let(:file_actor) { double }
before do
allow(CurationConcerns::TimeService).to receive(:time_in_utc) { xmas }
allow(CurationConcerns::Actors::FileActor).to receive(:new).and_return(file_actor)
end
it 'stamps each file with the access rights' do
expect(file_actor).to receive(:ingest_file).and_return(true)
expect(subject.create(attributes)).to be true
expect(curation_concern).to be_persisted
expect(curation_concern.date_uploaded).to eq xmas
expect(curation_concern.date_modified).to eq xmas
expect(curation_concern.depositor).to eq user.user_key
expect(curation_concern.representative).to_not be_nil
expect(curation_concern.file_sets.size).to eq 1
expect(curation_concern).to be_authenticated_only_access
# Sanity test to make sure the file_set has same permission as parent.
file_set = curation_concern.file_sets.first
expect(file_set).to be_authenticated_only_access
end
end
end
context 'with multiple files file' do
let(:file_actor) { double }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, visibility: visibility).tap do |a|
a[:files] = [file, file]
end
end
context 'authenticated visibility' do
before do
allow(CurationConcerns::TimeService).to receive(:time_in_utc) { xmas }
allow(CurationConcerns::Actors::FileActor).to receive(:new).and_return(file_actor)
end
it 'stamps each file with the access rights' do
expect(file_actor).to receive(:ingest_file).and_return(true).twice
expect(subject.create(attributes)).to be true
expect(curation_concern).to be_persisted
expect(curation_concern.date_uploaded).to eq xmas
expect(curation_concern.date_modified).to eq xmas
expect(curation_concern.depositor).to eq user.user_key
expect(curation_concern.file_sets.size).to eq 2
# Sanity test to make sure the file we uploaded is stored and has same permission as parent.
expect(curation_concern).to be_authenticated_only_access
end
end
end
context 'with a present and a blank title' do
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, title: ['this is present', ''])
end
it 'stamps each link with the access rights' do
expect(subject.create(attributes)).to be true
expect(curation_concern).to be_persisted
expect(curation_concern.title).to eq ['this is present']
end
end
end
end
describe '#update' do
let(:curation_concern) { create(:generic_work, user: user) }
context 'failure' do
let(:attributes) { {} }
it 'returns false' do
expect_any_instance_of(described_class).to receive(:save).and_return(false)
expect(subject.update(attributes)).to be false
end
end
context 'success' do
it "invokes the after_update_metadata callback" do
expect(CurationConcerns.config.callback).to receive(:run)
.with(:after_update_metadata, curation_concern, user)
subject.update(title: ['Other Title'])
end
end
context 'with in_works_ids' do
let(:parent) { FactoryGirl.create(:generic_work) }
let(:old_parent) { FactoryGirl.create(:generic_work) }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work).merge(
in_works_ids: [parent.id]
)
end
before do
curation_concern.apply_depositor_metadata(user.user_key)
curation_concern.save!
old_parent.ordered_members << curation_concern
old_parent.save!
end
it "attaches the parent" do
expect(subject.update(attributes)).to be true
expect(curation_concern.in_works).to eq [parent]
expect(old_parent.reload.members).to eq []
end
end
context 'without in_works_ids' do
let(:old_parent) { FactoryGirl.create(:generic_work) }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work).merge(
in_works_ids: []
)
end
before do
curation_concern.apply_depositor_metadata(user.user_key)
curation_concern.save!
old_parent.ordered_members << curation_concern
old_parent.save!
end
it "removes the old parent" do
expect(subject.update(attributes)).to be true
expect(curation_concern.in_works).to eq []
expect(old_parent.reload.members).to eq []
end
end
context 'with nil in_works_ids' do
let(:parent) { FactoryGirl.create(:generic_work) }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work).merge(
in_works_ids: nil
)
end
before do
curation_concern.apply_depositor_metadata(user.user_key)
curation_concern.save!
parent.ordered_members << curation_concern
parent.save!
end
it "does nothing" do
expect(subject.update(attributes)).to be true
expect(curation_concern.in_works).to eq [parent]
end
end
context 'adding to collections' do
let!(:collection1) { create(:collection, user: user) }
let!(:collection2) { create(:collection, user: user) }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, collection_ids: [collection2.id])
end
before do
curation_concern.apply_depositor_metadata(user.user_key)
curation_concern.save!
collection1.members << curation_concern
collection1.save!
end
it 'remove from the old collection and adds to the new collection' do
curation_concern.reload
expect(curation_concern.in_collections).to eq [collection1]
# before running actor.update, the work is in collection1
expect(subject.update(attributes)).to be true
curation_concern.reload
expect(curation_concern.identifier).to be_blank
expect(curation_concern).to be_persisted
# after running actor.update, the work is in collection2 and no longer in collection1
expect(curation_concern.in_collections).to eq [collection2]
end
end
context 'with multiple file sets' do
let(:file_set1) { create(:file_set) }
let(:file_set2) { create(:file_set) }
let(:curation_concern) { create(:generic_work, user: user, ordered_members: [file_set1, file_set2]) }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, ordered_member_ids: [file_set2.id, file_set1.id])
end
it 'updates the order of file sets' do
expect(curation_concern.ordered_members.to_a).to eq [file_set1, file_set2]
expect(subject.update(attributes)).to be true
curation_concern.reload
expect(curation_concern.ordered_members.to_a).to eq [file_set2, file_set1]
end
## Is this something we want to support?
context "when told to stop ordering a file set" do
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, ordered_member_ids: [file_set2.id])
end
it "works" do
expect(curation_concern.ordered_members.to_a).to eq [file_set1, file_set2]
expect(subject.update(attributes)).to be true
curation_concern.reload
expect(curation_concern.ordered_members.to_a).to eq [file_set2]
end
end
end
end
end