예제 #1
0
    def post(self, request):
        """
        POST handler for the view. User data is posted to this handler
        which performs an SDN check and returns whether the user passed
        or failed.
        """
        name = request.data['name']
        city = request.data['city']
        country = request.data['country']
        hits = 0

        site_configuration = request.site.siteconfiguration
        basket = Basket.get_basket(request.user, site_configuration.site)

        if site_configuration.enable_sdn_check:
            sdn_check = SDNClient(api_url=site_configuration.sdn_api_url,
                                  api_key=site_configuration.sdn_api_key,
                                  sdn_list=site_configuration.sdn_api_list)
            try:
                response = sdn_check.search(name, city, country)
                hits = response['total']
                if hits > 0:
                    sdn_check.deactivate_user(basket, name, city, country,
                                              response)
                    logout(request)
            except (HTTPError, Timeout):
                # If the SDN API endpoint is down or times out
                # the user is allowed to make the purchase.
                pass

        return Response({'hits': hits})
예제 #2
0
파일: sdn.py 프로젝트: open-craft/ecommerce
    def post(self, request):
        """
        POST handler for the view. User data is posted to this handler
        which performs an SDN check and returns whether the user passed
        or failed.
        """
        name = request.data['name']
        country = request.data['country']
        hits = 0

        site_configuration = request.site.siteconfiguration
        if site_configuration.enable_sdn_check:
            sdn_check = SDNClient(
                api_url=site_configuration.sdn_api_url,
                api_key=site_configuration.sdn_api_key,
                sdn_list=site_configuration.sdn_api_list
            )
            try:
                response = sdn_check.search(name, country)
                hits = response['total']
                if hits > 0:
                    sdn_check.deactivate_user(
                        request.user,
                        request.site.siteconfiguration,
                        name,
                        country,
                        response
                    )
            except (HTTPError, Timeout):
                # If the SDN API endpoint is down or times out
                # the user is allowed to make the purchase.
                pass

        return Response({'hits': hits})
예제 #3
0
    def setUp(self):
        super(SDNCheckTests, self).setUp()
        self.name = 'Dr. Evil'
        self.city = 'Top-secret lair'
        self.country = 'EL'
        self.user = self.create_user(full_name=self.name)
        self.sdn_api_url = 'http://sdn-test.fake/'
        self.sdn_api_key = 'fake-key'
        self.site_configuration = self.site.siteconfiguration
        self.site_configuration.enable_sdn_check = True
        self.site_configuration.sdn_api_list = 'SDN,TEST'
        self.site_configuration.save()

        self.sdn_validator = SDNClient(self.sdn_api_url, self.sdn_api_key,
                                       self.site_configuration.sdn_api_list)
예제 #4
0
    def setUp(self):
        super(SDNCheckTests, self).setUp()
        self.name = 'Dr. Evil'
        self.country = 'Evilland'
        self.user = self.create_user(full_name=self.name)
        self.site_configuration = self.site.siteconfiguration
        self.site_configuration.enable_sdn_check = True,
        self.site_configuration.sdn_api_url = 'http://sdn-test.fake/'
        self.site_configuration.sdn_api_key = 'fake-key'
        self.site_configuration.sdn_api_list = 'SDN,TEST'
        self.site_configuration.save()

        self.sdn_validator = SDNClient(
            self.site_configuration.sdn_api_url,
            self.site_configuration.sdn_api_key,
            self.site_configuration.sdn_api_list
        )
