Skip to content

Commit

Permalink
Add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Aug 1, 2017
1 parent 3e5f794 commit 616b1e6
Show file tree
Hide file tree
Showing 17 changed files with 859 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ htmlcov/

# bunq specific
bunq.conf
**/tmp/
bunq-test.conf
**/tmp
config.json
tests/connectQr.png
42 changes: 42 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions tests/api_context_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.config import Config
from bunq.sdk.context import ApiContext
from bunq.sdk.context import ApiEnvironmentType
from bunq.sdk.model.generated import endpoint
from bunq.sdk.exception import ApiException


class ApiContextHandler:
# Config values
_API_KEY = Config.get_api_key()
_BUNQ_CONFIG_FILE = "bunq-test.conf"
_DEVICE_DESCRIPTION = 'Python test device'

@classmethod
def get_api_context(cls):
"""
Calls IsSessionActive to check if the session token is still active
and returns the ApiContext.
:rtype: ApiContext
"""

if cls._is_session_active():
return ApiContext.restore(cls._BUNQ_CONFIG_FILE)
else:
api_context = ApiContext(ApiEnvironmentType.SANDBOX, cls._API_KEY,
cls._DEVICE_DESCRIPTION, [])
api_context.save(cls._BUNQ_CONFIG_FILE)

return api_context

@classmethod
def _is_session_active(cls):
"""
Catches ApiExceptoin if the session is inactive.
Catches BunqEception if the conf file does not exist.
:rtype: bool
:return: True If exception is not called, otherwise False.
"""
try:
api_context = ApiContext.restore(cls._BUNQ_CONFIG_FILE)
endpoint.User.list(api_context)

return True
except ApiException:
return False
except FileNotFoundError:
return False
Binary file added tests/assets/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tests/assets/config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"API_KEY": "<API KEY>",
"USER_ID": "xxxx",
"MA_ID": "xxxx",
"MA_ID2": "xxxx",
"ipAddress": "<Ip address>",
"AttachmentPublicTest":{
"CONTENT_TYPE": "image/png",
"DESCRIPTION": "TEST PNG PHP",
"PATH_IN": "/[email protected]"
},
"TabUsageSingleTest": {
"CASH_REGISTER_ID": "xxx"
},
"CounterPartySelf": {
"Alias": "<Email address>",
"Type": "EMAIL"
},
"CounterPartyOther": {
"Alias": "<Email address>",
"Type": "EMAIL"
}
}
134 changes: 134 additions & 0 deletions tests/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import json
import os

from bunq.sdk.model.generated.object_ import Pointer


class Config:
_FIELD_IP_ADDRESS_ALLOWED = "ipAddress"
_FIELD_COUNTER_PARTY_OTHER = "CounterPartyOther"
_FIELD_COUNTER_PARTY_SELF = "CounterPartySelf"
_FIELD_TYPE = "Type"
_FIELD_ALIAS = "Alias"
_FIELD_TAB_USAGE = "TabUsageSingleTest"
_FIELD_CASH_REGISTER_ID = "CASH_REGISTER_ID"
_FIELD_MONETARY_ACCOUNT_ID_1 = "MA_ID"
_FIELD_MONETARY_ACCOUNT_ID_2 = "MA_ID2"
_FIELD_USER_ID = "USER_ID"
_FIELD_API_KEY = "API_KEY"
_FIELD_ATTACHMENT_PUBLIC = "AttachmentPublicTest"
_FIELD_ATTACHMENT_PATH_IN = "PATH_IN"
_FIELD_ATTACHMENT_DESCRIPTION = "DESCRIPTION"
_FIELD_ATTACHMENT_CONTENT_TYPE = "CONTENT_TYPE"

@classmethod
def get_attachment_content_type(cls):
"""
:rtype: str
"""

return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][
cls._FIELD_ATTACHMENT_CONTENT_TYPE]

@classmethod
def get_attachment_description(cls):
"""
:rtype: str
"""

return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][
cls._FIELD_ATTACHMENT_DESCRIPTION]

@classmethod
def get_attachment_path_in(cls):
"""
:rtype: str
"""

return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][
cls._FIELD_ATTACHMENT_PATH_IN]

@classmethod
def get_api_key(cls):
"""
:rtype: str
"""

return cls._get_config_file()[cls._FIELD_API_KEY]

@classmethod
def get_user_id(cls):
"""
:rtype: int
"""

return int(cls._get_config_file()[cls._FIELD_USER_ID])

@classmethod
def get_monetary_account_id_2(cls):
"""
:rtype: int
"""

return int(cls._get_config_file()[cls._FIELD_MONETARY_ACCOUNT_ID_2])

@classmethod
def get_monetary_account_id_1(cls):
"""
:rtype: int
"""

return int(cls._get_config_file()[cls._FIELD_MONETARY_ACCOUNT_ID_1])

@classmethod
def get_cash_register_id(cls):
"""
:rtype str
"""

return cls._get_config_file()[cls._FIELD_TAB_USAGE][
cls._FIELD_CASH_REGISTER_ID]

@classmethod
def get_pointer_counter_party_self(cls):
"""
:rtype: Pointer
"""

type_ = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_SELF][
cls._FIELD_TYPE]
alias = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_SELF][
cls._FIELD_ALIAS]

