Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Add a couple more test for EncryptingPacker
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed May 9, 2016
1 parent 34ba9cd commit 5ce0ff8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/py/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from aspen import Response
from aspen.http.request import Request
from base64 import urlsafe_b64decode
from cryptography.fernet import Fernet, InvalidToken
from gratipay import security
from gratipay.models.participant import Participant
from gratipay.security.crypto import EncryptingPacker
from gratipay.testing import Harness
from pytest import raises

Expand Down Expand Up @@ -61,6 +63,15 @@ def test_ep_packs_encryptingly(self):
def test_ep_unpacks_decryptingly(self):
assert Participant.encrypting_packer.unpack(self.packed) == {"foo": "bar"}

def test_ep_fails_to_unpack_old_data_with_a_new_key(self):
encrypting_packer = EncryptingPacker(Fernet.generate_key())
raises(InvalidToken, encrypting_packer.unpack, self.packed)

def test_ep_can_unpack_if_old_key_is_provided(self):
old_key = str(self.client.website.env.crypto_keys)
encrypting_packer = EncryptingPacker(Fernet.generate_key(), old_key)
assert encrypting_packer.unpack(self.packed) == {"foo": "bar"}

def test_ep_leaks_timestamp_derp(self):
# https://github.com/pyca/cryptography/issues/2714
timestamp, = struct.unpack(">Q", urlsafe_b64decode(self.packed)[1:9]) # unencrypted!
Expand Down

0 comments on commit 5ce0ff8

Please sign in to comment.