Exemplo n.º 1
0
    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 test_should_send_sync_event_and_fail_when_status_code_500(self):
        options = SecureNativeOptions(
            api_key="YOUR_API_KEY",
            api_url="https://api.securenative-stg.com/collector/api/v1")

        responses.add(
            responses.POST,
            "https://api.securenative-stg.com/collector/api/v1/some-path/to-api",
            json={},
            status=500)
        event_manager = EventManager(options)

        res = event_manager.send_sync(self.event, "some-path/to-api")

        self.assertEqual(res.status_code, 500)
    def test_should_successfully_send_sync_event_with_status_code_200(self):
        options = SecureNativeOptions(
            api_key="YOUR_API_KEY",
            api_url="https://api.securenative-stg.com/collector/api/v1")

        res_body = "{\"data\": true}"
        responses.add(
            responses.POST,
            "https://api.securenative-stg.com/collector/api/v1/some-path/to-api",
            json=json.loads(res_body),
            status=200)
        event_manager = EventManager(options)

        data = event_manager.send_sync(self.event, "some-path/to-api")
        self.assertEqual(res_body, data.text)
Exemplo n.º 4
0
    def test_should_timeout_on_post(self):
        options = SecureNativeOptions(api_key="YOUR_API_KEY", auto_send=True, timeout=0.000001,
                                      api_url="https://api.securenative-stg.com/collector/api/v1")

        responses.add(responses.POST, "https://api.securenative-stg.com/collector/api/v1/verify",
                      json={"event": "SOME_EVENT_NAME"}, status=408)

        event_manager = EventManager(options)
        event_manager.start_event_persist()
        api_manager = ApiManager(event_manager, options)

        verify_result = VerifyResult(RiskLevel.LOW.value, 0, [])
        res = api_manager.verify(self.event_options)

        self.assertEqual(res.risk_level, verify_result.risk_level)
        self.assertEqual(res.score, verify_result.score)
        self.assertEqual(res.triggers, verify_result.triggers)
Exemplo n.º 5
0
    def test_verify_event(self):
        options = SecureNativeOptions(api_key="YOUR_API_KEY",
                                      api_url="https://api.securenative-stg.com/collector/api/v1")

        responses.add(responses.POST, "https://api.securenative-stg.com/collector/api/v1/verify",
                      json={
                          "riskLevel": "low",
                          "score": 0,
                          "triggers": None
                      }, status=200)
        verify_result = VerifyResult(RiskLevel.LOW, 0, None)

        event_manager = EventManager(options)
        event_manager.start_event_persist()
        api_manager = ApiManager(event_manager, options)

        result = api_manager.verify(self.event_options)

        self.assertIsNotNone(result)
        self.assertEqual(result.risk_level, verify_result.risk_level.value)
        self.assertEqual(result.score, verify_result.score)
        self.assertEqual(result.triggers, verify_result.triggers)
Exemplo n.º 6
0
    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()
Exemplo n.º 7
0
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)