def test_create(self): with mock.patch.object(self.dbapi, 'create_registry', autospec=True) as mock_create_registry: mock_create_registry.return_value = self.fake_registry registry_dict = dict(self.fake_registry) registry = objects.Registry(self.context, **registry_dict) registry.create(self.context) mock_create_registry.assert_called_once_with( self.context, self.fake_registry) self.assertEqual(self.context, registry._context)
def get_test_registry(context, **kwargs): """Return a test registry object with appropriate attributes. NOTE: The object leaves the attributes marked as changed, such that a create() could be used to commit it to the DB. """ db_registry = db_utils.get_test_registry(**kwargs) registry = objects.Registry(context) for key in db_registry: setattr(registry, key, db_registry[key]) return registry
def test_get_all_registries_with_unknown_parameter(self, mock_registry_list): test_registry = utils.get_test_registry() registries = [objects.Registry(self.context, **test_registry)] mock_registry_list.return_value = registries response = self.get('/v1/registries/?unknown=fake-name', expect_errors=True) mock_registry_list.assert_not_called() self.assertEqual(400, response.status_int) self.assertEqual('application/json', response.content_type) self.assertEqual("Unknown parameters: unknown", response.json['errors'][0]['detail'])
def test_get_all_registries_with_filter(self, mock_registry_list): test_registry = utils.get_test_registry() registries = [objects.Registry(self.context, **test_registry)] mock_registry_list.return_value = registries response = self.get('/v1/registries/?name=fake-name') mock_registry_list.assert_called_once_with( mock.ANY, 1000, None, 'id', 'asc', filters={'name': 'fake-name'}) self.assertEqual(200, response.status_int) actual_registries = response.json['registries'] self.assertEqual(1, len(actual_registries)) self.assertEqual(test_registry['uuid'], actual_registries[0].get('uuid'))
def post(self, run=False, **registry_dict): context = pecan.request.context policy_action = policies.REGISTRY % 'create' policy.enforce(context, policy_action, action=policy_action) registry_dict = registry_dict.get(RESOURCE_NAME) registry_dict['project_id'] = context.project_id registry_dict['user_id'] = context.user_id new_registry = objects.Registry(context, **registry_dict) new_registry.create(context) # Set the HTTP Location Header pecan.response.location = link.build_url(COLLECTION_NAME, new_registry.uuid) pecan.response.status = 201 return RegistryItem.render_response(new_registry)
def test_get_all_registries_with_pagination_marker(self, mock_registry_list): registry_list = [] for id_ in range(4): test_registry = utils.create_test_registry( id=id_, uuid=uuidutils.generate_uuid(), name='registry' + str(id_), context=self.context) registry_list.append( objects.Registry(self.context, **test_registry)) mock_registry_list.return_value = registry_list[-1:] response = self.get('/v1/registries/?limit=3&marker=%s' % registry_list[2].uuid) self.assertEqual(200, response.status_int) actual_registries = response.json['registries'] self.assertEqual(1, len(actual_registries)) self.assertEqual(registry_list[-1].uuid, actual_registries[0].get('uuid'))
def test_get_all_registries_all_projects(self, mock_registry_list, mock_policy): mock_policy.return_value = True test_registry = utils.get_test_registry() registries = [objects.Registry(self.context, **test_registry)] mock_registry_list.return_value = registries response = self.get('/v1/registries/?all_projects=1') mock_registry_list.assert_called_once_with(mock.ANY, 1000, None, 'id', 'asc', filters={}) context = mock_registry_list.call_args[0][0] self.assertIs(True, context.all_projects) self.assertEqual(200, response.status_int) actual_registries = response.json['registries'] self.assertEqual(1, len(actual_registries)) self.assertEqual(test_registry['uuid'], actual_registries[0].get('uuid'))