Ejemplo n.º 1
0
class APITest(BaseAPITest):
    @classmethod
    def setUpClass(cls):
        super(APITest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(APITest, self).setUp()
        self.email = '*****@*****.**'
        self.password = '******'
        self.user = CtsUserFactory(email=self.email,
                                   password=self.password)

    def test_api_root(self):
        # we should get back a nice JSON listing of what's available
        rsp = self.call_api('/api/')
        self.assertEqual(200, rsp.status_code)
        data_json = rsp.content
        data = json.loads(data_json)
        self.assertIn(u'auth/users', data)
        self.assertIn(u'catalog/items', data)

    def test_api_users(self):
        # The users API should list our test user
        rsp = self.call_api('/api/auth/users/')
        self.assertEqual(200, rsp.status_code)
        data = json.loads(rsp.content)['results']
        user = data[0]
        self.assertEqual(user['email'], self.email)
        self.assertEqual(user['is_active'], True)
        self.assertEqual(user['is_superuser'], False)

    def test_auth(self):
        rsp = self.call_api('/api/auth/users/', token='invalidkey')
        self.assertEqual(401, rsp.status_code)

    def test_model_permissions(self):
        self.user.role = ROLE_PARTNER  # No permissions on users
        self.user.save()
        rsp = self.call_api('/api/auth/users/')
        self.assertEqual(403, rsp.status_code)

    def test_post_user(self):
        # The API is read-only. Any POST should return a 405
        user = CtsUserFactory(name="barney fife")
        rsp = self.call_api('/api/auth/users/%d/' % user.pk)
        self.assertEqual(200, rsp.status_code)
        data = json.loads(rsp.content)
        data['name'] = "Freddy Fife"
        rsp = self.post_api('/api/auth/users/%d/' % user.pk, data=data)
        self.assertEqual(405, rsp.status_code)
Ejemplo n.º 2
0
 def test_is_just_partner(self):
     user = CtsUserFactory(role=ROLE_PARTNER, is_superuser=False)
     self.assertTrue(user.is_just_partner())
     user.role = ROLE_COORDINATOR
     user.save()
     self.assertFalse(user.is_just_partner())
     user.role = ROLE_PARTNER
     user.is_superuser = True
     user.save()
     self.assertFalse(user.is_just_partner())
     user = CtsUserFactory(role=ROLE_PARTNER, is_superuser=False)
     self.assertTrue(user.is_just_partner())
     manager_group = Group.objects.get(name=ROLE_MANAGER)
     user.groups.add(manager_group)
     self.assertFalse(user.is_just_partner())
Ejemplo n.º 3
0
 def test_is_just_partner(self):
     user = CtsUserFactory(role=ROLE_PARTNER, is_superuser=False)
     self.assertTrue(user.is_just_partner())
     user.role = ROLE_COORDINATOR
     user.save()
     self.assertFalse(user.is_just_partner())
     user.role = ROLE_PARTNER
     user.is_superuser = True
     user.save()
     self.assertFalse(user.is_just_partner())
     user = CtsUserFactory(role=ROLE_PARTNER, is_superuser=False)
     self.assertTrue(user.is_just_partner())
     manager_group = Group.objects.get(name=ROLE_MANAGER)
     user.groups.add(manager_group)
     self.assertFalse(user.is_just_partner())
Ejemplo n.º 4
0
class TestReportList(TestCase):
    url_name = 'reports_list'
    template_name = 'reports/reports_list.html'

    def setUp(self):
        super(TestReportList, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**")
        self.user.set_password("password")
        self.user.save()
        assert self.client.login(email="*****@*****.**", password="******")

    def get_expected_reports(self):
        """Update this list each time a report is added or removed."""
        return [
            PackageReport,
            DonorByShipmentReport,
            DonorByCategoryReport,
            ItemReport,
            ShipmentReport,
            ReceivedItemsByShipmentReport,
            ReceivedItemsByDonorOrPartnerReport,
            ShipmentMonthlySummaryReport,
            # add new reports above the test report
            BadReportClassForTesting,
        ]

    def test_unauthenticated(self):
        """View requires authentication."""
        self.client.logout()
        response = self.client.get(reverse(self.url_name))
        self.assertEqual(response.status_code, 302)

    def test_expected_reports(self):
        """Basic check that the correct reports are returned."""
        response = self.client.get(reverse(self.url_name))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, self.template_name)
        self.assertTrue('reports' in response.context)
        expected = self.get_expected_reports()
        actual = response.context['reports']
        self.assertEqual([c.__name__ for c in actual],
                         [c.__name__ for c in expected])
Ejemplo n.º 5
0
 def test_save_sets_groups(self):
     user = CtsUserFactory(role=ROLE_PARTNER)
     self.assertTrue(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_COORDINATOR).exists())
     user.role = ROLE_OFFICER
     user.save()
     self.assertFalse(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertTrue(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_COORDINATOR).exists())
     user.role = ROLE_COORDINATOR
     user.save()
     self.assertFalse(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertTrue(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertTrue(user.groups.filter(name=ROLE_COORDINATOR).exists())
     user.role = ROLE_PARTNER
     user.save()
     self.assertTrue(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_COORDINATOR).exists())
Ejemplo n.º 6
0
 def test_save_sets_groups(self):
     user = CtsUserFactory(role=ROLE_PARTNER)
     self.assertTrue(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_COORDINATOR).exists())
     user.role = ROLE_OFFICER
     user.save()
     self.assertFalse(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertTrue(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_COORDINATOR).exists())
     user.role = ROLE_COORDINATOR
     user.save()
     self.assertFalse(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertTrue(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertTrue(user.groups.filter(name=ROLE_COORDINATOR).exists())
     user.role = ROLE_PARTNER
     user.save()
     self.assertTrue(user.groups.filter(name=ROLE_PARTNER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_OFFICER).exists())
     self.assertFalse(user.groups.filter(name=ROLE_COORDINATOR).exists())
Ejemplo n.º 7
0
class DonorViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(DonorViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(DonorViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**", password="******")
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('donor_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'donor_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need coordinator role
        self.user.role = ROLE_OFFICER
        self.user.save()
        rsp = self.client.get(reverse('donor_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'donor_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        donor = DonorFactory()
        rsp = self.client.get(reverse('donor_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp,
                            donor.name,
                            msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a donor
        # Get the form page
        url = reverse('new_donor_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        Donor.objects.get(name='test')

    def test_update(self):
        # Update a donor
        donor = DonorFactory()
        url = reverse('edit_donor_modal', args=[donor.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        donor2 = Donor.objects.get(name=name)
        self.assertEqual(donor.pk, donor2.pk)

    def test_update_and_create_new_codes(self):
        # Update a donor, create some new codes, and add them to the donor
        donor = DonorFactory()
        url = reverse('edit_donor_modal', args=[donor.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        t1_codeA = DonorCode.objects.create(code='t1A',
                                            donor_code_type=DonorCode.T1)
        t1_codeB = DonorCode.objects.create(code='t1B',
                                            donor_code_type=DonorCode.T1)
        t3_codeA = DonorCode.objects.create(code='t1A',
                                            donor_code_type=DonorCode.T3)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
            't1_codes_1': [t1_codeA.pk, t1_codeB.pk],
            'new_t1_codes': 'New code C, t1A',
            't3_codes_1': [t3_codeA.pk],
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        donor2 = Donor.objects.get(name=name)
        self.assertEqual(donor.pk, donor2.pk)
        code_c = DonorCode.objects.get(code='New code C',
                                       donor_code_type=DonorCode.T1)
        self.assertIn(code_c, donor2.t1_codes.all())
        self.assertIn(t1_codeA, donor2.t1_codes.all())
        self.assertIn(t3_codeA, donor2.t3_codes.all())

    def test_delete(self):
        # Delete a donor
        donor = DonorFactory()
        url = reverse('donor_delete', args=[donor.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        Donor.objects.get(pk=donor.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('donor_list'))
        self.assertFalse(Donor.objects.filter(pk=donor.pk).exists())

    def test_edit_modal(self):
        donor = DonorFactory()
        url = reverse('edit_donor_modal', kwargs={'pk': donor.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/donor_edit_modal.html')
Ejemplo n.º 8
0
class TransporterViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        bootstrap_permissions()

    def setUp(self):
        self.user = CtsUserFactory(email="*****@*****.**", password="******")
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('transporter_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('transporter_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need coordinator role
        self.user.role = ROLE_OFFICER
        self.user.save()
        rsp = self.client.get(reverse('transporter_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('transporter_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        transporter = TransporterFactory()
        rsp = self.client.get(reverse('transporter_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, transporter.name, msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a transporter
        # Get the form page
        url = reverse('new_transporter_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        Transporter.objects.get(name='test')

    def test_update(self):
        # Update a transporter
        transporter = TransporterFactory()
        url = reverse('edit_transporter_modal', args=[transporter.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        transporter2 = Transporter.objects.get(name=name)
        self.assertEqual(transporter.pk, transporter2.pk)

    def test_delete(self):
        # Delete a transporter
        transporter = TransporterFactory()
        url = reverse('transporter_delete', args=[transporter.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        Transporter.objects.get(pk=transporter.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('transporter_list'))
        self.assertFalse(Transporter.objects.filter(pk=transporter.pk).exists())

    def test_edit_modal(self):
        transporter = TransporterFactory()
        url = reverse('edit_transporter_modal', kwargs={'pk': transporter.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/transporter_edit_modal.html')
Ejemplo n.º 9
0
class ReportTestMixin(object):
    report_class = None
    template_name = 'reports/report.html'

    important_date = date(1972, 11, 3)
    day_before = date(1972, 11, 2)
    day_after = date(1972, 11, 4)

    def setUp(self):
        self.user = CtsUserFactory(email="*****@*****.**")
        self.user.set_password("password")
        self.user.save()
        assert self.client.login(email="*****@*****.**", password="******")
        self.url = reverse(self.report_class.get_report_url_name())

    @classmethod
    def setUpClass(cls):
        bootstrap_permissions()

        cls.partner1 = PartnerFactory()
        cls.partner2 = PartnerFactory()
        cls.partner3 = PartnerFactory()

        cls.donor1 = DonorFactory()
        cls.donor2 = DonorFactory()
        cls.donor3 = DonorFactory()

        cls.category1 = ItemCategoryFactory()
        cls.category2 = ItemCategoryFactory()
        cls.category3 = ItemCategoryFactory()

        cls.shipment1 = ShipmentFactory(partner=cls.partner1,
                                        shipment_date=cls.day_before,
                                        status=Shipment.STATUS_IN_TRANSIT)

        cls.package1 = PackageFactory(shipment=cls.shipment1,
                                      status=Shipment.STATUS_IN_TRANSIT)
        cls.item1 = PackageItemFactory(package=cls.package1, donor=cls.donor1,
                                       item_category=cls.category1)

        cls.shipment2 = ShipmentFactory(partner=cls.partner2,
                                        shipment_date=cls.important_date,
                                        status=Shipment.STATUS_RECEIVED)
        cls.package2 = PackageFactory(shipment=cls.shipment2,
                                      status=Shipment.STATUS_RECEIVED)
        cls.item2 = PackageItemFactory(package=cls.package2, donor=cls.donor2,
                                       item_category=cls.category2)

        cls.shipment3 = ShipmentFactory(partner=cls.partner3,
                                        shipment_date=cls.day_after,
                                        status=Shipment.STATUS_CANCELED)
        cls.package3 = PackageFactory(shipment=cls.shipment3,
                                      status=Shipment.STATUS_CANCELED)
        cls.item3 = PackageItemFactory(package=cls.package3, donor=cls.donor3,
                                       item_category=cls.category3)

    @classmethod
    def tearDownClass(cls):
        PackageItem.objects.all().delete()
        CatalogItem.objects.all().delete()
        Package.objects.all().delete()
        Shipment.objects.all().delete()
        CtsUser.objects.all().delete()
        ItemCategory.objects.all().delete()
        Donor.objects.all().delete()

    def ajax_get(self, *args, **kwargs):
        """Like self.client.get, but looks like it came via ajax"""
        kwargs['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
        return self.client.get(*args, **kwargs)

    def csv_get(self, url, *args, **kwargs):
        """Like self.client.get, but asks for response as CSV"""
        parm_name = "%s-%s" % (DEFAULT_PARAM_PREFIX,
                               self.report_class.table_class.__name__.lower())
        if "?" in url:
            url = url + "&" + parm_name + "=csv"
        else:
            url = url + "?" + parm_name + "=csv"
        return self.client.get(url, *args, **kwargs)

    def test_200(self):
        rsp = self.client.get(self.url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, self.template_name)

    def test_ajax_200(self):
        rsp = self.ajax_get(self.url)
        self.assertEqual(200, rsp.status_code)
        self.assertEqual(rsp['content-type'], 'text/html; charset=utf-8')
        self.assertTemplateUsed(rsp, self.ajax_template_name)

    def test_report_title(self):
        self.assertEqual(self.expected_report_title, self.report_class.get_report_title())

    def test_csv_200(self):
        rsp = self.csv_get(self.url)
        self.assertEqual(200, rsp.status_code)
        self.assertEqual(rsp['content-type'], 'text/csv; charset=utf-8')
Ejemplo n.º 10
0
class CatalogViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(CatalogViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(CatalogViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**", password="******", role=ROLE_OFFICER)
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('catalog_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('catalog_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need OFFICER to work with catalog
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('catalog_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('catalog_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        cat = ItemCategoryFactory()
        description = "The meow of a cat"
        CatalogItemFactory(item_category=cat, description=description)
        rsp = self.client.get(reverse('catalog_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, description, msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create an item
        ItemCategoryFactory(name="horse")
        ItemCategoryFactory(name="snowcat")
        cat = ItemCategoryFactory(name="cat")
        ItemCategoryFactory(name="catapult")
        description = "The meow of a cat"
        # Get the form page
        url = reverse('new_catalog_item_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'item_code': '007',
            'description': description,
            'unit': 'purr',
            'weight': str(1.005),
            'price_local': '1.23',
            'price_usd': '4.072',
            'item_category': cat.name.upper(),
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        item = CatalogItem.objects.get(description=description)
        self.assertEqual(cat, item.item_category)
        self.assertEqual('007', item.item_code)
        self.assertEqual(1.005, item.weight)
        self.assertEqual(Decimal('4.072'), item.price_usd)

    def test_update(self):
        # Update an item
        cat = ItemCategoryFactory()
        description = "The meow of a cat"
        item = CatalogItemFactory(item_category=cat, description=description)
        url = reverse('edit_catalog_item_modal', args=[item.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        description2 = "The bark of a dog"
        data = {
            'item_code': item.item_code,
            'description': description2,
            'unit': 'purr',
            'weight': str(1.005),
            'price_local': '1.23',
            'price_usd': '4.07',
            'item_category': cat.pk,
            'quantity': 3,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        item2 = CatalogItem.objects.get(description=description2)
        self.assertEqual(item.pk, item2.pk)

    def test_delete(self):
        # Delete an item
        cat = ItemCategoryFactory()
        description = "The meow of a cat"
        item = CatalogItemFactory(item_category=cat, description=description)
        url = reverse('catalog_delete', args=[item.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        CatalogItem.objects.get(pk=item.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('catalog_list'))
        self.assertFalse(CatalogItem.objects.filter(pk=item.pk).exists())

    def test_price_validator(self):
        # No negative price
        cat = ItemCategoryFactory()
        price_local = 5.000
        item = CatalogItemFactory(item_category=cat, price_local=price_local)
        url = reverse('edit_catalog_item_modal', args=[item.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        data = {
            'item_code': item.item_code,
            'description': 'desc',
            'unit': 'purr',
            'weight': str(item.weight),
            'price_local': '-1.23',
            'price_usd': '4.07',
            'item_category': cat.pk,
        }
        rsp = self.client.post(url, data=data)
        self.assertEqual(400, rsp.status_code)
        self.assertIn('Ensure this value is greater than', rsp.context['form'].errors.as_text())

    def test_import_view_error(self):
        # Catalog import raises an error. We respond with 400 and the page displays the error.
        url = reverse('catalog_import_modal')

        # We're going to mock the catalog import, so the spreadsheet code will
        # never look at the "file" we upload. But the form validation will at least
        # check that it's not empty, so we need to upload at least 1 byte to get
        # past that.

        fp = io.BytesIO(b'1')
        setattr(fp, 'name', 'fakefilename.xls')
        with patch('catalog.views.catalog_import') as mock_import:
            mock_import.side_effect = CatalogImportFailure(["Killer bunny"])
            rsp = self.client.post(url, {'file': fp})
        self.assertContains(rsp, "Killer bunny", status_code=400)

    def test_import_view_okay(self):
        # Catalog import succeeds, apparently. We display how many records were "imported".
        url = reverse('catalog_import_modal')

        fp = io.BytesIO(b'1')
        setattr(fp, 'name', 'fakefilename.xls')
        with patch('catalog.views.catalog_import') as mock_import:
            mock_import.return_value = 666
            rsp = self.client.post(url, {'file': fp}, follow=True)
        self.assertContains(rsp, '666')

    @override_settings(FILE_UPLOAD_MAX_MEMORY_SIZE=1000)
    def test_import_large_file(self):
        # Not actually importing the large file, but making sure the view can handle it
        url = reverse('catalog_import_modal')
        path = None
        try:
            tmpf = NamedTemporaryFile(mode='wb', delete=False)
            path = tmpf.name
            tmpf.write(b'1' * 1001)
            tmpf.close()
            fp = open(path, 'rb')
            with patch('catalog.views.catalog_import') as mock_import:
                mock_import.return_value = 666
                rsp = self.client.post(url, {'file': fp}, follow=True)
            self.assertContains(rsp, '666')
        finally:
            if path:
                os.remove(path)

    def test_edit_modal(self):
        item = CatalogItemFactory()
        url = reverse('edit_catalog_item_modal', kwargs={'pk': item.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/catalogitem_edit_modal.html')

    def test_add_item_to_kit(self):
        item1 = CatalogItemFactory()
        kit = KitFactory()
        quantity = 500
        url = reverse('add_item_to_kit', args=[kit.pk, item1.pk, quantity])
        rsp = self.client.post(url)
        self.assertEqual(200, rsp.status_code)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item1)
        self.assertEqual(quantity, kit_item.quantity)

    def test_add_more_item_to_kit(self):
        item1 = CatalogItemFactory()
        kit = KitFactory()
        # Start with some in the kit
        KitItemFactory(kit=kit, catalog_item=item1, quantity=100)
        quantity = 500
        url = reverse('add_item_to_kit', args=[kit.pk, item1.pk, quantity])
        rsp = self.client.post(url)
        self.assertEqual(200, rsp.status_code)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item1)
        self.assertEqual(600, kit_item.quantity)

    def test_add_too_many_of_item_to_kit(self):
        item1 = CatalogItemFactory()
        kit = KitFactory()
        quantity = MAX_QUANTITY + 1
        url = reverse('add_item_to_kit', args=[kit.pk, item1.pk, quantity])
        rsp = self.client.post(url)
        self.assertEqual(400, rsp.status_code)

    def test_add_items_to_kit(self):
        item1 = CatalogItemFactory()
        item2 = CatalogItemFactory()
        kit = KitFactory()
        quantity1 = 50
        quantity2 = 60
        url = reverse('catalog_list')
        data = {
            'selected_kit': kit.pk,
            'quantity-%d' % item1.pk: quantity1,
            'quantity-%d' % item2.pk: quantity2,
        }
        rsp = self.client.post(url, data)
        self.assertEqual(302, rsp.status_code)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item1)
        self.assertEqual(quantity1, kit_item.quantity)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item2)
        self.assertEqual(quantity2, kit_item.quantity)

    def test_add_too_many_items_to_kit(self):
        item1 = CatalogItemFactory()
        item2 = CatalogItemFactory()
        kit = KitFactory()
        quantity1 = 50
        quantity2 = MAX_QUANTITY + 1
        url = reverse('catalog_list')
        data = {
            'selected_kit': kit.pk,
            'quantity-%d' % item1.pk: quantity1,
            'quantity-%d' % item2.pk: quantity2,
        }
        rsp = self.client.post(url, data)
        self.assertEqual(400, rsp.status_code)
        with self.assertRaises(KitItem.DoesNotExist):
            KitItem.objects.get(kit=kit, catalog_item=item1)
        with self.assertRaises(KitItem.DoesNotExist):
            KitItem.objects.get(kit=kit, catalog_item=item2)
Ejemplo n.º 11
0
class CtsUserViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        bootstrap_permissions()

    def setUp(self):
        self.user = CtsUserFactory(email='*****@*****.**', password='******')
        assert self.client.login(email='*****@*****.**', password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('user_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'user_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        user = CtsUserFactory()
        rsp = self.client.get(reverse('user_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp,
                            user.name,
                            msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a user
        # Get the form page
        url = reverse('new_cts_user_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
            'email': '*****@*****.**',
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'is_active': True,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        user = CtsUser.objects.get(email='*****@*****.**')
        self.assertEqual(user.role, ROLE_PARTNER)
        # email was sent to set password
        # Test that one message has been sent.
        self.assertEqual(len(mail.outbox), 1)
        # Verify that the subject of the first message is correct.
        self.assertIn("New account", mail.outbox[0].subject)

    def test_create_duplicate_email(self):
        data = {
            'name': 'test',
            'email': '*****@*****.**',
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'is_active': True,
        }
        CtsUserFactory(email=data['email'])
        url = reverse('new_cts_user_modal')
        rsp = self.client.post(url, data=data)
        self.assertEqual(400, rsp.status_code)

    def test_create_with_password(self):
        # Create a user and specify a password
        # Get the form page
        url = reverse('new_cts_user_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        new_password = '******'
        data = {
            'name': 'test',
            'email': '*****@*****.**',
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'is_active': True,
            'password1': new_password,
            'password2': new_password,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        user = CtsUser.objects.get(email='*****@*****.**')
        self.assertEqual(user.role, ROLE_PARTNER)
        # email was NOT sent to set password
        # Test that no message has been sent.
        self.assertEqual(len(mail.outbox), 0)
        # Validate password
        self.assertEqual(
            user, authenticate(email='*****@*****.**', password=new_password))

    def test_update(self):
        # Update a user
        user = self.user
        url = reverse('edit_cts_user_modal', args=[user.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        email = "*****@*****.**"
        data = {
            'name': 'test',
            'email': email,
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        user2 = CtsUser.objects.get(email=email)
        self.assertEqual(user.pk, user2.pk)
        self.assertEqual(user2.role, ROLE_PARTNER)

    def test_password_change(self):
        # Change a user's password
        old_password = '******'
        user = self.user
        old_email = user.email
        self.assertEqual(user,
                         authenticate(email=old_email, password=old_password))
        url = reverse('edit_cts_user_modal', args=[user.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        new_email = "*****@*****.**"
        new_password = '******'
        data = {
            'name': 'test',
            'email': new_email,
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'password1': new_password,
            'password2': new_password,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        user = CtsUser.objects.get(pk=user.pk)
        self.assertEqual(user,
                         authenticate(email=new_email, password=new_password))

    def test_delete(self):
        # Delete a user
        # Need to be logged in as one user, then delete another user
        user = CtsUserFactory()
        url = reverse('user_delete', args=[user.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        CtsUser.objects.get(pk=user.pk)
        # Now delete it!
        # Should really just change them to not active.
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('user_list'))
        user = CtsUser.objects.get(pk=user.pk)
        self.assertFalse(user.is_active)

    def test_edit_modal(self):
        user = self.user
        url = reverse('edit_cts_user_modal', kwargs={'pk': user.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'accounts/ctsuser_edit_modal.html')

    def test_reset_api_token_get(self):
        user = self.user
        url = reverse('reset_api_token', kwargs={'pk': user.pk})
        rsp = self.client.get(url)
        self.assertEqual(405, rsp.status_code)

    def test_reset_api_token_post(self):
        user = self.user
        original_token = user.auth_token.key
        url = reverse('reset_api_token', kwargs={'pk': user.pk})
        rsp = self.client.post(url)
        self.assertRedirects(rsp, reverse('user_list'))
        user = CtsUser.objects.get(pk=user.pk)
        self.assertNotEqual(original_token, user.auth_token.key)

    def test_reset_api_token_post_without_permission(self):
        self.user.role = ROLE_PARTNER  # least permissions
        self.user.save()
        user = CtsUserFactory()
        original_token = user.auth_token.key
        url = reverse('reset_api_token', kwargs={'pk': user.pk})
        rsp = self.client.post(url)
        self.assertEqual(403, rsp.status_code)
        user = CtsUser.objects.get(pk=user.pk)
        self.assertEqual(original_token, user.auth_token.key)

    def test_home_view_not_logged_in(self):
        self.client.logout()
        rsp = self.client.get(reverse('home'))
        self.assertRedirects(rsp, reverse('account_login'))

    def test_home_view_with_catalog_perms(self):
        self.user.role = ROLE_OFFICER
        self.user.save()
        rsp = self.client.get(reverse('home'))
        self.assertRedirects(rsp, reverse('catalog_list'))

    def test_home_view_without_catalog_perms(self):
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('home'))
        self.assertRedirects(rsp, reverse('reports_list'))
Ejemplo n.º 12
0
class CategoryViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(CategoryViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(CategoryViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**", password="******", role=ROLE_OFFICER)
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('category_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('category_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need officer role
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('category_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('category_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        category = ItemCategoryFactory()
        rsp = self.client.get(reverse('category_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, category.name, msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a category
        # Get the form page
        url = reverse('new_category_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        ItemCategory.objects.get(name='test')

    def test_update(self):
        # Update a category
        category = ItemCategoryFactory()
        url = reverse('edit_category_modal', args=[category.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        category2 = ItemCategory.objects.get(name=name)
        self.assertEqual(category.pk, category2.pk)

    def test_delete(self):
        # Delete a category
        category = ItemCategoryFactory()
        url = reverse('category_delete', args=[category.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        ItemCategory.objects.get(pk=category.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('category_list'))
        self.assertFalse(ItemCategory.objects.filter(pk=category.pk).exists())

    def test_edit_modal(self):
        category = ItemCategoryFactory()
        url = reverse('edit_category_modal', kwargs={'pk': category.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/category_edit_modal.html')
Ejemplo n.º 13
0
class DonorCodeViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(DonorCodeViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(DonorCodeViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**",
                                   password="******",
                                   role=ROLE_COORDINATOR)
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('donorcode_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'donorcode_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need officer role
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('donorcode_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'donorcode_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        donorcode = DonorCodeT1Factory()
        rsp = self.client.get(reverse('donorcode_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp,
                            donorcode.code,
                            msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a donorcode
        # Get the form page
        url = reverse('new_donorcode_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'code': 'test',
            'donor_code_type': 't1',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        DonorCode.objects.get(code='test')

    def test_update(self):
        # Update a donorcode
        donorcode = DonorCodeT1Factory()
        url = reverse('edit_donorcode_modal', args=[donorcode.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        code = "edited"
        data = {
            'code': code,
            'donor_code_type': 't1',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        donorcode2 = DonorCode.objects.get(code=code)
        self.assertEqual(donorcode.pk, donorcode2.pk)

    def test_delete(self):
        # Delete a donorcode
        donorcode = DonorCodeT1Factory()
        url = reverse('donorcode_delete', args=[donorcode.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        DonorCode.objects.get(pk=donorcode.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('donorcode_list'))
        self.assertFalse(DonorCode.objects.filter(pk=donorcode.pk).exists())

    def test_edit_modal(self):
        donorcode = DonorCodeT1Factory()
        url = reverse('edit_donorcode_modal', kwargs={'pk': donorcode.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/donorcode_edit_modal.html')
Ejemplo n.º 14
0
class CtsUserViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(CtsUserViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(CtsUserViewTest, self).setUp()
        self.user = CtsUserFactory(email='*****@*****.**', password='******')
        assert self.client.login(email='*****@*****.**', password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('user_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('user_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        user = CtsUserFactory()
        rsp = self.client.get(reverse('user_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, user.name, msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a user
        # Get the form page
        url = reverse('new_cts_user_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
            'email': '*****@*****.**',
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'is_active': True,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        user = CtsUser.objects.get(email='*****@*****.**')
        self.assertEqual(user.role, ROLE_PARTNER)
        # email was sent to set password
        # Test that one message has been sent.
        self.assertEqual(len(mail.outbox), 1)
        # Verify that the subject of the first message is correct.
        self.assertIn("New account", mail.outbox[0].subject)

    def test_create_duplicate_email(self):
        data = {
            'name': 'test',
            'email': '*****@*****.**',
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'is_active': True,
        }
        CtsUserFactory(email=data['email'])
        url = reverse('new_cts_user_modal')
        rsp = self.client.post(url, data=data)
        self.assertEqual(400, rsp.status_code)

    def test_create_with_password(self):
        # Create a user and specify a password
        # Get the form page
        url = reverse('new_cts_user_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        new_password = '******'
        data = {
            'name': 'test',
            'email': '*****@*****.**',
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'is_active': True,
            'password1': new_password,
            'password2': new_password,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        user = CtsUser.objects.get(email='*****@*****.**')
        self.assertEqual(user.role, ROLE_PARTNER)
        # email was NOT sent to set password
        # Test that no message has been sent.
        self.assertEqual(len(mail.outbox), 0)
        # Validate password
        self.assertEqual(user, authenticate(email='*****@*****.**', password=new_password))

    def test_update(self):
        # Update a user
        user = self.user
        url = reverse('edit_cts_user_modal', args=[user.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        email = "*****@*****.**"
        data = {
            'name': 'test',
            'email': email,
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        user2 = CtsUser.objects.get(email=email)
        self.assertEqual(user.pk, user2.pk)
        self.assertEqual(user2.role, ROLE_PARTNER)

    def test_password_change(self):
        # Change a user's password
        old_password = '******'
        user = self.user
        old_email = user.email
        self.assertEqual(user, authenticate(email=old_email, password=old_password))
        url = reverse('edit_cts_user_modal', args=[user.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        new_email = "*****@*****.**"
        new_password = '******'
        data = {
            'name': 'test',
            'email': new_email,
            'mobile': '999',
            'skype': 'testtest',
            'role': ROLE_PARTNER,
            'password1': new_password,
            'password2': new_password,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        user = CtsUser.objects.get(pk=user.pk)
        self.assertEqual(user, authenticate(email=new_email, password=new_password))

    def test_delete(self):
        # Delete a user
        # Need to be logged in as one user, then delete another user
        user = CtsUserFactory()
        url = reverse('user_delete', args=[user.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        CtsUser.objects.get(pk=user.pk)
        # Now delete it!
        # Should really just change them to not active.
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('user_list'))
        user = CtsUser.objects.get(pk=user.pk)
        self.assertFalse(user.is_active)

    def test_edit_modal(self):
        user = self.user
        url = reverse('edit_cts_user_modal', kwargs={'pk': user.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'accounts/ctsuser_edit_modal.html')

    def test_reset_api_token_get(self):
        user = self.user
        url = reverse('reset_api_token', kwargs={'pk': user.pk})
        rsp = self.client.get(url)
        self.assertEqual(405, rsp.status_code)

    def test_reset_api_token_post(self):
        user = self.user
        original_token = user.auth_token.key
        url = reverse('reset_api_token', kwargs={'pk': user.pk})
        rsp = self.client.post(url)
        self.assertRedirects(rsp, reverse('user_list'))
        user = CtsUser.objects.get(pk=user.pk)
        self.assertNotEqual(original_token, user.auth_token.key)

    def test_reset_api_token_post_without_permission(self):
        self.user.role = ROLE_PARTNER  # least permissions
        self.user.save()
        user = CtsUserFactory()
        original_token = user.auth_token.key
        url = reverse('reset_api_token', kwargs={'pk': user.pk})
        rsp = self.client.post(url)
        self.assertEqual(403, rsp.status_code)
        user = CtsUser.objects.get(pk=user.pk)
        self.assertEqual(original_token, user.auth_token.key)

    def test_home_view_not_logged_in(self):
        self.client.logout()
        rsp = self.client.get(reverse('home'))
        self.assertRedirects(rsp, reverse('account_login'))

    def test_home_view_with_catalog_perms(self):
        self.user.role = ROLE_OFFICER
        self.user.save()
        rsp = self.client.get(reverse('home'))
        self.assertRedirects(rsp, reverse('catalog_list'))

    def test_home_view_without_catalog_perms(self):
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('home'))
        self.assertRedirects(rsp, reverse('reports_list'))
Ejemplo n.º 15
0
class TransporterViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        bootstrap_permissions()

    def setUp(self):
        self.user = CtsUserFactory(email="*****@*****.**", password="******")
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('transporter_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'transporter_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need coordinator role
        self.user.role = ROLE_OFFICER
        self.user.save()
        rsp = self.client.get(reverse('transporter_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'transporter_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        transporter = TransporterFactory()
        rsp = self.client.get(reverse('transporter_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp,
                            transporter.name,
                            msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a transporter
        # Get the form page
        url = reverse('new_transporter_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        Transporter.objects.get(name='test')

    def test_update(self):
        # Update a transporter
        transporter = TransporterFactory()
        url = reverse('edit_transporter_modal', args=[transporter.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        transporter2 = Transporter.objects.get(name=name)
        self.assertEqual(transporter.pk, transporter2.pk)

    def test_delete(self):
        # Delete a transporter
        transporter = TransporterFactory()
        url = reverse('transporter_delete', args=[transporter.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        Transporter.objects.get(pk=transporter.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('transporter_list'))
        self.assertFalse(
            Transporter.objects.filter(pk=transporter.pk).exists())

    def test_edit_modal(self):
        transporter = TransporterFactory()
        url = reverse('edit_transporter_modal', kwargs={'pk': transporter.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/transporter_edit_modal.html')
Ejemplo n.º 16
0
class CategoryViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(CategoryViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(CategoryViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**",
                                   password="******",
                                   role=ROLE_OFFICER)
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('category_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'category_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need officer role
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('category_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'category_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        category = ItemCategoryFactory()
        rsp = self.client.get(reverse('category_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp,
                            category.name,
                            msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a category
        # Get the form page
        url = reverse('new_category_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        ItemCategory.objects.get(name='test')

    def test_update(self):
        # Update a category
        category = ItemCategoryFactory()
        url = reverse('edit_category_modal', args=[category.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        category2 = ItemCategory.objects.get(name=name)
        self.assertEqual(category.pk, category2.pk)

    def test_delete(self):
        # Delete a category
        category = ItemCategoryFactory()
        url = reverse('category_delete', args=[category.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        ItemCategory.objects.get(pk=category.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('category_list'))
        self.assertFalse(ItemCategory.objects.filter(pk=category.pk).exists())

    def test_edit_modal(self):
        category = ItemCategoryFactory()
        url = reverse('edit_category_modal', kwargs={'pk': category.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/category_edit_modal.html')
Ejemplo n.º 17
0
class CatalogViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(CatalogViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(CatalogViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**",
                                   password="******",
                                   role=ROLE_OFFICER)
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('catalog_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'catalog_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need OFFICER to work with catalog
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('catalog_list'))
        expected_url = reverse('account_login') + '?next=' + reverse(
            'catalog_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        cat = ItemCategoryFactory()
        description = "The meow of a cat"
        CatalogItemFactory(item_category=cat, description=description)
        rsp = self.client.get(reverse('catalog_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp,
                            description,
                            msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create an item
        ItemCategoryFactory(name="horse")
        ItemCategoryFactory(name="snowcat")
        cat = ItemCategoryFactory(name="cat")
        ItemCategoryFactory(name="catapult")
        description = "The meow of a cat"
        # Get the form page
        url = reverse('new_catalog_item_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'item_code': '007',
            'description': description,
            'unit': 'purr',
            'weight': str(1.005),
            'price_local': '1.23',
            'price_usd': '4.072',
            'item_category': cat.name.upper(),
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        item = CatalogItem.objects.get(description=description)
        self.assertEqual(cat, item.item_category)
        self.assertEqual('007', item.item_code)
        self.assertEqual(1.005, item.weight)
        self.assertEqual(Decimal('4.072'), item.price_usd)

    def test_update(self):
        # Update an item
        cat = ItemCategoryFactory()
        description = "The meow of a cat"
        item = CatalogItemFactory(item_category=cat, description=description)
        url = reverse('edit_catalog_item_modal', args=[item.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        description2 = "The bark of a dog"
        data = {
            'item_code': item.item_code,
            'description': description2,
            'unit': 'purr',
            'weight': str(1.005),
            'price_local': '1.23',
            'price_usd': '4.07',
            'item_category': cat.pk,
            'quantity': 3,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        item2 = CatalogItem.objects.get(description=description2)
        self.assertEqual(item.pk, item2.pk)

    def test_delete(self):
        # Delete an item
        cat = ItemCategoryFactory()
        description = "The meow of a cat"
        item = CatalogItemFactory(item_category=cat, description=description)
        url = reverse('catalog_delete', args=[item.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        CatalogItem.objects.get(pk=item.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('catalog_list'))
        self.assertFalse(CatalogItem.objects.filter(pk=item.pk).exists())

    def test_price_validator(self):
        # No negative price
        cat = ItemCategoryFactory()
        price_local = 5.000
        item = CatalogItemFactory(item_category=cat, price_local=price_local)
        url = reverse('edit_catalog_item_modal', args=[item.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        data = {
            'item_code': item.item_code,
            'description': 'desc',
            'unit': 'purr',
            'weight': str(item.weight),
            'price_local': '-1.23',
            'price_usd': '4.07',
            'item_category': cat.pk,
        }
        rsp = self.client.post(url, data=data)
        self.assertEqual(400, rsp.status_code)
        self.assertIn('Ensure this value is greater than',
                      rsp.context['form'].errors.as_text())

    def test_import_view_error(self):
        # Catalog import raises an error. We respond with 400 and the page displays the error.
        url = reverse('catalog_import_modal')

        # We're going to mock the catalog import, so the spreadsheet code will
        # never look at the "file" we upload. But the form validation will at least
        # check that it's not empty, so we need to upload at least 1 byte to get
        # past that.

        fp = io.BytesIO(b'1')
        setattr(fp, 'name', 'fakefilename.xls')
        with patch('catalog.views.catalog_import') as mock_import:
            mock_import.side_effect = CatalogImportFailure(["Killer bunny"])
            rsp = self.client.post(url, {'file': fp})
        self.assertContains(rsp, "Killer bunny", status_code=400)

    def test_import_view_okay(self):
        # Catalog import succeeds, apparently. We display how many records were "imported".
        url = reverse('catalog_import_modal')

        fp = io.BytesIO(b'1')
        setattr(fp, 'name', 'fakefilename.xls')
        with patch('catalog.views.catalog_import') as mock_import:
            mock_import.return_value = 666
            rsp = self.client.post(url, {'file': fp}, follow=True)
        self.assertContains(rsp, '666')

    @override_settings(FILE_UPLOAD_MAX_MEMORY_SIZE=1000)
    def test_import_large_file(self):
        # Not actually importing the large file, but making sure the view can handle it
        url = reverse('catalog_import_modal')
        path = None
        try:
            tmpf = NamedTemporaryFile(mode='wb', delete=False)
            path = tmpf.name
            tmpf.write(b'1' * 1001)
            tmpf.close()
            fp = open(path, 'rb')
            with patch('catalog.views.catalog_import') as mock_import:
                mock_import.return_value = 666
                rsp = self.client.post(url, {'file': fp}, follow=True)
            self.assertContains(rsp, '666')
        finally:
            if path:
                os.remove(path)

    def test_edit_modal(self):
        item = CatalogItemFactory()
        url = reverse('edit_catalog_item_modal', kwargs={'pk': item.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/catalogitem_edit_modal.html')

    def test_add_item_to_kit(self):
        item1 = CatalogItemFactory()
        kit = KitFactory()
        quantity = 500
        url = reverse('add_item_to_kit', args=[kit.pk, item1.pk, quantity])
        rsp = self.client.post(url)
        self.assertEqual(200, rsp.status_code)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item1)
        self.assertEqual(quantity, kit_item.quantity)

    def test_add_more_item_to_kit(self):
        item1 = CatalogItemFactory()
        kit = KitFactory()
        # Start with some in the kit
        KitItemFactory(kit=kit, catalog_item=item1, quantity=100)
        quantity = 500
        url = reverse('add_item_to_kit', args=[kit.pk, item1.pk, quantity])
        rsp = self.client.post(url)
        self.assertEqual(200, rsp.status_code)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item1)
        self.assertEqual(600, kit_item.quantity)

    def test_add_too_many_of_item_to_kit(self):
        item1 = CatalogItemFactory()
        kit = KitFactory()
        quantity = MAX_QUANTITY + 1
        url = reverse('add_item_to_kit', args=[kit.pk, item1.pk, quantity])
        rsp = self.client.post(url)
        self.assertEqual(400, rsp.status_code)

    def test_add_items_to_kit(self):
        item1 = CatalogItemFactory()
        item2 = CatalogItemFactory()
        kit = KitFactory()
        quantity1 = 50
        quantity2 = 60
        url = reverse('catalog_list')
        data = {
            'selected_kit': kit.pk,
            'quantity-%d' % item1.pk: quantity1,
            'quantity-%d' % item2.pk: quantity2,
        }
        rsp = self.client.post(url, data)
        self.assertEqual(302, rsp.status_code)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item1)
        self.assertEqual(quantity1, kit_item.quantity)
        kit_item = KitItem.objects.get(kit=kit, catalog_item=item2)
        self.assertEqual(quantity2, kit_item.quantity)

    def test_add_too_many_items_to_kit(self):
        item1 = CatalogItemFactory()
        item2 = CatalogItemFactory()
        kit = KitFactory()
        quantity1 = 50
        quantity2 = MAX_QUANTITY + 1
        url = reverse('catalog_list')
        data = {
            'selected_kit': kit.pk,
            'quantity-%d' % item1.pk: quantity1,
            'quantity-%d' % item2.pk: quantity2,
        }
        rsp = self.client.post(url, data)
        self.assertEqual(400, rsp.status_code)
        with self.assertRaises(KitItem.DoesNotExist):
            KitItem.objects.get(kit=kit, catalog_item=item1)
        with self.assertRaises(KitItem.DoesNotExist):
            KitItem.objects.get(kit=kit, catalog_item=item2)
Ejemplo n.º 18
0
class DonorCodeViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(DonorCodeViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(DonorCodeViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**", password="******", role=ROLE_COORDINATOR)
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('donorcode_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('donorcode_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need officer role
        self.user.role = ROLE_PARTNER
        self.user.save()
        rsp = self.client.get(reverse('donorcode_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('donorcode_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        donorcode = DonorCodeT1Factory()
        rsp = self.client.get(reverse('donorcode_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, donorcode.code, msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a donorcode
        # Get the form page
        url = reverse('new_donorcode_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'code': 'test',
            'donor_code_type': 't1',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        DonorCode.objects.get(code='test')

    def test_update(self):
        # Update a donorcode
        donorcode = DonorCodeT1Factory()
        url = reverse('edit_donorcode_modal', args=[donorcode.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        code = "edited"
        data = {
            'code': code,
            'donor_code_type': 't1',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        donorcode2 = DonorCode.objects.get(code=code)
        self.assertEqual(donorcode.pk, donorcode2.pk)

    def test_delete(self):
        # Delete a donorcode
        donorcode = DonorCodeT1Factory()
        url = reverse('donorcode_delete', args=[donorcode.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        DonorCode.objects.get(pk=donorcode.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('donorcode_list'))
        self.assertFalse(DonorCode.objects.filter(pk=donorcode.pk).exists())

    def test_edit_modal(self):
        donorcode = DonorCodeT1Factory()
        url = reverse('edit_donorcode_modal', kwargs={'pk': donorcode.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/donorcode_edit_modal.html')
Ejemplo n.º 19
0
class DonorViewTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(DonorViewTest, cls).setUpClass()
        bootstrap_permissions()

    def setUp(self):
        super(DonorViewTest, self).setUp()
        self.user = CtsUserFactory(email="*****@*****.**", password="******")
        assert self.client.login(email="*****@*****.**", password="******")

    def test_login_required(self):
        self.client.logout()
        rsp = self.client.get(reverse('donor_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('donor_list')
        self.assertRedirects(rsp, expected_url)

    def test_role_required(self):
        # Need coordinator role
        self.user.role = ROLE_OFFICER
        self.user.save()
        rsp = self.client.get(reverse('donor_list'))
        expected_url = reverse('account_login') + '?next=' + reverse('donor_list')
        self.assertRedirects(rsp, expected_url)

    def test_list(self):
        donor = DonorFactory()
        rsp = self.client.get(reverse('donor_list'))
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, donor.name, msg_prefix=rsp.content.decode('utf-8'))

    def test_create(self):
        # Create a donor
        # Get the form page
        url = reverse('new_donor_modal')
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertIn('form', rsp.context)
        # Submit the new object
        data = {
            'name': 'test',
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors.as_text())
        self.assertEqual(rsp.status_code, 200)
        Donor.objects.get(name='test')

    def test_update(self):
        # Update a donor
        donor = DonorFactory()
        url = reverse('edit_donor_modal', args=[donor.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        donor2 = Donor.objects.get(name=name)
        self.assertEqual(donor.pk, donor2.pk)

    def test_update_and_create_new_codes(self):
        # Update a donor, create some new codes, and add them to the donor
        donor = DonorFactory()
        url = reverse('edit_donor_modal', args=[donor.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        t1_codeA = DonorCode.objects.create(code='t1A', donor_code_type=DonorCode.T1)
        t1_codeB = DonorCode.objects.create(code='t1B', donor_code_type=DonorCode.T1)
        t3_codeA = DonorCode.objects.create(code='t1A', donor_code_type=DonorCode.T3)
        # Submit the new object
        name = "edited"
        data = {
            'name': name,
            't1_codes_1': [t1_codeA.pk, t1_codeB.pk],
            'new_t1_codes': 'New code C, t1A',
            't3_codes_1': [t3_codeA.pk],
        }
        rsp = self.client.post(url, data=data)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertEqual(rsp.status_code, 200)
        donor2 = Donor.objects.get(name=name)
        self.assertEqual(donor.pk, donor2.pk)
        code_c = DonorCode.objects.get(code='New code C', donor_code_type=DonorCode.T1)
        self.assertIn(code_c, donor2.t1_codes.all())
        self.assertIn(t1_codeA, donor2.t1_codes.all())
        self.assertIn(t3_codeA, donor2.t3_codes.all())

    def test_delete(self):
        # Delete a donor
        donor = DonorFactory()
        url = reverse('donor_delete', args=[donor.pk])
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertContains(rsp, 'Confirm')
        Donor.objects.get(pk=donor.pk)
        # Now delete it!
        rsp = self.client.post(url)
        if rsp.status_code == 400:
            self.fail(rsp.context['form'].errors)
        self.assertRedirects(rsp, reverse('donor_list'))
        self.assertFalse(Donor.objects.filter(pk=donor.pk).exists())

    def test_edit_modal(self):
        donor = DonorFactory()
        url = reverse('edit_donor_modal', kwargs={'pk': donor.pk})
        rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
        self.assertTemplateUsed(rsp, 'catalog/donor_edit_modal.html')