11import os
2+ import time
23import unittest
34
4- from tests import config
55from 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
612
713
814class 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+
922 # Config values
1023 _API_KEY = config .Config .get_api_key ()
1124
@@ -15,33 +28,148 @@ class BunqSdkTestCase(unittest.TestCase):
1528 # Device description used for python tests
1629 _DEVICE_DESCRIPTION = 'Python test device'
1730
31+ _PATH_ATTACHMENT = 'tests/assets/'
32+ _READ_BYTES = "rb"
33+ _ATTACHMENT_PATH_IN = 'bunq_App_Icon_Square@4x.png'
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 = 'sugardaddy@bunq.com'
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 = 'bravo@bunq.com'
50+
51+ __TIME_OUT_AUTO_ACCEPT_SPENDING_MONEY = 0.5
52+
53+ _second_monetary_account = None
54+ _cash_register = None
55+
1856 @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+ )
2371
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
2675
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+ """
27106 :rtype: context.ApiContext
28107 """
29108
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+ """
32115
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 )
40117
41- api_context .save (filename_bunq_config_full )
118+ def _get_alias_second_account (self ):
119+ """
120+ :rtype: object_.Pointer
121+ """
42122
43- return api_context
123+ return self . _second_monetary_account . alias [ self . _FIRST_INDEX ]
44124
45125 @staticmethod
46126 def _get_directory_test_root ():
47127 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