return Pointer(type_, alias)

@classmethod
def get_pointer_counter_party_other(cls):
"""
:rtype: Pointer
"""

type_ = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_OTHER][
cls._FIELD_TYPE]
alias = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_OTHER][
cls._FIELD_ALIAS]

return Pointer(type_, alias)

@classmethod
def get_ip_address(cls):
"""
:rtype: str
"""

return cls._get_config_file()[cls._FIELD_IP_ADDRESS_ALLOWED]

@classmethod
def _get_config_file(cls):
"""
:rtype: json.load
"""

file_path = os.path.dirname(os.path.realpath(__file__))
with open(file_path + "/assets/config.json", "r") as f:
return json.load(f)
Empty file added tests/model/__init__.py
Empty file.
Empty file.
58 changes: 58 additions & 0 deletions tests/model/generated/test_attachment_public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import unittest

from tests.api_context_handler import ApiContextHandler
from tests.config import Config
from bunq.sdk.model.generated.endpoint import AttachmentPublic
from bunq.sdk.model.generated.endpoint import AttachmentPublicContent
from bunq.sdk.client import ApiClient


class TestAttachmentPublic(unittest.TestCase):
"""
Tests:
AttachmentPublic
AttachmentPublicContent
"""

@classmethod
def setUpClass(cls):
# config values
cls._PATH_TO_ATTACHMENT = '/Users/khellemun/bunq/sdk_python/tests/' \
'assets'
cls._READ_BYTES = "rb"
cls._CONTENT_TYPE = Config.get_attachment_content_type()
cls._ATTACHMENT_DESCRIPTION = Config.get_attachment_description()
cls._ATTACHMENT_PATH_IN = Config.get_attachment_path_in()
cls._API_CONTEXT = ApiContextHandler.get_api_context()

def test_file_upload_and_retrieval(self):
"""
Tests uploading an attachment, retrieves it and compare them to see
if the uploaded attachment is indeed the attachment we are getting
back.
"""

custom_headers = {
ApiClient.HEADER_CONTENT_TYPE: self._CONTENT_TYPE,
ApiClient.HEADER_ATTACHMENT_DESCRIPTION:
self._ATTACHMENT_DESCRIPTION,
}

attachment_uuid = AttachmentPublic.create(self._API_CONTEXT,
self.attachment_contents,
custom_headers)

contents_from_response = AttachmentPublicContent.list(self._API_CONTEXT,
attachment_uuid)

self.assertEqual(self.attachment_contents, contents_from_response)

@property
def attachment_contents(self):
"""
:rtype: bytes
"""

with open(self._PATH_TO_ATTACHMENT + self._ATTACHMENT_PATH_IN,
self._READ_BYTES) as f:
return f.read()
64 changes: 64 additions & 0 deletions tests/model/generated/test_avatar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import unittest

from bunq.sdk.client import ApiClient
from bunq.sdk.model.generated.endpoint import Avatar
from bunq.sdk.model.generated.endpoint import AttachmentPublic
from bunq.sdk.model.generated.endpoint import AttachmentPublicContent
from tests.api_context_handler import ApiContextHandler
from tests.config import Config


class AvatarTest(unittest.TestCase):
"""
Tests:
Avatar
AttachmentPublic
AttachmentPublicContent
"""

@classmethod
def setUpClass(cls):
cls._FIRST_INDEX = 0
cls._PATH_TO_ATTACHMENT = '/Users/khellemun/bunq/sdk_python/tests' \
'/assets'
cls._READ_FILE_BYTES = 'rb'
cls._CONTENT_TYPE = Config.get_attachment_content_type()
cls._ATTACHMENT_DESCRIPTION = Config.get_attachment_description()
cls._ATTACHMENT_PATH_IN = Config.get_attachment_path_in()
cls._API_CONTEXT = ApiContextHandler.get_api_context()

def test_avatar_creation(self):
"""
Tests the creation of an avatar by uploading a picture via
AttachmentPublic and setting it as avatar via the uuid
"""

custom_header = {
ApiClient.HEADER_ATTACHMENT_DESCRIPTION:
self._ATTACHMENT_DESCRIPTION,
ApiClient.HEADER_CONTENT_TYPE: self._CONTENT_TYPE
}
attachment_public_uuid = AttachmentPublic \
.create(self._API_CONTEXT, self.attachment_contents, custom_header)

avatar_map = {
Avatar.FIELD_ATTACHMENT_PUBLIC_UUID: attachment_public_uuid
}
avatar_uuid = Avatar.create(self._API_CONTEXT, avatar_map)
attachment_uuid_after = Avatar.get(self._API_CONTEXT, avatar_uuid) \
.image[self._FIRST_INDEX].attachment_public_uuid

file_contents_received = AttachmentPublicContent.list(
self._API_CONTEXT, attachment_uuid_after
)
self.assertEqual(self.attachment_contents, file_contents_received)

@property
def attachment_contents(self):
"""
:rtype: bytes
"""

with open(self._PATH_TO_ATTACHMENT + self._ATTACHMENT_PATH_IN,
self._READ_FILE_BYTES) as f:
return f.read()
Loading

0 comments on commit 616b1e6

Please sign in to comment.