Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Georgios Dagkakis
erp5
Commits
f53ef1a4
Commit
f53ef1a4
authored
Jun 23, 2021
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core_test: add missing tests for CaptchaField
parent
4d5d4c93
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
108 additions
and
0 deletions
+108
-0
bt5/erp5_core_test/TestTemplateItem/portal_components/test.erp5.testFields.py
...estTemplateItem/portal_components/test.erp5.testFields.py
+108
-0
No files found.
bt5/erp5_core_test/TestTemplateItem/portal_components/test.erp5.testFields.py
View file @
f53ef1a4
...
@@ -30,6 +30,8 @@
...
@@ -30,6 +30,8 @@
# TODO: Some tests from this file can be merged into Formulator
# TODO: Some tests from this file can be merged into Formulator
from
Products.ERP5Type.tests.ERP5TypeTestCase
import
ERP5TypeTestCase
from
Products.ERP5Type.tests.ERP5TypeTestCase
import
ERP5TypeTestCase
import
hashlib
import
mock
import
unittest
import
unittest
# Initialize ERP5Form Product to load monkey patches
# Initialize ERP5Form Product to load monkey patches
...
@@ -39,6 +41,7 @@ from Products.Formulator.Validator import ValidationError
...
@@ -39,6 +41,7 @@ from Products.Formulator.Validator import ValidationError
from
Products.Formulator.StandardFields
import
FloatField
,
StringField
,
\
from
Products.Formulator.StandardFields
import
FloatField
,
StringField
,
\
DateTimeField
,
TextAreaField
,
CheckBoxField
,
ListField
,
LinesField
,
\
DateTimeField
,
TextAreaField
,
CheckBoxField
,
ListField
,
LinesField
,
\
MultiListField
,
IntegerField
MultiListField
,
IntegerField
from
Products.ERP5Form.CaptchaField
import
CaptchaField
from
Products.Formulator.MethodField
import
Method
from
Products.Formulator.MethodField
import
Method
from
Products.Formulator.TALESField
import
TALESMethod
from
Products.Formulator.TALESField
import
TALESMethod
...
@@ -1084,6 +1087,110 @@ class TestFieldValueCache(ERP5TypeTestCase):
...
@@ -1084,6 +1087,110 @@ class TestFieldValueCache(ERP5TypeTestCase):
self
.
assertEqual
(
True
,
cache_size
==
self
.
_getCacheSize
(
'ProxyField.get_value'
))
self
.
assertEqual
(
True
,
cache_size
==
self
.
_getCacheSize
(
'ProxyField.get_value'
))
class
TestCaptchaField
(
ERP5TypeTestCase
):
"""Test Captcha field
"""
def
afterSetUp
(
self
):
self
.
field
=
CaptchaField
(
'test_field'
).
__of__
(
self
.
portal
)
self
.
widget
=
self
.
field
.
widget
self
.
validator
=
self
.
field
.
validator
def
beforeTearDown
(
self
):
self
.
portal
.
portal_sessions
.
manage_delObjects
(
[
hashlib
.
md5
(
b'1 + 1'
).
hexdigest
(),
hashlib
.
md5
(
b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
).
hexdigest
(),
]
)
if
'Base_viewTestCaptcha'
in
self
.
portal
.
portal_skins
.
custom
.
objectIds
():
self
.
portal
.
portal_skins
.
custom
.
manage_delObjects
([
'Base_viewTestCaptcha'
])
self
.
tic
()
def
test_numeric_good_captcha
(
self
):
self
.
field
.
values
[
'captcha_type'
]
=
'numeric'
with
mock
.
patch
(
'Products.ERP5Form.CaptchaField.random.randint'
,
return_value
=
1
),
\
mock
.
patch
(
'Products.ERP5Form.CaptchaField.random.choice'
,
side_effect
=
lambda
seq
:
seq
[
0
]):
field_html
=
self
.
field
.
render
(
REQUEST
=
self
.
portal
.
REQUEST
)
self
.
assertIn
(
'1 plus 1'
,
field_html
)
self
.
assertIn
(
hashlib
.
md5
(
b'1 + 1'
).
hexdigest
(),
field_html
)
self
.
assertEqual
(
self
.
validator
.
validate
(
self
.
field
,
'field_test'
,
{
'field_test'
:
'2'
,
'__captcha_field_test__'
:
hashlib
.
md5
(
b'1 + 1'
).
hexdigest
()
}),
'2'
,
)
def
test_numeric_bad_captcha
(
self
):
self
.
field
.
values
[
'captcha_type'
]
=
'numeric'
with
mock
.
patch
(
'Products.ERP5Form.CaptchaField.random.randint'
,
return_value
=
1
),
\
mock
.
patch
(
'Products.ERP5Form.CaptchaField.random.choice'
,
side_effect
=
lambda
seq
:
seq
[
0
]):
self
.
field
.
render
(
REQUEST
=
self
.
portal
.
REQUEST
)
self
.
assertRaises
(
ValidationError
,
self
.
validator
.
validate
,
self
.
field
,
'field_test'
,
{
'field_test'
:
'3'
,
'__captcha_field_test__'
:
hashlib
.
md5
(
b'1 + 1'
).
hexdigest
()
})
# once a reply was entered, the correct reply no longer work
self
.
assertRaises
(
ValidationError
,
self
.
validator
.
validate
,
self
.
field
,
'field_test'
,
{
'field_test'
:
'2'
,
'__captcha_field_test__'
:
hashlib
.
md5
(
b'1 + 1'
).
hexdigest
()
})
def
test_text_good_captcha
(
self
):
self
.
field
.
values
[
'captcha_type'
]
=
'text'
self
.
field
.
values
[
'captcha_dot_net_client'
]
=
'demo'
self
.
field
.
values
[
'captcha_dot_net_secret'
]
=
'secret'
self
.
field
.
values
[
'captcha_dot_net_use_ssl'
]
=
True
with
mock
.
patch
(
'Products.ERP5Form.CaptchasDotNet.random.choice'
,
side_effect
=
lambda
seq
:
seq
[
0
]):
field_html
=
self
.
field
.
render
(
REQUEST
=
self
.
portal
.
REQUEST
)
self
.
assertIn
(
'src="https://image.captchas.net/?client=demo&random=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&alphabet=abcdefghkmnopqrstuvwxyz"'
,
field_html
)
self
.
assertIn
(
hashlib
.
md5
(
b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
).
hexdigest
(),
field_html
)
self
.
assertEqual
(
self
.
validator
.
validate
(
self
.
field
,
'field_test'
,
{
'field_test'
:
'cbktzg'
,
'__captcha_field_test__'
:
hashlib
.
md5
(
b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
).
hexdigest
()
}),
'cbktzg'
,
)
def
test_text_bad_captcha
(
self
):
self
.
field
.
values
[
'captcha_type'
]
=
'text'
self
.
field
.
values
[
'captcha_dot_net_client'
]
=
'demo'
self
.
field
.
values
[
'captcha_dot_net_secret'
]
=
'secret'
self
.
field
.
values
[
'captcha_dot_net_use_ssl'
]
=
True
with
mock
.
patch
(
'Products.ERP5Form.CaptchasDotNet.random.choice'
,
side_effect
=
lambda
seq
:
seq
[
0
]):
self
.
field
.
render
(
REQUEST
=
self
.
portal
.
REQUEST
)
self
.
assertRaises
(
ValidationError
,
self
.
validator
.
validate
,
self
.
field
,
'field_test'
,
{
'field_test'
:
'wrong'
,
'__captcha_field_test__'
:
hashlib
.
md5
(
b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
).
hexdigest
()
})
# once a reply was entered, the correct reply no longer work
self
.
assertRaises
(
ValidationError
,
self
.
validator
.
validate
,
self
.
field
,
'field_test'
,
{
'field_test'
:
'cbktzg'
,
'__captcha_field_test__'
:
hashlib
.
md5
(
b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
).
hexdigest
()
})
def
makeDummyOid
():
def
makeDummyOid
():
import
time
,
random
import
time
,
random
return
'%s%s'
%
(
time
.
time
(),
random
.
random
())
return
'%s%s'
%
(
time
.
time
(),
random
.
random
())
...
@@ -1103,4 +1210,5 @@ def test_suite():
...
@@ -1103,4 +1210,5 @@ def test_suite():
suite
.
addTest
(
unittest
.
makeSuite
(
TestMultiListField
))
suite
.
addTest
(
unittest
.
makeSuite
(
TestMultiListField
))
suite
.
addTest
(
unittest
.
makeSuite
(
TestProxyField
))
suite
.
addTest
(
unittest
.
makeSuite
(
TestProxyField
))
suite
.
addTest
(
unittest
.
makeSuite
(
TestFieldValueCache
))
suite
.
addTest
(
unittest
.
makeSuite
(
TestFieldValueCache
))
suite
.
addTest
(
unittest
.
makeSuite
(
TestCaptchaField
))
return
suite
return
suite
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment