Ejemplo n.º 1
0
 def test_search_products_issuer_name(self):
     product1 = PrepaidProduct(issuer_name='Bank of CFPB')
     product1.save()
     product2 = PrepaidProduct(issuer_name='Bank of Foo Bar')
     product2.save()
     results = search_products(search_term='cfpb',
                               search_field=['issuer_name'],
                               products=PrepaidProduct.objects.all())
     self.assertIn(product1, results)
     self.assertEqual(results.count(), 1)
Ejemplo n.º 2
0
 def test_get_available_filters(self):
     product1 = PrepaidProduct(issuer_name='Bank of CFPB',
                               prepaid_type='Tax')
     product1.save()
     product2 = PrepaidProduct(prepaid_type='Travel')
     product2.save()
     products = PrepaidProduct.objects.all()
     self.assertEqual(
         get_available_filters(products), {
             'prepaid_type': ['Tax', 'Travel'],
             'status': [],
             'issuer_name': ['Bank of CFPB']
         })
Ejemplo n.º 3
0
class TestMostRecentAgreement(TestCase):
    """ Test that the latest agreement is based on its created date.

    A product can have multiple agreements with the same effective date,
    but the most recent one is considered the one created last.

    An individual agreement is considered most recent if it was
    created last among its product's agreements.
    """
    def setUp(self):
        self.product = PrepaidProduct()
        self.product.save()

        effective_date = date(month=2, day=3, year=2019)
        self.agreement_old = PrepaidAgreement(
            effective_date=effective_date,
            created_time=timezone.now() - timedelta(hours=1),
            product=self.product,
        )
        self.agreement_old.save()
        self.agreement_older = PrepaidAgreement(
            effective_date=effective_date,
            created_time=timezone.now() - timedelta(hours=2),
            product=self.product,
        )
        self.agreement_older.save()
        self.agreement_new = PrepaidAgreement(
            effective_date=effective_date,
            created_time=timezone.now(),
            product=self.product,
        )
        self.agreement_new.save()

    def test_product_most_recent_agreement(self):
        self.assertEqual(self.product.most_recent_agreement,
                         self.agreement_new)

    def test_agreement_is_most_recent(self):
        self.assertEqual(self.agreement_old.is_most_recent, False)
        self.assertEqual(self.agreement_older.is_most_recent, False)
        self.assertEqual(self.agreement_new.is_most_recent, True)
Ejemplo n.º 4
0
 def test_search_products_all_fields(self):
     product1 = PrepaidProduct(issuer_name='XYZ Bank')
     product1.save()
     product2 = PrepaidProduct(program_manager='xyz manager')
     product2.save()
     product3 = PrepaidProduct(name='Foo Bar Product')
     product3.save()
     results = search_products(search_term='Xyz',
                               search_field=[],
                               products=PrepaidProduct.objects.all())
     self.assertIn(product1, results)
     self.assertIn(product2, results)
     self.assertEqual(results.count(), 2)
Ejemplo n.º 5
0
 def test_filter_products(self):
     product1 = PrepaidProduct(status='Active', prepaid_type='Student')
     product1.save()
     product2 = PrepaidProduct(status='Withdrawn',
                               prepaid_type='Travel',
                               issuer_name='XYZ Bank')
     product2.save()
     product3 = PrepaidProduct(status='Active',
                               prepaid_type='Payroll',
                               issuer_name='ABC Bank')
     product3.save()
     results = filter_products(filters={
         'status': ['Active'],
         'prepaid_type': ['Travel', 'Payroll', 'Student'],
         'issuer_name': ['ABC Bank', 'XYZ Bank']
     },
                               products=PrepaidProduct.objects.all())
     self.assertIn(product3, results)
     self.assertEqual(results.count(), 1)
