1#! /usr/bin/python 2 3# Copyright 2014 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Unit tests for devices.py.""" 8 9import mox 10import unittest 11 12import common 13from fake_device_server import commands 14from fake_device_server import devices 15from fake_device_server import fail_control 16from fake_device_server import oauth 17from fake_device_server import resource_delegate 18from fake_device_server import server_errors 19 20 21class DevicesTest(mox.MoxTestBase): 22 """Tests for the Devices class.""" 23 24 def setUp(self): 25 """Sets up mox and a ticket / registration objects.""" 26 mox.MoxTestBase.setUp(self) 27 self.devices_resource = {} 28 self.fail_control = fail_control.FailControl() 29 self.oauth = oauth.OAuth(self.fail_control) 30 self.commands = commands.Commands(self.oauth, self.fail_control) 31 self.devices = devices.Devices( 32 resource_delegate.ResourceDelegate(self.devices_resource), 33 self.commands, 34 self.oauth, 35 self.fail_control) 36 37 38 def testCreateDevice(self): 39 """Tests that we can create a new device.""" 40 good_device_config = dict(userEmail='buffet@tasty.org', 41 name='buffet_device', 42 channel=dict(supportedType='xmpp')) 43 44 new_device = self.devices.create_device(None, good_device_config) 45 self.assertTrue('id' in new_device) 46 device_id = new_device['id'] 47 # New device should be registered with commands handler. 48 self.assertTrue(device_id in self.commands.device_commands) 49 50 bad_device_config = dict(name='buffet_device') 51 self.assertRaises(server_errors.HTTPError, 52 self.devices.create_device, None, bad_device_config) 53 54 55 def testGet(self): 56 """Tests that we can retrieve a device correctly.""" 57 self.devices_resource[(1234, None)] = dict(id=1234) 58 returned_json = self.devices.GET(1234) 59 self.assertEquals(returned_json, self.devices_resource[(1234, None)]) 60 61 # Non-existing device. 62 self.assertRaises(server_errors.HTTPError, 63 self.devices.GET, 1235) 64 65 66 def testListing(self): 67 """Tests that we can get a listing back correctly using the GET method. 68 """ 69 self.devices_resource[(1234, None)] = dict(id=1234) 70 self.devices_resource[(1235, None)] = dict(id=1235, boogity='taco') 71 72 returned_json = self.devices.GET() 73 self.assertEqual('clouddevices#devicesListResponse', 74 returned_json['kind']) 75 self.assertTrue('devices' in returned_json) 76 for device in self.devices_resource.values(): 77 self.assertIn(device, returned_json['devices']) 78 79 80 def testDeleteDevice(self): 81 """Tests that we correctly delete a device.""" 82 # Register device with commands handler first. 83 self.commands.new_device(12345) 84 self.devices_resource[(12345, None)] = dict(id=12345, nobody='care') 85 self.devices.DELETE(12345) 86 87 self.assertTrue(12345 not in self.devices_resource) 88 # Make sure the device is deleted from the command handler. 89 self.assertRaises(KeyError, self.commands.remove_device, 12345) 90 91 # Should error out if we try to delete something that doesn't exist. 92 self.assertRaises(server_errors.HTTPError, 93 self.devices.DELETE, 12500) 94 95 96if __name__ == '__main__': 97 unittest.main() 98