Skip to content

Commit

Permalink
Better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aldosolorzano committed Jan 21, 2019
1 parent a39f2a8 commit d9e0e92
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
38 changes: 29 additions & 9 deletions spec/mifiel/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
it { expect(documents.status).to eq 'success' }
end

let!(:password) { '1234567890' }
let(:params) do
{
file: 'spec/fixtures/example.pdf',
Expand All @@ -74,33 +75,52 @@
]
}
end
let(:expected_body) do
{
e_client: { e_pass: password },
e_user: { e_pass: password }
}
end

shared_examples 'create_encrypted request' do
it 'should make post & put requests' do
expect(WebMock).to have_requested(:post, Mifiel::Document.base_url + '/documents').once
expect(WebMock).to have_requested(:put, Mifiel::Document.base_url + '/documents' + '/' + document.id)
.with(body: { signatories: { document.signers.first['id'] => expected_body } }).once
end
end

context 'create encrypted document with encrypted: true' do
let(:document) { Mifiel::Document.create(params) }
# it { expect(Mifiel::Document).to have_received(:create_ecnrypted).once }
before do
allow_any_instance_of(Mifiel::Crypto::ECIES).to receive(:encrypt).and_return(password.htb)
end
context 'create encrypted document with encrypted:true' do
let!(:document) { Mifiel::Document.create(params) }
it { expect(document).to be_a(Mifiel::Document) }
it { expect(File.exist?(params[:file] + '.der')).to be false }
it_behaves_like 'create_encrypted request'
end

context 'create_encrypted method' do
let(:document) { Mifiel::Document.create_encrypted(params) }
# it { expect(Mifiel::Document).to have_received(:create).once }
# it { expect(Mifiel::Document).to have_received(:encrypt_password).once }
let!(:document) { Mifiel::Document.create_encrypted(params) }
it { expect(document).to be_a(Mifiel::Document) }
it { expect(File.exist?(params[:file] + '.der')).to be false }
it_behaves_like 'create_encrypted request'
end

context 'encrypt password with public keys' do
let!(:document) { Mifiel::Document.create_encrypted(params) }
let(:passwords) { Mifiel::Document.encrypt_password(document.signers, '1234567890') }
let(:passwords) { Mifiel::Document.encrypt_password(document.signers, 'random-password') }
let(:e_pass_length) { 10 }
it 'should encrypt e_client & e_user passwords' do
passwords.each do |_k, v|
expect(v['e_client'][:e_pass]).to be_a String
expect(v['e_client'][:e_pass]).to be_truthy
expect(v['e_client'][:e_pass].length).to eq 258
expect(v['e_client'][:e_pass].length).to eq e_pass_length
expect(v['e_client'][:e_pass]).to eq password
expect(v['e_user'][:e_pass]).to be_a String
expect(v['e_user'][:e_pass]).to be_truthy
expect(v['e_user'][:e_pass].length).to eq 258
expect(v['e_user'][:e_pass].length).to eq e_pass_length
expect(v['e_user'][:e_pass]).to eq password
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/fake_mifiel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class FakeMifiel < Sinatra::Base
put '/api/v1/documents/:id' do
content_type :json
status 200
document.to_json
document(id: params[:id]).to_json
end

private
Expand Down Expand Up @@ -155,7 +155,7 @@ def document(args={})
file_signed_download: "/api/v1/documents/#{id}/file_signed?download=true",
file_zipped: "/api/v1/documents/#{id}/zip",
signatures: [{
id: SecureRandom.uuid,
id: 'd25edfc8-9439-43f6-a36c-13491ec07a2b',
email: 'signer1@email.com',
signed: true,
signed_at: (Time.now.utc - 10_000).iso8601,
Expand Down

0 comments on commit d9e0e92

Please sign in to comment.