Exemple #1
0
class StockTakingTests(TestCase, ClientMixin):
    def setUp(self):
        super().setUp()
        self.user1 = factories.UserFactory()
        self.user2 = factories.UserFactory()
        self.service_env = ServiceEnvironmentFactory()
        self.model = BackOfficeAssetModelFactory()
        self.warehouse = WarehouseFactory()
        self.warehouse.stocktaking_enabled = True
        self.warehouse.save()
        self.asset = BackOfficeAssetFactory(
            warehouse=self.warehouse,
            model=self.model,
        )
        self.asset.user = self.user1
        self.asset.save()

        self.base_tag = settings.INVENTORY_TAG
        if self.asset.warehouse.stocktaking_tag_suffix != '':
            self.base_tag = '{prefix}-{warehouse}'.format(
                prefix=self.base_tag,
                warehouse=self.asset.warehouse.stocktaking_tag_suffix,
            )
        self.date_tag = None
        if settings.INVENTORY_TAG_APPEND_DATE:
            self.date_tag = '{base}_{date}'.format(
                base=self.base_tag,
                date=date.today().isoformat(),
            )
        self.tags = [
            self.base_tag,
            settings.INVENTORY_TAG_USER,
            self.date_tag,
        ]

    def test_tag_asset(self):
        self.assertTrue(self.login_as_user(self.user1))
        response = self.client.post(reverse('inventory_tag'), {
            'asset_id': self.asset.id,
            'confirm_button': 'Yes',
            'answer': 'yes'
        },
                                    follow=True)

        self.assertEquals(response.status_code, 200)
        for t in self.tags:
            self.assertIn(t, self.asset.tags.names())

    def test_ownership_verification(self):
        self.assertTrue(self.login_as_user(self.user2))
        response = self.client.post(reverse('inventory_tag'), {
            'asset_id': self.asset.id,
            'confirm_button': 'Yes',
            'answer': 'yes'
        },
                                    follow=True)
        self.assertEquals(response.status_code, 403)
