Skip to content

Commit

Permalink
Merge pull request #378 from slovensko-digital/GO-66/update_assigning…
Browse files Browse the repository at this point in the history
…_object_tags

GO-66 Update assigning tags to objects in MessagesAPI
  • Loading branch information
luciajanikova authored Apr 16, 2024
2 parents be75e48 + 0d7ce98 commit f286f52
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
12 changes: 9 additions & 3 deletions app/controllers/api/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def message_drafts
message_object = @message.objects.create(object_params.except(:content, :tags))

object_params.fetch(:tags, []).each do |tag_name|
tag = @tenant.tags.find_by(name: tag_name)
tag = @tenant.user_signature_tags.find_by(name: tag_name)
tag.assign_to_message_object(message_object)
tag.assign_to_thread(@message.thread)
end
Expand Down Expand Up @@ -87,13 +87,19 @@ def check_message_type
end

def check_tags
tag_names = permitted_params.fetch(:tags, []) + permitted_params.fetch(:objects, []).map {|o| o['tags'] }.compact

tag_names = permitted_params.fetch(:tags, [])
tag_names.each do |tag_name|
@tenant.tags.find_by!(name: tag_name)
rescue ActiveRecord::RecordNotFound
render_unprocessable_entity("Tag with name #{tag_name} does not exist") and return
end

message_object_tag_names = permitted_params.fetch(:objects, []).map {|o| o['tags'] }.compact.flatten
message_object_tag_names.each do |tag_name|
@tenant.user_signature_tags.find_by!(name: tag_name)
rescue ActiveRecord::RecordNotFound
render_unprocessable_entity("Signature tag with name #{tag_name} does not exist") and return
end
end

def load_box
Expand Down
5 changes: 0 additions & 5 deletions app/models/signature_requested_from_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
# tenant_id :bigint not null
#
class SignatureRequestedFromTag < Tag
def assign_to_message_object(message_object)
super
tenant.signature_requested_tag.assign_to_message_object(message_object)
end

def assign_to_thread(thread)
super
tenant.signature_requested_tag.assign_to_thread(thread)
Expand Down
5 changes: 0 additions & 5 deletions app/models/signed_by_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
# tenant_id :bigint not null
#
class SignedByTag < Tag
def assign_to_message_object(message_object)
super
tenant.signed_tag.assign_to_message_object(message_object)
end

def assign_to_thread(thread)
super
tenant.signed_tag.assign_to_thread(thread)
Expand Down
4 changes: 4 additions & 0 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def signed_tag!
signed_tag || raise(ActiveRecord::RecordNotFound, "`SignatureRequestedTag` not found in tenant: #{id}")
end

def user_signature_tags
tags.where(type: %w[SignatureRequestedFromTag SignedByTag])
end

def feature_enabled?(feature)
raise "Unknown feature #{feature}" unless feature.in? AVAILABLE_FEATURE_FLAGS

Expand Down
47 changes: 42 additions & 5 deletions test/integration/upvs_message_drafts_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ class UpvsMessageDraftsApiTest < ActionDispatch::IntegrationTest
<subject>Všeobecný predmet</subject>
<text>Všeobecný text</text>
</GeneralAgenda>'),
tags: ['Na podpis']
tags: ['Na podpis: Another user']
}
],
tags: ['Legal', 'Other']
Expand All @@ -940,8 +940,6 @@ class UpvsMessageDraftsApiTest < ActionDispatch::IntegrationTest

assert_response :created
assert_not_equal Message.count, @before_request_messages_count

assert Upvs::MessageDraft.last.objects.last.tags.map(&:name) == ['Na podpis']
end

test 'can upload valid message with object SignatureRequestedFromTags if they exist' do
Expand Down Expand Up @@ -981,7 +979,6 @@ class UpvsMessageDraftsApiTest < ActionDispatch::IntegrationTest
assert_not_equal Message.count, @before_request_messages_count

assert Upvs::MessageDraft.last.objects.last.tags.map(&:name).include?('Na podpis: Another user')
assert Upvs::MessageDraft.last.objects.last.tags.map(&:name).include?('Na podpis')
end

test 'can upload valid message with object SignedByTags if they exist' do
Expand Down Expand Up @@ -1021,7 +1018,6 @@ class UpvsMessageDraftsApiTest < ActionDispatch::IntegrationTest
assert_not_equal Message.count, @before_request_messages_count

assert Upvs::MessageDraft.last.objects.last.tags.map(&:name).include?('Podpisane: Another user')
assert Upvs::MessageDraft.last.objects.last.tags.map(&:name).include?('Podpisane')
end

test 'does not create message unless object name present' do
Expand Down Expand Up @@ -1154,4 +1150,45 @@ class UpvsMessageDraftsApiTest < ActionDispatch::IntegrationTest

assert_equal Message.count, @before_request_messages_count
end

test 'does not create message unless user signature tags with given names exist' do
message_params = {
type: 'Upvs::MessageDraft',
title: 'Všeobecná agenda',
uuid: SecureRandom.uuid,
metadata: {
posp_id: 'App.GeneralAgenda',
posp_version: '1.9',
message_type: 'App.GeneralAgenda',
correlation_id: SecureRandom.uuid,
sender_uri: 'SSDMainURI',
recipient_uri: 'ico://sk/12345678',
},
objects: [
{
name: 'Form.xml',
is_signed: false,
to_be_signed: true,
mimetype: 'application/x-eform-xml',
object_type: 'FORM',
content: Base64.encode64('<?xml version="1.0" encoding="utf-8"?>
<GeneralAgenda xmlns="http://schemas.gov.sk/form/App.GeneralAgenda/1.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<subject>Všeobecný predmet</subject>
<text>Všeobecný text</text>
</GeneralAgenda>'),
tags: ['Podpisane']
}
],
tags: ['Legal', 'Other']
}

post '/api/messages/message_drafts', params: message_params.merge({ token: generate_api_token(sub: @tenant.id, key_pair: @key_pair) }), as: :json

assert_response :unprocessable_entity

json_response = JSON.parse(response.body)
assert_equal 'Signature tag with name Podpisane does not exist', json_response['message']

assert_equal Message.count, @before_request_messages_count
end
end

0 comments on commit f286f52

Please sign in to comment.