1
1
import os
2
+ import time
2
3
import unittest
3
4
4
- from tests import config
5
5
from bunq .sdk import context
6
+ from bunq .sdk import util
7
+ from bunq .sdk .client import ApiClient
8
+ from bunq .sdk .exception import BunqException
9
+ from bunq .sdk .model .generated import endpoint
10
+ from bunq .sdk .model .generated import object_
11
+ from tests import config
6
12
7
13
8
14
class BunqSdkTestCase (unittest .TestCase ):
15
+ """
16
+ :type _second_monetary_account: endpoint.MonetaryAccountBank
17
+ :type _cash_register: endpoint.CashRegister
18
+ """
19
+
20
+ __ERROR_COULD_NOT_DETERMINE_USER = 'Could not determine user alias.'
21
+
9
22
# Config values
10
23
_API_KEY = config .Config .get_api_key ()
11
24
@@ -15,33 +28,148 @@ class BunqSdkTestCase(unittest.TestCase):
15
28
# Device description used for python tests
16
29
_DEVICE_DESCRIPTION = 'Python test device'
17
30
31
+ _PATH_ATTACHMENT = 'tests/assets/'
32
+ _READ_BYTES = "rb"
33
+ _ATTACHMENT_PATH_IN = '[email protected] '
34
+ _CONTENT_TYPE = 'image/png'
35
+ _ATTACHMENT_DESCRIPTION = 'SDK python test'
36
+ _FIRST_INDEX = 0
37
+
38
+ __SPENDING_MONEY_AMOUNT = '500'
39
+ __CURRENCY_EUR = 'EUR'
40
+ __POINTER_EMAIL = 'EMAIL'
41
+ __SPENDING_MONEY_RECIPIENT = '[email protected] '
42
+ __REQUEST_SPENDING_DESCRIPTION = 'sdk python test, thanks daddy <3'
43
+
44
+ __CASH_REGISTER_STATUS = 'PENDING_APPROVAL'
45
+ __CASH_REGISTER_DESCRIPTION = 'python test cash register'
46
+
47
+ __SECOND_MONETARY_ACCOUNT_DESCRIPTION = 'test account python'
48
+
49
+ __EMAIL_BRAVO = '[email protected] '
50
+
51
+ __TIME_OUT_AUTO_ACCEPT_SPENDING_MONEY = 0.5
52
+
53
+ _second_monetary_account = None
54
+ _cash_register = None
55
+
18
56
@classmethod
19
- def _get_api_context (cls ):
20
- """
21
- Calls IsSessionActive to check if the session token is still active
22
- and returns the context.ApiContext.
57
+ def setUpClass (cls ):
58
+ context .BunqContext .load_api_context (cls ._get_api_context ())
59
+
60
+ def setUp (self ):
61
+ self .__set_second_monetary_account ()
62
+ self .__request_spending_money ()
63
+ time .sleep (self .__TIME_OUT_AUTO_ACCEPT_SPENDING_MONEY )
64
+ context .BunqContext .user_context ().refresh_user_context ()
65
+
66
+ def __set_second_monetary_account (self ):
67
+ response = endpoint .MonetaryAccountBank .create (
68
+ self .__CURRENCY_EUR ,
69
+ self .__SECOND_MONETARY_ACCOUNT_DESCRIPTION
70
+ )
23
71
24
- Catches ApiException if the session is inactive.
25
- Catches BunqException if the conf file does not exist.
72
+ self ._second_monetary_account = endpoint .MonetaryAccountBank .get (
73
+ response .value
74
+ ).value
26
75
76
+ def __request_spending_money (self ):
77
+ endpoint .RequestInquiry .create (
78
+ object_ .Amount (self .__SPENDING_MONEY_AMOUNT , self .__CURRENCY_EUR ),
79
+ object_ .Pointer (
80
+ self .__POINTER_EMAIL ,
81
+ self .__SPENDING_MONEY_RECIPIENT
82
+ ),
83
+ self .__REQUEST_SPENDING_DESCRIPTION ,
84
+ False
85
+ )
86
+ endpoint .RequestInquiry .create (
87
+ object_ .Amount (self .__SPENDING_MONEY_AMOUNT , self .__CURRENCY_EUR ),
88
+ object_ .Pointer (
89
+ self .__POINTER_EMAIL ,
90
+ self .__SPENDING_MONEY_RECIPIENT
91
+ ),
92
+ self .__REQUEST_SPENDING_DESCRIPTION ,
93
+ False ,
94
+ self ._second_monetary_account .id_
95
+ )
96
+
97
+ def _get_cash_register_id (self ):
98
+ if self ._cash_register is None :
99
+ self ._set_cash_register ()
100
+
101
+ return self ._cash_register .id_
102
+
103
+ @classmethod
104
+ def _get_api_context (cls ):
105
+ """
27
106
:rtype: context.ApiContext
28
107
"""
29
108
30
- filename_bunq_config_full = (cls ._get_directory_test_root () +
31
- cls ._FILENAME_BUNQ_CONFIG )
109
+ return util .automatic_sandbox_install ()
110
+
111
+ def _get_pointer_bravo (self ):
112
+ """
113
+ :rtype: object_.Pointer
114
+ """
32
115
33
- try :
34
- api_context = context .ApiContext .restore (filename_bunq_config_full )
35
- except FileNotFoundError :
36
- api_context = context .ApiContext (context .ApiEnvironmentType .SANDBOX , cls ._API_KEY ,
37
- cls ._DEVICE_DESCRIPTION , [])
38
- else :
39
- api_context .ensure_session_active ()
116
+ return object_ .Pointer (self .__POINTER_EMAIL , self .__EMAIL_BRAVO )
40
117
41
- api_context .save (filename_bunq_config_full )
118
+ def _get_alias_second_account (self ):
119
+ """
120
+ :rtype: object_.Pointer
121
+ """
42
122
43
- return api_context
123
+ return self . _second_monetary_account . alias [ self . _FIRST_INDEX ]
44
124
45
125
@staticmethod
46
126
def _get_directory_test_root ():
47
127
return os .path .dirname (os .path .abspath (__file__ ))
128
+
129
+ def _set_cash_register (self ):
130
+ attachment_uuid = endpoint .AttachmentPublic .create (
131
+ self ._attachment_contents ,
132
+ {
133
+ ApiClient .HEADER_CONTENT_TYPE : self ._CONTENT_TYPE ,
134
+ ApiClient .HEADER_ATTACHMENT_DESCRIPTION :
135
+ self ._ATTACHMENT_DESCRIPTION ,
136
+ }
137
+ )
138
+ avatar_uuid = endpoint .Avatar .create (attachment_uuid .value )
139
+ cash_register_id = endpoint .CashRegister .create (
140
+ self .__CASH_REGISTER_DESCRIPTION ,
141
+ self .__CASH_REGISTER_STATUS ,
142
+ avatar_uuid .value
143
+ )
144
+
145
+ self ._cash_register = endpoint .CashRegister .get (cash_register_id .value )
146
+
147
+ @property
148
+ def _attachment_contents (self ):
149
+ """
150
+ :rtype: bytes
151
+ """
152
+
153
+ with open (
154
+ self ._get_directory_test_root () +
155
+ self ._PATH_ATTACHMENT +
156
+ self ._ATTACHMENT_PATH_IN ,
157
+ self ._READ_BYTES
158
+ ) as file :
159
+ return file .read ()
160
+
161
+ @property
162
+ def alias_first (self ):
163
+ """
164
+ :rtype: Pointer
165
+ """
166
+
167
+ if context .BunqContext .user_context ().is_only_user_company_set ():
168
+ return context .BunqContext .user_context ().user_company .alias [
169
+ self ._FIRST_INDEX ]
170
+
171
+ if context .BunqContext .user_context ().is_only_user_person_set ():
172
+ return context .BunqContext .user_context ().user_person .alias [
173
+ self ._FIRST_INDEX ]
174
+
175
+ raise BunqException (self .__ERROR_COULD_NOT_DETERMINE_USER )
0 commit comments