예제 #1
0
파일: tests.py 프로젝트: szaydel/ralph
 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()
예제 #2
0
파일: api.py 프로젝트: deejay1/ralph
 def hydrate_m2m(self, bundle):
     ci = bundle.obj
     CIRelation.objects.filter(
         Q(child_id=ci.id) | Q(parent_id=ci.id)
     ).delete()
     for relation_data in bundle.data.get('related', []):
         relation = CIRelation()
         try:
             type_id = CI_RELATION_TYPES.id_from_name(relation_data['type'])
         except ValueError:
             raise tastypie.exceptions.BadRequest(
                 'No such relation type {}'.format(relation_data['type'])
             )
         relation.type = type_id
         other_ci = CI.objects.get(id=relation_data['id'])
         if relation_data['dir'] == 'OUTGOING':
             relation.parent = ci
             relation.child = other_ci
         elif relation_data['dir'] == 'INCOMING':
             relation.parent = other_ci
             relation.child = ci
         else:
             raise tastypie.exceptions.BadRequest(
                 'dir should be OUTGOING or INCOMING'
             )
         relation.save()
     return []
예제 #3
0
파일: api.py 프로젝트: pombreda/ralph
 def hydrate_m2m(self, bundle):
     ci = bundle.obj
     CIRelation.objects.filter(Q(child_id=ci.id)
                               | Q(parent_id=ci.id)).delete()
     for relation_data in bundle.data.get('related', []):
         relation = CIRelation()
         try:
             type_id = CI_RELATION_TYPES.id_from_name(relation_data['type'])
         except ValueError:
             raise tastypie.exceptions.BadRequest(
                 'No such relation type {}'.format(relation_data['type']))
         relation.type = type_id
         other_ci = CI.objects.get(id=relation_data['id'])
         if relation_data['dir'] == 'OUTGOING':
             relation.parent = ci
             relation.child = other_ci
         elif relation_data['dir'] == 'INCOMING':
             relation.parent = other_ci
             relation.child = ci
         else:
             raise tastypie.exceptions.BadRequest(
                 'dir should be OUTGOING or INCOMING')
         relation.save()
     return []
예제 #4
0
파일: tests.py 프로젝트: szaydel/ralph
class CMDBApiTest(TestCase):
    def setUp(self):
        self.creatre_user()
        self.create_cilayers()
        self.create_citypes()
        self.create_owners()
        self.create_cis()
        self.create_ownerships()
        self.create_relations()

    def creatre_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
        }

    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_owner'][0]['username'],
            '{}.{}'.format(self.owner1.first_name, self.owner1.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.assertEqual(
            json_data['technical_owner'][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'])