Example #1
0
    def post(self, *args, **kwargs):
        self.form = None
        self.ci = None
        if self.Form:
            self.form = self.Form(self.request.POST, **self.form_options)
            if self.form.is_valid():
                model = self.form.save()
                if not model.content_object:
                    model.uid = "%s-%s" % ("mm", model.id)
                    model.save(user=self.request.user)
                model.owners.clear()
                model.layers.clear()
                layers = self.form.data.getlist("base-layers")
                for layer in layers:
                    model.layers.add(CILayer.objects.get(pk=int(layer[0])))

                owners_t = self.form.data.getlist("base-technical_owners")
                for owner in owners_t:
                    own = CIOwnership(ci=model, owner=CIOwner.objects.get(pk=owner[0]), type=1)
                    own.save()
                owners_b = self.form.data.getlist("base-business_owners")
                for owner in owners_b:
                    own = CIOwnership(ci=model, owner=CIOwner.objects.get(pk=owner[0]), type=2)
                    own.save()
                messages.success(self.request, _("Changes saved."))
                return HttpResponseRedirect("/cmdb/ci/edit/" + str(model.id))
            else:
                messages.error(self.request, _("Correct the errors."))

        return super(Add, self).get(*args, **kwargs)
Example #2
0
 def post(self, *args, **kwargs):
     self.initialize_vars()
     ci_id = self.kwargs.get('ci_id')
     if ci_id:
         self.ci = get_object_or_404(db.CI, id=ci_id)
         self.form_options['instance'] = self.ci
         self.form = self.Form(
             self.request.POST, **self.form_options
         )
         self.form_attributes = EditAttributeFormFactory(
             ci=self.ci).factory(
                 self.request.POST,
                 **self.form_attributes_options
             )
         if self.form.is_valid() and self.form_attributes.is_valid():
             model = self.form.save(commit=False)
             model.id = self.ci.id
             model.owners.clear()
             model.layers.clear()
             layers = self.form_attributes.data.getlist('base-layers')
             for layer in layers:
                 model.layers.add(CILayer.objects.get(pk=int(layer)))
             owners_t = self.form_attributes.data.getlist(
                 'base-technical_owners')
             for owner in owners_t:
                 own = CIOwnership(
                     ci=model,
                     owner=CIOwner.objects.get(pk=owner),
                     type=1,)
                 own.save()
             owners_b = self.form_attributes.data.getlist(
                 'base-business_owners')
             for owner in owners_b:
                 own = CIOwnership(
                     ci=model, owner=CIOwner.objects.get(pk=owner),
                     type=2,)
                 own.save()
             model.save(user=self.request.user)
             self.form_attributes.ci = model
             self.form_attributes.save()
             messages.success(self.request, "Changes saved.")
             return HttpResponseRedirect(self.request.path)
         else:
             messages.error(self.request, "Correct the errors.")
     return super(Edit, self).get(*args, **kwargs)
Example #3
0
File: api.py Project: smagowr/ralph
    def hydrate_m2m(self, bundle):
        # Managing m2m
        classes = {'layers': CILayer, 'owners': CIOwner}

        # Usual M2M
        if field in bundle.data:
            m2m_objects = []
            for entry in bundle.data[field]:
                m2m_obj = classes[field].objects.filter(pk=entry['id'])
                if m2m_obj:
                    m2m_obj = m2m_obj[0]
                else:
                    m2m_obj = classes[field](name=entry['name'])
                    m2m_obj.save()
                m2m_objects.append(m2m_obj)

            setattr(bundle.obj, field, m2m_objects)

        # owners is M2M using Intermediary model
        for field in ('business_owners', 'technical_owners'):
            m2m_objects = []
            if field in bundle.data:
                for entry in bundle.data[field]:
                    if 'id' in entry and entry['id'] and CIOwner.objects.filter(
                            pk=entry['id']).count() == 1:
                        m2m_obj = CIOwner.objects.get(pk=entry['id'])
                    else:
                        first_name = entry.get('first_name', '')
                        last_name = entry.get('last_name', '')
                        email = entry.get('email', '')
                        m2m_obj = CIOwner(first_name=first_name,
                                          last_name=last_name,
                                          email=email)
                        m2m_obj.save()
                    m2m_objects.append(m2m_obj)

            for m2m_obj in m2m_objects:
                owner_type = getattr(CIOwnershipType,
                                     field.replace("_owners", ""), "business")
                ownership = CIOwnership(ci=bundle.obj,
                                        owner=m2m_obj,
                                        type=owner_type)
                ownership.save()
        return bundle
Example #4
0
    def hydrate_m2m(self, bundle):
        # Managing m2m
        classes = {'layers': CILayer, 'owners': CIOwner}

        # Usual M2M
        if field in bundle.data:
            m2m_objects = []
            for entry in bundle.data[field]:
                m2m_obj = classes[field].objects.filter(pk=entry['id'])
                if m2m_obj:
                    m2m_obj = m2m_obj[0]
                else:
                    m2m_obj = classes[field](name=entry['name'])
                    m2m_obj.save()
                m2m_objects.append(m2m_obj)

            setattr(bundle.obj, field, m2m_objects)

        # owners is M2M using Intermediary model
        for field in ('business_owners', 'technical_owners'):
            m2m_objects = []
            if field in bundle.data:
                for entry in bundle.data[field]:
                    if 'id' in entry and entry['id'] and CIOwner.objects.filter(pk=entry['id']).count() == 1:
                        m2m_obj = CIOwner.objects.get(pk=entry['id'])
                    else:
                        first_name = entry.get('first_name', '')
                        last_name = entry.get('last_name', '')
                        email = entry.get('email', '')
                        m2m_obj = CIOwner(first_name=first_name, last_name=last_name, email=email)
                        m2m_obj.save()
                    m2m_objects.append(m2m_obj)

            for m2m_obj in m2m_objects:
                owner_type = getattr(CIOwnershipType, field.replace("_owners", ""), "business")
                ownership = CIOwnership(ci=bundle.obj, owner=m2m_obj, type=owner_type)
                ownership.save()
        return bundle
