Page Menu
Home
GRNET
Search
Configure Global Search
Log In
Files
F1615579
tests.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Mar 22, 2:00 AM
Size
2 KB
Mime Type
text/x-python
Expires
Tue, Mar 24, 2:00 AM (12 h, 15 m)
Engine
blob
Format
Raw Data
Handle
354484
Attached To
rDJANGONOCAPTCHA django-nocaptcha-recaptcha
tests.py
View Options
import
os
import
json
from
django.forms
import
Form
from
django.test
import
TestCase
import
mock
from
nocaptcha_recaptcha
import
fields
,
client
class
TestForm
(
Form
):
captcha
=
fields
.
NoReCaptchaField
(
gtag_attrs
=
{
'data-theme'
:
'dark'
})
class
TestCase
(
TestCase
):
def
setUp
(
self
):
os
.
environ
[
'NORECAPTCHA_TESTING'
]
=
'True'
def
test_envvar_enabled
(
self
):
form_params
=
{
'g-recaptcha-response'
:
'PASSED'
}
form
=
TestForm
(
form_params
)
self
.
assertTrue
(
form
.
is_valid
())
def
test_envvar_disabled
(
self
):
os
.
environ
[
'NORECAPTCHA_TESTING'
]
=
'False'
form_params
=
{
'g-recaptcha-response'
:
'PASSED'
}
form
=
TestForm
(
form_params
)
self
.
assertFalse
(
form
.
is_valid
())
@mock.patch
(
'nocaptcha_recaptcha.client.urlopen'
)
def
test_client_submit_empty_input
(
self
,
mock_urlopen
):
"""
Should return False if input is empty string
"""
result
=
client
.
submit
(
''
,
''
,
''
)
self
.
assertFalse
(
result
.
is_valid
)
self
.
assertEqual
([
'incorrect-captcha-sol'
],
result
.
error_codes
)
@mock.patch
(
'nocaptcha_recaptcha.client.urlopen'
)
def
test_client_submit_correct
(
self
,
mock_urlopen
):
"""
Should return True if response is correct
"""
mock_resp
=
mock
.
Mock
()
mock_resp
.
read
.
return_value
=
json
.
dumps
(
{
'success'
:
True
,
'error-codes'
:
[]})
mock_urlopen
.
return_value
=
mock_resp
result
=
client
.
submit
(
'a'
,
'a'
,
'a'
)
self
.
assertTrue
(
result
.
is_valid
)
self
.
assertEqual
(
result
.
error_codes
,
None
)
@mock.patch
(
'nocaptcha_recaptcha.client.urlopen'
)
def
test_client_submit_response_not_json
(
self
,
mock_urlopen
):
"""
Should return json read error if response is not json
"""
mock_resp
=
mock
.
Mock
()
mock_resp
.
read
.
return_value
=
"{'success': True, 'error-codes': []}"
mock_urlopen
.
return_value
=
mock_resp
result
=
client
.
submit
(
'a'
,
'a'
,
'a'
)
self
.
assertFalse
(
result
.
is_valid
)
self
.
assertEqual
(
result
.
error_codes
,
[
'json-read-issue'
])
@mock.patch
(
'nocaptcha_recaptcha.client.urlopen'
)
def
test_client_submit_response_incorrect
(
self
,
mock_urlopen
):
"""
Should return false if response is incorrect
"""
mock_resp
=
mock
.
Mock
()
mock_resp
.
read
.
return_value
=
json
.
dumps
(
{
'success'
:
False
,
'error-codes'
:
[
'ERROR'
]})
mock_urlopen
.
return_value
=
mock_resp
result
=
client
.
submit
(
'a'
,
'a'
,
'a'
)
self
.
assertFalse
(
result
.
is_valid
)
self
.
assertEqual
(
result
.
error_codes
,
[
'ERROR'
])
def
tearDown
(
self
):
del
os
.
environ
[
'NORECAPTCHA_TESTING'
]
Event Timeline
Log In to Comment