class VirgilCardTest(unittest.TestCase): def __init__(self, *args, **kwargs): super(VirgilCardTest, self).__init__(*args, **kwargs) self._context = None self._app_private_key = None self.__crypto = VirgilCrypto() self.__client = VirgilClient(access_token=config.VIRGIL_ACCESS_TOKEN) self.__request_signer = RequestSigner(self.__crypto) self.__key_pair = self.__crypto.generate_keys() def test_encrypt(self): card_model = self.__get_card_model(Card.Scope.APPLICATION) data_string = "hello world" data = VirgilBuffer.from_string(data_string) vc = VirgilCard(self.__context, card_model) cipher_data = vc.encrypt(data) self.assertEqual( bytearray( self.__crypto.decrypt(cipher_data.get_bytearray(), self.__key_pair.private_key)), data.get_bytearray()) def test_verify(self): card_model = self.__get_card_model(Card.Scope.APPLICATION) data_string = "hello world" data = VirgilBuffer.from_string(data_string) vc = VirgilCard(self.__context, card_model) signature = self.__crypto.sign(data.get_bytearray(), self.__key_pair.private_key) self.assertTrue(vc.verify(data, VirgilBuffer(signature))) def test_publish(self): card_model = self.__get_card_model(scope=Card.Scope.APPLICATION) creds = Credentials(config.VIRGIL_APP_ID, open(config.VIRGIL_APP_KEY_PATH, "rb").read(), config.VIRGIL_APP_KEY_PASSWORD) context = VirgilContext(access_token=config.VIRGIL_ACCESS_TOKEN, credentials=creds) vc = VirgilCard(context, card_model) try: vc.publish() self.assertIsNotNone(self.__client.get_card(card_model.id)) finally: try: self.__cleanup_cards(card_model) except Exception: pass def __build_card_model(self, identity, identity_type, scope, owner_key, custom_fields=None): card_config = { 'identity': identity, 'identity_type': identity_type, 'public_key': tuple(owner_key.export_public_key().get_bytearray()), 'data': custom_fields, } card = Card(**card_config) if scope == Card.Scope.APPLICATION: card_request = CreateCardRequest(**card_config) elif scope == Card.Scope.GLOBAL: card_request = CreateGlobalCardRequest(**card_config) else: raise ValueError("Unknown scope value") card.snapshot = card_request.snapshot snapshot_fingerprint = self.__crypto.calculate_fingerprint( card.snapshot) card.scope = scope card.id = snapshot_fingerprint.to_hex self_signature = owner_key.sign( VirgilBuffer(snapshot_fingerprint.value)) card.signatures = {card.id: self_signature.to_string("base64")} return card def __cleanup_cards(self, *cards): for card in cards: request = RevokeCardRequest(card_id=card.id, ) self.__request_signer.authority_sign(request, config.VIRGIL_APP_ID, self.__app_private_key) self.__client.revoke_card_from_request(request) def __get_card_model(self, scope): identity = "alice" identity_type = "username" scope = scope owner_key = VirgilKey(self.__context, self.__key_pair.private_key) self._card_model = self.__build_card_model(identity, identity_type, scope, owner_key) return self._card_model @property def __context(self): return VirgilContext() @property def __app_private_key(self): if self._app_private_key: return self._app_private_key with open(config.VIRGIL_APP_KEY_PATH, "r") as key_file: raw_private_key = self.__crypto.strtobytes(key_file.read()) self._app_private_key = self.__crypto.import_private_key( key_data=raw_private_key, password=config.VIRGIL_APP_KEY_PASSWORD) return self._app_private_key
class CardManagerTest(unittest.TestCase): def __init__(self, *args, **kwargs): super(CardManagerTest, self).__init__(*args, **kwargs) self._context = None self.__key_pair_alice = self.__context.crypto.generate_keys() self.__key_pair_bob = self.__context.crypto.generate_keys() self.__global_card_config = { "identity": "bob", "identity_type": "email", "owner_key": VirgilKey(self.__context, self.__key_pair_bob.private_key) } self._app_private_key = None self.__crypto = VirgilCrypto() self.__client = VirgilClient(access_token=config.VIRGIL_ACCESS_TOKEN) self.__request_signer = RequestSigner(self.__crypto) self._compatibility_data = None self._compatibility_data_path = None self._decode_data = None def test_create_user(self): identity = IdentitiesManager().create_user("alice", "username") owner_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) cm = CardManager(self.__context) card = cm.create(identity, owner_key) self.assertIsInstance(card, VirgilCard) def test_create_email(self): identity = IdentitiesManager().create_email("bob@localhost") identity._validation_token = "test_token" owner_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) cm = CardManager(self.__context) card = cm.create(identity, owner_key) self.assertIsInstance(card, VirgilCard) def test_create_app(self): identity = IdentitiesManager().create_app("someapp") owner_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) cm = CardManager(self.__context) card = cm.create(identity, owner_key) self.assertIsInstance(card, VirgilCard) def test_find(self): cm = CardManager(self.__context) identity = IdentitiesManager().create_user("alice", "username") owner_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) card = cm.create(identity, owner_key) try: cm.publish(card) finded = cm.find("alice") self.assertIn(card, finded) finally: try: self.__cleanup_cards(card) except Exception: pass def test_find_multiply(self): cm = CardManager(self.__context) identity_alice = IdentitiesManager().create_user("alice", "username") identity_bob = IdentitiesManager().create_user("bob", "username") alice_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) bob_key = VirgilKey(self.__context, self.__key_pair_bob.private_key) alice_card = cm.create(identity_alice, alice_key) bob_card = cm.create(identity_bob, bob_key) try: cm.publish(alice_card) cm.publish(bob_card) finded = cm.find(["alice", "bob"]) self.assertIn(alice_card, finded) and self.assertIn(bob_card, finded) finally: try: self.__cleanup_cards(alice_card) except Exception: pass def test_import_card_unpublished_local(self): data = self.__compatibility_data["export_unpublished_local_virgil_card"] cm = CardManager(self.__context) imported_card = cm.import_card(data["exported_card"]) self.assertEqual(imported_card.id, data["card_id"]) def test_import_card_unpublished_local_without_id(self): data = self.__compatibility_data["export_unpublished_local_virgil_card_without_id"] cm = CardManager(self.__context) imported_card = cm.import_card(data["exported_card"]) imported_card_id = self.__crypto.calculate_fingerprint(bytearray(imported_card._VirgilCard__card.snapshot)).to_hex self.assertEqual(imported_card_id, data["card_id"]) def test_import_card_published_global(self): data = self.__compatibility_data["export_published_global_virgil_card"] cm = CardManager(self.__context) imported_card = cm.import_card(data["exported_card"]) self.assertEqual(imported_card, cm.get(data["card_id"])) def test_publish(self): cm = CardManager(self.__context) identity = IdentitiesManager().create_user("alice", "username") owner_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) card = cm.create(identity, owner_key) try: card.publish() self.assertIsInstance(cm.get(card.id), VirgilCard) self.assertEqual(cm.get(card.id).identity, card.identity) finally: try: self.__cleanup_cards(card) except Exception: pass def test_publish_imported_card(self): identity = IdentitiesManager().create_user("alice", "username") owner_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) cm = CardManager(self.__context) original_card = cm.create(identity, owner_key) exported_card = original_card.export() card = cm.import_card(exported_card) try: card.publish() self.assertIsInstance(cm.get(original_card.id), VirgilCard) self.assertEqual(cm.get(original_card.id).identity, card.identity) finally: try: self.__cleanup_cards(card) except Exception: pass def test_revoke(self): cm = CardManager(self.__context) identity = IdentitiesManager().create_user("alice", "username") owner_key = VirgilKey(self.__context, self.__key_pair_alice.private_key) card = cm.create(identity, owner_key) try: card.publish() self.assertIsInstance(cm.get(card.id), VirgilCard) self.assertEqual(cm.get(card.id).identity, card.identity) cm.revoke(card) with self.assertRaises(Exception) as context: cm.get(card.id) self.assertTrue("" in str(context.exception)) finally: try: self.__cleanup_cards(card) except Exception: pass def __cleanup_cards(self, *cards): for card in cards: request = RevokeCardRequest( card_id=card.id, ) self.__request_signer.authority_sign(request, config.VIRGIL_APP_ID, self.__app_private_key) self.__client.revoke_card_from_request(request) @property def __context(self): if not self._context: creds = Credentials( config.VIRGIL_APP_ID, open(config.VIRGIL_APP_KEY_PATH, "rb").read(), config.VIRGIL_APP_KEY_PASSWORD ) self._context = VirgilContext( config.VIRGIL_ACCESS_TOKEN, creds ) return self._context @property def __app_private_key(self): if self._app_private_key: return self._app_private_key with open(config.VIRGIL_APP_KEY_PATH, "r") as key_file: raw_private_key = self.__crypto.strtobytes(key_file.read()) self._app_private_key = self.__crypto.import_private_key( key_data=raw_private_key, password=config.VIRGIL_APP_KEY_PASSWORD ) return self._app_private_key @property def __compatibility_data(self): if self._compatibility_data: return self._compatibility_data with open(self.__compatibility_data_path, "r") as data_file: raw_data = data_file.read() json_data = json.loads(raw_data) return json_data @property def __compatibility_data_path(self): if self._compatibility_data_path: return self._compatibility_data_path this_file_path = os.path.abspath(__file__) cwd = os.path.dirname(this_file_path) data_file_path = os.path.join( cwd, "..", "data", "sdk_compatibility_data.json" ) self._compatibility_data_path = data_file_path return data_file_path