Example #5
0
class CMDBApiTest(TestCase):
    def setUp(self):
        self.user = create_user('api_user', '*****@*****.**', 'password')
        self.layers = CILayer.objects.all()
        self.types = CIType.objects.all()
        self.create_owners()
        self.create_cis()
        self.create_ownerships()
        self.create_relations()
        self.data = {
            'format': 'json',
            'username': self.user.username,
            'api_key': self.user.api_key.key
        }
        cache.delete("api_user_accesses")

    def create_owners(self):
        self.owner1 = CIOwner(
            first_name='first_name_owner1',
            last_name='last_name_owner1',
            email='*****@*****.**',
        )
        self.owner1.save()
        self.owner2 = CIOwner(
            first_name='first_name_owner2',
            last_name='last_name_owner2',
            email='*****@*****.**',
        )
        self.owner2.save()

    def create_cis(self):
        self.ci1 = CI(
            uid='uid-ci1',
            type=self.types[0],
            barcode='barcodeci1',
            name='ciname1',
        )
        self.ci1.save()
        self.ci1.layers = [self.layers[0].id, self.layers[1].id]
        self.ci1.save()
        self.ci2 = CI(
            uid='uid-ci2',
            type=self.types[1],
            barcode='barcodeci2',
            name='ciname2',
        )
        self.ci2.save()
        self.ci2.layers = [self.layers[0].id]
        self.ci2.save()
        self.ci3 = CI(
            uid='other-ci3',
            type=self.types[1],
            barcode='otherbarcodeci3',
            name='otherci',
        )
        self.ci3.save()
        self.ci3.layers = [self.layers[1].id]
        self.ci3.save()

    def create_ownerships(self):
        self.ciownership1 = CIOwnership(
            ci=self.ci1,
            owner=self.owner1,
            type=CIOwnershipType.technical,
        )
        self.ciownership1.save()
        self.ciownership2 = CIOwnership(
            ci=self.ci1,
            owner=self.owner2,
            type=CIOwnershipType.business,
        )
        self.ciownership2.save()
        self.ciownership3 = CIOwnership(
            ci=self.ci2,
            owner=self.owner2,
            type=CIOwnershipType.business,
        )
        self.ciownership3.save()

    def create_relations(self):
        self.relation1 = CIRelation(
            parent=self.ci1,
            child=self.ci2,
            type=CI_RELATION_TYPES.CONTAINS,
        )
        self.relation1.save()
        self.relation2 = CIRelation(
            parent=self.ci2,
            child=self.ci3,
            type=CI_RELATION_TYPES.HASROLE,
        )
        self.relation2.save()

    def test_layers(self):
        path = "/api/v0.9/cilayers/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [
            ci_layer['resource_uri'] for ci_layer in json_data['objects']
        ]

        response = self.client.get(
            path=resource_uris[0],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.layers[0].name)

        response = self.client.get(
            path=resource_uris[1],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.layers[1].name)

    def test_types(self):
        path = "/api/v0.9/citypes/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [
            ci_type['resource_uri'] for ci_type in json_data['objects']
        ]

        response = self.client.get(
            path=resource_uris[0],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)

        self.assertEqual(json_data['name'], self.types[0].name)

        response = self.client.get(
            path=resource_uris[1],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.types[1].name)

    def test_ci(self):
        path = "/api/v0.9/ci/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci['resource_uri'] for ci in json_data['objects']]

        response = self.client.get(
            path=resource_uris[0],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['layers'][1]['name'], self.layers[1].name)
        self.assertEqual(json_data['barcode'], self.ci1.barcode)
        self.assertEqual(json_data['name'], self.ci1.name)
        self.assertEqual(json_data['type']['name'], self.ci1.type.name)
        self.assertEqual(json_data['uid'], self.ci1.uid)
        self.assertEqual(
            json_data['technical_owners'][0]['username'],
            '{}.{}'.format(self.owner1.first_name, self.owner1.last_name))
        self.assertEqual(
            json_data['business_owners'][0]['username'],
            '{}.{}'.format(self.owner2.first_name, self.owner2.last_name))

        response = self.client.get(
            path=resource_uris[1],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['barcode'], self.ci2.barcode)
        self.assertEqual(json_data['name'], self.ci2.name)
        self.assertEqual(json_data['type']['name'], self.ci2.type.name)
        self.assertEqual(json_data['uid'], self.ci2.uid)
        self.assertFalse(json_data['technical_owners'])
        self.assertEqual(
            json_data['business_owners'][0]['username'],
            '{}.{}'.format(self.owner2.first_name, self.owner2.last_name))

    def test_relations(self):
        path = "/api/v0.9/cirelation/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [
            ci_relation['resource_uri'] for ci_relation in json_data['objects']
        ]

        response = self.client.get(
            path=resource_uris[0],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['parent'], self.ci1.id)
        self.assertEqual(json_data['child'], self.ci2.id)
        self.assertEqual(json_data['type'], CI_RELATION_TYPES.CONTAINS)

        response = self.client.get(
            path=resource_uris[1],
            data=self.data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['parent'], self.ci2.id)
        self.assertEqual(json_data['child'], self.ci3.id)
        self.assertEqual(json_data['type'], CI_RELATION_TYPES.HASROLE)

    def test_ci_filter_exact(self):
        path = "/api/v0.9/ci/"
        data = self.data.copy()
        data['name__exact'] = 'otherci'
        response = self.client.get(path=path, data=data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci['resource_uri'] for ci in json_data['objects']]
        self.assertEqual(len(resource_uris), 1)
        response = self.client.get(
            path=resource_uris[0],
            data=data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[1].name)
        self.assertEqual(json_data['barcode'], self.ci3.barcode)
        self.assertEqual(json_data['name'], self.ci3.name)
        self.assertEqual(json_data['type']['name'], self.ci3.type.name)
        self.assertEqual(json_data['uid'], self.ci3.uid)

    def test_ci_filter_startswith(self):
        data = self.data.copy()
        path = "/api/v0.9/ci/"
        data['name__startswith'] = 'ciname'
        response = self.client.get(path=path, data=data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci['resource_uri'] for ci in json_data['objects']]
        self.assertEqual(len(resource_uris), 2)
        response = self.client.get(
            path=resource_uris[0],
            data=data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['barcode'], self.ci1.barcode)
        self.assertEqual(json_data['name'], self.ci1.name)
        self.assertEqual(json_data['type']['name'], self.ci1.type.name)
        self.assertEqual(json_data['uid'], self.ci1.uid)

        response = self.client.get(
            path=resource_uris[1],
            data=data,
            format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['barcode'], self.ci2.barcode)
        self.assertEqual(json_data['name'], self.ci2.name)
        self.assertEqual(json_data['type']['name'], self.ci2.type.name)
        self.assertEqual(json_data['uid'], self.ci2.uid)
