class TestEncryptedPassportElement: type_ = 'type' data = 'data' phone_number = 'phone_number' email = 'email' files = [PassportFile('file_id', 50, 0)] front_side = PassportFile('file_id', 50, 0) reverse_side = PassportFile('file_id', 50, 0) selfie = PassportFile('file_id', 50, 0) def test_expected_values(self, encrypted_passport_element): assert encrypted_passport_element.type == self.type_ assert encrypted_passport_element.data == self.data assert encrypted_passport_element.phone_number == self.phone_number assert encrypted_passport_element.email == self.email assert encrypted_passport_element.files == self.files assert encrypted_passport_element.front_side == self.front_side assert encrypted_passport_element.reverse_side == self.reverse_side assert encrypted_passport_element.selfie == self.selfie def test_to_dict(self, encrypted_passport_element): encrypted_passport_element_dict = encrypted_passport_element.to_dict() assert isinstance(encrypted_passport_element_dict, dict) assert encrypted_passport_element_dict['type'] == encrypted_passport_element.type assert encrypted_passport_element_dict['data'] == encrypted_passport_element.data assert ( encrypted_passport_element_dict['phone_number'] == encrypted_passport_element.phone_number ) assert encrypted_passport_element_dict['email'] == encrypted_passport_element.email assert isinstance(encrypted_passport_element_dict['files'], list) assert ( encrypted_passport_element_dict['front_side'] == encrypted_passport_element.front_side.to_dict() ) assert ( encrypted_passport_element_dict['reverse_side'] == encrypted_passport_element.reverse_side.to_dict() ) assert ( encrypted_passport_element_dict['selfie'] == encrypted_passport_element.selfie.to_dict() ) def test_equality(self): a = EncryptedPassportElement(self.type_, data=self.data) b = EncryptedPassportElement(self.type_, data=self.data) c = EncryptedPassportElement(self.data, '') d = PassportElementError('source', 'type', 'message') assert a == b assert hash(a) == hash(b) assert a is not b assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d)
def de_json_decrypted(cls, data, bot, credentials): if not data: return None data = super(EncryptedPassportElement, cls).de_json(data, bot) if data['type'] not in ('phone_number', 'email'): secure_data = getattr(credentials.secure_data, data['type']) if secure_data.data is not None: # If not already decrypted if not isinstance(data['data'], dict): data['data'] = decrypt_json(b64decode(secure_data.data.secret), b64decode(secure_data.data.hash), b64decode(data['data'])) if data['type'] == 'personal_details': data['data'] = PersonalDetails.de_json(data['data'], bot=bot) elif data['type'] in ('passport', 'internal_passport', 'driver_license', 'identity_card'): data['data'] = IdDocumentData.de_json(data['data'], bot=bot) elif data['type'] == 'address': data['data'] = ResidentialAddress.de_json(data['data'], bot=bot) data['files'] = PassportFile.de_list_decrypted(data.get('files'), bot, secure_data.files) or None data['front_side'] = PassportFile.de_json_decrypted(data.get('front_side'), bot, secure_data.front_side) data['reverse_side'] = PassportFile.de_json_decrypted(data.get('reverse_side'), bot, secure_data.reverse_side) data['selfie'] = PassportFile.de_json_decrypted(data.get('selfie'), bot, secure_data.selfie) data['translation'] = PassportFile.de_list_decrypted(data.get('translation'), bot, secure_data.translation) or None return cls(bot=bot, **data)
def de_json(cls, data, bot): if not data: return None data = super(EncryptedPassportElement, cls).de_json(data, bot) data['files'] = PassportFile.de_list(data.get('files'), bot) or None data['front_side'] = PassportFile.de_json(data.get('front_side'), bot) data['reverse_side'] = PassportFile.de_json(data.get('reverse_side'), bot) data['selfie'] = PassportFile.de_json(data.get('selfie'), bot) return cls(bot=bot, **data)
def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['EncryptedPassportElement']: data = cls.parse_data(data) if not data: return None data['files'] = PassportFile.de_list(data.get('files'), bot) or None data['front_side'] = PassportFile.de_json(data.get('front_side'), bot) data['reverse_side'] = PassportFile.de_json(data.get('reverse_side'), bot) data['selfie'] = PassportFile.de_json(data.get('selfie'), bot) data['translation'] = PassportFile.de_list(data.get('translation'), bot) or None return cls(bot=bot, **data)
def de_json(cls, data, bot): if not data: return None data = super().de_json(data, bot) data['files'] = PassportFile.de_list(data.get('files'), bot) or None data['front_side'] = PassportFile.de_json(data.get('front_side'), bot) data['reverse_side'] = PassportFile.de_json(data.get('reverse_side'), bot) data['selfie'] = PassportFile.de_json(data.get('selfie'), bot) data['translation'] = PassportFile.de_list(data.get('translation'), bot) or None return cls(bot=bot, **data)
def passport_file(): return PassportFile( file_id=TestPassportFile.file_id, file_unique_id=TestPassportFile.file_unique_id, file_size=TestPassportFile.file_size, file_date=TestPassportFile.file_date, )
def test_equality(self): a = PassportFile(self.file_id, self.file_unique_id, self.file_size, self.file_date) b = PassportFile("", self.file_unique_id, self.file_size, self.file_date) c = PassportFile(self.file_id, self.file_unique_id, "", "") d = PassportFile("", "", self.file_size, self.file_date) e = PassportElementError("source", "type", "message") assert a == b assert hash(a) == hash(b) assert a is not b assert a == c assert hash(a) == hash(c) assert a != d assert hash(a) != hash(d) assert a != e assert hash(a) != hash(e)
def test_equality(self): a = PassportFile(self.file_id, self.file_unique_id, self.file_size, self.file_date) b = PassportFile('', self.file_unique_id, self.file_size, self.file_date) c = PassportFile(self.file_id, self.file_unique_id, '', '') d = PassportFile('', '', self.file_size, self.file_date) e = PassportElementError('source', 'type', 'message') assert a == b assert hash(a) == hash(b) assert a is not b assert a == c assert hash(a) == hash(c) assert a != d assert hash(a) != hash(d) assert a != e assert hash(a) != hash(e)
def de_json_decrypted( cls, data: Optional[JSONDict], bot: 'Bot', credentials: 'Credentials' ) -> Optional['EncryptedPassportElement']: """Variant of :meth:`telegram.TelegramObject.de_json` that also takes into account passport credentials. Args: data (Dict[:obj:`str`, ...]): The JSON data. bot (:class:`telegram.Bot`): The bot associated with this object. credentials (:class:`telegram.FileCredentials`): The credentials Returns: :class:`telegram.EncryptedPassportElement`: """ if not data: return None if data['type'] not in ('phone_number', 'email'): secure_data = getattr(credentials.secure_data, data['type']) if secure_data.data is not None: # If not already decrypted if not isinstance(data['data'], dict): data['data'] = decrypt_json( b64decode(secure_data.data.secret), b64decode(secure_data.data.hash), b64decode(data['data']), ) if data['type'] == 'personal_details': data['data'] = PersonalDetails.de_json(data['data'], bot=bot) elif data['type'] in ( 'passport', 'internal_passport', 'driver_license', 'identity_card', ): data['data'] = IdDocumentData.de_json(data['data'], bot=bot) elif data['type'] == 'address': data['data'] = ResidentialAddress.de_json(data['data'], bot=bot) data['files'] = ( PassportFile.de_list_decrypted(data.get('files'), bot, secure_data.files) or None ) data['front_side'] = PassportFile.de_json_decrypted( data.get('front_side'), bot, secure_data.front_side ) data['reverse_side'] = PassportFile.de_json_decrypted( data.get('reverse_side'), bot, secure_data.reverse_side ) data['selfie'] = PassportFile.de_json_decrypted( data.get('selfie'), bot, secure_data.selfie ) data['translation'] = ( PassportFile.de_list_decrypted( data.get('translation'), bot, secure_data.translation ) or None ) return cls(bot=bot, **data)
class TestEncryptedPassportElement: type_ = "type" hash = "this is a hash" data = "data" phone_number = "phone_number" email = "email" files = [PassportFile("file_id", 50, 0, 25)] front_side = PassportFile("file_id", 50, 0, 25) reverse_side = PassportFile("file_id", 50, 0, 25) selfie = PassportFile("file_id", 50, 0, 25) def test_slot_behaviour(self, encrypted_passport_element, mro_slots): inst = encrypted_passport_element for attr in inst.__slots__: assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'" assert len(mro_slots(inst)) == len(set( mro_slots(inst))), "duplicate slot" def test_expected_values(self, encrypted_passport_element): assert encrypted_passport_element.type == self.type_ assert encrypted_passport_element.hash == self.hash assert encrypted_passport_element.data == self.data assert encrypted_passport_element.phone_number == self.phone_number assert encrypted_passport_element.email == self.email assert encrypted_passport_element.files == self.files assert encrypted_passport_element.front_side == self.front_side assert encrypted_passport_element.reverse_side == self.reverse_side assert encrypted_passport_element.selfie == self.selfie def test_to_dict(self, encrypted_passport_element): encrypted_passport_element_dict = encrypted_passport_element.to_dict() assert isinstance(encrypted_passport_element_dict, dict) assert encrypted_passport_element_dict[ "type"] == encrypted_passport_element.type assert encrypted_passport_element_dict[ "data"] == encrypted_passport_element.data assert (encrypted_passport_element_dict["phone_number"] == encrypted_passport_element.phone_number) assert encrypted_passport_element_dict[ "email"] == encrypted_passport_element.email assert isinstance(encrypted_passport_element_dict["files"], list) assert (encrypted_passport_element_dict["front_side"] == encrypted_passport_element.front_side.to_dict()) assert (encrypted_passport_element_dict["reverse_side"] == encrypted_passport_element.reverse_side.to_dict()) assert (encrypted_passport_element_dict["selfie"] == encrypted_passport_element.selfie.to_dict()) def test_equality(self): a = EncryptedPassportElement(self.type_, self.hash, data=self.data) b = EncryptedPassportElement(self.type_, self.hash, data=self.data) c = EncryptedPassportElement(self.data, "") d = PassportElementError("source", "type", "message") assert a == b assert hash(a) == hash(b) assert a is not b assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d)
class TestEncryptedPassportElement: type_ = 'type' data = 'data' phone_number = 'phone_number' email = 'email' files = [PassportFile('file_id', 50, 0)] front_side = PassportFile('file_id', 50, 0) reverse_side = PassportFile('file_id', 50, 0) selfie = PassportFile('file_id', 50, 0) def test_slot_behaviour(self, encrypted_passport_element, recwarn, mro_slots): inst = encrypted_passport_element for attr in inst.__slots__: assert getattr(inst, attr, 'err') != 'err', f"got extra slot '{attr}'" assert not inst.__dict__, f"got missing slot(s): {inst.__dict__}" assert len(mro_slots(inst)) == len(set( mro_slots(inst))), "duplicate slot" inst.custom, inst.phone_number = 'should give warning', self.phone_number assert len(recwarn) == 1 and 'custom' in str( recwarn[0].message), recwarn.list def test_expected_values(self, encrypted_passport_element): assert encrypted_passport_element.type == self.type_ assert encrypted_passport_element.data == self.data assert encrypted_passport_element.phone_number == self.phone_number assert encrypted_passport_element.email == self.email assert encrypted_passport_element.files == self.files assert encrypted_passport_element.front_side == self.front_side assert encrypted_passport_element.reverse_side == self.reverse_side assert encrypted_passport_element.selfie == self.selfie def test_to_dict(self, encrypted_passport_element): encrypted_passport_element_dict = encrypted_passport_element.to_dict() assert isinstance(encrypted_passport_element_dict, dict) assert encrypted_passport_element_dict[ 'type'] == encrypted_passport_element.type assert encrypted_passport_element_dict[ 'data'] == encrypted_passport_element.data assert (encrypted_passport_element_dict['phone_number'] == encrypted_passport_element.phone_number) assert encrypted_passport_element_dict[ 'email'] == encrypted_passport_element.email assert isinstance(encrypted_passport_element_dict['files'], list) assert (encrypted_passport_element_dict['front_side'] == encrypted_passport_element.front_side.to_dict()) assert (encrypted_passport_element_dict['reverse_side'] == encrypted_passport_element.reverse_side.to_dict()) assert (encrypted_passport_element_dict['selfie'] == encrypted_passport_element.selfie.to_dict()) def test_equality(self): a = EncryptedPassportElement(self.type_, data=self.data) b = EncryptedPassportElement(self.type_, data=self.data) c = EncryptedPassportElement(self.data, '') d = PassportElementError('source', 'type', 'message') assert a == b assert hash(a) == hash(b) assert a is not b assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d)