@@ -23,57 +23,83 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
2323 stub_asset ( asset_manager_id , draft : true )
2424 end
2525
26- context "given a draft document with file attachment" do
27- let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] ) }
28-
26+ context "forwarding the preview token to Asset Manager" do
2927 before do
30- add_file_attachment_with_asset ( "sample.docx " , to : edition )
28+ add_file_attachment_with_asset ( "logo.png " , to : edition )
3129 edition . save!
3230 end
3331
34- context "when document is marked as access limited in Whitehall" do
32+ context "when the draft has a preview token" do
33+ let ( :edition ) { create ( :detailed_guide , :with_auth_bypass_id ) }
34+
35+ it "sends the attachment with the document's auth_bypass_id" do
36+ Services . asset_manager . expects ( :create_asset ) . at_least_once . with (
37+ has_entries ( auth_bypass_ids : [ edition . auth_bypass_id ] ) ,
38+ ) . returns ( asset_manager_response )
39+
40+ AssetManagerCreateAssetJob . drain
41+ end
42+ end
43+
44+ context "when the draft has no preview token" do
45+ let ( :edition ) { create ( :detailed_guide ) }
46+
47+ it "sends the attachment with an empty auth_bypass_ids" do
48+ Services . asset_manager . expects ( :create_asset ) . at_least_once . with (
49+ has_entries ( auth_bypass_ids : [ ] ) ,
50+ ) . returns ( asset_manager_response )
51+
52+ AssetManagerCreateAssetJob . drain
53+ end
54+ end
55+ end
56+
57+ context "applying access limiting to a draft's attachments" do
58+ context "when a draft is marked as access limited" do
59+ let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] ) }
60+
3561 before do
62+ add_file_attachment_with_asset ( "sample.docx" , to : edition )
63+ edition . save!
3664 visit edit_admin_edition_path ( edition )
3765 check "Limit access"
3866 click_button "Save"
3967 assert_text "Your document has been saved"
4068 end
4169
42- it "marks attachment as access limited in Asset Manager" do
70+ it "marks the attachment as access limited in Asset Manager" do
4371 Services . asset_manager
4472 . expects ( :update_asset )
4573 . at_least_once . with ( asset_manager_id , has_entry ( "access_limited_organisation_ids" , [ organisation . content_id ] ) )
4674
4775 AssetManagerAttachmentMetadataJob . drain
4876 end
4977 end
50- end
5178
52- context "given a draft document with an image attachment " do
53- let ( :edition ) { create ( :draft_detailed_guide ) }
79+ context "when a draft is unmarked as access limited " do
80+ let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] , access_limited : true ) }
5481
55- before do
56- visit admin_edition_path ( edition )
57- click_link "Edit draft"
58- click_link "Images"
59- attach_file "images[][image_data_attributes][file]" , path_to_attachment ( "minister-of-funk.960x640.jpg" )
60- click_button "Upload"
61- end
82+ before do
83+ add_file_attachment_with_asset ( "sample.docx" , to : edition )
84+ edition . save!
85+ visit edit_admin_edition_path ( edition )
86+ uncheck "Limit access"
87+ click_button "Save"
88+ assert_text "Your document has been saved"
89+ end
6290
63- # Note that there is no access limiting applied to non attachments. This is existing behaviour that probably needs changing.
64- it "sends an image to asset manager with the document's auth_bypass_id" do
65- Services . asset_manager . expects ( :create_asset ) . at_least_once . with (
66- has_entry ( auth_bypass_ids : [ edition . auth_bypass_id ] ) ,
67- ) . returns ( asset_manager_response )
91+ it "unmarks the attachment as access limited in Asset Manager" do
92+ Services . asset_manager
93+ . expects ( :update_asset )
94+ . at_least_once . with ( asset_manager_id , has_entry ( "access_limited_organisation_ids" , [ ] ) )
6895
69- AssetManagerCreateAssetJob . drain
96+ AssetManagerAttachmentMetadataJob . drain
97+ end
7098 end
71- end
7299
73- context "given an access-limited draft document " do
74- let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] , access_limited : true ) }
100+ context "when an attachment is added to an access-limited draft" do
101+ let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] , access_limited : true ) }
75102
76- context "when an attachment is added to the draft document" do
77103 before do
78104 visit admin_edition_path ( edition )
79105 click_link "Add attachments"
@@ -84,46 +110,18 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
84110 assert_text "Attachment 'logo.png' uploaded"
85111 end
86112
87- it "marks attachment as access limited and sends it with an auth_bypass_id in Asset Manager" do
113+ it "marks the attachment as access limited in Asset Manager" do
88114 Services . asset_manager . expects ( :create_asset ) . with (
89- has_entries (
90- access_limited_organisation_ids : [ organisation . content_id ] ,
91- auth_bypass_ids : [ edition . auth_bypass_id ] ,
92- ) ,
115+ has_entries ( access_limited_organisation_ids : [ organisation . content_id ] ) ,
93116 ) . returns ( asset_manager_response )
94117
95118 AssetManagerCreateAssetJob . drain
96119 end
97120 end
98121
99- context "when an html attachment is added to the draft document" do
100- let ( :edition ) { create ( :publication , :policy_paper ) }
101-
102- before do
103- visit admin_edition_path ( edition )
104- click_link "Edit attachments"
105- click_link "Add new HTML attachment"
106- fill_in "Title" , with : "html-attachment"
107- fill_in "Body" , with : "some html content"
108- end
109-
110- it "sends an html attachment to publishing api with its edition's auth_bypass_id" do
111- Services . publishing_api . expects ( :put_content )
112- . with ( anything , has_entries ( title : edition . title ) )
113- Services . publishing_api . expects ( :put_content )
114- . with ( anything , has_entries ( title : edition . attachments . first . title ) )
122+ context "when multiple files are uploaded to an access-limited draft" do
123+ let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] , access_limited : true ) }
115124
116- Services . publishing_api . expects ( :put_content ) . at_least_once
117- . with ( anything , has_entries (
118- title : "html-attachment" ,
119- auth_bypass_ids : [ edition . auth_bypass_id ] ,
120- ) )
121-
122- click_button "Save"
123- end
124- end
125-
126- context "when uploaded to draft document" do
127125 before do
128126 visit admin_edition_path ( edition )
129127 click_link "Add attachments"
@@ -136,49 +134,24 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
136134 assert find ( "li" , text : "logo.png" )
137135 end
138136
139- it "marks attachment as access limited in Asset Manager" do
137+ it "marks each attachment as access limited in Asset Manager" do
140138 Services . asset_manager
141139 . expects ( :create_asset )
142140 . at_least ( 2 )
143141 . with (
144- has_entries (
145- access_limited_organisation_ids : [ organisation . content_id ] ,
146- auth_bypass_ids : [ edition . auth_bypass_id ] ,
147- ) ,
142+ has_entries ( access_limited_organisation_ids : [ organisation . content_id ] ) ,
148143 ) . returns ( asset_manager_response )
149144
150145 AssetManagerCreateAssetJob . drain
151146 end
152147 end
153- end
154148
155- context "given an access-limited draft document and a file attachment" do
156- let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] , access_limited : true ) }
157-
158- before do
159- add_file_attachment_with_asset ( "sample.docx" , to : edition )
160- edition . save!
161- end
149+ context "when an attachment is replaced on an access-limited draft" do
150+ let ( :edition ) { create ( :detailed_guide , organisations : [ organisation ] , access_limited : true ) }
162151
163- context "when document is unmarked as access limited in Whitehall" do
164- before do
165- visit edit_admin_edition_path ( edition )
166- uncheck "Limit access"
167- click_button "Save"
168- assert_text "Your document has been saved"
169- end
170-
171- it "unmarks attachment as access limited in Asset Manager" do
172- Services . asset_manager
173- . expects ( :update_asset )
174- . at_least_once . with ( asset_manager_id , has_entry ( "access_limited_organisation_ids" , [ ] ) )
175-
176- AssetManagerAttachmentMetadataJob . drain
177- end
178- end
179-
180- context "when attachment is replaced" do
181152 before do
153+ add_file_attachment_with_asset ( "sample.docx" , to : edition )
154+ edition . save!
182155 visit admin_edition_path ( edition )
183156 click_link "Edit attachments"
184157 click_link "Edit"
@@ -187,25 +160,20 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
187160 assert_text "Attachment 'sample.docx' updated"
188161 end
189162
190- it "marks replacement attachment as access limited in Asset Manager" do
163+ it "marks the replacement attachment as access limited in Asset Manager" do
191164 Services . asset_manager . stubs ( :create_asset ) . returns ( asset_manager_response )
192- Services . asset_manager . expects ( :create_asset ) . with { |params |
193- params [ :access_limited_organisation_ids ] == [ organisation . content_id ] &&
194- params [ :auth_bypass_ids ] == [ edition . auth_bypass_id ]
195- } . returns ( asset_manager_response )
165+ Services . asset_manager . expects ( :create_asset ) . with { |params | params [ :access_limited_organisation_ids ] == [ organisation . content_id ] } . returns ( asset_manager_response )
196166
197167 AssetManagerCreateAssetJob . drain
198168 end
199169 end
200- end
201170
202- context "given a draft access-limited consultation" do
203- # the edition has to have same organisation as logged in user, otherwise it's not visible when access_limited = true
204- let ( :edition ) { create ( :consultation , organisations : [ organisation ] , access_limited : true ) }
205- let ( :outcome_attributes ) { FactoryBot . attributes_for ( :consultation_outcome ) }
206- let! ( :outcome ) { edition . create_outcome! ( outcome_attributes ) }
171+ context "when an attachment is added to an access-limited consultation outcome " do
172+ # the edition has to have same organisation as logged in user, otherwise it's not visible when access_limited = true
173+ let ( :edition ) { create ( :consultation , organisations : [ organisation ] , access_limited : true ) }
174+ let ( :outcome_attributes ) { FactoryBot . attributes_for ( :consultation_outcome ) }
175+ let! ( :outcome ) { edition . create_outcome! ( outcome_attributes ) }
207176
208- context "when an attachment is added to the consultation's outcome" do
209177 before do
210178 visit admin_consultation_path ( edition )
211179 click_link "Edit draft"
@@ -217,33 +185,13 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
217185 assert_text "Attachment 'asset-title' uploaded"
218186 end
219187
220- it "marks attachment as access limited in Asset Manager and sends with the consultation's auth_bypass_id " do
188+ it "marks the attachment as access limited in Asset Manager" do
221189 Services . asset_manager . expects ( :create_asset ) . with (
222- has_entries (
223- access_limited_organisation_ids : [ organisation . content_id ] ,
224- auth_bypass_ids : [ edition . auth_bypass_id ] ,
225- ) ,
190+ has_entries ( access_limited_organisation_ids : [ organisation . content_id ] ) ,
226191 ) . returns ( asset_manager_response )
227192 AssetManagerCreateAssetJob . drain
228193 end
229194 end
230-
231- it "sends a consultation form to asset manager with the consultation's auth_bypass_id" do
232- visit admin_consultation_path ( edition )
233- click_link "Edit draft"
234- name_of_form_uploader = "edition[consultation_participation_attributes][consultation_response_form_attributes][consultation_response_form_data_attributes][file]"
235- fill_in "edition[consultation_participation_attributes][consultation_response_form_attributes][title]" , with : "Consultation response form"
236- attach_file name_of_form_uploader , path_to_attachment ( "simple.pdf" )
237- click_button "Save"
238-
239- # Note that there is no access limiting applied to non attachments. This is existing behaviour that probably needs changing.
240- Services . asset_manager . expects ( :create_asset ) . with { |args |
241- args [ :file ] . path =~ /simple\. pdf/
242- args [ :auth_bypass_ids ] == [ edition . auth_bypass_id ]
243- } . returns ( asset_manager_response )
244-
245- AssetManagerCreateAssetJob . drain
246- end
247195 end
248196
249197 private
0 commit comments