예제 #5
0
class SDNCheckTests(TestCase):
    """ Tests for the SDN check function. """
    def setUp(self):
        super(SDNCheckTests, self).setUp()
        self.name = 'Dr. Evil'
        self.city = 'Top-secret lair'
        self.country = 'EL'
        self.user = self.create_user(full_name=self.name)
        self.sdn_api_url = 'http://sdn-test.fake/'
        self.sdn_api_key = 'fake-key'
        self.site_configuration = self.site.siteconfiguration
        self.site_configuration.enable_sdn_check = True
        self.site_configuration.sdn_api_list = 'SDN,TEST'
        self.site_configuration.save()

        self.sdn_validator = SDNClient(self.sdn_api_url, self.sdn_api_key,
                                       self.site_configuration.sdn_api_list)

    def mock_sdn_response(self, response, status_code=200):
        """ Mock the SDN check API endpoint response. """
        params = urlencode({
            'sources': self.site_configuration.sdn_api_list,
            'api_key': self.sdn_api_key,
            'type': 'individual',
            'name': str(self.name).encode('utf-8'),
            'address': str(self.city).encode('utf-8'),
            'countries': self.country
        })
        sdn_check_url = '{api_url}?{params}'.format(api_url=self.sdn_api_url,
                                                    params=params)

        httpretty.register_uri(httpretty.GET,
                               sdn_check_url,
                               status=status_code,
                               body=response,
                               content_type='application/json')

    @httpretty.activate
    @override_settings(SDN_CHECK_REQUEST_TIMEOUT=0.1)
    def test_sdn_check_timeout(self):
        """Verify SDN check logs an exception if the request times out."""
        def mock_timeout(_request, _uri, headers):
            time.sleep(settings.SDN_CHECK_REQUEST_TIMEOUT + 0.1)
            return (200, headers, {'total': 1})

        self.mock_sdn_response(mock_timeout, status_code=200)
        with self.assertRaises(Timeout):
            with mock.patch(
                    'ecommerce.extensions.payment.utils.logger.exception'
            ) as mock_logger:
                self.sdn_validator.search(self.name, self.city, self.country)
                self.assertTrue(mock_logger.called)

    @httpretty.activate
    def test_sdn_check_connection_error(self):
        """ Verify the check logs an exception in case of a connection error. """
        self.mock_sdn_response(json.dumps({'total': 1}), status_code=400)
        with self.assertRaises(HTTPError):
            with mock.patch(
                    'ecommerce.extensions.payment.utils.logger.exception'
            ) as mock_logger:
                self.sdn_validator.search(self.name, self.city, self.country)
                self.assertTrue(mock_logger.called)

    @httpretty.activate
    def test_sdn_check_match(self):
        """ Verify the SDN check returns the number of matches and records the match. """
        sdn_response = {'total': 1}
        self.mock_sdn_response(json.dumps(sdn_response))
        response = self.sdn_validator.search(self.name, self.city,
                                             self.country)
        self.assertEqual(response, sdn_response)

    @httpretty.activate
    def test_sdn_check_unicode_match(self):
        """ Verify the SDN check returns the number of matches and records the match. """
        sdn_response = {'total': 1}
        self.name = u'Keyser Söze'
        self.mock_sdn_response(json.dumps(sdn_response))
        response = self.sdn_validator.search(self.name, self.city,
                                             self.country)
        self.assertEqual(response, sdn_response)

    def test_deactivate_user(self):
        """ Verify an SDN failure is logged. """
        response = {'description': 'Bad dude.'}
        product1 = factories.ProductFactory(
            stockrecords__partner__short_code='first')
        product2 = factories.ProductFactory(
            stockrecords__partner__short_code='second')
        basket = factories.BasketFactory(owner=self.user,
                                         site=self.site_configuration.site)
        basket.add(product1)
        basket.add(product2)
        self.assertEqual(SDNCheckFailure.objects.count(), 0)
        with mock.patch.object(User,
                               'deactivate_account') as deactivate_account:
            deactivate_account.return_value = True
            self.sdn_validator.deactivate_user(basket, self.name, self.city,
                                               self.country, response)

            self.assertEqual(SDNCheckFailure.objects.count(), 1)
            sdn_object = SDNCheckFailure.objects.first()
            self.assertEqual(sdn_object.full_name, self.name)
            self.assertEqual(sdn_object.city, self.city)
            self.assertEqual(sdn_object.country, self.country)
            self.assertEqual(sdn_object.site, self.site_configuration.site)
            self.assertEqual(sdn_object.sdn_check_response, response)
            self.assertEqual(sdn_object.products.count(), basket.lines.count())
            self.assertIn(product1, sdn_object.products.all())
            self.assertIn(product2, sdn_object.products.all())
