add more specs
This commit is contained in:
committed by
Pete Matsyburka
parent
6f90147a11
commit
178ee47093
@@ -65,11 +65,11 @@ module Params
|
|||||||
raise_error(message || "#{key} must follow the #{regexp.source} format")
|
raise_error(message || "#{key} must follow the #{regexp.source} format")
|
||||||
end
|
end
|
||||||
|
|
||||||
def unique(params, key, message: nil)
|
def unique_value(params, key, message: nil)
|
||||||
return unless params.is_a?(Array)
|
return unless params.is_a?(Array)
|
||||||
return if params.none?
|
return if params.none?
|
||||||
return if params.all? { |p| p[key].blank? }
|
return if params.all? { |p| p[key].blank? }
|
||||||
return if params.pluck(key).uniq.size == params.pluck(key).size
|
return if params.pluck(key).compact_blank.uniq.size == params.pluck(key).compact_blank.size
|
||||||
|
|
||||||
raise_error(message || "#{key} must be unique")
|
raise_error(message || "#{key} must be unique")
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ module Params
|
|||||||
if params[:submitters].present?
|
if params[:submitters].present?
|
||||||
in_path(params, :submitters) do |submitters_params|
|
in_path(params, :submitters) do |submitters_params|
|
||||||
type(submitters_params, 0, Hash)
|
type(submitters_params, 0, Hash)
|
||||||
unique(submitters_params, :role)
|
unique_value(submitters_params, :role)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+43
-32
@@ -7,7 +7,11 @@ FactoryBot.define do
|
|||||||
author factory: %i[user]
|
author factory: %i[user]
|
||||||
name { Faker::Book.title }
|
name { Faker::Book.title }
|
||||||
|
|
||||||
after(:create) do |template|
|
transient do
|
||||||
|
submitter_count { 1 }
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:create) do |template, evaluator|
|
||||||
blob = ActiveStorage::Blob.create_and_upload!(
|
blob = ActiveStorage::Blob.create_and_upload!(
|
||||||
io: Rails.root.join('spec/fixtures/sample-document.pdf').open,
|
io: Rails.root.join('spec/fixtures/sample-document.pdf').open,
|
||||||
filename: 'sample-document.pdf',
|
filename: 'sample-document.pdf',
|
||||||
@@ -22,39 +26,46 @@ FactoryBot.define do
|
|||||||
Templates::ProcessDocument.call(attachment, attachment.download)
|
Templates::ProcessDocument.call(attachment, attachment.download)
|
||||||
|
|
||||||
template.schema = [{ attachment_uuid: attachment.uuid, name: 'sample-document' }]
|
template.schema = [{ attachment_uuid: attachment.uuid, name: 'sample-document' }]
|
||||||
template.submitters = [
|
number_words = %w[first second third fourth fifth sixth seventh eighth ninth tenth]
|
||||||
|
|
||||||
|
template.submitters = Array.new(evaluator.submitter_count) do |i|
|
||||||
{
|
{
|
||||||
'name' => 'First Party',
|
'name' => "#{number_words[i]&.capitalize} Party",
|
||||||
'uuid' => '513848eb-1096-4abc-a743-68596b5aaa4c'
|
'uuid' => SecureRandom.uuid
|
||||||
}
|
}
|
||||||
]
|
end
|
||||||
template.fields = [
|
|
||||||
{
|
template.fields = template.submitters.reduce([]) do |fields, submitter|
|
||||||
'uuid' => '21637fc9-0655-45df-8952-04ec64949e85',
|
fields += [
|
||||||
'submitter_uuid' => '513848eb-1096-4abc-a743-68596b5aaa4c',
|
{
|
||||||
'name' => 'First Name',
|
'uuid' => SecureRandom.uuid,
|
||||||
'type' => 'text',
|
'submitter_uuid' => submitter['uuid'],
|
||||||
'required' => true,
|
'name' => 'First Name',
|
||||||
'areas' => [
|
'type' => 'text',
|
||||||
{
|
'required' => true,
|
||||||
'x' => 0.09027777777777778,
|
'areas' => [
|
||||||
'y' => 0.1197252208047105,
|
{
|
||||||
'w' => 0.3069444444444444,
|
'x' => 0.09027777777777778,
|
||||||
'h' => 0.03336604514229637,
|
'y' => 0.1197252208047105,
|
||||||
'attachment_uuid' => attachment.uuid,
|
'w' => 0.3069444444444444,
|
||||||
'page' => 0
|
'h' => 0.03336604514229637,
|
||||||
}
|
'attachment_uuid' => attachment.uuid,
|
||||||
]
|
'page' => 0
|
||||||
},
|
}
|
||||||
{
|
]
|
||||||
'uuid' => '1f97f8e3-dc82-4586-aeea-6ebed6204e46',
|
},
|
||||||
'submitter_uuid' => '513848eb-1096-4abc-a743-68596b5aaa4c',
|
{
|
||||||
'name' => '',
|
'uuid' => SecureRandom.uuid,
|
||||||
'type' => 'signature',
|
'submitter_uuid' => submitter['uuid'],
|
||||||
'required' => true,
|
'name' => '',
|
||||||
'areas' => []
|
'type' => 'signature',
|
||||||
}
|
'required' => true,
|
||||||
]
|
'areas' => []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
fields
|
||||||
|
end
|
||||||
|
|
||||||
template.save!
|
template.save!
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ describe 'Submission API', type: :request do
|
|||||||
let!(:author) { create(:user, account:) }
|
let!(:author) { create(:user, account:) }
|
||||||
let!(:folder) { create(:template_folder, account:) }
|
let!(:folder) { create(:template_folder, account:) }
|
||||||
let!(:templates) { create_list(:template, 2, account:, author:, folder:) }
|
let!(:templates) { create_list(:template, 2, account:, author:, folder:) }
|
||||||
|
let!(:multiple_submitters_template) { create(:template, submitter_count: 3, account:, author:, folder:) }
|
||||||
|
|
||||||
describe 'GET /api/submissions' do
|
describe 'GET /api/submissions' do
|
||||||
it 'returns a list of submissions' do
|
it 'returns a list of submissions' do
|
||||||
@@ -57,6 +58,31 @@ describe 'Submission API', type: :request do
|
|||||||
expect(response.parsed_body).to eq(JSON.parse(create_submission_body(submission).to_json))
|
expect(response.parsed_body).to eq(JSON.parse(create_submission_body(submission).to_json))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'creates a submission when some submitter roles are not provided' do
|
||||||
|
post '/api/submissions', headers: { 'x-auth-token': author.access_token.token }, params: {
|
||||||
|
template_id: multiple_submitters_template.id,
|
||||||
|
send_email: true,
|
||||||
|
submitters: [
|
||||||
|
{ role: 'First Role', email: 'john.doe@example.com' },
|
||||||
|
{ email: 'jane.doe@example.com' },
|
||||||
|
{ email: 'mike.doe@example.com' }
|
||||||
|
]
|
||||||
|
}.to_json
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
submission = Submission.last
|
||||||
|
|
||||||
|
expect(response.parsed_body).to eq(JSON.parse(create_submission_body(submission).to_json))
|
||||||
|
expect(response.parsed_body).to eq(JSON.parse(create_submission_body(submission).to_json))
|
||||||
|
expect(response.parsed_body[0]['role']).to eq('First Party')
|
||||||
|
expect(response.parsed_body[0]['email']).to eq('john.doe@example.com')
|
||||||
|
expect(response.parsed_body[1]['role']).to eq('Second Party')
|
||||||
|
expect(response.parsed_body[1]['email']).to eq('jane.doe@example.com')
|
||||||
|
expect(response.parsed_body[2]['role']).to eq('Third Party')
|
||||||
|
expect(response.parsed_body[2]['email']).to eq('mike.doe@example.com')
|
||||||
|
end
|
||||||
|
|
||||||
it 'returns an error if the template fields are missing' do
|
it 'returns an error if the template fields are missing' do
|
||||||
templates[0].update(fields: [])
|
templates[0].update(fields: [])
|
||||||
|
|
||||||
@@ -72,7 +98,7 @@ describe 'Submission API', type: :request do
|
|||||||
|
|
||||||
it 'returns an error if submitter roles are not unique' do
|
it 'returns an error if submitter roles are not unique' do
|
||||||
post '/api/submissions', headers: { 'x-auth-token': author.access_token.token }, params: {
|
post '/api/submissions', headers: { 'x-auth-token': author.access_token.token }, params: {
|
||||||
template_id: templates[0].id,
|
template_id: multiple_submitters_template.id,
|
||||||
send_email: true,
|
send_email: true,
|
||||||
submitters: [
|
submitters: [
|
||||||
{ role: 'First Role', email: 'john.doe@example.com' },
|
{ role: 'First Role', email: 'john.doe@example.com' },
|
||||||
|
|||||||
@@ -144,8 +144,8 @@ describe 'Templates API', type: :request do
|
|||||||
name: template.name,
|
name: template.name,
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
'uuid' => '21637fc9-0655-45df-8952-04ec64949e85',
|
'uuid' => template.fields[0]['uuid'],
|
||||||
'submitter_uuid' => '513848eb-1096-4abc-a743-68596b5aaa4c',
|
'submitter_uuid' => template.submitters[0]['uuid'],
|
||||||
'name' => 'First Name',
|
'name' => 'First Name',
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
'required' => true,
|
'required' => true,
|
||||||
@@ -161,8 +161,8 @@ describe 'Templates API', type: :request do
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'uuid' => '1f97f8e3-dc82-4586-aeea-6ebed6204e46',
|
'uuid' => template.fields[1]['uuid'],
|
||||||
'submitter_uuid' => '513848eb-1096-4abc-a743-68596b5aaa4c',
|
'submitter_uuid' => template.submitters[0]['uuid'],
|
||||||
'name' => '',
|
'name' => '',
|
||||||
'type' => 'signature',
|
'type' => 'signature',
|
||||||
'required' => true,
|
'required' => true,
|
||||||
|
|||||||
Reference in New Issue
Block a user