|
| 1 | +import unittest |
| 2 | + |
| 3 | +from securenative.api_manager import ApiManager |
| 4 | +from securenative.config.configuration_manager import ConfigurationManager |
| 5 | +from securenative.context.context_builder import ContextBuilder |
| 6 | +from securenative.enums.event_types import EventTypes |
| 7 | +from securenative.enums.risk_level import RiskLevel |
| 8 | +from securenative.event_manager import EventManager |
| 9 | +from securenative.event_options_builder import EventOptionsBuilder |
| 10 | +from securenative.exceptions.securenative_invalid_options_exception import SecureNativeInvalidOptionsException |
| 11 | +from securenative.models.user_traits import UserTraits |
| 12 | +from securenative.models.verify_result import VerifyResult |
| 13 | +from securenative.utils.date_utils import DateUtils |
| 14 | + |
| 15 | + |
| 16 | +class ApiManagerTest(unittest.TestCase): |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + self.context = ContextBuilder(). \ |
| 20 | + with_ip("127.0.0.1"). \ |
| 21 | + with_client_token("SECURED_CLIENT_TOKEN"). \ |
| 22 | + with_headers( |
| 23 | + { |
| 24 | + "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" |
| 25 | + }).build() |
| 26 | + |
| 27 | + self.event_options = EventOptionsBuilder(EventTypes.LOG_IN). \ |
| 28 | + with_user_id("USER_ID"). \ |
| 29 | + with_user_traits(UserTraits("USER_NAME", "USER_EMAIL")). \ |
| 30 | + with_context(self.context). \ |
| 31 | + with_properties({"prop1": "CUSTOM_PARAM_VALUE", |
| 32 | + "prop2": True, |
| 33 | + "prop3": 3}).with_timestamp(DateUtils.to_timestamp(None)).build() |
| 34 | + |
| 35 | + def test_track_event(self): |
| 36 | + options = ConfigurationManager.config_builder(). \ |
| 37 | + with_api_key("YOUR_API_KEY"). \ |
| 38 | + with_auto_send(True). \ |
| 39 | + with_interval(10) |
| 40 | + |
| 41 | + client = MockHttpClient() # TODO! |
| 42 | + event_manager = EventManager(options, client) |
| 43 | + event_manager.start_event_persist() |
| 44 | + api_manager = ApiManager(event_manager, options) |
| 45 | + |
| 46 | + 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) |
| 56 | + finally: |
| 57 | + event_manager.stop_event_persist() |
| 58 | + |
| 59 | + def test_securenative_invalid_options_exception(self): |
| 60 | + options = ConfigurationManager.config_builder(). \ |
| 61 | + with_api_key("YOUR_API_KEY"). \ |
| 62 | + with_auto_send(True). \ |
| 63 | + with_interval(10) |
| 64 | + |
| 65 | + properties = {} |
| 66 | + for i in range(1, 12): |
| 67 | + properties[i] = i |
| 68 | + |
| 69 | + client = MockHttpClient() # TODO! |
| 70 | + event_manager = EventManager(client, options) |
| 71 | + event_manager.start_event_persist() |
| 72 | + api_manager = ApiManager(event_manager, options) |
| 73 | + |
| 74 | + 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)) |
| 106 | + finally: |
| 107 | + event_manager.stop_event_persist() |
| 108 | + |
| 109 | + def test_verify_event(self): |
| 110 | + options = ConfigurationManager.config_builder(). \ |
| 111 | + with_api_key("YOUR_API_KEY") |
| 112 | + |
| 113 | + verify_result = VerifyResult(RiskLevel.LOW, 0, {}) |
| 114 | + |
| 115 | + client = MockHttpClient() # TODO! |
| 116 | + event_manager = EventManager(client, options) |
| 117 | + event_manager.start_event_persist() |
| 118 | + api_manager = ApiManager(event_manager, options) |
| 119 | + |
| 120 | + result, body = api_manager.verify(self.event_options) |
| 121 | + |
| 122 | + self.assertEqual(result.risk_level, verify_result.risk_level) |
| 123 | + self.assertEqual(result.score, verify_result.score) |
| 124 | + self.assertEqual(result.triggers, verify_result.triggers) |
| 125 | + |
| 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 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + unittest.main() |
0 commit comments