def __call__(self, value):
        value = force_text(value)

        # Catch invalid emails before we check if they're disposable
        try:
            validators.validate_email(value)
        except ValidationError:
            return

        user_part, domain_part = value.rsplit('@', 1)

        if domain_part not in self.whitelist:
            if self.BDEA_APIKEY:  # Validate using block-disposable-email.com
                client = BDEAClient(self.BDEA_APIKEY, timeout=self.BDEA_TIMEOUT)
                response = client.get_domain_status(domain_part)

                if response.status() and response.is_disposable():
                    raise ValidationError(self.message, code=self.code)

            """
            This will run if we are not using BDEA, we're out of BDEA credits,
            there was an error contacting BDEA's servers or we did not get a
            hit on BDEA. Basically always check using local list as a backup
            """
            for email_group in self.chunk(self.emails, 20):
                regex = "(.*" + "$)|(.*".join(email_group) + "$)"
                if re.match(regex, value):
                    raise ValidationError(self.message, code=self.code)
Example #2
0
    def __call__(self, value):
        value = force_text(value)

        # Catch invalid emails before we check if they're disposable
        try:
            validators.validate_email(value)
        except ValidationError:
            return

        user_part, domain_part = value.rsplit('@', 1)

        if domain_part not in self.whitelist:
            if self.BDEA_APIKEY:  # Validate using block-disposable-email.com
                client = BDEAClient(self.BDEA_APIKEY, timeout=self.BDEA_TIMEOUT)
                response = client.get_domain_status(domain_part)

                if response.status() and response.is_disposable():
                    raise ValidationError(self.message, code=self.code)

            """
            This will run if we are not using BDEA, we're out of BDEA credits,
            there was an error contacting BDEA's servers or we did not get a
            hit on BDEA. Basically always check using local list as a backup
            """
            for email_group in self.chunk(self.emails, 20):
                regex = "(.*" + "$)|(.*".join(email_group) + "$)"
                if re.match(regex, value):
                    raise ValidationError(self.message, code=self.code)
Example #3
0
 def test_domain_urlopen_args(self):
     with patch('bdea.client.urlopen') as urlopen_mock:
         urlopen_mock.return_value = StringIO('{}')
         cl = BDEAClient('apikey')
         cl.get_domain_status('example.com')
         url = 'http://check.block-disposable-email.com/easyapi/json/apikey/example.com'
         urlopen_mock.assert_called_with(url, timeout=5)
Example #4
0
 def test_status_urlopen_args(self):
     with patch('bdea.client.urlopen') as urlopen_mock:
         urlopen_mock.return_value = StringIO('{}')
         cl = BDEAClient('apikey')
         cl.get_api_status()
         url = 'http://status.block-disposable-email.com/status/?apikey=apikey'
         urlopen_mock.assert_called_with(url, timeout=5)
Example #5
0
 def test_valid_apikey_domain_block(self):
     res = BDEAClient(self.APIKEY_VALID).get_domain_status(BDEAClient.TEST_DOMAIN_BLOCK)
     assert res.response['domain_status'] == 'block'
     assert res.response['request_status'] == 'success'
Example #6
0
 def test_valid_apikey_api_status(self):
     res = BDEAClient(self.APIKEY_VALID).get_api_status()
     assert res.response['request_status'] == 'ok'
     assert res.response['apikeystatus'] == 'active'
Example #7
0
 def test_invalid_apikey_domain_block(self):
     res = BDEAClient(self.APIKEY_INVALID).get_domain_status(BDEAClient.TEST_DOMAIN_BLOCK)
     assert res.response['domain_status'] == 'ok'
     assert res.response['request_status'] == 'fail_key'
Example #8
0
 def test_do_not_accept_email(self):
     cl = BDEAClient('apikey')
     with pytest.raises(ValueError):
         cl.get_domain_status('*****@*****.**')
Example #9
0
 def test_valid_json(self):
     with patch('bdea.client.urlopen') as urlopen_mock:
         urlopen_mock.return_value = StringIO('{"blah": "blah"}')
         cl = BDEAClient('apikey')
         assert cl.request('http://www.rottentomatoes.com/') == {'blah': 'blah'}
Example #10
0
 def test_invalid_json_returns_empty(self):
     with patch('bdea.client.urlopen') as urlopen_mock:
         urlopen_mock.return_value = StringIO('invalid json')
         cl = BDEAClient('apikey')
         assert cl.request('http://www.rottentomatoes.com/') == {}
Example #11
0
 def test_urlerror_returns_empty(self):
     with patch('bdea.client.urlopen') as urlopen_mock:
         urlopen_mock.side_effect = URLError('No luck!')
         cl = BDEAClient('apikey')
         assert cl.request('http://www.rottentomatoes.com/') == {}