Beispiel #1
0
    def setUp(self):
        login = "******"
        password = "******"
        user = User.objects.create_user(login, "*****@*****.**", password)
        self.user = user
        self.user.is_staff = True
        self.user.is_superuser = True
        self.user.save()
        self.client = Client()
        self.client.login(username=login, password=password)

        venture = Venture(name=DEVICE_VENTURE, symbol=DEVICE_VENTURE_SYMBOL)
        venture.save()
        self.venture = venture
        venture_role = VentureRole(name=VENTURE_ROLE, venture=self.venture)
        venture_role.save()
        self.venture_role = venture_role
        self.device = Device.create(
            sn=DEVICE_SN,
            barcode=DEVICE_BARCODE,
            remarks=DEVICE_REMARKS,
            model_name="xxxx",
            model_type=DeviceType.unknown,
            venture=self.venture,
            venture_role=self.venture_role,
            rack=DEVICE_RACK,
            position=DEVICE_POSITION,
            dc=DATACENTER,
        )
        self.device.name = DEVICE_NAME
        self.device.save()

        self.layer = CILayer(name="layer1")
        self.layer.save()
        self.citype = CIType(name="xxx")
        self.citype.save()
Beispiel #2
0
 def create_cilayers(self):
     self.cilayer1 = CILayer(name='layer1')
     self.cilayer1.save()
     self.cilayer2 = CILayer(name='layer2')
     self.cilayer2.save()
