Skip to content

Commit 2962349

Browse files
author
Kevin Hellemun
committed
Removed magic values. (#78)
1 parent 84a95e0 commit 2962349

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

bunq/sdk/util.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33

44
import requests
55

6+
from bunq.sdk.client import ApiClient
67
from bunq.sdk.context import ApiContext, ApiEnvironmentType
78
from bunq.sdk.exception import BunqException
89
from bunq.sdk.model.generated import endpoint
910

11+
__UNIQUE_REQUEST_ID = "uniqueness-is-required"
12+
__FIELD_API_KEY = "ApiKey"
13+
__INDEX_FIRST = 0
14+
__FIELD_RESPONSE = "Response"
15+
__ENDPOINT_SANDBOX_USER = "sandbox-user"
16+
1017
_ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER = "Could not create new sandbox" \
1118
" user."
1219

@@ -29,21 +36,22 @@ def __generate_new_sandbox_user():
2936
:rtype: endpoint.SandboxUser
3037
"""
3138

32-
url = "https://sandbox.public.api.bunq.com/v1/sandbox-user"
39+
url = ApiEnvironmentType.SANDBOX.uri_base + __ENDPOINT_SANDBOX_USER
3340

3441
headers = {
35-
'x-bunq-client-request-id': "uniqueness-is-required",
36-
'cache-control': "no-cache",
37-
'x-bunq-geolocation': "0 0 0 0 NL",
38-
'x-bunq-language': "en_US",
39-
'x-bunq-region': "en_US",
42+
ApiClient.HEADER_REQUEST_ID: __UNIQUE_REQUEST_ID,
43+
ApiClient.HEADER_CACHE_CONTROL: ApiClient._CACHE_CONTROL_NONE,
44+
ApiClient.HEADER_GEOLOCATION: ApiClient._GEOLOCATION_ZERO,
45+
ApiClient.HEADER_LANGUAGE: ApiClient._LANGUAGE_EN_US,
46+
ApiClient.HEADER_REGION: ApiClient._REGION_NL_NL,
4047
}
4148

42-
response = requests.request("POST", url, headers=headers)
49+
response = requests.request(ApiClient._METHOD_POST, url, headers=headers)
4350

44-
if response.status_code is 200:
51+
if response.status_code is ApiClient._STATUS_CODE_OK:
4552
response_json = json.loads(response.text)
4653
return endpoint.SandboxUser.from_json(
47-
json.dumps(response_json["Response"][0]["ApiKey"]))
54+
json.dumps(response_json[__FIELD_RESPONSE][__INDEX_FIRST][
55+
__FIELD_API_KEY]))
4856

4957
raise BunqException(_ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER)

tests/bunq_test.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class BunqSdkTestCase(unittest.TestCase):
1616
:type _cash_register: endpoint.CashRegister
1717
"""
1818

19+
__ERROR_COULD_NOT_DETERMINE_USER = 'Could not determine user alias.'
20+
1921
# Config values
2022
_API_KEY = config.Config.get_api_key()
2123

@@ -32,6 +34,19 @@ class BunqSdkTestCase(unittest.TestCase):
3234
_ATTACHMENT_DESCRIPTION = 'SDK python test'
3335
_FIRST_INDEX = 0
3436

37+
__SPENDING_MONEY_AMOUNT = '500'
38+
__CURRENCY_EUR = 'EUR'
39+
__POINTER_EMAIL = 'EMAIL'
40+
__SPENDING_MONEY_RECIPIENT = '[email protected]'
41+
__REQUEST_SPENDING_DESCRIPTION = 'sdk python test, thanks daddy <3'
42+
43+
__CASH_REGISTER_STATUS = 'PENDING_APPROVAL'
44+
__CASH_REGISTER_DESCRIPTION = 'python test cash register'
45+
46+
__SECOND_MONETARY_ACCOUNT_DESCRIPTION = 'test account python'
47+
48+
__EMAIL_BRAVO = '[email protected]'
49+
3550
_second_monetary_account = None
3651
_cash_register = None
3752

@@ -47,8 +62,8 @@ def setUp(self):
4762

4863
def __set_second_monetary_account(self):
4964
response = endpoint.MonetaryAccountBank.create(
50-
'EUR',
51-
'test account python'
65+
self.__CURRENCY_EUR,
66+
self.__SECOND_MONETARY_ACCOUNT_DESCRIPTION
5267
)
5368

5469
self._second_monetary_account = endpoint.MonetaryAccountBank.get(
@@ -57,15 +72,21 @@ def __set_second_monetary_account(self):
5772

5873
def __request_spending_money(self):
5974
endpoint.RequestInquiry.create(
60-
object_.Amount('500', 'EUR'),
61-
object_.Pointer('EMAIL', '[email protected]'),
62-
'sdk python test, thanks daddy <3 - OG',
75+
object_.Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
76+
object_.Pointer(
77+
self.__POINTER_EMAIL,
78+
self.__SPENDING_MONEY_RECIPIENT
79+
),
80+
self.__REQUEST_SPENDING_DESCRIPTION,
6381
False
6482
)
6583
endpoint.RequestInquiry.create(
66-
object_.Amount('500', 'EUR'),
67-
object_.Pointer('EMAIL', '[email protected]'),
68-
'sdk python test, thanks daddy <3 - OG',
84+
object_.Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
85+
object_.Pointer(
86+
self.__POINTER_EMAIL,
87+
self.__SPENDING_MONEY_RECIPIENT
88+
),
89+
self.__REQUEST_SPENDING_DESCRIPTION,
6990
False,
7091
self._second_monetary_account.id_
7192
)
@@ -86,20 +107,19 @@ def _get_api_context(cls):
86107

87108
return context.ApiContext.restore('bunq-test.conf')
88109

89-
@staticmethod
90-
def _get_pointer_bravo():
110+
def _get_pointer_bravo(self):
91111
"""
92112
:rtype: object_.Pointer
93113
"""
94114

95-
return object_.Pointer('EMAIL', '[email protected]')
115+
return object_.Pointer(self.__POINTER_EMAIL, self.__EMAIL_BRAVO)
96116

97117
def _get_alias_second_account(self):
98118
"""
99119
:rtype: object_.Pointer
100120
"""
101121

102-
return self._second_monetary_account.alias[0]
122+
return self._second_monetary_account.alias[self._FIRST_INDEX]
103123

104124
@staticmethod
105125
def _get_directory_test_root():
@@ -116,8 +136,8 @@ def _set_cash_register(self):
116136
)
117137
avatar_uuid = endpoint.Avatar.create(attachment_uuid.value)
118138
cash_register_id = endpoint.CashRegister.create(
119-
'python test cash register',
120-
'PENDING_APPROVAL',
139+
self.__CASH_REGISTER_DESCRIPTION,
140+
self.__CASH_REGISTER_STATUS,
121141
avatar_uuid.value
122142
)
123143

@@ -151,4 +171,4 @@ def alias_first(self):
151171
return BunqContext.user_context().user_person.alias[
152172
self._FIRST_INDEX]
153173

154-
raise BunqException('Could not determine user alias.')
174+
raise BunqException(self.__ERROR_COULD_NOT_DETERMINE_USER)

0 commit comments

Comments
 (0)