diff --git a/castle/api/get_devices.py b/castle/api/get_devices.py new file mode 100644 index 0000000..4c48db9 --- /dev/null +++ b/castle/api/get_devices.py @@ -0,0 +1,8 @@ +from castle.api_request import APIRequest +from castle.commands.get_devices import CommandsGetDevices + + +class APIGetDevices(object): + @staticmethod + def retrieve(user_id): + return APIRequest().call(CommandsGetDevices.build(user_id)) diff --git a/castle/commands/get_devices.py b/castle/commands/get_devices.py new file mode 100644 index 0000000..eaa7919 --- /dev/null +++ b/castle/commands/get_devices.py @@ -0,0 +1,15 @@ +from castle.command import Command +from castle.validators.present import ValidatorsPresent + + +class CommandsGetDevices(object): + + @staticmethod + def build(user_id): + ValidatorsPresent.call({'user_id': user_id}, 'user_id') + + return Command( + method='get', + path="users/{user_id}/devices".format(user_id=user_id), + data=None + ) diff --git a/castle/test/__init__.py b/castle/test/__init__.py index 5f600f9..705d1f1 100644 --- a/castle/test/__init__.py +++ b/castle/test/__init__.py @@ -5,6 +5,7 @@ TEST_MODULES = [ + 'castle.test.api.get_devices_test', 'castle.test.api.review_test', 'castle.test.api_request_test', 'castle.test.client_id.extract_test', @@ -13,6 +14,7 @@ 'castle.test.commands.authenticate_test', 'castle.test.commands.identify_test', 'castle.test.commands.impersonate_test', + 'castle.test.commands.get_devices_test', 'castle.test.commands.review_test', 'castle.test.commands.track_test', 'castle.test.configuration_test', diff --git a/castle/test/api/get_devices_test.py b/castle/test/api/get_devices_test.py new file mode 100644 index 0000000..8854787 --- /dev/null +++ b/castle/test/api/get_devices_test.py @@ -0,0 +1,27 @@ +import json +import responses + +from castle.test import unittest +from castle.api.get_devices import APIGetDevices +from castle.configuration import configuration + + +class APIGetDevicesTestCase(unittest.TestCase): + def setUp(self): + configuration.api_secret = 'test' + + def tearDown(self): + configuration.api_secret = None + + @responses.activate + def test_retrieve(self): + # pylint: disable=line-too-long + response_text = "{\"total_count\":1,\"data\":[{\"token\":\"abcdefg12345\",\"risk\":0.0,\"created_at\":\"2018-06-15T16:36:22.916Z\",\"last_seen_at\":\"2018-07-19T23:09:29.681Z\",\"context\":{\"ip\":\"1.1.1.1\",\"user_agent\":{\"raw\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.51\",\"browser\":\"Opera\",\"version\":\"54.0.2952\",\"os\":\"Mac OS X 10.13.6\",\"mobile\":false,\"platform\":\"Mac OS X\",\"device\":\"Unknown\",\"family\":\"Opera\"},\"type\":\"desktop\"},\"is_current_device\":true}]}" + responses.add( + responses.GET, + 'https://api.castle.io/v1/users/1234/devices', + body=response_text, + status=200 + ) + user_id = '1234' + self.assertEqual(APIGetDevices.retrieve(user_id), json.loads(response_text)) diff --git a/castle/test/commands/get_devices_test.py b/castle/test/commands/get_devices_test.py new file mode 100644 index 0000000..5853211 --- /dev/null +++ b/castle/test/commands/get_devices_test.py @@ -0,0 +1,21 @@ +from castle.test import unittest +from castle.command import Command +from castle.commands.get_devices import CommandsGetDevices +from castle.errors import InvalidParametersError + + +def user_id(): + return '1234' + + +class CommandsGetDevicesTestCase(unittest.TestCase): + def test_build_no_user_id(self): + with self.assertRaises(InvalidParametersError): + CommandsGetDevices.build('') + + def test_build(self): + command = CommandsGetDevices.build(user_id()) + self.assertIsInstance(command, Command) + self.assertEqual(command.method, 'get') + self.assertEqual(command.path, 'users/1234/devices') + self.assertEqual(command.data, None)