def render(self, name, value, attrs=None): client = Cleanweb(key=self.key) cap_dict = client.get_captcha() return render_to_string(DEFAULT_WIDGET_TEMPLATE, {'yacaptcha_img_url': cap_dict['url'], 'yacaptcha_response_field': cap_dict['captcha'] })
def render(self, name, value, attrs=None): client = Cleanweb(key=self.key) cap_dict = client.get_captcha() return render_to_string( DEFAULT_WIDGET_TEMPLATE, { 'yacaptcha_img_url': cap_dict['url'], 'yacaptcha_response_field': cap_dict['captcha'] })
def validate(self, values): super(YaCaptchaField, self).validate(values[0]) recaptcha_challenge_value = values[0] recaptcha_response_value = values[1] client = Cleanweb(key=self.widget.key) check = client.check_captcha(captcha=recaptcha_response_value, value=recaptcha_challenge_value) if not check: raise forms.ValidationError(self.error_messages['captcha_invalid'])
def test_is_spam(self): HTTPretty.register_uri(HTTPretty.POST, "http://cleanweb-api.yandex.ru/1.0/check-spam", body=self.spam_response) spam_text = 'ШОК! Видео скачать без СМС! http://cnn.com http://yandex.ru' spam_or_ham = Cleanweb(key='yyy').check_spam(subject=spam_text, body='123', ip='10.178.33.2', name='Vasia', body_type='html') assert spam_or_ham == { 'id': '123456789efgh', 'spam_flag': True, 'links': [('http://cnn.com', True), ('http://yandex.ru', False)] } assert HTTPretty.last_request.method == "POST" self.assertBodyQueryString( **{ 'ip': ['10.178.33.2'], 'body-html': ['123'], 'subject-plain': [spam_text], 'name': ['Vasia'] }) assert HTTPretty.last_request.querystring == { 'key': ['yyy'], }
def test_can_be_obtained_without_msg_id(self): HTTPretty.register_uri(HTTPretty.GET, "http://cleanweb-api.yandex.ru/1.0/get-captcha", body=self.valid_response) assert Cleanweb(key='xxx').get_captcha() == { 'captcha': 'abcd12345', 'url': 'http://i.captcha.yandex.net/image?key=abcd12345' } assert HTTPretty.last_request.querystring == { "key": ["xxx"], }
def test_is_not_spam(self): HTTPretty.register_uri(HTTPretty.POST, "http://cleanweb-api.yandex.ru/1.0/check-spam", body=self.ham_response) assert Cleanweb(key='yyy').check_spam(body='Питон') == { 'id': '123456789abcd', 'spam_flag': False, 'links': [] } assert HTTPretty.last_request.method == "POST" self.assertBodyQueryString(**{'body-plain': ['Питон']}) assert HTTPretty.last_request.querystring == {'key': ['yyy']}
def test_xml_error_is_handled(self): error_repsonse = """ <!DOCTYPE get-captcha-result PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <error key="key-not-registered"><message>Provided API key not registered</message></error>""" HTTPretty.register_uri(HTTPretty.GET, "http://cleanweb-api.yandex.ru/1.0/get-captcha", body=error_repsonse, status=403) with pytest.raises(CleanwebError) as excinfo: Cleanweb(key='xxx').get_captcha() assert str(excinfo.value ) == 'Provided API key not registered (key-not-registered)'
def test_valid_captcha_no_msg_is_ok(self): HTTPretty.register_uri( HTTPretty.GET, "http://cleanweb-api.yandex.ru/1.0/check-captcha", body=self.valid_response) assert Cleanweb(key='xxx').check_captcha(captcha='abcd12345', value='48151632') assert HTTPretty.last_request.querystring == { "key": ["xxx"], "captcha": ["abcd12345"], "value": ["48151632"], }
def test_invalid_captcha(self): HTTPretty.register_uri( HTTPretty.GET, "http://cleanweb-api.yandex.ru/1.0/check-captcha", body=self.invalid_response) assert Cleanweb(key='xxx').check_captcha(id='somekindofmsgid', captcha='abcd12345', value='000') == False assert HTTPretty.last_request.querystring == { "key": ["xxx"], "captcha": ["abcd12345"], "value": ["000"], "id": ["somekindofmsgid"] }
def test_raises_exception_when_instantiated_with_no_key(self): with pytest.raises(CleanwebError) as excinfo: Cleanweb() assert str( excinfo.value ) == "Cleanweb needs API key to operate. Get it here: http://api.yandex.ru/cleanweb/form.xml"
def test_spam_is_spam(self): HTTPretty.register_uri(HTTPretty.POST, "http://cleanweb-api.yandex.ru/1.0/complain", body=self.valid_response) Cleanweb(key='zzz').complain(id='somekindofmsgid', is_spam=False) self.assertBodyQueryString(spamtype=['ham'], id=['somekindofmsgid'])