Example #6
0
class CMDBApiTest(TestCase):
    def setUp(self):
        self.create_user()
        self.create_cilayers()
        self.create_citypes()
        self.create_owners()
        self.create_cis()
        self.create_ownerships()
        self.create_relations()

    def create_user(self):
        self.user = User.objects.create_user(
            'api_user',
            '*****@*****.**',
            'password'
        )
        self.user.save()
        self.api_key = ApiKey.objects.get(user=self.user)
        self.data = {
            'format': 'json',
            'username': self.user.username,
            'api_key': self.api_key.key
        }
        cache.delete("api_user_accesses")

    def create_cilayers(self):
        self.cilayer1 = CILayer(name='layer1')
        self.cilayer1.save()
        self.cilayer2 = CILayer(name='layer2')
        self.cilayer2.save()

    def create_citypes(self):
        self.citype1 = CIType(name='type1')
        self.citype1.save()
        self.citype2 = CIType(name='type2')
        self.citype2.save()

    def create_owners(self):
        self.owner1 = CIOwner(
            first_name='first_name_owner1',
            last_name='last_name_owner1',
            email='*****@*****.**'
        )
        self.owner1.save()
        self.owner2 = CIOwner(
            first_name='first_name_owner2',
            last_name='last_name_owner2',
            email='*****@*****.**'
        )
        self.owner2.save()

    def create_cis(self):
        self.ci1 = CI(
            uid='uid-ci1',
            type=self.citype1,
            barcode='barcodeci1',
            name='ciname1',
        )
        self.ci1.save()
        self.ci1.layers = [self.cilayer1, self.cilayer2]
        self.ci1.save()
        self.ci2 = CI(
            uid='uid-ci2',
            type=self.citype2,
            barcode='barcodeci2',
            name='ciname2',
        )
        self.ci2.save()
        self.ci2.layers = [self.cilayer1]
        self.ci2.save()
        self.ci3 = CI(
            uid='other-ci3',
            type=self.citype2,
            barcode='otherbarcodeci3',
            name='otherci',
        )
        self.ci3.save()
        self.ci3.layers = [self.cilayer2]
        self.ci3.save()

    def create_ownerships(self):
        self.ciownership1 = CIOwnership(
            ci=self.ci1,
            owner=self.owner1,
            type=CIOwnershipType.technical
        )
        self.ciownership1.save()
        self.ciownership2 = CIOwnership(
            ci=self.ci1,
            owner=self.owner2,
            type=CIOwnershipType.business
        )
        self.ciownership2.save()
        self.ciownership3 = CIOwnership(
            ci=self.ci2,
            owner=self.owner2,
            type=CIOwnershipType.business
        )
        self.ciownership3.save()

    def create_relations(self):
        self.relation1 = CIRelation(
            parent=self.ci1,
            child=self.ci2,
            type=CI_RELATION_TYPES.CONTAINS,
        )
        self.relation1.save()
        self.relation2 = CIRelation(
            parent=self.ci2,
            child=self.ci3,
            type=CI_RELATION_TYPES.HASROLE,
        )
        self.relation2.save()

    def test_layers(self):
        path = "/api/v0.9/cilayers/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [x['resource_uri'] for x in json_data['objects']]

        response = self.client.get(
            path=resource_uris[0], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.cilayer1.name)

        response = self.client.get(
            path=resource_uris[1], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.cilayer2.name)

    def test_types(self):
        path = "/api/v0.9/citypes/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [x['resource_uri'] for x in json_data['objects']]

        response = self.client.get(
            path=resource_uris[0], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.citype1.name)

        response = self.client.get(
            path=resource_uris[1], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.citype2.name)

    def test_ci(self):
        path = "/api/v0.9/ci/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [x['resource_uri'] for x in json_data['objects']]

        response = self.client.get(
            path=resource_uris[0], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name)
        self.assertEqual(json_data['layers'][1]['name'], self.cilayer2.name)
        self.assertEqual(json_data['barcode'], self.ci1.barcode)
        self.assertEqual(json_data['name'], self.ci1.name)
        self.assertEqual(json_data['type']['name'], self.ci1.type.name)
        self.assertEqual(json_data['uid'], self.ci1.uid)
        self.assertEqual(
            json_data['technical_owners'][0]['username'],
            '{}.{}'.format(self.owner1.first_name, self.owner1.last_name)
        )
        self.assertEqual(
            json_data['business_owners'][0]['username'],
            '{}.{}'.format(self.owner2.first_name, self.owner2.last_name)
        )

        response = self.client.get(
            path=resource_uris[1], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name)
        self.assertEqual(json_data['barcode'], self.ci2.barcode)
        self.assertEqual(json_data['name'], self.ci2.name)
        self.assertEqual(json_data['type']['name'], self.ci2.type.name)
        self.assertEqual(json_data['uid'], self.ci2.uid)
        self.assertFalse(json_data['technical_owners'])
        self.assertEqual(
            json_data['business_owners'][0]['username'],
            '{}.{}'.format(self.owner2.first_name, self.owner2.last_name)
        )

    def test_relations(self):
        path = "/api/v0.9/cirelation/"
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [x['resource_uri'] for x in json_data['objects']]

        response = self.client.get(
            path=resource_uris[0], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['parent'], self.ci1.id)
        self.assertEqual(json_data['child'], self.ci2.id)
        self.assertEqual(json_data['type'], CI_RELATION_TYPES.CONTAINS)

        response = self.client.get(
            path=resource_uris[1], data=self.data, format='json',
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['parent'], self.ci2.id)
        self.assertEqual(json_data['child'], self.ci3.id)
        self.assertEqual(json_data['type'], CI_RELATION_TYPES.HASROLE)

    def test_ci_filter_exact(self):
        path = "/api/v0.9/ci/"
        self.data['name__exact'] = 'otherci'
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [x['resource_uri'] for x in json_data['objects']]
        self.assertEqual(len(resource_uris), 1)
        response = self.client.get(
            path=resource_uris[0], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.cilayer2.name)
        self.assertEqual(json_data['barcode'], self.ci3.barcode)
        self.assertEqual(json_data['name'], self.ci3.name)
        self.assertEqual(json_data['type']['name'], self.ci3.type.name)
        self.assertEqual(json_data['uid'], self.ci3.uid)
        del(self.data['name__exact'])

    def test_ci_filter_startswith(self):
        path = "/api/v0.9/ci/"
        self.data['name__startswith'] = 'ciname'
        response = self.client.get(path=path, data=self.data, format='json')
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [x['resource_uri'] for x in json_data['objects']]
        self.assertEqual(len(resource_uris), 2)
        response = self.client.get(
            path=resource_uris[0], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name)
        self.assertEqual(json_data['barcode'], self.ci1.barcode)
        self.assertEqual(json_data['name'], self.ci1.name)
        self.assertEqual(json_data['type']['name'], self.ci1.type.name)
        self.assertEqual(json_data['uid'], self.ci1.uid)

        response = self.client.get(
            path=resource_uris[1], data=self.data, format='json'
        )
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name)
        self.assertEqual(json_data['barcode'], self.ci2.barcode)
        self.assertEqual(json_data['name'], self.ci2.name)
        self.assertEqual(json_data['type']['name'], self.ci2.type.name)
        self.assertEqual(json_data['uid'], self.ci2.uid)

        del(self.data['name__startswith'])
