Skip to content

Commit 92a537a

Browse files
author
Maria Korlotian
authored
Get device by device token (#89)
1 parent 8771e77 commit 92a537a

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

castle/api/get_device.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from castle.api_request import APIRequest
2+
from castle.commands.get_device import CommandsGetDevice
3+
4+
5+
class APIGetDevice(object):
6+
@staticmethod
7+
def retrieve(device_token):
8+
return APIRequest().call(CommandsGetDevice.build(device_token))

castle/commands/get_device.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from castle.command import Command
2+
from castle.validators.present import ValidatorsPresent
3+
4+
5+
class CommandsGetDevice(object):
6+
7+
@staticmethod
8+
def build(device_token):
9+
ValidatorsPresent.call({'device_token': device_token}, 'device_token')
10+
11+
return Command(
12+
method='get',
13+
path="devices/{device_token}".format(device_token=device_token),
14+
data=None
15+
)

castle/test/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
TEST_MODULES = [
8+
'castle.test.api.get_device_test',
89
'castle.test.api.get_devices_test',
910
'castle.test.api.review_test',
1011
'castle.test.api_request_test',
@@ -14,6 +15,7 @@
1415
'castle.test.commands.authenticate_test',
1516
'castle.test.commands.identify_test',
1617
'castle.test.commands.impersonate_test',
18+
'castle.test.commands.get_device_test',
1719
'castle.test.commands.get_devices_test',
1820
'castle.test.commands.review_test',
1921
'castle.test.commands.track_test',

castle/test/api/get_device_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
import responses
3+
4+
from castle.test import unittest
5+
from castle.api.get_device import APIGetDevice
6+
from castle.configuration import configuration
7+
8+
9+
class APIGetDeviceTestCase(unittest.TestCase):
10+
def setUp(self):
11+
configuration.api_secret = 'test'
12+
13+
def tearDown(self):
14+
configuration.api_secret = None
15+
16+
@responses.activate
17+
def test_retrieve(self):
18+
# pylint: disable=line-too-long
19+
response_text = "{\"id\":\"d_id\",\"manufacturer\":\"d_manufacturer\",\"model\":\"d_model\",\"name\":\"d_name\",\"type\":\"d_type\"}"
20+
responses.add(
21+
responses.GET,
22+
'https://api.castle.io/v1/devices/1234',
23+
body=response_text,
24+
status=200
25+
)
26+
device_token = '1234'
27+
self.assertEqual(APIGetDevice.retrieve(device_token), json.loads(response_text))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from castle.test import unittest
2+
from castle.command import Command
3+
from castle.commands.get_device import CommandsGetDevice
4+
from castle.errors import InvalidParametersError
5+
6+
7+
def device_token():
8+
return '1234'
9+
10+
11+
class CommandsGetDeviceTestCase(unittest.TestCase):
12+
def test_build_no_device_token(self):
13+
with self.assertRaises(InvalidParametersError):
14+
CommandsGetDevice.build('')
15+
16+
def test_build(self):
17+
command = CommandsGetDevice.build(device_token())
18+
self.assertIsInstance(command, Command)
19+
self.assertEqual(command.method, 'get')
20+
self.assertEqual(command.path, 'devices/1234')
21+
self.assertEqual(command.data, None)

0 commit comments

Comments
 (0)