Skip to content

Commit

Permalink
Write tests for receive transferred encryptables before recrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleisa committed Jul 5, 2023
1 parent b95e659 commit 1d1b052
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions spec/controllers/api/teams_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
let!(:team4) { Fabricate(:non_private_team) }
let(:bobs_private_key) { bob.decrypt_private_key('password') }
let!(:team3_user) { team3.teammembers.first.user }
let(:test_file) { FixturesHelper.read_file('test_file.txt') }

context 'GET index' do
it 'should get team for search term' do
Expand Down Expand Up @@ -413,6 +414,47 @@
end
end
end

context 'Receive transferred encryptables' do
it 'Receive method gets called to encrypt transferred encryptable' do
login_as(:bob)

prepare_transferred_encryptable

expect_any_instance_of(EncryptableTransfer)
.to receive(:receive)
.exactly(1).times
.and_return(nil)

default_folder = Folder.new(name: 'default', team: personal_team_bob)

personal_team_bob.folders = [default_folder, bob.inbox_folder]

get :index, params: { team_id: personal_team_bob.id }, xhr: true
end

it 'Receive method gets not called if encryptables are not transferred' do
login_as(:bob)

params = {}
params[:name] = 'Shopping Account'
params[:folder_id] = bob.inbox_folder.id
params[:type] = 'Encryptable::Credentials'
params[:cleartext_username] = 'username'
Encryptable::Credentials.new(params)

expect_any_instance_of(EncryptableTransfer)
.not_to receive(:receive)
.exactly(1).times
.and_return(nil)

default_folder = Folder.new(name: 'default', team: personal_team_bob)

personal_team_bob.folders = [default_folder, bob.inbox_folder]

get :index, params: { team_id: personal_team_bob.id }, xhr: true
end
end
end

context 'PUT update' do
Expand Down Expand Up @@ -606,6 +648,28 @@

private

def prepare_transferred_encryptable
encryptable_file = Encryptable::File.new(name: 'file',
folder_id: bob.inbox_folder.id,
cleartext_file: test_file,
content_type: 'text/plain')

transfer_password = Crypto::Symmetric::Aes256iv.random_key

encryptable_file.encrypt(transfer_password)

encrypted_transfer_password = Crypto::Rsa.encrypt(
transfer_password,
bob.public_key
)
encryptable_file.encrypted_transfer_password = Base64.encode64(encrypted_transfer_password)
encryptable_file.sender_id = alice.id
encryptable_file.folder = bob.inbox_folder
encryptable_file.save!

encryptable_file
end

def add_bob_to_team(team, user)
decrypted_team_password = team.decrypt_team_password(user, user.decrypt_private_key('password'))
team.add_user(bob, decrypted_team_password)
Expand Down

0 comments on commit 1d1b052

Please sign in to comment.