mirror of https://github.com/docusealco/docuseal
parent
c303159c63
commit
e2f930f6f7
@ -0,0 +1,20 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: completed_documents
|
||||||
|
#
|
||||||
|
# id :bigint not null, primary key
|
||||||
|
# sha256 :string not null
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
# submitter_id :bigint not null
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_completed_documents_on_sha256 (sha256)
|
||||||
|
# index_completed_documents_on_submitter_id (submitter_id)
|
||||||
|
#
|
||||||
|
class CompletedDocument < ApplicationRecord
|
||||||
|
belongs_to :submitter
|
||||||
|
end
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: completed_submitters
|
||||||
|
#
|
||||||
|
# id :bigint not null, primary key
|
||||||
|
# completed_at :datetime not null
|
||||||
|
# sms_count :integer not null
|
||||||
|
# source :string not null
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
# account_id :bigint not null
|
||||||
|
# submission_id :bigint not null
|
||||||
|
# submitter_id :bigint not null
|
||||||
|
# template_id :bigint not null
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_completed_submitters_on_account_id (account_id)
|
||||||
|
#
|
||||||
|
class CompletedSubmitter < ApplicationRecord
|
||||||
|
belongs_to :submitter
|
||||||
|
belongs_to :submission
|
||||||
|
belongs_to :account
|
||||||
|
belongs_to :template
|
||||||
|
end
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class CreateCompletedSubmittersAndDocuments < ActiveRecord::Migration[7.2]
|
||||||
|
def change
|
||||||
|
create_table :completed_submitters do |t|
|
||||||
|
t.bigint :submitter_id, null: false
|
||||||
|
t.bigint :submission_id, null: false
|
||||||
|
t.bigint :account_id, null: false, index: true
|
||||||
|
t.bigint :template_id, null: false
|
||||||
|
t.string :source, null: false
|
||||||
|
t.integer :sms_count, null: false
|
||||||
|
t.datetime :completed_at, null: false
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table :completed_documents do |t|
|
||||||
|
t.bigint :submitter_id, null: false, index: true
|
||||||
|
t.string :sha256, null: false, index: true
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,73 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class PopulateCompletedSubmittersAndDocuments < ActiveRecord::Migration[7.2]
|
||||||
|
disable_ddl_transaction
|
||||||
|
|
||||||
|
class MigrationSubmitter < ApplicationRecord
|
||||||
|
self.table_name = 'submitters'
|
||||||
|
|
||||||
|
belongs_to :submission, class_name: 'MigrationSubmission'
|
||||||
|
has_many :submission_events, class_name: 'MigrationSubmissionEvent', foreign_key: :submitter_id
|
||||||
|
end
|
||||||
|
|
||||||
|
class MigrationSubmission < ApplicationRecord
|
||||||
|
self.table_name = 'submissions'
|
||||||
|
end
|
||||||
|
|
||||||
|
class MigrationSubmissionEvent < ApplicationRecord
|
||||||
|
self.table_name = 'submission_events'
|
||||||
|
end
|
||||||
|
|
||||||
|
class MigrationCompletedSubmitter < ApplicationRecord
|
||||||
|
self.table_name = 'completed_submitters'
|
||||||
|
end
|
||||||
|
|
||||||
|
class MigrationCompletedDocument < ApplicationRecord
|
||||||
|
self.table_name = 'completed_documents'
|
||||||
|
end
|
||||||
|
|
||||||
|
def up
|
||||||
|
completed_submitters = MigrationSubmitter.where.not(completed_at: nil)
|
||||||
|
|
||||||
|
completed_submitters.order(created_at: :asc).preload(:submission).find_each do |submitter|
|
||||||
|
submission = submitter.submission
|
||||||
|
sms_count = submitter.submission_events.where(event_type: %w[send_sms send_2fa_sms]).count
|
||||||
|
completed_submitter = MigrationCompletedSubmitter.where(submitter_id: submitter.id).first_or_initialize
|
||||||
|
completed_submitter.assign_attributes(
|
||||||
|
submission_id: submitter.submission_id,
|
||||||
|
account_id: submission.account_id,
|
||||||
|
template_id: submission.template_id,
|
||||||
|
source: submission.source,
|
||||||
|
sms_count:,
|
||||||
|
completed_at: submitter.completed_at,
|
||||||
|
created_at: submitter.completed_at,
|
||||||
|
updated_at: submitter.completed_at
|
||||||
|
)
|
||||||
|
|
||||||
|
completed_submitter.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveStorage::Attachment.where(record_id: completed_submitters.select(:id),
|
||||||
|
record_type: 'Submitter',
|
||||||
|
name: 'documents')
|
||||||
|
.order(created_at: :asc)
|
||||||
|
.find_each do |attachment|
|
||||||
|
sha256 = attachment.metadata['sha256']
|
||||||
|
submitter_id = attachment.record_id
|
||||||
|
|
||||||
|
next if sha256.blank?
|
||||||
|
|
||||||
|
completed_document = MigrationCompletedDocument.where(submitter_id:, sha256:).first_or_initialize
|
||||||
|
completed_document.assign_attributes(
|
||||||
|
created_at: attachment.created_at,
|
||||||
|
updated_at: attachment.created_at
|
||||||
|
)
|
||||||
|
|
||||||
|
completed_document.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
FactoryBot.define do
|
||||||
|
factory :completed_document do
|
||||||
|
submitter
|
||||||
|
sha256 { SecureRandom.hex(32) }
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe ProcessSubmitterCompletionJob, sidekiq: :inline, type: :job do
|
||||||
|
let(:account) { create(:account) }
|
||||||
|
let(:user) { create(:user, account:) }
|
||||||
|
let(:template) { create(:template, account:, author: user) }
|
||||||
|
let(:submission) { create(:submission, template:, created_by_user: user) }
|
||||||
|
let(:submitter) { create(:submitter, submission:, uuid: SecureRandom.uuid, completed_at: Time.current) }
|
||||||
|
let!(:encrypted_config) do
|
||||||
|
create(:encrypted_config, key: EncryptedConfig::ESIGN_CERTS_KEY,
|
||||||
|
value: GenerateCertificate.call.transform_values(&:to_pem))
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#perform' do
|
||||||
|
it 'creates a completed submitter' do
|
||||||
|
expect do
|
||||||
|
described_class.perform_async('submitter_id' => submitter.id)
|
||||||
|
end.to change(CompletedSubmitter, :count).by(1)
|
||||||
|
|
||||||
|
completed_submitter = CompletedSubmitter.last
|
||||||
|
submitter.reload
|
||||||
|
|
||||||
|
expect(completed_submitter.submitter_id).to eq(submitter.id)
|
||||||
|
expect(completed_submitter.submission_id).to eq(submitter.submission_id)
|
||||||
|
expect(completed_submitter.account_id).to eq(submitter.submission.account_id)
|
||||||
|
expect(completed_submitter.template_id).to eq(submitter.submission.template_id)
|
||||||
|
expect(completed_submitter.source).to eq(submitter.submission.source)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a completed document' do
|
||||||
|
expect do
|
||||||
|
described_class.perform_async('submitter_id' => submitter.id)
|
||||||
|
end.to change(CompletedDocument, :count).by(1)
|
||||||
|
|
||||||
|
completed_document = CompletedDocument.last
|
||||||
|
|
||||||
|
expect(completed_document.submitter_id).to eq(submitter.id)
|
||||||
|
expect(completed_document.sha256).to be_present
|
||||||
|
expect(completed_document.sha256).to eq(submitter.documents.first.metadata['sha256'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises an error if the submitter is not found' do
|
||||||
|
expect do
|
||||||
|
described_class.perform_async('submitter_id' => 'invalid_id')
|
||||||
|
end.to raise_error(ActiveRecord::RecordNotFound)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe 'Tools API', type: :request do
|
||||||
|
let!(:account) { create(:account) }
|
||||||
|
let!(:author) { create(:user, account:) }
|
||||||
|
let!(:file_path) { Rails.root.join('spec/fixtures/sample-document.pdf') }
|
||||||
|
let!(:encrypted_config) do
|
||||||
|
create(:encrypted_config, key: EncryptedConfig::ESIGN_CERTS_KEY,
|
||||||
|
value: GenerateCertificate.call.transform_values(&:to_pem))
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST /api/tools/verify' do
|
||||||
|
it 'returns a verification result' do
|
||||||
|
template = create(:template, account:, author:)
|
||||||
|
submission = create(:submission, :with_submitters, :with_events, template:, created_by_user: author)
|
||||||
|
blob = ActiveStorage::Blob.create_and_upload!(
|
||||||
|
io: file_path.open,
|
||||||
|
filename: 'sample-document.pdf',
|
||||||
|
content_type: 'application/pdf'
|
||||||
|
)
|
||||||
|
create(:completed_document, submitter: submission.submitters.first,
|
||||||
|
sha256: Base64.urlsafe_encode64(Digest::SHA256.digest(blob.download)))
|
||||||
|
|
||||||
|
ActiveStorage::Attachment.create!(
|
||||||
|
blob:,
|
||||||
|
name: :documents,
|
||||||
|
record: submission.submitters.first
|
||||||
|
)
|
||||||
|
|
||||||
|
post '/api/tools/verify', headers: { 'x-auth-token': author.access_token.token }, params: {
|
||||||
|
file: Base64.encode64(File.read(file_path))
|
||||||
|
}.to_json
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(response.parsed_body['checksum_status']).to eq('verified')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in new issue