def test_track_event(self): options = SecureNativeOptions(api_key="YOUR_API_KEY", auto_send=True, interval=10, api_url="https://api.securenative-stg.com/collector/api/v1") 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}}" responses.add(responses.POST, "https://api.securenative-stg.com/collector/api/v1/track", json=json.loads(expected), status=200) event_manager = EventManager(options) event_manager.start_event_persist() api_manager = ApiManager(event_manager, options) try: api_manager.track(self.event_options) finally: event_manager.stop_event_persist()
class SecureNative: _securenative = None def __init__(self, options): if Utils.is_null_or_empty(options.api_key): raise SecureNativeSDKException("You must pass your SecureNative api key") self._options = options self._event_manager = EventManager(self._options) if self._options.api_url: self._event_manager.start_event_persist() self._api_manager = ApiManager(self._event_manager, self._options) Logger.init_logger(self._options.log_level) def __exit__(self, exc_type, exc_val, exc_tb): self._event_manager.stop_event_persist() @classmethod def init_with_options(cls, options): if cls._securenative is None: cls._securenative = SecureNative(options) return cls._securenative else: Logger.debug('This SDK was already initialized.') raise SecureNativeSDKException(u'This SDK was already initialized.') @classmethod def init_with_api_key(cls, api_key): if Utils.is_null_or_empty(api_key): raise SecureNativeConfigException("You must pass your SecureNative api key") if cls._securenative is None: options = SecureNativeOptions(api_key=api_key) cls._securenative = SecureNative(options) return cls._securenative else: Logger.debug('This SDK was already initialized.') raise SecureNativeSDKException(u'This SDK was already initialized.') @classmethod def init(cls, resource_path=None): options = ConfigurationManager.load_config(resource_path) return cls.init_with_options(options) @classmethod def get_instance(cls): if not cls._securenative: raise SecureNativeSDKIllegalStateException() return cls._securenative def get_options(self): return self._options def track(self, event_options): return self._api_manager.track(event_options) def verify(self, event_options): return self._api_manager.verify(event_options) @classmethod def _flush(cls): cls._securenative = None def from_http_request(self, request): return SecureNativeContext.from_http_request(request, self._options) def verify_request_payload(self, request): request_signature = request.header[SignatureUtils.SignatureHeader] body = request.body SignatureUtils.is_valid_signature(self._options.api_key, body, request_signature)