예제 #6
0
class SDNCheckTests(TestCase):
    """ Tests for the SDN check function. """

    def setUp(self):
        super(SDNCheckTests, self).setUp()
        self.name = 'Dr. Evil'
        self.country = 'Evilland'
        self.user = self.create_user(full_name=self.name)
        self.site_configuration = self.site.siteconfiguration
        self.site_configuration.enable_sdn_check = True,
        self.site_configuration.sdn_api_url = 'http://sdn-test.fake/'
        self.site_configuration.sdn_api_key = 'fake-key'
        self.site_configuration.sdn_api_list = 'SDN,TEST'
        self.site_configuration.save()

        self.sdn_validator = SDNClient(
            self.site_configuration.sdn_api_url,
            self.site_configuration.sdn_api_key,
            self.site_configuration.sdn_api_list
        )

    def mock_sdn_response(self, response, status_code=200):
        """ Mock the SDN check API endpoint response. """
        params = urlencode({
            'sources': self.site_configuration.sdn_api_list,
            'api_key': self.site_configuration.sdn_api_key,
            'type': 'individual',
            'name': self.name,
            'countries': self.country
        })
        sdn_check_url = '{api_url}?{params}'.format(
            api_url=self.site_configuration.sdn_api_url,
            params=params
        )

        httpretty.register_uri(
            httpretty.GET,
            sdn_check_url,
            status=status_code,
            body=response,
            content_type='application/json'
        )

    def assert_sdn_check_failure_recorded(self, response):
        """ Assert an SDN check failure is logged and has the correct values. """
        self.assertEqual(SDNCheckFailure.objects.count(), 1)
        sdn_object = SDNCheckFailure.objects.first()
        self.assertEqual(sdn_object.full_name, self.name)
        self.assertEqual(sdn_object.country, self.country)
        self.assertEqual(sdn_object.site, self.site_configuration.site)
        self.assertEqual(sdn_object.sdn_check_response, response)

    @httpretty.activate
    @override_settings(SDN_CHECK_REQUEST_TIMEOUT=0.1)
    def test_sdn_check_timeout(self):
        """Verify SDN check logs an exception if the request times out."""
        def mock_timeout(_request, _uri, headers):
            time.sleep(settings.SDN_CHECK_REQUEST_TIMEOUT + 0.1)
            return (200, headers, {'total': 1})

        self.mock_sdn_response(mock_timeout, status_code=200)
        with self.assertRaises(Timeout):
            with mock.patch('ecommerce.extensions.payment.utils.logger.exception') as mock_logger:
                self.sdn_validator.search(self.name, self.country)
                self.assertTrue(mock_logger.called)

    @httpretty.activate
    def test_sdn_check_connection_error(self):
        """ Verify the check logs an exception in case of a connection error. """
        self.mock_sdn_response(json.dumps({'total': 1}), status_code=400)
        with self.assertRaises(HTTPError):
            with mock.patch('ecommerce.extensions.payment.utils.logger.exception') as mock_logger:
                self.sdn_validator.search(self.name, self.country)
                self.assertTrue(mock_logger.called)

    @httpretty.activate
    def test_sdn_check_match(self):
        """ Verify the SDN check returns the number of matches and records the match. """
        sdn_response = {'total': 1}
        self.mock_sdn_response(json.dumps(sdn_response))
        response = self.sdn_validator.search(self.name, self.country)
        self.assertEqual(response, sdn_response)

    def test_deactivate_user(self):
        """ Verify an SDN failure is logged. """
        response = {'description': 'Bad dude.'}
        self.assertEqual(SDNCheckFailure.objects.count(), 0)
        with mock.patch.object(User, 'deactivate_account') as deactivate_account:
            deactivate_account.return_value = True
            self.sdn_validator.deactivate_user(
                self.user,
                self.site_configuration,
                self.name,
                self.country,
                response)
            self.assert_sdn_check_failure_recorded(response)