Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cubicweb
cubes
registration
Commits
ab07dab3d118
Commit
5a6b6bdf
authored
Feb 10, 2010
by
Sylvain Thénault
Browse files
3.6 api update
parent
06c7e00544bc
Changes
2
Show whitespace changes
Inline
Side-by-side
test/unittest_registration.py
View file @
ab07dab3
...
...
@@ -2,13 +2,12 @@ import re, cgi, urlparse
from
cubicweb
import
ValidationError
from
cubicweb.web
import
Redirect
from
cubicweb.devtools.apptest
import
MAILBOX
from
cubicweb.devtools.testlib
import
WebTest
from
cubicweb.devtools.testlib
import
MAILBOX
,
CubicWebTC
from
cubes.registration.views
import
RegistrationSendMailController
,
decrypt
,
encrypt
class
RegistrationTC
(
WebT
est
):
class
RegistrationTC
(
Cubic
WebT
C
):
captcha_value
=
u
'captcha value'
...
...
@@ -38,7 +37,7 @@ class RegistrationTC(WebTest):
def
_check_redirect
(
self
,
ctrl
,
urlpath
,
**
urlparams
):
try
:
ctrl
.
req
.
set_session_data
(
'captcha'
,
self
.
captcha_value
)
ctrl
.
_cw
.
set_session_data
(
'captcha'
,
self
.
captcha_value
)
ctrl
.
publish
()
self
.
fail
(
'should redirect to '
+
urlpath
)
except
Redirect
,
ex
:
...
...
@@ -50,8 +49,8 @@ class RegistrationTC(WebTest):
self
.
login
(
'anon'
)
req
=
self
.
request
()
req
.
form
=
self
.
data
.
copy
()
ctrl
=
self
.
env
.
vreg
[
'controllers'
].
select
(
'registration_sendmail'
,
req
,
appli
=
self
.
env
.
app
)
ctrl
=
self
.
vreg
[
'controllers'
].
select
(
'registration_sendmail'
,
req
,
appli
=
self
.
app
)
# check redirect
self
.
_check_redirect
(
ctrl
,
''
,
__message
=
'Your registration email has been sent. Follow instructions in there to activate your account.'
)
# check email contains activation url...
...
...
@@ -70,29 +69,29 @@ class RegistrationTC(WebTest):
(
'captcha'
,
'incorrect captcha value'
)):
req
.
form
=
self
.
data
.
copy
()
req
.
form
.
pop
(
param
)
ctrl
=
self
.
env
.
vreg
[
'controllers'
].
select
(
'registration_sendmail'
,
req
,
appli
=
self
.
env
.
app
)
ctrl
.
req
.
set_session_data
(
'captcha'
,
self
.
captcha_value
)
ctrl
=
self
.
vreg
[
'controllers'
].
select
(
'registration_sendmail'
,
req
,
appli
=
self
.
app
)
ctrl
.
_cw
.
set_session_data
(
'captcha'
,
self
.
captcha_value
)
try
:
ctrl
.
publish
()
self
.
fail
(
'should raise ValidationError'
)
except
ValidationError
,
e
:
self
.
assertDictEquals
(
e
.
errors
,
{
param
:
msg
})
# check captcha has expired
self
.
assertEquals
(
ctrl
.
req
.
get_session_data
(
'captcha'
),
None
)
self
.
assertEquals
(
ctrl
.
_cw
.
get_session_data
(
'captcha'
),
None
)
def
_confirm_ctrl
(
self
,
key
=
None
):
self
.
login
(
'anon'
)
req
=
self
.
request
()
req
.
form
=
{
'key'
:
key
or
encrypt
(
self
.
data
,
self
.
config
[
'cypher-seed'
])}
return
self
.
env
.
vreg
[
'controllers'
].
select
(
'registration_confirm'
,
req
,
appli
=
self
.
env
.
app
)
return
self
.
vreg
[
'controllers'
].
select
(
'registration_confirm'
,
req
,
appli
=
self
.
app
)
def
test_confirm_ok
(
self
):
ctrl
=
self
.
_confirm_ctrl
()
self
.
_check_redirect
(
ctrl
,
''
,
__message
=
u
'Congratulations, your registration is complete. Welcome %(firstname)s %(surname)s !'
%
self
.
data
)
ctrl
.
req
.
cnx
.
commit
()
ctrl
.
req
.
cnx
.
close
()
ctrl
.
_cw
.
cnx
.
commit
()
ctrl
.
_cw
.
cnx
.
close
()
self
.
restore_connection
()
rset
=
self
.
execute
(
'Any U WHERE U login %(login)s, U firstname %(firstname)s, '
'U surname %(surname)s, U use_email M, M address %(use_email)s'
,
...
...
views.py
View file @
ab07dab3
...
...
@@ -48,15 +48,14 @@ class CaptchaView(StartupView):
content_type
=
'image/jpg'
def
call
(
self
):
text
,
data
=
captcha
.
captcha
(
self
.
config
[
'captcha-font-file'
],
self
.
config
[
'captcha-font-size'
])
text
,
data
=
captcha
.
captcha
(
self
.
_cw
.
vreg
.
config
[
'captcha-font-file'
],
self
.
_cw
.
vreg
.
config
[
'captcha-font-size'
])
self
.
_cw
.
set_session_data
(
'captcha'
,
text
)
self
.
w
(
data
.
read
())
class
CaptchaWidget
(
wdg
.
TextInput
):
def
render
(
self
,
form
,
field
,
renderer
=
None
):
name
,
curvalues
,
attrs
=
self
.
_render_attrs
(
form
,
field
)
# t=int(time()*100) to make sure img is not cached
src
=
form
.
_cw
.
build_url
(
'view'
,
vid
=
'captcha'
,
t
=
int
(
time
()
*
100
))
img
=
tags
.
img
(
src
=
src
,
alt
=
u
'captcha'
)
...
...
@@ -88,8 +87,8 @@ class RegistrationFormView(form.FormViewMixIn, StartupView):
__regid__
=
'registration'
def
call
(
self
):
form
=
self
.
vreg
[
'forms'
].
select
(
'registration'
,
self
.
_cw
)
self
.
w
(
form
.
form_
render
())
form
=
self
.
_cw
.
vreg
[
'forms'
].
select
(
'registration'
,
self
.
_cw
)
self
.
w
(
form
.
render
())
class
RegistrationSendMailController
(
controller
.
Controller
):
...
...
@@ -110,14 +109,14 @@ See you soon on %(base_url)s !
data
=
self
.
checked_data
()
recipient
=
data
[
'use_email'
]
msg
=
self
.
build_email
(
recipient
,
data
)
self
.
config
.
sendmails
([(
msg
,
(
recipient
,))])
self
.
_cw
.
vreg
.
config
.
sendmails
([(
msg
,
(
recipient
,))])
raise
Redirect
(
self
.
success_redirect_url
())
def
checked_data
(
self
):
'''only basic data check here (required attributes and password
confirmation check)
'''
fieldsform
=
self
.
vreg
[
'forms'
].
select
(
'registration'
,
self
.
_cw
)
fieldsform
=
self
.
_cw
.
vreg
[
'forms'
].
select
(
'registration'
,
self
.
_cw
)
data
=
{}
errors
=
{}
for
field
in
fieldsform
.
_fields_
:
...
...
@@ -140,15 +139,15 @@ See you soon on %(base_url)s !
def
build_email
(
self
,
recipient
,
data
):
if
not
data
.
get
(
'firstname'
)
or
data
.
get
(
'surtname'
):
data
[
'firstname'
]
=
data
[
'login'
]
data
.
update
({
'base_url'
:
self
.
config
[
'base-url'
],
data
.
update
({
'base_url'
:
self
.
_cw
.
vreg
.
config
[
'base-url'
],
'url'
:
self
.
activation_url
(
data
)})
content
=
self
.
_cw
.
_
(
self
.
content
)
%
data
subject
=
self
.
_cw
.
_
(
self
.
subject
)
%
data
return
mail
.
format_mail
({},
[
recipient
],
content
=
content
,
subject
=
subject
,
config
=
self
.
config
)
subject
=
subject
,
config
=
self
.
_cw
.
vreg
.
config
)
def
activation_url
(
self
,
data
):
key
=
encrypt
(
data
,
self
.
config
[
'cypher-seed'
])
key
=
encrypt
(
data
,
self
.
_cw
.
vreg
.
config
[
'cypher-seed'
])
return
self
.
_cw
.
build_url
(
'registration_confirm'
,
key
=
key
)
def
success_redirect_url
(
self
):
...
...
@@ -163,7 +162,7 @@ class RegistrationConfirmController(controller.Controller):
def
publish
(
self
,
rset
=
None
):
req
=
self
.
_cw
try
:
data
=
decrypt
(
req
.
form
[
'key'
],
self
.
vreg
.
config
[
'cypher-seed'
])
data
=
decrypt
(
req
.
form
[
'key'
],
self
.
_cw
.
vreg
.
config
[
'cypher-seed'
])
self
.
debug
(
'registration data: %s'
,
data
)
except
:
msg
=
req
.
_
(
u
'Invalid registration data. Please try registering again.'
)
...
...
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