OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
deduct attachment from json snapshot
ulfertsedab2b315791
diff --git a/modules/bcf/app/services/bcf/viewpoints/set_attributes_service.rb b/modules/bcf/app/services/bcf/viewpoints/set_attributes_service.rbindex 6511a95c606..3dbd6a80530 100644--- a/modules/bcf/app/services/bcf/viewpoints/set_attributes_service.rb+++ b/modules/bcf/app/services/bcf/viewpoints/set_attributes_service.rb@@ -30,8 +30,49 @@module Bcf::Viewpointsclass SetAttributesService < ::BaseServices::SetAttributes+ def set_attributes(params)+ super++ set_snapshot+ end+def set_default_attributes(_params)model.json_viewpoint['guid'] = model.uuidend++ def set_snapshot+ # This might break once the service is also used+ # to update existing viewpoints as the snapshot method will+ # delete any existing snapshot right away while the expectation+ # on a SetAttributesService is to not perform persisted changes.+ file = OpenProject::Files+ .create_uploaded_file(name: "snapshot.#{snapshot_extension}",+ content_type: snapshot_content_type,+ content: snapshot_binary_contents,+ binary: true)++ model.snapshot = file+ end++ def snapshot_content_type+ extension = model.json_viewpoint['snapshot']['snapshot_type']++ case extension+ when 'png'+ 'image/png'+ when 'jpg'+ 'image/jpeg'+ else+ raise 'Unsupported content type'+ end+ end++ def snapshot_extension+ model.json_viewpoint['snapshot']['snapshot_type']+ end++ def snapshot_binary_contents+ Base64.decode64 model.json_viewpoint['snapshot']['snapshot_data']+ endendenddiff --git a/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rbindex e21f3e690cc..c8c5d6a9064 100644--- a/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb+++ b/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb@@ -175,7 +175,15 @@ describe 'BCF 2.1 viewpoints resource', type: :request, content_type: :json, witlet(:path) { "/api/bcf/2.1/projects/#{project.id}/topics/#{bcf_issue.uuid}/viewpoints" }let(:current_user) { create_user }let(:params) do- FactoryBot.attributes_for(:bcf_viewpoint)[:json_viewpoint]+ FactoryBot+ .attributes_for(:bcf_viewpoint)[:json_viewpoint]+ .merge(+ "snapshot" =>+ {+ "snapshot_type" => "png",+ "snapshot_data" => "SGVsbG8gV29ybGQh"+ }+ )endbefore do@@ -187,16 +195,19 @@ describe 'BCF 2.1 viewpoints resource', type: :request, content_type: :json, witlet(:expected_body) donew_viewpoint = Bcf::Viewpoint.last- viewpoint_json+ params.merge(guid: new_viewpoint.uuid)endlet(:expected_status) { 201 }end- it 'creates the viewpoint' do+ it 'creates the viewpoint with an attachment for the snapshot' doexpect(Bcf::Viewpoint.count).to eql 2++ expect(Bcf::Viewpoint.last.attachments.count)+ .to eql 1endcontext 'lacking permission to see project' dodiff --git a/modules/bcf/spec/services/bcf/viewpoints/set_attributes_service_spec.rb b/modules/bcf/spec/services/bcf/viewpoints/set_attributes_service_spec.rbindex ce8c13979df..04cb37d76b1 100644--- a/modules/bcf/spec/services/bcf/viewpoints/set_attributes_service_spec.rb+++ b/modules/bcf/spec/services/bcf/viewpoints/set_attributes_service_spec.rb@@ -67,6 +67,10 @@ describe Bcf::Viewpoints::SetAttributesService, type: :model dolet(:call_attributes) doattributes = FactoryBot.attributes_for(:bcf_viewpoint)attributes[:json_viewpoint].delete('guid')+ attributes[:json_viewpoint]["snapshot"] = {+ "snapshot_type" => "png",+ "snapshot_data" => "SGVsbG8gV29ybGQh"+ }attributesend@@ -91,11 +95,28 @@ describe Bcf::Viewpoints::SetAttributesService, type: :model doexpected_attributes = FactoryBot.attributes_for(:bcf_viewpoint)expected_attributes[:json_viewpoint]['guid'] = viewpoint.uuid+ expected_attributes[:json_viewpoint]["snapshot"] = {+ "snapshot_type" => "png",+ "snapshot_data" => "SGVsbG8gV29ybGQh"+ }expect(viewpoint.attributes.slice(*viewpoint.changed).symbolize_keys).to eql expected_attributesend+ it 'sets the snapshot attachment based on the data in the json_viewpoint' do+ subject++ expect(viewpoint.attachments.size)+ .to eql 1++ expect(viewpoint.attachments.first.file.read)+ .to eql call_attributes[:json_viewpoint]['snapshot']['snapshot_data']++ expect(viewpoint.attachments.first.file.filename)+ .to eql 'snapshot.png'+ end+it 'does not persist the viewpoint' doexpect(viewpoint).not_to receive(:save)