From b88234eb763da63fc249f5109966c9b7ada4b0b7 Mon Sep 17 00:00:00 2001 From: marysieek Date: Mon, 16 Nov 2020 15:35:59 +0100 Subject: [PATCH 1/2] Add get device command --- castle/api/get_device.py | 8 ++++++++ castle/commands/get_device.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 castle/api/get_device.py create mode 100644 castle/commands/get_device.py diff --git a/castle/api/get_device.py b/castle/api/get_device.py new file mode 100644 index 0000000..7773dc5 --- /dev/null +++ b/castle/api/get_device.py @@ -0,0 +1,8 @@ +from castle.api_request import APIRequest +from castle.commands.get_devices import CommandsGetDevice + + +class APIGetDevice(object): + @staticmethod + def retrieve(device_token): + return APIRequest().call(CommandsGetDevice.build(device_token)) diff --git a/castle/commands/get_device.py b/castle/commands/get_device.py new file mode 100644 index 0000000..ab141a6 --- /dev/null +++ b/castle/commands/get_device.py @@ -0,0 +1,15 @@ +from castle.command import Command +from castle.validators.present import ValidatorsPresent + + +class CommandsGetDevices(object): + + @staticmethod + def build(device_token): + ValidatorsPresent.call({'device_token': device_token}, 'device_token') + + return Command( + method='get', + path="devices/{device_token}".format(device_token=device_token), + data=None + ) From 3e821e1262d85ccc9241350cdbfe64fa46d46289 Mon Sep 17 00:00:00 2001 From: marysieek Date: Mon, 16 Nov 2020 15:41:38 +0100 Subject: [PATCH 2/2] Add get device by device token --- castle/api/get_device.py | 2 +- castle/commands/get_device.py | 2 +- castle/test/__init__.py | 2 ++ castle/test/api/get_device_test.py | 27 +++++++++++++++++++++++++ castle/test/commands/get_device_test.py | 21 +++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 castle/test/api/get_device_test.py create mode 100644 castle/test/commands/get_device_test.py diff --git a/castle/api/get_device.py b/castle/api/get_device.py index 7773dc5..d6a1035 100644 --- a/castle/api/get_device.py +++ b/castle/api/get_device.py @@ -1,5 +1,5 @@ from castle.api_request import APIRequest -from castle.commands.get_devices import CommandsGetDevice +from castle.commands.get_device import CommandsGetDevice class APIGetDevice(object): diff --git a/castle/commands/get_device.py b/castle/commands/get_device.py index ab141a6..783bd77 100644 --- a/castle/commands/get_device.py +++ b/castle/commands/get_device.py @@ -2,7 +2,7 @@ from castle.validators.present import ValidatorsPresent -class CommandsGetDevices(object): +class CommandsGetDevice(object): @staticmethod def build(device_token): diff --git a/castle/test/__init__.py b/castle/test/__init__.py index 705d1f1..d11aa83 100644 --- a/castle/test/__init__.py +++ b/castle/test/__init__.py @@ -5,6 +5,7 @@ TEST_MODULES = [ + 'castle.test.api.get_device_test', 'castle.test.api.get_devices_test', 'castle.test.api.review_test', 'castle.test.api_request_test', @@ -14,6 +15,7 @@ 'castle.test.commands.authenticate_test', 'castle.test.commands.identify_test', 'castle.test.commands.impersonate_test', + 'castle.test.commands.get_device_test', 'castle.test.commands.get_devices_test', 'castle.test.commands.review_test', 'castle.test.commands.track_test', diff --git a/castle/test/api/get_device_test.py b/castle/test/api/get_device_test.py new file mode 100644 index 0000000..f5b94e4 --- /dev/null +++ b/castle/test/api/get_device_test.py @@ -0,0 +1,27 @@ +import json +import responses + +from castle.test import unittest +from castle.api.get_device import APIGetDevice +from castle.configuration import configuration + + +class APIGetDeviceTestCase(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 = "{\"id\":\"d_id\",\"manufacturer\":\"d_manufacturer\",\"model\":\"d_model\",\"name\":\"d_name\",\"type\":\"d_type\"}" + responses.add( + responses.GET, + 'https://api.castle.io/v1/devices/1234', + body=response_text, + status=200 + ) + device_token = '1234' + self.assertEqual(APIGetDevice.retrieve(device_token), json.loads(response_text)) diff --git a/castle/test/commands/get_device_test.py b/castle/test/commands/get_device_test.py new file mode 100644 index 0000000..80b87f5 --- /dev/null +++ b/castle/test/commands/get_device_test.py @@ -0,0 +1,21 @@ +from castle.test import unittest +from castle.command import Command +from castle.commands.get_device import CommandsGetDevice +from castle.errors import InvalidParametersError + + +def device_token(): + return '1234' + + +class CommandsGetDeviceTestCase(unittest.TestCase): + def test_build_no_device_token(self): + with self.assertRaises(InvalidParametersError): + CommandsGetDevice.build('') + + def test_build(self): + command = CommandsGetDevice.build(device_token()) + self.assertIsInstance(command, Command) + self.assertEqual(command.method, 'get') + self.assertEqual(command.path, 'devices/1234') + self.assertEqual(command.data, None)