Skip to content

Commit

Permalink
Merge pull request #1 from getyoti/handle_empty_profiles
Browse files Browse the repository at this point in the history
Handle empty profiles
  • Loading branch information
Vasile Zaremba authored Apr 27, 2017
2 parents 5f48d38 + c86442a commit ffb407d
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ ENV/
# IDE
.idea/
*.un~
.history/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.1.0] - 2017-03-20
### Changed
- Allow empty profiles

## [0.1.2] - 2016-11-23

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions yoti_python_sdk/activity_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@


class ActivityDetails:
def __init__(self, receipt, decrypted_profile):
def __init__(self, receipt, decrypted_profile=None):
self.decrypted_profile = decrypted_profile
self.user_profile = {}

if hasattr(decrypted_profile, 'attributes'):
if decrypted_profile and hasattr(decrypted_profile, 'attributes'):
for field in decrypted_profile.attributes:
value = Protobuf().value_based_on_content_type(
field.value,
Expand Down
3 changes: 3 additions & 0 deletions yoti_python_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def get_activity_details(self, encrypted_request_token):

encrypted_data = protobuf.Protobuf().current_user(receipt)

if not encrypted_data:
return ActivityDetails(receipt)

unwrapped_key = self.__crypto.decrypt_token(receipt['wrapped_receipt_key'])
decrypted_data = self.__crypto.decipher(
unwrapped_key,
Expand Down
5 changes: 3 additions & 2 deletions yoti_python_sdk/protobuf/v1/protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class Protobuf(object):
CT_PNG = 4 # standard .png image

def current_user(self, receipt):
if receipt.get('other_party_profile_content') is None:
raise ValueError('The receipt has invalid data')
if receipt.get('other_party_profile_content') is None or receipt.get('other_party_profile_content') == '':
return None

profile_content = receipt['other_party_profile_content']
decoded_profile_content = base64.b64decode(profile_content)

Expand Down
16 changes: 16 additions & 0 deletions yoti_python_sdk/tests/fixtures/response_empty_profile.txt

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions yoti_python_sdk/tests/fixtures/response_missing_profile.txt

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions yoti_python_sdk/tests/fixtures/response_null_profile.txt

Large diffs are not rendered by default.

29 changes: 24 additions & 5 deletions yoti_python_sdk/tests/mocks.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
from uuid import UUID


def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, status_code, text):
self.status_code = status_code
self.text = text
class MockResponse:
def __init__(self, status_code, text):
self.status_code = status_code
self.text = text


def mocked_requests_get(*args, **kwargs):
with open('yoti_python_sdk/tests/fixtures/response.txt', 'r') as f:
response = f.read()
return MockResponse(status_code=200, text=response)


def mocked_requests_get_null_profile(*args, **kwargs):
with open('yoti_python_sdk/tests/fixtures/response_null_profile.txt', 'r') as f:
response = f.read()
return MockResponse(status_code=200, text=response)


def mocked_requests_get_empty_profile(*args, **kwargs):
with open('yoti_python_sdk/tests/fixtures/response_empty_profile.txt', 'r') as f:
response = f.read()
return MockResponse(status_code=200, text=response)


def mocked_requests_get_missing_profile(*args, **kwargs):
with open('yoti_python_sdk/tests/fixtures/response_missing_profile.txt', 'r') as f:
response = f.read()
return MockResponse(status_code=200, text=response)


def mocked_timestamp():
return 1476441361.2395663

Expand Down
43 changes: 43 additions & 0 deletions yoti_python_sdk/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from yoti_python_sdk.tests.conftest import YOTI_CLIENT_SDK_ID, PEM_FILE_PATH
from yoti_python_sdk.tests.mocks import (
mocked_requests_get,
mocked_requests_get_null_profile,
mocked_requests_get_empty_profile,
mocked_requests_get_missing_profile,
mocked_timestamp,
mocked_uuid4
)
Expand Down Expand Up @@ -105,6 +108,46 @@ def test_requesting_activity_details_with_correct_data(

mock_get.assert_called_once_with(url=expected_url, headers=expected_headers)
assert isinstance(activity_details, ActivityDetails)
assert activity_details.user_id == "ijH4kkqMKTG0FSNUgQIvd2Z3Nx1j8f5RjVQMyoKOvO/hkv43Ik+t6d6mGfP2tdrN"
selfie = activity_details.user_profile.get('selfie')
assert isinstance(selfie, basestring)
assert selfie.startswith('data:image/jpeg;base64')


@mock.patch('requests.get', side_effect=mocked_requests_get_null_profile)
@mock.patch('time.time', side_effect=mocked_timestamp)
@mock.patch('uuid.uuid4', side_effect=mocked_uuid4)
def test_requesting_activity_details_with_null_profile(
mock_uuid4, mock_time, mock_get, client, expected_url,
expected_headers, encrypted_request_token):
activity_details = client.get_activity_details(encrypted_request_token)

mock_get.assert_called_once_with(url=expected_url, headers=expected_headers)
assert activity_details.user_id == "ijH4kkqMKTG0FSNUgQIvd2Z3Nx1j8f5RjVQMyoKOvO/hkv43Ik+t6d6mGfP2tdrN"
assert isinstance(activity_details, ActivityDetails)


@mock.patch('requests.get', side_effect=mocked_requests_get_empty_profile)
@mock.patch('time.time', side_effect=mocked_timestamp)
@mock.patch('uuid.uuid4', side_effect=mocked_uuid4)
def test_requesting_activity_details_with_empty_profile(
mock_uuid4, mock_time, mock_get, client, expected_url,
expected_headers, encrypted_request_token):
activity_details = client.get_activity_details(encrypted_request_token)

mock_get.assert_called_once_with(url=expected_url, headers=expected_headers)
assert activity_details.user_id == "ijH4kkqMKTG0FSNUgQIvd2Z3Nx1j8f5RjVQMyoKOvO/hkv43Ik+t6d6mGfP2tdrN"
assert isinstance(activity_details, ActivityDetails)


@mock.patch('requests.get', side_effect=mocked_requests_get_missing_profile)
@mock.patch('time.time', side_effect=mocked_timestamp)
@mock.patch('uuid.uuid4', side_effect=mocked_uuid4)
def test_requesting_activity_details_with_missing_profile(
mock_uuid4, mock_time, mock_get, client, expected_url,
expected_headers, encrypted_request_token):
activity_details = client.get_activity_details(encrypted_request_token)

mock_get.assert_called_once_with(url=expected_url, headers=expected_headers)
assert activity_details.user_id == "ijH4kkqMKTG0FSNUgQIvd2Z3Nx1j8f5RjVQMyoKOvO/hkv43Ik+t6d6mGfP2tdrN"
assert isinstance(activity_details, ActivityDetails)
1 change: 0 additions & 1 deletion yoti_python_sdk/tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ def test_given_proper_encrypted_token__decrypting_should_yield_decrypted_token(
])
def test_strip_pkcs5_padding(with_padding, stripped):
assert Crypto.strip_pkcs5_padding(with_padding) == stripped

0 comments on commit ffb407d

Please sign in to comment.