Example #7
0
class CMDBApiTest(TestCase):
    def setUp(self):
        self.user = create_user("api_user", "*****@*****.**", "password")
        self.layers = CILayer.objects.all()
        self.types = CIType.objects.all()
        self.create_owners()
        self.create_cis()
        self.create_ownerships()
        self.create_attributes()
        self.create_relations()
        self.headers = {
            "HTTP_ACCEPT": "application/json",
            "HTTP_AUTHORIZATION": "ApiKey {}:{}".format(self.user.username, self.user.api_key.key),
        }
        cache.delete("api_user_accesses")

    def create_owners(self):
        self.owner1 = CIOwner(
            first_name="first_name_owner1",
            last_name="last_name_owner1",
            email="*****@*****.**",
        )
        self.owner1.save()
        self.owner2 = CIOwner(
            first_name="first_name_owner2",
            last_name="last_name_owner2",
            email="*****@*****.**",
        )
        self.owner2.save()

    def create_cis(self):
        self.ci1 = CI(uid="uid-ci1", type=self.types[0], barcode="barcodeci1", name="ciname1")
        self.ci1.save()
        self.ci1.layers = [self.layers[0].id, self.layers[1].id]
        self.ci1.save()
        self.ci2 = CI(uid="uid-ci2", type=self.types[1], barcode="barcodeci2", name="ciname2")
        self.ci2.save()
        self.ci2.layers = [self.layers[0].id]
        self.ci2.save()
        self.ci3 = CI(uid="other-ci3", type=self.types[1], barcode="otherbarcodeci3", name="otherci")
        self.ci3.save()
        self.ci3.layers = [self.layers[1].id]
        self.ci3.save()

    def create_ownerships(self):
        self.ciownership1 = CIOwnership(ci=self.ci1, owner=self.owner1, type=CIOwnershipType.technical)
        self.ciownership1.save()
        self.ciownership2 = CIOwnership(ci=self.ci1, owner=self.owner2, type=CIOwnershipType.business)
        self.ciownership2.save()
        self.ciownership3 = CIOwnership(ci=self.ci2, owner=self.owner2, type=CIOwnershipType.business)
        self.ciownership3.save()

    def create_relations(self):
        self.relation1 = CIRelation(parent=self.ci1, child=self.ci2, type=CI_RELATION_TYPES.CONTAINS)
        self.relation1.save()
        self.relation2 = CIRelation(parent=self.ci2, child=self.ci3, type=CI_RELATION_TYPES.HASROLE)
        self.relation2.save()

    def create_attributes(self):
        self.attribute1 = CIAttribute(name="Attribute 1", attribute_type=CI_ATTRIBUTE_TYPES.INTEGER, choices="")
        self.attribute1.save()
        self.attribute1.ci_types.add(self.types[0]),
        self.attribute_value1 = CIAttributeValue(ci=self.ci1, attribute=self.attribute1)
        self.attribute_value1.value = 10
        self.attribute_value1.save()

    def test_layers(self):
        path = "/api/v0.9/cilayers/"
        response = self.client.get(path=path, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci_layer["resource_uri"] for ci_layer in json_data["objects"]]

        response = self.client.get(path=resource_uris[0], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["name"], self.layers[0].name)

        response = self.client.get(resource_uris[1], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["name"], self.layers[1].name)

    def test_types(self):
        path = "/api/v0.9/citypes/"
        response = self.client.get(path=path, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci_type["resource_uri"] for ci_type in json_data["objects"]]
        response = self.client.get(path=resource_uris[0], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)

        self.assertEqual(json_data["name"], self.types[0].name)

        response = self.client.get(resource_uris[1], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["name"], self.types[1].name)

    def test_ci(self):
        path = "/api/v0.9/ci/"
        response = self.client.get(path, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci["resource_uri"] for ci in json_data["objects"]]

        response = self.client.get(resource_uris[0], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["layers"][0]["name"], self.layers[0].name)
        self.assertEqual(json_data["layers"][1]["name"], self.layers[1].name)
        self.assertEqual(json_data["barcode"], self.ci1.barcode)
        self.assertEqual(json_data["name"], self.ci1.name)
        self.assertEqual(json_data["type"]["name"], self.ci1.type.name)
        self.assertEqual(json_data["uid"], self.ci1.uid)
        self.assertEqual(json_data["technical_owners"][0]["first_name"], self.owner1.first_name)
        self.assertEqual(json_data["business_owners"][0]["first_name"], self.owner2.first_name)

        response = self.client.get(resource_uris[1], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["layers"][0]["name"], self.layers[0].name)
        self.assertEqual(json_data["barcode"], self.ci2.barcode)
        self.assertEqual(json_data["name"], self.ci2.name)
        self.assertEqual(json_data["type"]["name"], self.ci2.type.name)
        self.assertEqual(json_data["uid"], self.ci2.uid)
        self.assertFalse(json_data["technical_owners"])
        self.assertEqual(json_data["business_owners"][0]["first_name"], self.owner2.first_name)

    def test_ownership(self):
        """Test the direct and effective ownerships."""
        path1 = "/api/v0.9/ci/{}/".format(self.ci1.id)
        path3 = "/api/v0.9/ci/{}/".format(self.ci3.id)
        ci1_data = json.loads(self.client.get(path1, **self.headers).content)
        ci3_data = json.loads(self.client.get(path3, **self.headers).content)
        # CI1 has its own owners
        self.assertListEqual(ci1_data["business_owners"], ci1_data["effective_business_owners"])
        # CI3 inherits owners from CI1
        self.assertListEqual(ci3_data["business_owners"], [])
        self.assertListEqual(ci3_data["effective_business_owners"], ci1_data["business_owners"])

    def test_post_ci(self):
        """POST to ci collection should create a new CI"""
        ci_count_before = CI.objects.count()
        ci_data = json.dumps(
            {
                "uid": "uid-ci-api-1",
                "type": "/api/v0.9/citypes/{0}/".format(self.types[0].id),
                "barcode": "barcodeapi1",
                "name": "ciname from api 1",
                "layers": ["/api/v0.9/cilayers/5/"],
                "business_owners": ["/api/v0.9/ciowners/{0}/".format(self.owner1.id)],
                "technical_owners": ["/api/v0.9/ciowners/{0}/".format(self.owner2.id)],
                "attributes": [
                    {"name": "SLA value", "value": 0.7},
                    {
                        "name": "Documentation Link",
                        "value": "http://www.gutenberg.org/files/27827/" "27827-h/27827-h.htm",
                    },
                ],
            }
        )
        resp = self.client.post("/api/v0.9/ci/?", ci_data, content_type="application/json", **self.headers)
        self.assertEqual(CI.objects.count(), ci_count_before + 1)
        created_id = int(resp["Location"].split("/")[-2])
        created = CI.objects.get(pk=created_id)
        self.assertEqual(created.name, "ciname from api 1")
        self.assertSetEqual(set(created.business_owners.all()), {self.owner1})
        self.assertSetEqual(
            set(av.value for av in created.ciattributevalue_set.all()),
            {0.7, "http://www.gutenberg.org/files/27827/27827-h/27827-h.htm"},
        )

    def test_put_ci(self):
        """PUT should edit existing CI"""
        ci_count_before = CI.objects.count()
        ci_data = json.dumps(
            {
                "uid": "uid-ci-api-1",
                "type": "/api/v0.9/citypes/{0}/".format(self.types[0].id),
                "barcode": "barcodeapi1",
                "name": "ciname from api 1",
                "layers": ["/api/v0.9/cilayers/5/"],
                "business_owners": ["/api/v0.9/ciowners/{0}/".format(self.owner1.id)],
                "technical_owners": ["/api/v0.9/ciowners/{0}/".format(self.owner2.id)],
                "attributes": [
                    {"name": "SLA value", "value": 0.7},
                    {
                        "name": "Documentation Link",
                        "value": "http://www.gutenberg.org/files/27827/" "27827-h/27827-h.htm",
                    },
                ],
            }
        )
        self.client.put(
            "/api/v0.9/ci/{0}/".format(self.ci1.id), ci_data, content_type="application/json", **self.headers
        )
        self.assertEqual(CI.objects.count(), ci_count_before)
        edited = CI.objects.get(pk=self.ci1.id)
        self.assertEqual(edited.name, "ciname from api 1")
        self.assertSetEqual(set(edited.business_owners.all()), {self.owner1})
        self.assertSetEqual(
            set(av.value for av in edited.ciattributevalue_set.all()),
            {0.7, "http://www.gutenberg.org/files/27827/27827-h/27827-h.htm"},
        )

    def test_patch(self):
        """PATCH should edit some attributes."""
        ci_count_before = CI.objects.count()
        ci_data = json.dumps(
            {
                "business_owners": ["/api/v0.9/ciowners/{0}/".format(self.owner1.id)],
                "technical_owners": ["/api/v0.9/ciowners/{0}/".format(self.owner2.id)],
                "attributes": [
                    {"name": "SLA value", "value": 0.7},
                    {
                        "name": "Documentation Link",
                        "value": "http://www.gutenberg.org/files/27827/" "27827-h/27827-h.htm",
                    },
                ],
            }
        )
        req_data = {
            "CONTENT_LENGTH": len(ci_data),
            "CONTENT_TYPE": "application/json",
            "PATH_INFO": "/api/v0.9/ci/{0}/".format(self.ci1.id),
            "REQUEST_METHOD": "PATCH",
            "wsgi.input": FakePayload(ci_data),
        }
        req_data.update(self.headers)
        self.client.request(**req_data)
        self.assertEqual(CI.objects.count(), ci_count_before)
        edited = CI.objects.get(pk=self.ci1.id)
        self.assertEqual(edited.name, "ciname1")
        self.assertEqual(edited.uid, "uid-ci1")
        self.assertSetEqual(set(edited.business_owners.all()), {self.owner1})
        self.assertSetEqual(
            set(av.value for av in edited.ciattributevalue_set.all()),
            {0.7, "http://www.gutenberg.org/files/27827/27827-h/27827-h.htm"},
        )

    def test_get_attribute(self):
        path = "/api/v0.9/ci/{0}/".format(self.ci1.id)
        response = self.client.get(path, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertListEqual(json_data["attributes"], [{"name": "Attribute 1", "value": 10}])

    def test_relations(self):
        path = "/api/v0.9/cirelation/"
        response = self.client.get(path, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci_relation["resource_uri"] for ci_relation in json_data["objects"]]

        response = self.client.get(resource_uris[0], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["parent"], self.ci1.id)
        self.assertEqual(json_data["child"], self.ci2.id)
        self.assertEqual(json_data["type"], CI_RELATION_TYPES.CONTAINS)

        response = self.client.get(resource_uris[1], **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["parent"], self.ci2.id)
        self.assertEqual(json_data["child"], self.ci3.id)
        self.assertEqual(json_data["type"], CI_RELATION_TYPES.HASROLE)

    def test_ci_filter_exact(self):
        path = "/api/v0.9/ci/"
        data = {"name__exact": "otherci"}
        response = self.client.get(path, data=data, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci["resource_uri"] for ci in json_data["objects"]]
        self.assertEqual(len(resource_uris), 1)
        response = self.client.get(resource_uris[0], data, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["layers"][0]["name"], self.layers[1].name)
        self.assertEqual(json_data["barcode"], self.ci3.barcode)
        self.assertEqual(json_data["name"], self.ci3.name)
        self.assertEqual(json_data["type"]["name"], self.ci3.type.name)
        self.assertEqual(json_data["uid"], self.ci3.uid)

    def test_ci_filter_startswith(self):
        path = "/api/v0.9/ci/"
        data = {"name__startswith": "ciname"}
        response = self.client.get(path=path, data=data, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci["resource_uri"] for ci in json_data["objects"]]
        self.assertEqual(len(resource_uris), 2)
        response = self.client.get(resource_uris[0], data=data, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["layers"][0]["name"], self.layers[0].name)
        self.assertEqual(json_data["barcode"], self.ci1.barcode)
        self.assertEqual(json_data["name"], self.ci1.name)
        self.assertEqual(json_data["type"]["name"], self.ci1.type.name)
        self.assertEqual(json_data["uid"], self.ci1.uid)

        response = self.client.get(resource_uris[1], data=data, **self.headers)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data["layers"][0]["name"], self.layers[0].name)
        self.assertEqual(json_data["barcode"], self.ci2.barcode)
        self.assertEqual(json_data["name"], self.ci2.name)
        self.assertEqual(json_data["type"]["name"], self.ci2.type.name)
        self.assertEqual(json_data["uid"], self.ci2.uid)
