Пример #1
0
    def test_create_incorrect_data(self):
        carrier = CellularCarrierFactory()

        warehouse = WarehouseFactory()

        user = UserFactory()

        owner = UserFactory()

        sim_card_data = {
            'status': BackOfficeAssetStatus.new,
            'pin1': '12343777448484',
            'pin2': '568fg458',
            'puk1': '123456789123456',
            'puk2': '567hdj8',
            'card_number': '1938462hdhd98',
            'phone_number': '481234568911',
            'warehouse': warehouse.pk,
            'user': user.pk,
            'owner': owner.pk,
            'carrier': carrier.pk,
        }
        url = reverse('admin:sim_cards_simcard_add')

        response = self.client.post(url, sim_card_data, follow=True)
        self.assertIn('errors', response.context_data)
        self.assertEqual(6, len(response.context_data['errors']))
Пример #2
0
    def setUp(self):
        super().setUp()
        self.asset = BackOfficeAssetFactory()
        self.user = UserFactory()
        self.user.is_superuser = False
        self.user.is_staff = True
        self.passwd = 'ralph'
        self.user.set_password(self.passwd)
        # Grant all permissions to the user
        permissions = Permission.objects.exclude(
            codename__in=[
                'view_backofficeasset_hostname_field',
                'view_backofficeasset_service_env_field',
                'change_backofficeasset_hostname_field',
                'change_backofficeasset_service_env_field',
            ]
        ).all()

        self.user.user_permissions.add(*permissions)
        self.user.regions.add(self.asset.region)
        self.user.save()
        self.login_as_user(user=self.user, password=self.passwd)
        self.data = {
            'hostname': self.asset.hostname,
            'model': self.asset.model.pk,
            'status': self.asset.status,
            'warehouse': self.asset.warehouse.pk,
            'region': self.asset.region.pk,
            'barcode': self.asset.barcode,
            'depreciation_rate': 0,
            'custom_fields-customfieldvalue-content_type-object_id-INITIAL_FORMS': '0',  # noqa: E501
            'custom_fields-customfieldvalue-content_type-object_id-MAX_NUM_FORMS': '1000',  # noqa: E501
            'custom_fields-customfieldvalue-content_type-object_id-MIN_NUM_FORMS': '0',  # noqa: E501
            'custom_fields-customfieldvalue-content_type-object_id-TOTAL_FORMS': '3',  # noqa: E501fhtml
        }
Пример #3
0
    def test_detail_access_card_returns_expected_fields(self):
        access_card = AccessCardFactory(issue_date=datetime.now(),
                                        user=UserFactory(),
                                        owner=UserFactory(),
                                        notes='test')
        access_card.access_zones.add(AccessZoneFactory())
        access_card.save()

        url = reverse('accesscard-detail', args=(access_card.id, ))
        response = self.client.get(url)
        self.assertEqual(status.HTTP_200_OK, response.status_code)

        self.assertAccessCardHasCertainFieldsAndValues(access_card,
                                                       response.data)
Пример #4
0
 def setUpClass(cls):
     super().setUpClass()
     cls.user_pl = UserFactory(country=Country.pl)
     cls.region_pl = RegionFactory(name='PL', country=Country.pl)
     cls.region_us = RegionFactory(name='US', country=Country.us)
     cls.region_us_2 = RegionFactory(name='US', country=Country.us)
     cls.category = CategoryFactory(code='PC')
     cls.model = BackOfficeAssetModelFactory(category=cls.category)
Пример #5
0
    def test_list_access_card_returns_expected_fields(self):
        access_card1 = AccessCardFactory(issue_date=datetime.now(),
                                         user=UserFactory(),
                                         owner=UserFactory(),
                                         notes='test')
        access_card2 = AccessCardFactory(issue_date=datetime.now(),
                                         user=UserFactory(),
                                         owner=UserFactory(),
                                         notes='test')

        url = reverse('accesscard-list')
        response = self.client.get(url)
        self.assertEqual(status.HTTP_200_OK, response.status_code)

        self.assertAccessCardHasCertainFieldsAndValues(
            access_card1, response.data['results'][0])

        self.assertAccessCardHasCertainFieldsAndValues(
            access_card2, response.data['results'][1])
