-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbunq_test.py
146 lines (116 loc) · 4.74 KB
/
bunq_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import time
import unittest
from typing import AnyStr
from bunq.sdk.context.api_context import ApiContext
from bunq.sdk.context.bunq_context import BunqContext
from bunq.sdk.exception.bunq_exception import BunqException
from bunq.sdk.http.api_client import ApiClient
from bunq.sdk.model.generated.endpoint import MonetaryAccountBank, RequestInquiry, AttachmentPublic, Avatar, \
CashRegister
from bunq.sdk.model.generated.object_ import Amount, Pointer
from bunq.sdk.util import util
class BunqSdkTestCase(unittest.TestCase):
"""
:type _second_monetary_account: MonetaryAccountBank
:type _cash_register: CashRegister
"""
# Error constants.
__ERROR_COULD_NOT_DETERMINE_USER = 'Could not determine user alias.'
# Name of bunq config file
_FILENAME_BUNQ_CONFIG = "/bunq-test.conf"
# Device description used for python tests
_DEVICE_DESCRIPTION = 'Python test device'
_PATH_ATTACHMENT = 'tests/assets/'
_READ_BYTES = "rb"
_ATTACHMENT_PATH_IN = 'vader.png'
_CONTENT_TYPE = 'image/png'
_ATTACHMENT_DESCRIPTION = 'SDK python test'
_FIRST_INDEX = 0
__SPENDING_MONEY_AMOUNT = '500'
__CURRENCY_EUR = 'EUR'
_POINTER_EMAIL = 'EMAIL'
__SPENDING_MONEY_RECIPIENT = 'sugardaddy@bunq.com'
__REQUEST_SPENDING_DESCRIPTION = 'sdk python test, thanks daddy <3'
__CASH_REGISTER_STATUS = 'PENDING_APPROVAL'
__CASH_REGISTER_DESCRIPTION = 'python test cash register'
__SECOND_MONETARY_ACCOUNT_DESCRIPTION = 'test account python'
_EMAIL_BRAVO = 'bravo@bunq.com'
__TIME_OUT_AUTO_ACCEPT_SPENDING_MONEY = 0.5
_second_monetary_account = None
_cash_register = None
@classmethod
def setUpClass(cls):
BunqContext.load_api_context(cls._get_api_context())
def setUp(self):
self.__set_second_monetary_account()
self.__request_spending_money()
time.sleep(self.__TIME_OUT_AUTO_ACCEPT_SPENDING_MONEY)
BunqContext.user_context().refresh_user_context()
def __set_second_monetary_account(self):
response = MonetaryAccountBank.create(
self.__CURRENCY_EUR,
self.__SECOND_MONETARY_ACCOUNT_DESCRIPTION
)
self._second_monetary_account = MonetaryAccountBank.get(
response.value
).value
def __request_spending_money(self):
RequestInquiry.create(
Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
Pointer(self._POINTER_EMAIL, self.__SPENDING_MONEY_RECIPIENT),
self.__REQUEST_SPENDING_DESCRIPTION,
False
)
RequestInquiry.create(
Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
Pointer(self._POINTER_EMAIL, self.__SPENDING_MONEY_RECIPIENT),
self.__REQUEST_SPENDING_DESCRIPTION,
False,
self._second_monetary_account.id_
)
def _get_cash_register_id(self):
if self._cash_register is None:
self._set_cash_register()
return self._cash_register.id_
@classmethod
def _get_api_context(cls) -> ApiContext:
return util.automatic_sandbox_install()
def _get_pointer_bravo(self) -> Pointer:
return Pointer(self._POINTER_EMAIL, self._EMAIL_BRAVO)
def _get_alias_second_account(self) -> Pointer:
return self._second_monetary_account.alias[self._FIRST_INDEX]
@staticmethod
def _get_directory_test_root():
return os.path.dirname(os.path.abspath(__file__))
def _set_cash_register(self):
attachment_uuid = AttachmentPublic.create(
self._attachment_contents,
{
ApiClient.HEADER_CONTENT_TYPE: self._CONTENT_TYPE,
ApiClient.HEADER_ATTACHMENT_DESCRIPTION: self._ATTACHMENT_DESCRIPTION,
}
)
avatar_uuid = Avatar.create(attachment_uuid.value)
cash_register_id = CashRegister.create(
self.__CASH_REGISTER_DESCRIPTION,
self.__CASH_REGISTER_STATUS,
avatar_uuid.value
)
self._cash_register = CashRegister.get(cash_register_id.value)
@property
def _attachment_contents(self) -> AnyStr:
with open(
self._get_directory_test_root() +
self._PATH_ATTACHMENT +
self._ATTACHMENT_PATH_IN,
self._READ_BYTES
) as file:
return file.read()
@property
def alias_first(self) -> Pointer:
if BunqContext.user_context().is_only_user_company_set():
return BunqContext.user_context().user_company.alias[self._FIRST_INDEX]
if BunqContext.user_context().is_only_user_person_set():
return BunqContext.user_context().user_person.alias[self._FIRST_INDEX]
raise BunqException(self.__ERROR_COULD_NOT_DETERMINE_USER)