Beispiel #3
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'])
Beispiel #4
0
class CIFormsTest(TestCase):
    def setUp(self):
        login = "******"
        password = "******"
        user = User.objects.create_user(login, "*****@*****.**", password)
        self.user = user
        self.user.is_staff = True
        self.user.is_superuser = True
        self.user.save()
        self.client = Client()
        self.client.login(username=login, password=password)

        venture = Venture(name=DEVICE_VENTURE, symbol=DEVICE_VENTURE_SYMBOL)
        venture.save()
        self.venture = venture
        venture_role = VentureRole(name=VENTURE_ROLE, venture=self.venture)
        venture_role.save()
        self.venture_role = venture_role
        self.device = Device.create(
            sn=DEVICE_SN,
            barcode=DEVICE_BARCODE,
            remarks=DEVICE_REMARKS,
            model_name="xxxx",
            model_type=DeviceType.unknown,
            venture=self.venture,
            venture_role=self.venture_role,
            rack=DEVICE_RACK,
            position=DEVICE_POSITION,
            dc=DATACENTER,
        )
        self.device.name = DEVICE_NAME
        self.device.save()

        self.layer = CILayer(name="layer1")
        self.layer.save()
        self.citype = CIType(name="xxx")
        self.citype.save()

    def add_ci(self, name="CI"):
        ci_add_url = "/cmdb/add/"
        attrs = {"base-layers": 1, "base-name": name, "base-state": 2, "base-status": 2, "base-type": 1}
        return self.client.post(ci_add_url, attrs)

    def add_ci_relation(self, parent_ci, child_ci, relation_type, relation_kind):
        ci_relation_add_url = "/cmdb/relation/add/{}?{}={}".format(parent_ci.id, relation_type, parent_ci.id)
        attrs = {"base-parent": parent_ci.id, "base-child": child_ci.id, "base-type": relation_kind.id}
        return self.client.post(ci_relation_add_url, attrs)

    def test_add_two_ci_with_the_same_content_object(self):
        response = self.add_ci(name="CI1")
        self.assertEqual(response.status_code, 302)
        ci1 = CI.objects.get(name="CI1")
        ci1.content_object = self.device
        ci1.save()

        response = self.add_ci(name="CI2")
        self.assertEqual(response.status_code, 302)
        with self.assertRaises(IntegrityError) as e:
            ci2 = CI.objects.get(name="CI2")
            ci2.content_object = self.device
            ci2.save()
        self.assertEqual("columns content_type_id, object_id are not unique", e.exception.message)

    def test_two_ci_without_content_object(self):
        response_ci1 = self.add_ci(name="CI1")
        response_ci2 = self.add_ci(name="CI1")
        cis = CI.objects.filter(name="CI1")
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        self.assertEqual(len(cis), 2)

    def test_add_ci_relation_rel_child(self):
        response_ci1 = self.add_ci(name="CI1")
        response_ci2 = self.add_ci(name="CI2")
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        ci1 = CI.objects.get(name="CI1")
        ci2 = CI.objects.get(name="CI2")

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_child", relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_child", relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.CONTAINS)
        self.assertEqual(rel.type, CI_RELATION_TYPES.CONTAINS)

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_child", relation_kind=CI_RELATION_TYPES.REQUIRES
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.REQUIRES)
        self.assertEqual(rel.type, CI_RELATION_TYPES.REQUIRES)

    def test_add_ci_relation_rel_parent(self):
        response_ci1 = self.add_ci(name="CI1")
        response_ci2 = self.add_ci(name="CI2")
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        ci1 = CI.objects.get(name="CI1")
        ci2 = CI.objects.get(name="CI2")

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.CONTAINS)
        self.assertEqual(rel.type, CI_RELATION_TYPES.CONTAINS)

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.REQUIRES
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.REQUIRES)
        self.assertEqual(rel.type, CI_RELATION_TYPES.REQUIRES)

    def test_add_ci_relation_with_himself(self):
        response_ci = self.add_ci(name="CI")
        self.assertEqual(response_ci.status_code, 302)
        ci = CI.objects.get(name="CI")

        response_r = self.add_ci_relation(
            parent_ci=ci, child_ci=ci, relation_type="rel_child", relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.context_data["form"].errors["__all__"][0], "CI can not have relation with himself")

    def test_ci_cycle_parent_child(self):
        response_ci1 = self.add_ci(name="CI1")
        response_ci2 = self.add_ci(name="CI2")
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)

        ci1 = CI.objects.get(name="CI1")
        ci2 = CI.objects.get(name="CI2")

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)
        response_r = self.add_ci_relation(
            parent_ci=ci2, child_ci=ci1, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)

    def test_ci_relations_cycle(self):
        response_ci1 = self.add_ci(name="CI1")
        response_ci2 = self.add_ci(name="CI2")
        response_ci3 = self.add_ci(name="CI3")
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        self.assertEqual(response_ci3.status_code, 302)
        ci1 = CI.objects.get(name="CI1")
        ci2 = CI.objects.get(name="CI2")
        ci3 = CI.objects.get(name="CI3")

        response_r = self.add_ci_relation(
            parent_ci=ci1, child_ci=ci2, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci2, child_ci=ci3, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci2.id, child_id=ci3.id, type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci3, child_ci=ci1, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci3.id, child_id=ci1.id, type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        cycle = CI.get_cycle()
        self.assertEqual(cycle, [1, 2, 3])
Beispiel #5
0
class CIFormsTest(TestCase):
    fixtures = [
        '0_types.yaml',
        '1_attributes.yaml',
        '2_layers.yaml',
        '3_prefixes.yaml'
    ]

    def setUp(self):
        login = '******'
        password = '******'
        user = User.objects.create_user(login, '*****@*****.**', password)
        self.user = user
        self.user.is_staff = True
        self.user.is_superuser = True
        self.user.save()
        self.client = Client()
        self.client.login(username=login, password=password)

        venture = Venture(name=DEVICE_VENTURE, symbol=DEVICE_VENTURE_SYMBOL)
        venture.save()
        self.venture = venture
        venture_role = VentureRole(name=VENTURE_ROLE, venture=self.venture)
        venture_role.save()
        self.venture_role = venture_role
        self.device = Device.create(
            sn=DEVICE_SN,
            barcode=DEVICE_BARCODE,
            remarks=DEVICE_REMARKS,
            model_name='xxxx',
            model_type=DeviceType.unknown,
            venture=self.venture,
            venture_role=self.venture_role,
            rack=DEVICE_RACK,
            position=DEVICE_POSITION,
            dc=DATACENTER,
        )
        self.device.name = DEVICE_NAME
        self.device.save()

        self.layer = CILayer(name='layer1')
        self.layer.save()
        self.citype = CIType(name='xxx')
        self.citype.save()

    def add_ci(self, name='CI', type=1):
        ci_add_url = '/cmdb/add/'
        attrs = {
            'base-layers': 1,
            'base-name': name,
            'base-state': 2,
            'base-status': 2,
            'base-type': type,
        }
        return self.client.post(ci_add_url, attrs)

    def edit_ci(self, ci, custom_attrs={}):
        ci_edit_url = '/cmdb/ci/edit/{}'.format(ci.id)
        attrs = {
            'base-name': ci.name,
            'base-type': 1,
            'base-state': 2,
            'base-status': 2,
            'base-layers': 1,
        }
        attrs.update(custom_attrs)
        return self.client.post(ci_edit_url, attrs)

    def add_ci_relation(self, parent_ci, child_ci, relation_type,
                        relation_kind):
        ci_relation_add_url = '/cmdb/relation/add/{}?{}={}'.format(
            parent_ci.id, relation_type, parent_ci.id
        )
        attrs = {
            'base-parent': parent_ci.id,
            'base-child': child_ci.id,
            'base-type': relation_kind.id,
        }
        return self.client.post(ci_relation_add_url, attrs)

    def test_add_two_ci_with_the_same_content_object(self):
        response = self.add_ci(name='CI1')
        self.assertEqual(response.status_code, 302)
        ci1 = CI.objects.get(name='CI1')
        ci1.content_object = self.device
        ci1.save()

        response = self.add_ci(name='CI2')
        self.assertEqual(response.status_code, 302)
        with self.assertRaises(IntegrityError) as e:
            ci2 = CI.objects.get(name='CI2')
            ci2.content_object = self.device
            ci2.save()
        self.assertEqual('columns content_type_id, object_id are not unique',
                         e.exception.message)

    def test_two_ci_without_content_object(self):
        response_ci1 = self.add_ci(name='CI1')
        response_ci2 = self.add_ci(name='CI1')
        cis = CI.objects.filter(name='CI1')
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        self.assertEqual(len(cis), 2)

    def test_add_ci_relation_rel_child(self):
        response_ci1 = self.add_ci(name='CI1')
        response_ci2 = self.add_ci(name='CI2')
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        ci1 = CI.objects.get(name='CI1')
        ci2 = CI.objects.get(name='CI2')

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_child',
            relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id,
                                     type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_child',
            relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(
            parent_id=ci1.id,
            child_id=ci2.id,
            type=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(rel.type, CI_RELATION_TYPES.CONTAINS)

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_child',
            relation_kind=CI_RELATION_TYPES.REQUIRES)
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id,
                                     type=CI_RELATION_TYPES.REQUIRES)
        self.assertEqual(rel.type, CI_RELATION_TYPES.REQUIRES)

    def test_add_ci_relation_rel_parent(self):
        response_ci1 = self.add_ci(name='CI1')
        response_ci2 = self.add_ci(name='CI2')
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        ci1 = CI.objects.get(name='CI1')
        ci2 = CI.objects.get(name='CI2')

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id,
                                     type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(
            parent_id=ci1.id,
            child_id=ci2.id,
            type=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(rel.type, CI_RELATION_TYPES.CONTAINS)

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.REQUIRES)
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id,
                                     type=CI_RELATION_TYPES.REQUIRES)
        self.assertEqual(rel.type, CI_RELATION_TYPES.REQUIRES)

    def test_add_ci_relation_with_himself(self):
        response_ci = self.add_ci(name='CI')
        self.assertEqual(response_ci.status_code, 302)
        ci = CI.objects.get(name='CI')

        response_r = self.add_ci_relation(
            parent_ci=ci,
            child_ci=ci,
            relation_type='rel_child',
            relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.context_data['form'].errors['__all__'][0],
                         'CI can not have relation with himself')

    def test_ci_cycle_parent_child(self):
        response_ci1 = self.add_ci(name='CI1')
        response_ci2 = self.add_ci(name='CI2')
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)

        ci1 = CI.objects.get(name='CI1')
        ci2 = CI.objects.get(name='CI2')

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)
        response_r = self.add_ci_relation(
            parent_ci=ci2,
            child_ci=ci1,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.CONTAINS
        )
        self.assertEqual(response_r.status_code, 302)

    def test_ci_relations_cycle(self):
        response_ci1 = self.add_ci(name='CI1')
        response_ci2 = self.add_ci(name='CI2')
        response_ci3 = self.add_ci(name='CI3')
        self.assertEqual(response_ci1.status_code, 302)
        self.assertEqual(response_ci2.status_code, 302)
        self.assertEqual(response_ci3.status_code, 302)
        ci1 = CI.objects.get(name='CI1')
        ci2 = CI.objects.get(name='CI2')
        ci3 = CI.objects.get(name='CI3')

        response_r = self.add_ci_relation(
            parent_ci=ci1,
            child_ci=ci2,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id,
                                     type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci2,
            child_ci=ci3,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci2.id, child_id=ci3.id,
                                     type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        response_r = self.add_ci_relation(
            parent_ci=ci3,
            child_ci=ci1,
            relation_type='rel_parent',
            relation_kind=CI_RELATION_TYPES.HASROLE
        )
        self.assertEqual(response_r.status_code, 302)
        rel = CIRelation.objects.get(parent_id=ci3.id, child_id=ci1.id,
                                     type=CI_RELATION_TYPES.HASROLE)
        self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE)

        cycle = CI.get_cycle()
        self.assertEqual(cycle, [1, 2, 3])

    def test_ci_custom_fields(self):
        # Create CI by the form, and edit CI  with completion of custom fields
        # (type - float) for CI type - device
        response_ci_application = self.add_ci(name='CI_application', type=1)
        self.assertEqual(response_ci_application.status_code, 302)
        ci_application = CI.objects.get(name='CI_application')
        response_ci_application_edit = self.edit_ci(
            ci_application, custom_attrs={
                'attr-attribute_float_4': 12345
            }
        )
        self.assertEqual(response_ci_application_edit.status_code, 302)
        ci_attrvalue = ci_application.ciattributevalue_set.all()
        values = {}
        values['float'] = [x.value_float_id for x in ci_attrvalue
                           if x.value_float]
        ci_values = CIValueFloat.objects.get(id__in=values['float'])
        self.assertEqual(ci_values.value, 12345)

        # Create CI by the form, and edit CI  with completion of custom fields
        # (type - date and float) for CI type - device
        response_ci_device = self.add_ci(name='CI_device', type=2)
        self.assertEqual(response_ci_device.status_code, 302)
        ci_device = CI.objects.get(name='CI_device')
        response_ci_device_edit = self.edit_ci(
            ci_device, custom_attrs={
                'attr-attribute_date_3': time.strftime('%Y-%m-%d'),
                'attr-attribute_float_4': 666,
            }
        )
        self.assertEqual(response_ci_device_edit.status_code, 302)
        ci_attrvalue = ci_device.ciattributevalue_set.all()
        values = {}
        values['float'] = [x.value_float_id for x in ci_attrvalue
                           if x.value_float]
        values['date'] = [x.value_date_id for x in ci_attrvalue
                          if x.value_date]
        ci_float_value = CIValueFloat.objects.get(id__in=values['float'])
        ci_date_value = CIValueDate.objects.get(id__in=values['date'])
        self.assertEqual(ci_date_value.value.strftime('%Y-%m-%d'),
                         time.strftime('%Y-%m-%d'))
        self.assertEqual(ci_float_value.value, 666)

        # Create CI by the form, and edit CI  with completion of custom fields
        # (type - string) for CI type - procedure
        response_ci_device = self.add_ci(name='CI_procedure', type=3)
        self.assertEqual(response_ci_device.status_code, 302)
        ci_device = CI.objects.get(name='CI_procedure')
        response_ci_device_edit = self.edit_ci(
            ci_device, custom_attrs={
                'attr-attribute_string_1': 'http://doc.local',
                'attr-attribute_string_2': 'name-test',
            }
        )
        self.assertEqual(response_ci_device_edit.status_code, 302)
        ci_attrvalue = ci_device.ciattributevalue_set.all()
        values = {}
        values['string'] = [x.value_string_id for x in ci_attrvalue
                            if x.value_string]
        ci_string_value = CIValueString.objects.filter(id__in=values['string'])
        val = [x.value for x in ci_string_value]
        val.sort()
        self.assertListEqual(val, ['http://doc.local', 'name-test'])