Пример #6
0
    def test_a_report_is_generated(self, mock_method):
        GENERATED_FILE_CONTENT = REPORT_TEMPLATE = b'some-content'
        mock_method.return_value = GENERATED_FILE_CONTENT
        report_template = ReportTemplateFactory(template__data=REPORT_TEMPLATE)
        user = UserFactory()
        instances = [
            BackOfficeAssetFactory(
                user=UserFactory(first_name="James", last_name="Bond")
            )
        ]
        context = BackOfficeAsset._get_report_context(instances)
        attachment = generate_report(
            report_template.name, user, instances, report_template.language,
            context=context)

        correct_filename = '{}_{}-{}_{}.pdf'.format(
            timezone.now().isoformat()[:10], 'james', 'bond',
            report_template.report.name,
        )
        self.assertEqual(attachment.original_filename, correct_filename)
        self.assertEqual(attachment.file.read(), GENERATED_FILE_CONTENT)
Пример #7
0
    def test_create_correct_data(self):
        carrier = CellularCarrierFactory()

        warehouse = WarehouseFactory()

        user = UserFactory()
        owner = UserFactory()

        features = [SIMCardFeatureFactory() for _ in range(2)]
        expected_features_id = sorted(f.pk for f in features)

        sim_card_data = {
            'status': BackOfficeAssetStatus.new.id,
            'pin1': '1234',
            'pin2': '5678',
            'puk1': '12346',
            'puk2': '56786',
            'card_number': '1938462528298',
            'phone_number': '+4812345678911',
            'warehouse': warehouse.pk,
            'user': user.pk,
            'owner': owner.pk,
            'carrier': carrier.pk,
            'features': expected_features_id
        }

        url = reverse('admin:sim_cards_simcard_add')

        response = self.client.post(url, sim_card_data, follow=True)
        self.assertNotIn('errors', response.context_data)

        sim_card_qs = SIMCard.objects.filter(
            card_number=sim_card_data['card_number'])
        self.assertTrue(sim_card_qs.exists())

        created_sim_card = sim_card_qs.first()
        assigned_features = sorted(f.pk
                                   for f in created_sim_card.features.all())

        self.assertEqual(expected_features_id, assigned_features)
Пример #8
0
 def login_as_user(self, user=None, password='******', *args, **kwargs):
     if not user:
         user = UserFactory(*args, **kwargs)
         user.is_superuser = True
         user.is_staff = True
         user.save()
     self.user = user
     return self.client.login(username=user.username, password=password)
Пример #9
0
 def setUp(self):
     self.dc_1 = DataCenterAssetFactory()
     self.dc_2 = DataCenterAssetFactory()
     self.bo_1 = BackOfficeAssetFactory()
     self.bo_2 = BackOfficeAssetFactory()
     self.support = SupportFactory()
     for obj in [self.dc_1, self.dc_2, self.bo_1, self.bo_2]:
         BaseObjectsSupport.objects.create(support=self.support,
                                           baseobject=obj)
     user = UserFactory()
     self.attachment = Attachment.objects.create_from_file_path(
         __file__, user)
     self.attachment_item = AttachmentItem.objects.attach(
         self.support.pk, get_content_type_for_model(self.support),
         [self.attachment])
Пример #10
0
    def test_filter_access_card_by_access_zone_name(self):
        zones = [AccessZoneFactory() for _ in range(2)]
        cards = [
            AccessCardFactory(issue_date=datetime.now(),
                              user=UserFactory(),
                              owner=UserFactory()) for _ in range(10)
        ]

        cards[0].access_zones.add(zones[0])
        cards[0].save()

        for card in cards[1:]:
            card.access_zones.add(zones[1])
            card.save()

        url = reverse('accesscard-list') + '?access_zones__name={}'.format(
            zones[0].name)

        response = self.client.get(url)
        self.assertEqual(status.HTTP_200_OK, response.status_code)

        self.assertEqual(1, response.data['count'])
        self.assertAccessCardHasCertainFieldsAndValues(
            cards[0], response.data['results'][0])
