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)
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)
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)
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'
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'
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'
def test_do_not_accept_email(self): cl = BDEAClient('apikey') with pytest.raises(ValueError): cl.get_domain_status('*****@*****.**')
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'}
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/') == {}
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/') == {}