Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions castle/api/get_devices.py
Original file line number Diff line number Diff line change
@@ -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))
15 changes: 15 additions & 0 deletions castle/commands/get_devices.py
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 2 additions & 0 deletions castle/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
27 changes: 27 additions & 0 deletions castle/test/api/get_devices_test.py
Original file line number Diff line number Diff line change
@@ -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))
21 changes: 21 additions & 0 deletions castle/test/commands/get_devices_test.py
Original file line number Diff line number Diff line change
@@ -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)