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']))
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)
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 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)
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)
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])
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)
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)
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])
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])