11import os
2+ import time
23import unittest
34
5+ from bunq .sdk import context , util
6+ from bunq .sdk .client import ApiClient
7+ from bunq .sdk .context import BunqContext
8+ from bunq .sdk .exception import BunqException
9+ from bunq .sdk .model .generated import endpoint , object_
410from tests import config
5- from bunq .sdk import context
611
712
813class BunqSdkTestCase (unittest .TestCase ):
14+ """
15+ :type _second_monetary_account: endpoint.MonetaryAccountBank
16+ :type _cash_register: endpoint.CashRegister
17+
18+ """
19+
920 # Config values
1021 _API_KEY = config .Config .get_api_key ()
1122
@@ -15,33 +26,126 @@ class BunqSdkTestCase(unittest.TestCase):
1526 # Device description used for python tests
1627 _DEVICE_DESCRIPTION = 'Python test device'
1728
29+ _PATH_ATTACHMENT = '/assets/'
30+ _READ_BYTES = "rb"
31+ _ATTACHMENT_PATH_IN = 'bunq_App_Icon_Square@4x.png'
32+ _CONTENT_TYPE = 'image/png'
33+ _ATTACHMENT_DESCRIPTION = 'SDK python test'
34+ _FIRST_INDEX = 0
35+
36+ _second_monetary_account = None
37+ _cash_register = None
38+
1839 @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.
40+ def setUpClass (cls ):
41+ BunqContext .load_api_context (cls ._get_api_context ())
42+
43+ def setUp (self ):
44+ self .__set_second_monetary_account ()
45+ self .__request_spending_money ()
46+ time .sleep (0.5 )
47+ BunqContext .user_context ().refresh_user_context ()
48+
49+ def __set_second_monetary_account (self ):
50+ response = endpoint .MonetaryAccountBank .create (
51+ 'EUR' ,
52+ 'test account python'
53+ )
54+
55+ self ._second_monetary_account = endpoint .MonetaryAccountBank .get (
56+ response .value
57+ ).value
2358
24- Catches ApiException if the session is inactive.
25- Catches BunqException if the conf file does not exist.
59+ def __request_spending_money (self ):
60+ endpoint .RequestInquiry .create (
61+ object_ .Amount ('500' , 'EUR' ),
62+ object_ .Pointer ('EMAIL' , 'sugardaddy@bunq.com' ),
63+ 'sdk python test, thanks daddy <3 - OG' ,
64+ False
65+ )
66+ endpoint .RequestInquiry .create (
67+ object_ .Amount ('500' , 'EUR' ),
68+ object_ .Pointer ('EMAIL' , 'sugardaddy@bunq.com' ),
69+ 'sdk python test, thanks daddy <3 - OG' ,
70+ False ,
71+ self ._second_monetary_account .id_
72+ )
2673
74+ def _get_cash_register_id (self ):
75+ if self ._cash_register is None :
76+ self ._set_cash_register ()
77+
78+ return self ._cash_register .id_
79+
80+ @classmethod
81+ def _get_api_context (cls ):
82+ """
2783 :rtype: context.ApiContext
2884 """
2985
30- filename_bunq_config_full = (cls ._get_directory_test_root () +
31- cls ._FILENAME_BUNQ_CONFIG )
86+ util .automatic_sandbox_install ('bunq-test.conf' )
3287
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 ()
88+ return context .ApiContext .restore ('bunq-test.conf' )
4089
41- api_context .save (filename_bunq_config_full )
90+ @staticmethod
91+ def _get_pointer_bravo ():
92+ """
93+ :rtype: object_.Pointer
94+ """
4295
43- return api_context
96+ return object_ .Pointer ('EMAIL' , 'bravo@bunq.com' )
97+
98+ def _get_alias_second_account (self ):
99+ """
100+ :rtype: object_.Pointer
101+ """
102+
103+ return self ._second_monetary_account .alias [0 ]
44104
45105 @staticmethod
46106 def _get_directory_test_root ():
47107 return os .path .dirname (os .path .abspath (__file__ ))
108+
109+ def _set_cash_register (self ):
110+ attachment_uuid = endpoint .AttachmentPublic .create (
111+ self ._attachment_contents ,
112+ {
113+ ApiClient .HEADER_CONTENT_TYPE : self ._CONTENT_TYPE ,
114+ ApiClient .HEADER_ATTACHMENT_DESCRIPTION :
115+ self ._ATTACHMENT_DESCRIPTION ,
116+ }
117+ )
118+ avatar_uuid = endpoint .Avatar .create (attachment_uuid .value )
119+ cash_register_id = endpoint .CashRegister .create (
120+ 'python test cash register' ,
121+ 'PENDING_APPROVAL' ,
122+ avatar_uuid .value
123+ )
124+
125+ self ._cash_register = endpoint .CashRegister .get (cash_register_id .value )
126+
127+ @property
128+ def _attachment_contents (self ):
129+ """
130+ :rtype: bytes
131+ """
132+
133+ with open (self ._get_directory_test_root () + self ._PATH_ATTACHMENT + self ._ATTACHMENT_PATH_IN ,
134+ self ._READ_BYTES ) as f :
135+ return f .read ()
136+
137+ @property
138+ def alias_first (self ):
139+ """
140+ :rtype: Pointer
141+ """
142+
143+ if BunqContext .user_context ().is_only_user_company_set ():
144+ return BunqContext .user_context ().user_company .alias [
145+ self ._FIRST_INDEX ]
146+
147+ if BunqContext .user_context ().is_only_user_person_set ():
148+ return BunqContext .user_context ().user_person .alias [
149+ self ._FIRST_INDEX ]
150+
151+ raise BunqException ('Could not determine user alias.' )
0 commit comments