@@ -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
145218end
0 commit comments