class TestViews(TestCase):

    def setUp(self):
        self.product1 = PrepaidProduct(
            issuer_name='Bank of CFPB',
            prepaid_type='Tax'
        )
        self.product1.save()
        self.product2 = PrepaidProduct(
            issuer_name='CFPB Bank',
            prepaid_type='Tax',
            other_relevant_parties='Party'
        )
        self.product2.save()

        effective_date = date(month=2, day=3, year=2019)
        created_date = make_aware(datetime(month=4, day=1, year=2020))
        self.agreement_old = PrepaidAgreement(
            effective_date=effective_date,
            created_time=created_date - timedelta(hours=1),
            product=self.product1,
        )
        self.agreement_old.save()
        self.agreement_older = PrepaidAgreement(
            effective_date=effective_date,
            created_time=created_date - timedelta(hours=2),
            product=self.product2,
        )
        self.agreement_older.save()
        self.agreement_new = PrepaidAgreement(
            effective_date=effective_date,
            created_time=created_date,
            product=self.product1,
        )
        self.agreement_new.save()

    @mock.patch("builtins.open", new_callable=mock.mock_open)
    def test_write_agreements_data(self, mock_open):
        # Run the write function
        write_agreements_data()

        mock_file_handle = mock_open()

        # Make sure each file's headers exist
        mock_file_handle.write.assert_any_call(
            'issuer_name,product_name,product_id,'
            'agreement_effective_date,agreement_id,most_recent_agreement,'
            'created_date,current_status,withdrawal_date,'
            'prepaid_product_type,program_manager_exists,program_manager,'
            'other_relevant_parties,path,direct_download\r\n'
        )
        mock_file_handle.write.assert_any_call(
            'issuer_name,product_name,product_id,'
            'agreement_effective_date,agreement_id,'
            'created_date,current_status,withdrawal_date,'
            'prepaid_product_type,program_manager_exists,program_manager,'
            'other_relevant_parties,path,direct_download\r\n'
        )

        # Make sure expected data rows exist
        mock_file_handle.write.assert_any_call(
            'Bank of CFPB,,'
            + str(self.product1.pk) +
            ',2019-02-03,'
            + str(self.agreement_old.pk) +
            ',No,2020-04-01 03:00:00,,,Tax,,,'
            'No information provided,,\r\n'
        )
        mock_file_handle.write.assert_any_call(
            'Bank of CFPB,,'
            + str(self.product1.pk) +
            ',2019-02-03,'
            + str(self.agreement_new.pk) +
            ',2020-04-01 04:00:00,,,Tax,,,'
            'No information provided,,\r\n'
        )
        mock_file_handle.write.assert_any_call(
            'Bank of CFPB,,'
            + str(self.product1.pk) +
            ',2019-02-03,'
            + str(self.agreement_new.pk) +
            ',Yes,2020-04-01 04:00:00,,,Tax,,,'
            'No information provided,,\r\n'
        )
        mock_file_handle.write.assert_any_call(
            'CFPB Bank,,'
            + str(self.product2.pk) +
            ',2019-02-03,'
            + str(self.agreement_older.pk) +
            ',2020-04-01 02:00:00,,,Tax,,,Party,,\r\n'
        )
        mock_file_handle.write.assert_any_call(
            'CFPB Bank,,'
            + str(self.product2.pk) +
            ',2019-02-03,'
            + str(self.agreement_older.pk) +
            ',Yes,2020-04-01 02:00:00,,,Tax,,,Party,,\r\n'
        )
