Skip to content

Commit 5b0b5f9

Browse files
committed
whenever new document uploaded - clear annexes - add more specs
1 parent 229befc commit 5b0b5f9

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

app/resources/concerns/operator_documentable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def attachment
5252

5353
def attachment=(attachment)
5454
@model.build_document_file(attachment: attachment)
55+
@model.annex_documents = [] # clear annexes when new document is uploaded
5556
# TODO: check if there is better workaround for this issue https://github.com/rails/rails/issues/49898
5657
# flipping activerecord flag about commit on first saved instance does not work
5758
# when using build_document_file probably because of autosaving - save is called twice and in after commit we lose saved_changes hash somehow

spec/integration/v1/operator_documents_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,78 @@ def sign_publication_authorization!
141141
end
142142
end
143143
end
144+
145+
describe "Uploading new document" do
146+
let(:operator_document) { create(:operator_document_fmu, operator: operator_user.operator) }
147+
148+
describe "For operator user" do
149+
it "Returns error object when validation fails" do
150+
patch(
151+
"/operator-document-fmus/#{operator_document.id}",
152+
params: jsonapi_params("operator-document-fmus", operator_document.id, {"start-date": nil, "expire-date": nil}),
153+
headers: operator_user_headers
154+
)
155+
expect(parsed_body).to eq(jsonapi_errors(422, 100, {"start-date": ["can't be blank"], "expire-date": ["can't be blank"]}))
156+
expect(status).to eq(422)
157+
end
158+
159+
it "Returns success object when document is uploaded" do
160+
create(:operator_document_annex, operator_document: operator_document)
161+
patch(
162+
"/operator-document-fmus/#{operator_document.id}",
163+
params: jsonapi_params(
164+
"operator-document-fmus",
165+
operator_document.id,
166+
{
167+
attachment: base64_file_data(Rails.root.join("spec", "support", "files", "doc.pdf")),
168+
"start-date": "2025-12-01",
169+
"expire-date": "2040-12-01"
170+
}
171+
),
172+
headers: operator_user_headers
173+
)
174+
175+
expect(parsed_data[:id]).not_to be_empty
176+
expect(parsed_attributes[:status]).to eq("doc_pending")
177+
expect(parsed_attributes[:"start-date"]).to eq("2025-12-01")
178+
expect(parsed_attributes[:"expire-date"]).to eq("2040-12-01")
179+
expect(parsed_attributes[:"source-type"]).to eq("company")
180+
expect(parsed_attributes[:"uploaded-by"]).to eq("operator")
181+
expect(status).to eq(200)
182+
183+
# annexes should be cleared as new document uploaded
184+
operator_document = OperatorDocument.find(parsed_data[:id])
185+
expect(operator_document.annex_documents.count).to eq(0)
186+
end
187+
188+
context "when trying to upload for another operator" do
189+
let(:other_operator_document) { create(:operator_document_fmu) }
190+
191+
it "Does not allow to upload document for another operator" do
192+
patch(
193+
"/operator-document-fmus/#{other_operator_document.id}",
194+
params: jsonapi_params("operator-document-fmus", other_operator_document.id, {}),
195+
headers: operator_user_headers
196+
)
197+
198+
expect(parsed_body).to eq(default_status_errors(401))
199+
expect(status).to eq(401)
200+
end
201+
end
202+
end
203+
204+
describe "For not operator user" do
205+
it "Does not allow to upload document by not operator users" do
206+
patch(
207+
"/operator-document-fmus/#{operator_document.id}",
208+
params: jsonapi_params("operator-document-fmus", operator_document.id, {}),
209+
headers: user_headers
210+
)
211+
212+
expect(parsed_body).to eq(default_status_errors(401))
213+
expect(status).to eq(401)
214+
end
215+
end
216+
end
144217
end
145218
end

spec/support/integration_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,8 @@ def jsonapi_params(type, id = nil, attributes = nil)
180180
def try_to_call(callable_or_not)
181181
callable_or_not.respond_to?(:call) ? instance_exec(&callable_or_not) : callable_or_not
182182
end
183+
184+
def base64_file_data(filepath)
185+
"data:application/pdf;base64,#{Base64.encode64(File.read(filepath))}"
186+
end
183187
end

0 commit comments

Comments
 (0)