Example #8
0
class CMDBApiTest(UserTestCase):

    def setUp(self):
        self.layers = CILayer.objects.all()
        self.types = CIType.objects.all()
        self.create_owners()
        self.create_cis()
        self.create_ownerships()
        self.create_attributes()
        self.create_relations()
        super(CMDBApiTest, self).setUp()

    def create_owners(self):
        self.owner1 = CIOwnerFactory()
        self.owner2 = CIOwnerFactory()

    def create_cis(self):
        self.ci1 = CI(
            uid='uid-ci1',
            type=self.types[0],
            barcode='barcodeci1',
            name='ciname1',
        )
        self.ci1.save()
        self.ci1.layers = [self.layers[0].id, self.layers[1].id]
        self.ci1.save()
        self.ci2 = CI(
            uid='uid-ci2',
            type=self.types[1],
            barcode='barcodeci2',
            name='ciname2',
        )
        self.ci2.save()
        self.ci2.layers = [self.layers[0].id]
        self.ci2.save()
        self.ci3 = CI(
            uid='other-ci3',
            type=self.types[1],
            barcode='otherbarcodeci3',
            name='otherci',
        )
        self.ci3.save()
        self.ci3.layers = [self.layers[1].id]
        self.ci3.save()

    def create_ownerships(self):
        self.ciownership1 = CIOwnership(
            ci=self.ci1,
            owner=self.owner1,
            type=CIOwnershipType.technical,
        )
        self.ciownership1.save()
        self.ciownership2 = CIOwnership(
            ci=self.ci1,
            owner=self.owner2,
            type=CIOwnershipType.business,
        )
        self.ciownership2.save()
        self.ciownership3 = CIOwnership(
            ci=self.ci2,
            owner=self.owner2,
            type=CIOwnershipType.business,
        )
        self.ciownership3.save()

    def create_relations(self):
        self.relation1 = CIRelation(
            parent=self.ci1,
            child=self.ci2,
            type=CI_RELATION_TYPES.CONTAINS,
        )
        self.relation1.save()
        self.relation2 = CIRelation(
            parent=self.ci2,
            child=self.ci3,
            type=CI_RELATION_TYPES.HASROLE,
        )
        self.relation2.save()

    def create_attributes(self):
        self.attribute1 = CIAttribute(
            name='Attribute 1', attribute_type=CI_ATTRIBUTE_TYPES.INTEGER,
            choices='',
        )
        self.attribute1.save()
        self.attribute1.ci_types.add(self.types[0]),
        self.attribute_value1 = CIAttributeValue(
            ci=self.ci1, attribute=self.attribute1,
        )
        self.attribute_value1.value = 10
        self.attribute_value1.save()

    def test_layers(self):
        path = "/api/v0.9/cilayers/"
        response = self.get(path)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [
            ci_layer['resource_uri'] for ci_layer in json_data['objects']
        ]
        response = self.get(resource_uris[0])
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.layers[0].name)

        response = self.get(resource_uris[1])
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.layers[1].name)

    def test_types(self):
        path = "/api/v0.9/citypes/"
        response = self.get(path=path)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [
            ci_type['resource_uri'] for ci_type in json_data['objects']
        ]
        response = self.get(path=resource_uris[0])
        json_string = response.content
        json_data = json.loads(json_string)

        self.assertEqual(json_data['name'], self.types[0].name)

        response = self.get(resource_uris[1])
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['name'], self.types[1].name)

    def test_ci(self):
        path = "/api/v0.9/ci/"
        response = self.get(path)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci['resource_uri'] for ci in json_data['objects']]

        response = self.get(resource_uris[0])
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['layers'][1]['name'], self.layers[1].name)
        self.assertEqual(json_data['barcode'], self.ci1.barcode)
        self.assertEqual(json_data['name'], self.ci1.name)
        self.assertEqual(json_data['type']['name'], self.ci1.type.name)
        self.assertEqual(json_data['uid'], self.ci1.uid)
        self.assertEqual(
            json_data['technical_owners'][0]['first_name'],
            self.owner1.first_name,
        )
        self.assertEqual(
            json_data['business_owners'][0]['first_name'],
            self.owner2.first_name,
        )

        response = self.get(resource_uris[1])
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['barcode'], self.ci2.barcode)
        self.assertEqual(json_data['name'], self.ci2.name)
        self.assertEqual(json_data['type']['name'], self.ci2.type.name)
        self.assertEqual(json_data['uid'], self.ci2.uid)
        self.assertFalse(json_data['technical_owners'])
        self.assertEqual(
            json_data['business_owners'][0]['first_name'],
            self.owner2.first_name,
        )

    def test_ownership(self):
        """Test the direct and effective ownerships."""
        path1 = '/api/v0.9/ci/{}/'.format(self.ci1.id)
        path3 = '/api/v0.9/ci/{}/'.format(self.ci3.id)
        ci1_data = json.loads(self.get(path1).content)
        ci3_data = json.loads(self.get(path3).content)
        # CI1 has its own owners
        self.assertListEqual(
            ci1_data['business_owners'],
            ci1_data['effective_business_owners']
        )
        # CI3 inherits owners from CI1
        self.assertListEqual(ci3_data['business_owners'], [])
        self.assertListEqual(
            ci3_data['effective_business_owners'],
            ci1_data['business_owners']
        )

    def test_post_ci(self):
        """POST to ci collection should create a new CI"""
        ci_count_before = CI.objects.count()
        ci_data = json.dumps({
            'uid': 'uid-ci-api-1',
            'type': '/api/v0.9/citypes/{0}/'.format(self.types[0].id),
            'barcode': 'barcodeapi1',
            'name': 'ciname from api 1',
            'layers': ['/api/v0.9/cilayers/5/'],
            'business_owners': [
                '/api/v0.9/ciowners/{0}/'.format(self.owner1.id)
            ],
            'technical_owners': [
                '/api/v0.9/ciowners/{0}/'.format(self.owner2.id)
            ],
            'attributes': [
                {
                    'name': 'SLA value',
                    'value': 0.7,
                }, {
                    'name': 'Documentation Link',
                    'value': 'http://www.gutenberg.org/files/27827/'
                    '27827-h/27827-h.htm',
                },
            ],
        })
        resp = self.post(
            '/api/v0.9/ci/?',
            ci_data,
            content_type='application/json',
        )
        self.assertEqual(CI.objects.count(), ci_count_before + 1)
        created_id = int(resp['Location'].split('/')[-2])
        created = CI.objects.get(pk=created_id)
        self.assertEqual(created.name, 'ciname from api 1')
        self.assertSetEqual(
            set(created.business_owners.all()),
            {self.owner1},
        )
        self.assertSetEqual(
            set(av.value for av in created.ciattributevalue_set.all()),
            {
                0.7,
                'http://www.gutenberg.org/files/27827/27827-h/27827-h.htm',
            },
        )

    def test_put_ci(self):
        """PUT should edit existing CI"""
        ci_count_before = CI.objects.count()
        ci_data = json.dumps({
            'uid': 'uid-ci-api-1',
            'type': '/api/v0.9/citypes/{0}/'.format(self.types[0].id),
            'barcode': 'barcodeapi1',
            'name': 'ciname from api 1',
            'layers': ['/api/v0.9/cilayers/5/'],
            'business_owners': [
                '/api/v0.9/ciowners/{0}/'.format(self.owner1.id)
            ],
            'technical_owners': [
                '/api/v0.9/ciowners/{0}/'.format(self.owner2.id)
            ],
            'related': [],
            'attributes': [
                {
                    'name': 'SLA value',
                    'value': 0.7,
                }, {
                    'name': 'Documentation Link',
                    'value': 'http://www.gutenberg.org/files/27827/'
                    '27827-h/27827-h.htm',
                },
            ],
        })
        self.put(
            '/api/v0.9/ci/{0}/'.format(
                self.ci1.id,
            ),
            ci_data,
            content_type='application/json',
        )
        self.assertEqual(CI.objects.count(), ci_count_before)
        edited = CI.objects.get(pk=self.ci1.id)
        self.assertEqual(edited.name, 'ciname from api 1')
        self.assertSetEqual(
            set(edited.business_owners.all()),
            {self.owner1},
        )
        self.assertSetEqual(
            set(av.value for av in edited.ciattributevalue_set.all()),
            {
                0.7,
                'http://www.gutenberg.org/files/27827/27827-h/27827-h.htm',
            },
        )

    def test_patch(self):
        """PATCH should edit some attributes."""
        ci_count_before = CI.objects.count()
        ci_data = json.dumps({
            'business_owners': [
                '/api/v0.9/ciowners/{0}/'.format(self.owner1.id)
            ],
            'technical_owners': [
                '/api/v0.9/ciowners/{0}/'.format(self.owner2.id)
            ],
            'attributes': [
                {
                    'name': 'SLA value',
                    'value': 0.7,
                }, {
                    'name': 'Documentation Link',
                    'value': 'http://www.gutenberg.org/files/27827/'
                    '27827-h/27827-h.htm',
                },
            ],
        })
        self.patch(
            '/api/v0.9/ci/{0}/'.format(self.ci1.id),
            ci_data,
            CONTENT_TYPE='application/json'

        )
        self.assertEqual(CI.objects.count(), ci_count_before)
        edited = CI.objects.get(pk=self.ci1.id)
        self.assertEqual(edited.name, 'ciname1')
        self.assertEqual(edited.uid, 'uid-ci1')
        self.assertSetEqual(
            set(edited.business_owners.all()),
            {self.owner1},
        )
        self.assertSetEqual(
            set(av.value for av in edited.ciattributevalue_set.all()),
            {
                0.7,
                'http://www.gutenberg.org/files/27827/27827-h/27827-h.htm',
            },
        )

    def test_patch_relations(self):
        """A test for adding relations via 0.10 version of API."""
        ci_data = json.dumps({
            'related': [
                {
                    'dir': 'INCOMING',
                    'type': 'HASROLE',
                    'id': self.ci2.id,
                }, {
                    'dir': 'OUTGOING',
                    'type': 'CONTAINS',
                    'id': self.ci3.id,
                }
            ],
        })
        req_data = {
            'CONTENT_LENGTH': len(ci_data),
            'CONTENT_TYPE': 'application/json',
            'PATH_INFO': '/api/v0.10/ci/{0}/'.format(self.ci1.id),
            'REQUEST_METHOD': 'PATCH',
            'wsgi.input': FakePayload(ci_data),
        }
        req_data.update(self.headers)
        self.client.request(**req_data)
        rel1 = CIRelation.objects.get(
            child_id=self.ci1.id,
            parent_id=self.ci2.id,
            type=CI_RELATION_TYPES.HASROLE.id,
        )
        rel2 = CIRelation.objects.get(
            child_id=self.ci3.id,
            parent_id=self.ci1.id,
            type=CI_RELATION_TYPES.CONTAINS.id,
        )


    def test_get_attribute(self):
        path = "/api/v0.9/ci/{0}/".format(self.ci1.id)
        response = self.get(path)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertListEqual(json_data['attributes'], [
            {'name': 'Attribute 1', 'value': 10}
        ])

    def test_relations(self):
        path = "/api/v0.9/cirelation/"
        response = self.get(path)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [
            ci_relation['resource_uri'] for ci_relation in json_data['objects']
        ]

        response = self.get(resource_uris[0])
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['parent'], self.ci1.id)
        self.assertEqual(json_data['child'], self.ci2.id)
        self.assertEqual(json_data['type'], CI_RELATION_TYPES.CONTAINS)

        response = self.get(resource_uris[1])
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['parent'], self.ci2.id)
        self.assertEqual(json_data['child'], self.ci3.id)
        self.assertEqual(json_data['type'], CI_RELATION_TYPES.HASROLE)

    def test_ci_filter_exact(self):
        path = "/api/v0.9/ci/"
        data = {'name__exact': 'otherci'}
        response = self.get(path, data=data)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci['resource_uri'] for ci in json_data['objects']]
        self.assertEqual(len(resource_uris), 1)
        response = self.get(resource_uris[0], data)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[1].name)
        self.assertEqual(json_data['barcode'], self.ci3.barcode)
        self.assertEqual(json_data['name'], self.ci3.name)
        self.assertEqual(json_data['type']['name'], self.ci3.type.name)
        self.assertEqual(json_data['uid'], self.ci3.uid)

    def test_ci_filter_startswith(self):
        path = "/api/v0.9/ci/"
        data = {'name__startswith': 'ciname'}
        response = self.get(path=path, data=data)
        json_string = response.content
        json_data = json.loads(json_string)
        resource_uris = [ci['resource_uri'] for ci in json_data['objects']]
        self.assertEqual(len(resource_uris), 2)
        response = self.get(resource_uris[0], data=data)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['barcode'], self.ci1.barcode)
        self.assertEqual(json_data['name'], self.ci1.name)
        self.assertEqual(json_data['type']['name'], self.ci1.type.name)
        self.assertEqual(json_data['uid'], self.ci1.uid)

        response = self.get(resource_uris[1], data=data)
        json_string = response.content
        json_data = json.loads(json_string)
        self.assertEqual(json_data['layers'][0]['name'], self.layers[0].name)
        self.assertEqual(json_data['barcode'], self.ci2.barcode)
        self.assertEqual(json_data['name'], self.ci2.name)
        self.assertEqual(json_data['type']['name'], self.ci2.type.name)
        self.assertEqual(json_data['uid'], self.ci2.uid)

    def test_filter_related(self):
        path = "/api/v0.10/ci/"
        data = {'parent': self.ci1.id, 'child': self.ci3.id}
        response = self.get(path=path, data=data)
        json_data = json.loads(response.content)
        self.assertEqual(len(json_data['objects']), 1)
        self.assertEqual(json_data['objects'][0]['id'], self.ci2.id)