Exemple #2
0
class TestBackOfficeAsset(RalphTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.region_pl = RegionFactory(name='PL', country=Country.pl)
        cls.region_us = RegionFactory(name='US', country=Country.us)
        cls.category = CategoryFactory(code='PC')
        cls.category_without_code = CategoryFactory()
        cls.model = BackOfficeAssetModelFactory(category=cls.category)
        cls.model_without_code = BackOfficeAssetModelFactory(
            category=cls.category_without_code)

    def setUp(self):
        super().setUp()
        AssetLastHostname.objects.create(prefix='POLPC', counter=1000)
        self.bo_asset = BackOfficeAssetFactory(
            model=self.model,
            hostname='abc',
            region=self.region_pl,
        )
        self.bo_asset_2 = BackOfficeAssetFactory(
            model=self.model,
            hostname='abc2',
            region=self.region_pl,
            status=BackOfficeAssetStatus.liquidated.id,
            invoice_date=None)
        self.bo_asset_3 = BackOfficeAssetFactory(
            model=self.model,
            hostname='abc3',
            region=self.region_pl,
            status=BackOfficeAssetStatus.liquidated.id,
            invoice_date=datetime(2016, 1, 11).date(),
            depreciation_rate=50)
        self.bo_asset_4 = BackOfficeAssetFactory(
            model=self.model,
            hostname='abc3',
            region=self.region_pl,
            status=BackOfficeAssetStatus.liquidated.id,
            invoice_date=datetime(2016, 1, 11).date(),
            depreciation_end_date=datetime(2015, 1, 11).date(),
            depreciation_rate=50)
        self.category_parent = CategoryFactory(code='Mob1',
                                               default_depreciation_rate=30)
        self.category_2 = CategoryFactory(code='Mob2',
                                          default_depreciation_rate=25)
        self.category_3 = CategoryFactory(code='Mob3',
                                          parent=self.category_parent,
                                          default_depreciation_rate=0)

    def test_try_assign_hostname(self):
        self.bo_asset._try_assign_hostname(commit=True)
        self.assertEqual(self.bo_asset.hostname, 'POLPC01001')

    def test_try_assign_hostname_no_change(self):
        self.bo_asset.hostname = 'POLPC01001'
        self.bo_asset.save()
        self.bo_asset._try_assign_hostname(commit=True)
        self.assertEqual(self.bo_asset.hostname, 'POLPC01001')

    def test_try_assign_hostname_no_hostname(self):
        self.bo_asset.hostname = ''
        self.bo_asset.save()
        self.bo_asset._try_assign_hostname(commit=True)
        self.assertEqual(self.bo_asset.hostname, 'POLPC01001')

    def test_try_assign_hostname_forced(self):
        self.bo_asset.hostname = 'POLPC001010'
        self.bo_asset.save()
        self.bo_asset._try_assign_hostname(commit=True, force=True)
        self.assertEqual(self.bo_asset.hostname, 'POLPC01001')

    def test_try_assign_hostname_with_country(self):
        self.bo_asset._try_assign_hostname(country='US', commit=True)
        self.assertEqual(self.bo_asset.hostname, 'USPC00001')

    def test_try_assign_hostname_category_without_code(self):
        bo_asset_2 = BackOfficeAssetFactory(model=self.model_without_code,
                                            hostname='abcd')
        bo_asset_2._try_assign_hostname(commit=True)
        self.assertEqual(bo_asset_2.hostname, 'abcd')

    def test_get_autocomplete_queryset(self):
        queryset = BackOfficeAsset.get_autocomplete_queryset()
        self.assertEquals(1, queryset.count())

    def test_buyout_date(self):
        self.assertEqual(self.bo_asset_3.buyout_date,
                         datetime(2018, 2, 11).date())

        self.assertEqual(self.bo_asset_2.buyout_date, None)

    def test_butout_date_with_depreciation_end_date(self):
        self.assertEqual(self.bo_asset_4.buyout_date,
                         datetime(2015, 1, 11).date())

    def test_get_depreciation_rate(self):
        self.assertEqual(self.category_2.get_default_depreciation_rate(), 25)
        self.assertEqual(self.category_3.get_default_depreciation_rate(), 30)
Exemple #3
0
class BackOfficeAssetAPITests(RalphAPITestCase):
    def setUp(self):
        super().setUp()
        self.service_env = ServiceEnvironmentFactory()
        self.model = BackOfficeAssetModelFactory()
        self.warehouse = WarehouseFactory()
        self.bo_asset = BackOfficeAssetFactory(
            warehouse=self.warehouse,
            model=self.model,
        )
        self.bo_asset.user = self.user1
        self.bo_asset.owner = self.user2
        self.bo_asset.save()

    def test_get_back_office_assets_list(self):
        url = reverse('backofficeasset-list')
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'],
                         BackOfficeAsset.objects.count())

    def test_get_back_office_asset_details(self):
        url = reverse('backofficeasset-detail', args=(self.bo_asset.id, ))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['hostname'], self.bo_asset.hostname)
        self.assertEqual(response.data['user']['id'], self.bo_asset.user.id)
        self.assertEqual(response.data['owner']['id'], self.bo_asset.owner.id)
        self.assertEqual(response.data['warehouse']['id'],
                         self.bo_asset.warehouse.id)
        self.assertEqual(response.data['model']['id'], self.bo_asset.model.id)

    def test_create_back_office_asset(self):
        region = Region.objects.create(name='EU')
        url = reverse('backofficeasset-list')
        data = {
            'hostname': '12345',
            'user': self.user1.id,
            'owner': self.superuser.id,
            'region': region.id,
            'warehouse': self.warehouse.id,
            'model': self.model.id,
            'service_env': self.service_env.id,
            'force_depreciation': False,
        }
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        bo_asset = BackOfficeAsset.objects.get(pk=response.data['id'])
        self.assertEqual(bo_asset.hostname, '12345')
        self.assertEqual(bo_asset.user, self.user1)
        self.assertEqual(bo_asset.owner, self.superuser)
        self.assertEqual(bo_asset.region, region)
        self.assertEqual(bo_asset.warehouse, self.warehouse)
        self.assertEqual(bo_asset.service_env, self.service_env)

    def test_patch_back_office_asset(self):
        url = reverse('backofficeasset-detail', args=(self.bo_asset.id, ))
        data = {
            'user': self.user2.id,
            'owner': None,
            'force_depreciation': True,
        }
        response = self.client.patch(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.bo_asset.refresh_from_db()
        self.assertEqual(self.bo_asset.user, self.user2)
        self.assertIsNone(self.bo_asset.owner)
        self.assertTrue(self.bo_asset.force_depreciation)