Skip to content

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

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.rb
index 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::Viewpoints
class SetAttributesService < ::BaseServices::SetAttributes
+ def set_attributes(params)
+ super
+
+ set_snapshot
+ end
+
def set_default_attributes(_params)
model.json_viewpoint['guid'] = model.uuid
end
+
+ 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']
+ end
end
end
diff --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.rb
index 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, wit
let(: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"
+ }
+ )
end
before do
@@ -187,16 +195,19 @@ describe 'BCF 2.1 viewpoints resource', type: :request, content_type: :json, wit
let(:expected_body) do
new_viewpoint = Bcf::Viewpoint.last
- viewpoint_json
+ params
.merge(guid: new_viewpoint.uuid)
end
let(:expected_status) { 201 }
end
- it 'creates the viewpoint' do
+ it 'creates the viewpoint with an attachment for the snapshot' do
expect(Bcf::Viewpoint.count)
.to eql 2
+
+ expect(Bcf::Viewpoint.last.attachments.count)
+ .to eql 1
end
context 'lacking permission to see project' do
diff --git a/modules/bcf/spec/services/bcf/viewpoints/set_attributes_service_spec.rb b/modules/bcf/spec/services/bcf/viewpoints/set_attributes_service_spec.rb
index 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 do
let(:call_attributes) do
attributes = FactoryBot.attributes_for(:bcf_viewpoint)
attributes[:json_viewpoint].delete('guid')
+ attributes[:json_viewpoint]["snapshot"] = {
+ "snapshot_type" => "png",
+ "snapshot_data" => "SGVsbG8gV29ybGQh"
+ }
attributes
end
@@ -91,11 +95,28 @@ describe Bcf::Viewpoints::SetAttributesService, type: :model do
expected_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_attributes
end
+ 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' do
expect(viewpoint)
.not_to receive(:save)