-
Notifications
You must be signed in to change notification settings - Fork 11
/
delete_draft_edition_service_spec.rb
235 lines (186 loc) · 9.16 KB
/
delete_draft_edition_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
# frozen_string_literal: true
RSpec.describe DeleteDraftEditionService do
let(:user) { create :user }
describe ".call" do
it "raises an exception if the edition is not current" do
edition = create :edition, current: false
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error "Only current editions can be deleted"
end
it "raises an exception if the current edition is live" do
document = create :document, :with_live_edition
expect { DeleteDraftEditionService.call(document.current_edition, user) }
.to raise_error "Trying to delete a live edition"
end
it "attempts to delete the document preview" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
request = stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document.current_edition, user)
expect(request).to have_been_requested
end
it "attempts to delete the assets from asset manager" do
image_revision = create :image_revision, :on_asset_manager
file_attachment_revision = create :file_attachment_revision, :on_asset_manager
edition = create(:edition,
lead_image_revision: image_revision,
file_attachment_revisions: [file_attachment_revision])
stub_publishing_api_discard_draft(edition.content_id)
stub_publishing_api_unreserve_path(edition.base_path)
delete_request = stub_asset_manager_deletes_any_asset
DeleteDraftEditionService.call(edition, user)
expect(delete_request).to have_been_requested.at_least_once
expect(image_revision.reload.assets.map(&:state).uniq).to eq(%w[absent])
expect(file_attachment_revision.reload.asset).to be_absent
end
it "attempts to delete path reservations for a first draft" do
edition = create :edition
previous_revision = create :revision
edition.revisions << previous_revision
stub_publishing_api_discard_draft(edition.content_id)
unreserve_request1 = stub_publishing_api_unreserve_path(
edition.base_path,
PreviewDraftEditionService::Payload::PUBLISHING_APP,
)
unreserve_request2 = stub_publishing_api_unreserve_path(
previous_revision.base_path,
PreviewDraftEditionService::Payload::PUBLISHING_APP,
)
DeleteDraftEditionService.call(edition, user)
expect(unreserve_request1).to have_been_requested
expect(unreserve_request2).to have_been_requested
end
it "does not delete path reservations for published documents" do
document = create :document, :with_current_and_live_editions
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to eq document.live_edition
end
it "sets the current edition to nil if there is no live edition" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to be_nil
end
it "sets the current edition to live_edition if there is a live edition" do
document = create :document, :with_current_and_live_editions
live_edition = document.live_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to eq(live_edition)
end
it "sets the status of the edition to discarded" do
document = create :document, :with_current_edition
edition = document.current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document.current_edition, user)
expect(edition.status).to be_discarded
end
it "copes if the document preview does not exist" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_any_publishing_api_call_to_return_not_found
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to be_nil
end
it "copes if the publishing API has a live but not draft edition" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
discard_draft_error = {
error: {
code: 422,
message: "There is not a draft edition of this document to discard",
},
}
stub_any_publishing_api_discard_draft
.to_return(status: 422, body: discard_draft_error.to_json)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to be_nil
end
it "doesn't capture all Publishing API unprocessable entity issues" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
discard_draft_error = {
error: {
code: 422,
message: "New Publishing API problem",
},
}
stub_any_publishing_api_discard_draft
.to_return(status: 422, body: discard_draft_error.to_json)
expect { DeleteDraftEditionService.call(document.current_edition, user) }
.to raise_error(GdsApi::HTTPUnprocessableEntity)
end
it "copes if an asset is not in Asset Manager" do
image_revision = create :image_revision
file_attachment_revision = create :file_attachment_revision
edition = create(:edition,
lead_image_revision: image_revision,
file_attachment_revisions: [file_attachment_revision])
stub_publishing_api_unreserve_path(edition.base_path)
stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition, user)
expect(edition.reload.status).to be_discarded
expect(image_revision.reload.assets.map(&:state).uniq).to eq(%w[absent])
expect(file_attachment_revision.reload.asset).to be_absent
end
it "copes if the base path is not reserved" do
edition = create :edition
stub_publishing_api_unreserve_path_not_found(edition.base_path)
stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition, user)
expect(edition.reload.status).to be_discarded
end
it "copes if the base path is not valid" do
edition = create :edition, base_path: nil
stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition, user)
expect(edition.reload.status).to be_discarded
end
it "removes assets if the asset is on Asset Manager" do
image_revision = create :image_revision, :on_asset_manager
file_attachment_revision = create :file_attachment_revision, :on_asset_manager
edition = create(:edition,
lead_image_revision: image_revision,
file_attachment_revisions: [file_attachment_revision])
(image_revision.assets + [file_attachment_revision.asset]).map do |asset|
stub_asset_manager_delete_asset(asset.asset_manager_id)
end
stub_publishing_api_unreserve_path(edition.base_path)
stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition, user)
expect(edition.reload.status).to be_discarded
expect(image_revision.reload.assets.map(&:state).uniq).to eq(%w[absent])
expect(file_attachment_revision.reload.asset).to be_absent
end
it "raises an error when a base path cannot be deleted" do
edition = create :edition
stub_publishing_api_discard_draft(edition.content_id)
stub_publishing_api_unreserve_path_invalid(edition.base_path)
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error(GdsApi::BaseError)
expect(edition.reload.revision_synced?).to be true
end
it "raises an error when the Pubishing API is down" do
edition = create :edition
stub_publishing_api_isnt_available
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error(GdsApi::BaseError)
expect(edition.reload.revision_synced?).to be false
end
it "raises an error when Asset Manager is down" do
image_revision = create :image_revision, :on_asset_manager
file_attachment_revision = create :file_attachment_revision, :on_asset_manager
edition = create(:edition,
lead_image_revision: image_revision,
file_attachment_revisions: [file_attachment_revision])
stub_asset_manager_isnt_available
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error(GdsApi::BaseError)
expect(edition.reload.revision_synced?).to be false
end
end
end