Ejemplo n.º 7
0
class TestViews(TestCase):
    def setUp(self):
        self.issuer_name1 = 'Bank of CFPB'
        self.issuer_name2 = 'CFPB Bank'
        self.issuer_name3 = 'Bank_of CFPB'

        self.product_name1 = self.issuer_name2
        self.product_name2 = self.issuer_name1
        self.product_name3 = self.issuer_name2.replace(' ', '_')

        self.product1 = PrepaidProduct(name=self.product_name1,
                                       issuer_name=self.issuer_name1,
                                       prepaid_type='Tax')
        self.product1.save()
        self.product2 = PrepaidProduct(name=self.product_name2,
                                       issuer_name=self.issuer_name2,
                                       prepaid_type='Tax',
                                       other_relevant_parties='Party')
        self.product2.save()
        self.product3 = PrepaidProduct(name=self.product_name3,
                                       issuer_name=self.issuer_name3,
                                       prepaid_type='Tax',
                                       other_relevant_parties='Party')
        self.product3.save()

        effective_date = date(month=2, day=3, year=2019)
        created_date = make_aware(datetime(month=4, day=1, year=2020))
        filename1 = self.product_name1.replace(' ', '_') + '_01_04_2020.zip'
        filename2 = self.product_name2.replace(' ', '_') + '_02_04_2020.zip'
        filename3 = self.product_name3.replace(' ', '_') + '_03_04_2020.zip'
        self.path1 = \
            self.issuer_name1 + '/' + self.product_name1 + '/20200401'
        self.path2 = \
            self.issuer_name2 + '/' + self.product_name2 + '/20200402'
        self.path3 = \
            self.issuer_name3 + '/' + self.product_name3 + '/20200403'
        self.direct_download1 = S3_PATH + filename1
        self.direct_download2 = S3_PATH + filename2
        self.direct_download3 = S3_PATH + filename3

        self.agreement_old = PrepaidAgreement(
            compressed_files_url=self.direct_download1,
            bulk_download_path=self.path1,
            filename=filename1,
            effective_date=effective_date,
            created_time=created_date - timedelta(hours=1),
            product=self.product1,
        )
        self.agreement_old.save()
        self.agreement_older = PrepaidAgreement(
            compressed_files_url=self.direct_download2,
            bulk_download_path=self.path2,
            filename=filename2,
            effective_date=effective_date,
            created_time=created_date - timedelta(hours=2),
            product=self.product2,
        )
        self.agreement_older.save()
        self.agreement_new = PrepaidAgreement(
            compressed_files_url=self.direct_download1,
            bulk_download_path=self.path1,
            filename=filename1,
            effective_date=effective_date,
            created_time=created_date,
            product=self.product1,
        )
        self.agreement_new.save()
        self.agreement_newer = PrepaidAgreement(
            compressed_files_url=self.direct_download3,
            bulk_download_path=self.path3,
            filename=filename3,
            effective_date=effective_date,
            created_time=created_date + timedelta(hours=1),
            product=self.product3,
        )
        self.agreement_newer.save()

    @mock.patch("builtins.open", new_callable=mock.mock_open)
    def test_write_agreements_data(self, mock_open):
        # Run the write function
        write_agreements_data()

        mock_file_handle = mock_open()

        # Make sure each file's headers exist
        mock_file_handle.write.assert_any_call(
            'issuer_name,product_name,product_id,'
            'agreement_effective_date,agreement_id,most_recent_agreement,'
            'created_date,current_status,withdrawal_date,'
            'prepaid_product_type,program_manager_exists,program_manager,'
            'other_relevant_parties,path,direct_download\r\n')
        mock_file_handle.write.assert_any_call(
            'issuer_name,product_name,product_id,'
            'agreement_effective_date,agreement_id,'
            'created_date,current_status,withdrawal_date,'
            'prepaid_product_type,program_manager_exists,program_manager,'
            'other_relevant_parties,path,direct_download\r\n')

        # Make sure expected data rows exist
        mock_file_handle.write.assert_any_call(
            self.issuer_name1 + ',' + self.product_name1 + ',' +
            str(self.product1.pk) + ',2019-02-03,' +
            str(self.agreement_old.pk) + ',No,2020-04-01 03:00:00,,,Tax,,,'
            'No information provided,' + self.path1 + ',' +
            self.direct_download1 + '\r\n')
        mock_file_handle.write.assert_any_call(self.issuer_name1 + ',' +
                                               self.product_name1 + ',' +
                                               str(self.product1.pk) +
                                               ',2019-02-03,' +
                                               str(self.agreement_new.pk) +
                                               ',2020-04-01 04:00:00,,,Tax,,,'
                                               'No information provided,' +
                                               self.path1 + ',' +
                                               self.direct_download1 + '\r\n')
        mock_file_handle.write.assert_any_call(
            self.issuer_name1 + ',' + self.product_name1 + ',' +
            str(self.product1.pk) + ',2019-02-03,' +
            str(self.agreement_new.pk) + ',Yes,2020-04-01 04:00:00,,,Tax,,,'
            'No information provided,' + self.path1 + ',' +
            self.direct_download1 + '\r\n')
        mock_file_handle.write.assert_any_call(
            self.issuer_name2 + ',' + self.product_name2 + ',' +
            str(self.product2.pk) + ',2019-02-03,' +
            str(self.agreement_older.pk) +
            ',2020-04-01 02:00:00,,,Tax,,,Party,' + self.path2 + ',' +
            self.direct_download2 + '\r\n')
        mock_file_handle.write.assert_any_call(
            self.issuer_name2 + ',' + self.product_name2 + ',' +
            str(self.product2.pk) + ',2019-02-03,' +
            str(self.agreement_older.pk) +
            ',Yes,2020-04-01 02:00:00,,,Tax,,,Party,' + self.path2 + ',' +
            self.direct_download2 + '\r\n')
        mock_file_handle.write.assert_any_call(
            self.issuer_name3 + ',' + self.product_name3 + ',' +
            str(self.product3.pk) + ',2019-02-03,' +
            str(self.agreement_newer.pk) +
            ',2020-04-01 05:00:00,,,Tax,,,Party,' + self.path3 + ',' +
            self.direct_download3 + '\r\n')
        mock_file_handle.write.assert_any_call(
            self.issuer_name3 + ',' + self.product_name3 + ',' +
            str(self.product3.pk) + ',2019-02-03,' +
            str(self.agreement_newer.pk) +
            ',Yes,2020-04-01 05:00:00,,,Tax,,,Party,' + self.path3 + ',' +
            self.direct_download3 + '\r\n')