Пример #11
0
class BackOfficeAssetFormTest(TransitionTestCase, ClientMixin):
    def setUp(self):
        super().setUp()
        self.asset = BackOfficeAssetFactory()
        self.user = UserFactory()
        self.user.is_superuser = False
        self.user.is_staff = True
        self.passwd = 'ralph'
        self.user.set_password(self.passwd)
        # Grant all permissions to the user
        permissions = Permission.objects.exclude(codename__in=[
            'view_backofficeasset_hostname_field',
            'view_backofficeasset_service_env_field',
            'change_backofficeasset_hostname_field',
            'change_backofficeasset_service_env_field',
        ]).all()

        self.user.user_permissions.add(*permissions)
        self.user.regions.add(self.asset.region)
        self.user.save()
        self.login_as_user(user=self.user, password=self.passwd)
        self.data = {
            'hostname':
            self.asset.hostname,
            'model':
            self.asset.model.pk,
            'status':
            self.asset.status,
            'warehouse':
            self.asset.warehouse.pk,
            'region':
            self.asset.region.pk,
            'barcode':
            self.asset.barcode,
            'depreciation_rate':
            0,
            'custom_fields-customfieldvalue-content_type-object_id-INITIAL_FORMS':
            '0',  # noqa: E501
            'custom_fields-customfieldvalue-content_type-object_id-MAX_NUM_FORMS':
            '1000',  # noqa: E501
            'custom_fields-customfieldvalue-content_type-object_id-MIN_NUM_FORMS':
            '0',  # noqa: E501
            'custom_fields-customfieldvalue-content_type-object_id-TOTAL_FORMS':
            '3',  # noqa: E501fhtml
        }

    def test_bo_admin_form_wo_access_to_service_env_and_hostname(self):
        url = reverse('admin:back_office_backofficeasset_change',
                      args=(self.asset.pk, ))
        resp = self.client.get(url, follow=True)

        self.assertEqual(resp.status_code, 200)

        self.assertIn('model', resp.context['adminform'].form.fields)
        self.assertNotIn('hostname', resp.context['adminform'].form.fields)
        self.assertNotIn('service_env', resp.context['adminform'].form.fields)

    @override_settings(BACKOFFICE_HOSTNAME_FIELD_READONLY=1)
    def test_bo_admin_form_with_readonly_hostname(self):
        self.assertTrue(self.login_as_user())
        asset = BackOfficeAssetFactory()

        url = reverse('admin:back_office_backofficeasset_change',
                      args=(asset.pk, ))
        resp = self.client.get(url, follow=True)

        self.assertEqual(resp.status_code, 200)
        self.assertIn('hostname', resp.context['adminform'].form.fields)
        self.assertTrue(
            resp.context['adminform'].form.fields['hostname'].widget.attrs.get(
                'readonly'))

    def test_model_asset_type_back_office_shall_pass(self):
        back_office_model = DataCenterAssetModelFactory(
            type=ObjectModelType.from_name('back_office'))
        self.data.update({'model': back_office_model.pk})
        response = self.client.post(self.asset.get_absolute_url(), self.data)
        self.asset.refresh_from_db()
        self.assertEqual(response.status_code, 302)
        self.assertEqual(self.asset.model, back_office_model)

    def test_model_asset_type_data_center_asset_shall_not_pass(self):
        back_office_model = DataCenterAssetModelFactory(
            type=ObjectModelType.from_name('data_center'))
        self.data.update({'model': back_office_model.pk})
        response = self.client.post(self.asset.get_absolute_url(), self.data)
        self.asset.refresh_from_db()
        self.assertIn('Model must be of', response.content.decode('utf-8'))
        self.assertNotEqual(self.asset.model, back_office_model)
        self.assertEqual(response.status_code, 200)