def test_build_no_user_id(self): context = {} options = default_options() options.pop('user_id') with self.assertRaises(InvalidParametersError): CommandsAuthenticate(context).build(options)
def test_call_no_event(self): context = {} options = default_options() options.pop('event') with self.assertRaises(InvalidParametersError): CommandsAuthenticate(context).call(options)
def test_build_no_user_id(self): context = {} options = default_options() options.pop('user_id') expected = default_command_with_data(**options) self.assertEqual(CommandsAuthenticate(context).build(options), expected)
def test_build_active_false(self): payload = default_payload() payload.update(context={'active': False}) command = CommandsAuthenticate({}).build(payload) self.assertIsInstance(command, Command) self.assertEqual(command.method, 'post') self.assertEqual(command.endpoint, 'authenticate') self.assertEqual(command.data, payload)
def test_call_user_traits_allowed(self): context = {} options = default_options_plus(user_traits={'email': '*****@*****.**'}) options.update({'context': context}) expected = default_command_with_data(**options) self.assertEqual(CommandsAuthenticate(context).call(options), expected)
def test_call_properties_allowed(self): context = {} options = default_options_plus(properties={'test': '1'}) options.update({'context': context}) expected = default_command_with_data(**options) self.assertEqual(CommandsAuthenticate(context).call(options), expected)
def test_build_traits(self): payload = default_payload() payload.update(traits={'test': '1'}) command_data = default_payload() command_data.update(traits={'test': '1'}, context={}) command = CommandsAuthenticate({}).build(payload) self.assertIsInstance(command, Command) self.assertEqual(command.method, 'post') self.assertEqual(command.endpoint, 'authenticate') self.assertEqual(command.data, command_data)
def test_call(self): context = {'test': '1'} options = default_options_plus(context={'spam': True}) # expect the original context to have been merged with the context specified in the options expected_data = UtilsClone.call(options) expected_data.update(context={'test': '1', 'spam': True}) expected = default_command_with_data(**expected_data) self.assertEqual(CommandsAuthenticate(context).call(options), expected)
def test_call_risk_policy_allowed(self): context = {} risk_policy = { 'id': 'q-rbeMzBTdW2Fd09sbz55A', 'revision_id': 'pke4zqO2TnqVr-NHJOAHEg', 'name': 'Block Users from X', 'type': 'bot' } options = default_options_plus(risk_policy=risk_policy) expected = default_command_with_data(**options) self.assertEqual(CommandsAuthenticate(context).call(options), expected)
def authenticate(self, options): if self.tracked(): try: response = self.api.call( CommandsAuthenticate(self.context).build(options)) response.update(failover=False, failover_reason=None) return response except InternalServerError as exception: return Client.failover(options, exception) else: return FailoverResponse(options['user_id'], 'allow', 'Castle set to do not track.').call()
def authenticate(self, options): if self.tracked(): self._add_timestamp_if_necessary(options) command = CommandsAuthenticate(self.context).build(options) try: response = self.api.call(command) response.update(failover=False, failover_reason=None) return response except (RequestError, InternalServerError) as exception: return Client.failover_response_or_raise(options, exception) else: return FailoverResponse(options.get('user_id'), 'allow', 'Castle set to do not track.').call()
def test_init(self): context = mock.sentinel.test_init_context obj = CommandsAuthenticate(context) self.assertEqual(obj.context, context)
def test_build_context(self): context = {'test': '1'} self.assertEqual(CommandsAuthenticate({}).build_context(context), context)
def test_build_no_user_id(self): payload = {'event': '$login.authenticate'} with self.assertRaises(InvalidParametersError): CommandsAuthenticate({}).build(payload)
def test_build_no_event(self): payload = {'user_id': '1234'} with self.assertRaises(InvalidParametersError): CommandsAuthenticate({}).build(payload)
def test_init(self): self.assertIsInstance(CommandsAuthenticate({}).context_merger, ContextMerger)