Ejemplo n.º 8
0
class TestViews(TestCase):
    def setUp(self):
        self.product1 = PrepaidProduct(
            issuer_name='Bank of CFPB',
            prepaid_type='Tax',
            status='Active',
        )
        self.product1.save()
        self.product2 = PrepaidProduct(program_manager='CFPB manager',
                                       prepaid_type='Travel',
                                       status='Withdrawn')
        self.product2.save()
        self.product3 = PrepaidProduct(
            name='ABC Product',
            issuer_name='ABC Bank',
            prepaid_type='Payroll',
            status='Active',
        )
        self.product3.save()

    def test_get_available_filters(self):
        products = PrepaidProduct.objects
        self.assertEqual(get_available_filters(products.none()), {
            'prepaid_type': [],
            'status': [],
            'issuer_name': []
        })
        self.assertEqual(
            get_available_filters(products), {
                'prepaid_type': ['Tax', 'Travel', 'Payroll'],
                'status': ['Active', 'Withdrawn'],
                'issuer_name': ['Bank of CFPB', 'ABC Bank']
            })

    @unittest.skipUnless(connection.vendor == 'postgresql',
                         'PostgreSQL-dependent')
    def test_search_products_issuer_name(self):
        results = search_products(search_term='cfpb',
                                  search_field=['issuer_name'],
                                  products=PrepaidProduct.objects.all())
        self.assertIn(self.product1, results)
        self.assertEqual(results.count(), 2)

    @unittest.skipUnless(connection.vendor == 'postgresql',
                         'PostgreSQL-dependent')
    def test_search_products_all_fields(self):
        results = search_products(search_term='cfpb',
                                  search_field=[],
                                  products=PrepaidProduct.objects.all())
        self.assertIn(self.product1, results)
        self.assertIn(self.product2, results)
        self.assertEqual(results.count(), 2)

    def test_filter_products(self):
        results = filter_products(filters={
            'status': ['Active'],
            'prepaid_type': ['Travel', 'Payroll', 'Student'],
            'issuer_name': ['ABC Bank', 'XYZ Bank']
        },
                                  products=PrepaidProduct.objects.all())
        self.assertIn(self.product3, results)
        self.assertEqual(results.count(), 1)

    def test_get_breadcrumb_if_referrer_is_search_page_with_query(self):
        request = HttpRequest()
        search_path_with_query = reverse("prepaid_agreements:index") + "?q=a"
        request.META.update({'HTTP_REFERER': search_path_with_query})
        self.assertEqual(get_detail_page_breadcrumb(request),
                         search_path_with_query)

    def test_get_breadcrumb_if_referrer_is_search_page_without_query(self):
        request = HttpRequest()
        search_path = reverse("prepaid_agreements:index")
        request.META.update({'HTTP_REFERER': search_path})
        self.assertEqual(get_detail_page_breadcrumb(request), search_path)

    def test_get_breadcrumb_if_referrer_is_not_search_page(self):
        request = HttpRequest()
        search_path = reverse("prepaid_agreements:index")
        request.META.update({'HTTP_REFERER': '/random-path/'})
        self.assertEqual(get_detail_page_breadcrumb(request), search_path)

    def test_get_breadcrumb_if_referrer_is_random_page_with_query(self):
        request = HttpRequest()
        search_path = reverse("prepaid_agreements:index")
        request.META.update({'HTTP_REFERER': '/random-path/?q=test'})
        self.assertEqual(get_detail_page_breadcrumb(request), search_path)

    def test_get_breadcrumb_if_no_referrer(self):
        request = HttpRequest()
        search_path = reverse("prepaid_agreements:index")
        self.assertEqual(get_detail_page_breadcrumb(request), search_path)

    def test_index_search(self):
        response = self.client.get(
            '/data-research/prepaid-accounts/search-agreements/', {
                'q': 'cfpb',
                'search_field': 'all',
                'page': 1
            })
        self.assertEqual(3, response.context_data['total_count'])
        self.assertEqual(2, response.context_data['current_count'])

    def test_index_filter(self):
        response = self.client.get(
            '/data-research/prepaid-accounts/search-agreements/',
            {'status': 'Active'})
        self.assertEqual(3, response.context_data['total_count'])
        self.assertEqual(2, response.context_data['current_count'])