def test_authorize_object_already_created(self, mock_create): # the expires time is calculated from the current time and # a ttl value in the object. Fix the current time so we can # test expires is calculated correctly as expected self.addCleanup(timeutils.clear_time_override) timeutils.set_time_override() ttl = 10 expires = timeutils.utcnow_ts() + ttl db_dict = copy.deepcopy(fakes.fake_token_dict) db_dict['expires'] = expires mock_create.return_value = db_dict obj = token_obj.ConsoleAuthToken( context=self.context, console_type=fakes.fake_token_dict['console_type'], host=fakes.fake_token_dict['host'], port=fakes.fake_token_dict['port'], internal_access_path=fakes.fake_token_dict['internal_access_path'], instance_uuid=fakes.fake_token_dict['instance_uuid'], access_url_base=fakes.fake_token_dict['access_url_base'], ) obj.authorize(100) self.assertRaises(exception.ObjectActionError, obj.authorize, 100)
def _test_authorize(self, console_type, mock_create): # the expires time is calculated from the current time and # a ttl value in the object. Fix the current time so we can # test expires is calculated correctly as expected self.addCleanup(timeutils.clear_time_override) timeutils.set_time_override() ttl = 10 expires = timeutils.utcnow_ts() + ttl db_dict = copy.deepcopy(fakes.fake_token_dict) db_dict['expires'] = expires db_dict['console_type'] = console_type mock_create.return_value = db_dict create_dict = copy.deepcopy(fakes.fake_token_dict) create_dict['expires'] = expires create_dict['console_type'] = console_type del create_dict['id'] del create_dict['created_at'] del create_dict['updated_at'] expected = copy.deepcopy(fakes.fake_token_dict) del expected['token_hash'] del expected['expires'] expected['token'] = fakes.fake_token expected['console_type'] = console_type obj = token_obj.ConsoleAuthToken( context=self.context, console_type=console_type, host=fakes.fake_token_dict['host'], port=fakes.fake_token_dict['port'], internal_access_path=fakes.fake_token_dict['internal_access_path'], instance_uuid=fakes.fake_token_dict['instance_uuid'], access_url_base=fakes.fake_token_dict['access_url_base'], ) with mock.patch('uuid.uuid4', return_value=fakes.fake_token): token = obj.authorize(ttl) mock_create.assert_called_once_with(self.context, create_dict) self.assertEqual(token, fakes.fake_token) self.compare_obj(obj, expected) url = obj.access_url if console_type != 'novnc': expected_url = '%s?token=%s' % ( fakes.fake_token_dict['access_url_base'], fakes.fake_token) else: path = urlparse.urlencode({'path': '?token=%s' % fakes.fake_token}) expected_url = '%s?%s' % ( fakes.fake_token_dict['access_url_base'], path) self.assertEqual(expected_url, url)
def test_authorize_duplicate_token(self, mock_create): mock_create.side_effect = DBDuplicateEntry() obj = token_obj.ConsoleAuthToken( context=self.context, console_type=fakes.fake_token_dict['console_type'], host=fakes.fake_token_dict['host'], port=fakes.fake_token_dict['port'], internal_access_path=fakes.fake_token_dict['internal_access_path'], instance_uuid=fakes.fake_token_dict['instance_uuid'], access_url_base=fakes.fake_token_dict['access_url_base'], ) self.assertRaises(exception.TokenInUse, obj.authorize, 100)
def test_authorize_instance_not_found(self, mock_create): mock_create.side_effect = exception.InstanceNotFound( instance_id=fakes.fake_token_dict['instance_uuid']) obj = token_obj.ConsoleAuthToken( context=self.context, console_type=fakes.fake_token_dict['console_type'], host=fakes.fake_token_dict['host'], port=fakes.fake_token_dict['port'], internal_access_path=fakes.fake_token_dict['internal_access_path'], instance_uuid=fakes.fake_token_dict['instance_uuid'], access_url_base=fakes.fake_token_dict['access_url_base'], ) self.assertRaises(exception.InstanceNotFound, obj.authorize, 100)