1+ import json
12import unittest
3+ import responses
24
35from securenative .api_manager import ApiManager
46from securenative .config .configuration_manager import ConfigurationManager
1012from securenative .exceptions .securenative_invalid_options_exception import SecureNativeInvalidOptionsException
1113from securenative .models .user_traits import UserTraits
1214from securenative .models .verify_result import VerifyResult
13- from securenative .utils .date_utils import DateUtils
1415
1516
1617class ApiManagerTest (unittest .TestCase ):
@@ -30,122 +31,77 @@ def setUp(self):
3031 with_context (self .context ). \
3132 with_properties ({"prop1" : "CUSTOM_PARAM_VALUE" ,
3233 "prop2" : True ,
33- "prop3" : 3 }).with_timestamp ( DateUtils . to_timestamp ( None )). build ()
34+ "prop3" : 3 }).build ()
3435
3536 def test_track_event (self ):
3637 options = ConfigurationManager .config_builder (). \
3738 with_api_key ("YOUR_API_KEY" ). \
3839 with_auto_send (True ). \
39- with_interval (10 )
40-
41- client = MockHttpClient () # TODO!
42- event_manager = EventManager (options , client )
40+ with_interval (10 ). \
41+ with_api_url ("https://api.securenative-stg.com/collector/api/v1" )
42+
43+ expected = "{\" eventType\" :\" sn.user.login\" ,\" userId\" :\" USER_ID\" ,\" userTraits\" :{" \
44+ "\" name\" :\" USER_NAME\" ,\" email\" :\" USER_EMAIL\" ,\" createdAt\" :null},\" request\" :{" \
45+ "\" cid\" :null,\" vid\" :null,\" fp\" :null,\" ip\" :\" 127.0.0.1\" ,\" remoteIp\" :null,\" headers\" :{" \
46+ "\" user-agent\" :\" Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) " \
47+ "AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\" },\" url\" :null,\" method\" :null}," \
48+ "\" properties\" :{\" prop2\" :true,\" prop1\" :\" CUSTOM_PARAM_VALUE\" ,\" prop3\" :3}}"
49+
50+ responses .add (responses .POST , "https://api.securenative-stg.com/collector/api/v1/track" ,
51+ json = json .loads (expected ), status = 200 )
52+ event_manager = EventManager (options )
4353 event_manager .start_event_persist ()
4454 api_manager = ApiManager (event_manager , options )
4555
4656 try :
47- res = api_manager .track (self .event_options )
48- expected = "{\" eventType\" :\" sn.user.login\" ,\" userId\" :\" USER_ID\" ,\" userTraits\" :{" \
49- "\" name\" :\" USER_NAME\" ,\" email\" :\" USER_EMAIL\" ,\" createdAt\" :null},\" request\" :{" \
50- "\" cid\" :null,\" vid\" :null,\" fp\" :null,\" ip\" :\" 127.0.0.1\" ,\" remoteIp\" :null,\" headers\" :{" \
51- "\" user-agent\" :\" Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) " \
52- "AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\" },\" url\" :null,\" method\" :null}," \
53- "\" properties\" :{\" prop2\" :true,\" prop1\" :\" CUSTOM_PARAM_VALUE\" ,\" prop3\" :3}} "
54-
55- self .assertEqual (res .body , expected )
57+ api_manager .track (self .event_options )
5658 finally :
5759 event_manager .stop_event_persist ()
5860
5961 def test_securenative_invalid_options_exception (self ):
6062 options = ConfigurationManager .config_builder (). \
6163 with_api_key ("YOUR_API_KEY" ). \
6264 with_auto_send (True ). \
63- with_interval (10 )
65+ with_interval (10 ). \
66+ with_api_url ("https://api.securenative-stg.com/collector/api/v1" )
6467
6568 properties = {}
6669 for i in range (1 , 12 ):
6770 properties [i ] = i
6871
69- client = MockHttpClient () # TODO!
70- event_manager = EventManager (client , options )
72+ responses .add (responses .POST , "https://api.securenative-stg.com/collector/api/v1/track" ,
73+ json = {}, status = 200 )
74+ event_manager = EventManager (options )
7175 event_manager .start_event_persist ()
7276 api_manager = ApiManager (event_manager , options )
7377
7478 try :
75- api_manager .track (EventOptionsBuilder (
76- EventTypes .LOG_IN ).with_properties (properties ).build ())
77- self .assertRaises (SecureNativeInvalidOptionsException )
78- finally :
79- event_manager .stop_event_persist ()
80-
81- def test_automatic_persistent_disable (self ):
82- options = ConfigurationManager .config_builder (). \
83- with_api_key ("YOUR_API_KEY" ). \
84- with_auto_send (True ). \
85- with_interval (10 )
86-
87- client = MockHttpClient () # TODO!
88- event_manager = EventManager (client , options )
89- api_manager = ApiManager (event_manager , options )
90-
91- self .assertIsNotNone (api_manager .track (self .event_options ))
92-
93- def test_unauthorized_track_event (self ):
94- options = ConfigurationManager .config_builder (). \
95- with_api_key ("YOUR_API_KEY" ). \
96- with_auto_send (True ). \
97- with_interval (10 )
98-
99- client = MockHttpClient () # TODO!
100- event_manager = EventManager (client , options )
101- event_manager .start_event_persist ()
102- api_manager = ApiManager (event_manager , options )
103-
104- try :
105- self .assertIsNotNone (api_manager .track (self .event_options ))
79+ with self .assertRaises (SecureNativeInvalidOptionsException ):
80+ api_manager .track (EventOptionsBuilder (
81+ EventTypes .LOG_IN ).with_properties (properties ).build ())
10682 finally :
10783 event_manager .stop_event_persist ()
10884
10985 def test_verify_event (self ):
11086 options = ConfigurationManager .config_builder (). \
111- with_api_key ("YOUR_API_KEY" )
87+ with_api_key ("YOUR_API_KEY" ). \
88+ with_api_url ("https://api.securenative-stg.com/collector/api/v1" )
11289
113- verify_result = VerifyResult (RiskLevel .LOW , 0 , {})
90+ responses .add (responses .POST , "https://api.securenative-stg.com/collector/api/v1/verify" ,
91+ json = {}, status = 200 )
92+ verify_result = VerifyResult (RiskLevel .LOW , 0 , None )
11493
115- client = MockHttpClient () # TODO!
116- event_manager = EventManager (client , options )
94+ event_manager = EventManager (options )
11795 event_manager .start_event_persist ()
11896 api_manager = ApiManager (event_manager , options )
11997
120- result , body = api_manager .verify (self .event_options )
98+ result = api_manager .verify (self .event_options )
12199
122- self .assertEqual (result .risk_level , verify_result .risk_level )
100+ self .assertIsNotNone (result )
101+ self .assertEqual (result .risk_level , verify_result .risk_level .value )
123102 self .assertEqual (result .score , verify_result .score )
124103 self .assertEqual (result .triggers , verify_result .triggers )
125104
126- expected = "{\" eventType\" :\" sn.user.login\" ,\" userId\" :\" USER_ID\" ,\" userTraits\" :{\" name\" :\" USER_NAME\" ,\" email\" :\" USER_EMAIL\" ,\" createdAt\" :null},\" request\" :{\" cid\" :null,\" vid\" :null,\" fp\" :null,\" ip\" :\" 127.0.0.1\" ,\" remoteIp\" :null,\" headers\" :{\" user-agent\" :\" Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\" },\" url\" :null,\" method\" :null},\" properties\" :{\" prop2\" :true,\" prop1\" :\" CUSTOM_PARAM_VALUE\" ,\" prop3\" :3}}" ;
127- self .assertEqual (body , expected )
128-
129- def test_unauthorized_verify_event (self ):
130- options = ConfigurationManager .config_builder (). \
131- with_api_key ("YOUR_API_KEY" )
132-
133- client = MockHttpClient () # TODO!
134- event_manager = EventManager (client , options )
135- event_manager .start_event_persist ()
136- api_manager = ApiManager (event_manager , options )
137-
138- result , body = api_manager .verify (self .event_options )
139-
140- self .assertIsNotNone (result )
141- self .assertEqual (result .risk_level , RiskLevel .LOW )
142- self .assertEqual (result .score , 0 )
143- self .assertEqual (result .triggers , 0 )
144-
145- expected = "{\" eventType\" :\" sn.user.login\" ,\" userId\" :\" USER_ID\" ,\" userTraits\" :{\" name\" :\" USER_NAME'\" ,\" email\" :\" USER_EMAIL'\" ,\" createdAt\" :null},\" request\" :{\" cid\" :null,\" vid\" :null,\" fp\" :null,\" ip\" :\" 127.0.0.1\" ,\" remoteIp\" :null,\" headers\" :{\" user-agent\" :\" Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\" },\" url\" :null,\" method\" :null},\" properties\" :{\" prop2\" :true,\" prop1\" :\" CUSTOM_PARAM_VALUE\" ,\" prop3\" :3}}" ;
146- self .assertIsNotNone (body )
147- self .assertEqual (body , expected )
148-
149105
150106if __name__ == '__main__' :
151107 unittest .main ()
0 commit comments