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 registration_tickets.py.""" 8 9 import mox 10 import unittest 11 12 import common 13 from fake_device_server import common_util 14 from fake_device_server import commands 15 from fake_device_server import devices 16 from fake_device_server import fail_control 17 from fake_device_server import oauth 18 from fake_device_server import registration_tickets 19 from fake_device_server import resource_delegate 20 from fake_device_server import server_errors 21 22 23 class RegistrationTicketsTest(mox.MoxTestBase): 24 """Tests for the RegistrationTickets class.""" 25 26 def setUp(self): 27 """Sets up mox and a ticket / registration objects.""" 28 mox.MoxTestBase.setUp(self) 29 self.tickets = {} 30 self.devices_resource = {} 31 self.fail_control = fail_control.FailControl() 32 self.oauth = oauth.OAuth(self.fail_control) 33 self.commands = commands.Commands(self.oauth, self.fail_control) 34 self.devices = devices.Devices( 35 resource_delegate.ResourceDelegate(self.devices_resource), 36 self.commands, 37 self.oauth, 38 self.fail_control) 39 40 self.registration = registration_tickets.RegistrationTickets( 41 resource_delegate.ResourceDelegate(self.tickets), self.devices, 42 self.fail_control) 43 44 45 def testFinalize(self): 46 """Tests that the finalize workflow does the right thing.""" 47 # Unclaimed ticket 48 self.tickets[(1234, None)] = dict(id=1234) 49 self.assertRaises(server_errors.HTTPError, 50 self.registration.POST, 1234, 'finalize') 51 52 # Claimed ticket 53 expected_ticket = dict( 54 id=1234, userEmail='buffet (at] tasty.org', 55 deviceDraft=dict(name='buffet_device', 56 channel=dict(supportedType='xmpp'))) 57 self.tickets[(1234, None)] = expected_ticket 58 returned_json = self.registration.POST(1234, 'finalize') 59 self.assertEquals(returned_json['id'], expected_ticket['id']) 60 self.assertEquals(returned_json['userEmail'], 61 expected_ticket['userEmail']) 62 self.assertIn('robotAccountEmail', returned_json) 63 self.assertIn('robotAccountAuthorizationCode', returned_json) 64 65 66 def testInsert(self): 67 """Tests that we can create a new ticket.""" 68 self.mox.StubOutWithMock(common_util, 'parse_serialized_json') 69 common_util.parse_serialized_json().AndReturn({'userEmail': 'me'}) 70 self.mox.StubOutWithMock(common_util, 'grab_header_field') 71 common_util.grab_header_field('Authorization').AndReturn( 72 'Bearer %s' % self.registration.TEST_ACCESS_TOKEN) 73 self.mox.ReplayAll() 74 returned_json = self.registration.POST() 75 self.assertIn('id', returned_json) 76 self.mox.VerifyAll() 77 78 79 def testGet(self): 80 """Tests that we can retrieve a ticket correctly.""" 81 self.tickets[(1234, None)] = dict(id=1234) 82 returned_json = self.registration.GET(1234) 83 self.assertEquals(returned_json, self.tickets[(1234, None)]) 84 85 # Non-existing ticket. 86 self.assertRaises(server_errors.HTTPError, 87 self.registration.GET, 1235) 88 89 90 def testPatchTicket(self): 91 """Tests that we correctly patch a ticket.""" 92 expected_ticket = dict(id=1234, blah='hi') 93 update_ticket = dict(blah='hi') 94 self.tickets[(1234, None)] = dict(id=1234) 95 96 self.mox.StubOutWithMock(common_util, 'parse_serialized_json') 97 98 common_util.parse_serialized_json().AndReturn(update_ticket) 99 100 self.mox.ReplayAll() 101 returned_json = self.registration.PATCH(1234) 102 self.assertEquals(expected_ticket, returned_json) 103 self.mox.VerifyAll() 104 105 106 def testReplaceTicket(self): 107 """Tests that we correctly replace a ticket.""" 108 update_ticket = dict(id=12345, blah='hi') 109 self.tickets[(12345, None)] = dict(id=12345) 110 111 self.mox.StubOutWithMock(common_util, 'parse_serialized_json') 112 113 common_util.parse_serialized_json().AndReturn(update_ticket) 114 115 self.mox.ReplayAll() 116 returned_json = self.registration.PUT(12345) 117 self.assertEquals(update_ticket, returned_json) 118 self.mox.VerifyAll() 119 120 self.mox.ResetAll() 121 122 # Ticket id doesn't match. 123 update_ticket = dict(id=12346, blah='hi') 124 common_util.parse_serialized_json().AndReturn(update_ticket) 125 126 self.mox.ReplayAll() 127 self.assertRaises(server_errors.HTTPError, 128 self.registration.PUT, 12345) 129 self.mox.VerifyAll() 130 131 132 if __name__ == '__main__': 133 unittest.main() 134