Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Collection of Practice
2019
1606885025-practice
Commits
67ab144c
Commit
67ab144c
authored
Nov 22, 2019
by
Rani Lasma Uli
Browse files
trying to pass test
parent
5dcbea0d
Changes
4
Hide whitespace changes
Inline
Side-by-side
accounts/tests/test_views.py
View file @
67ab144c
from
unittest.mock
import
patch
,
call
from
django.test
import
TestCase
import
accounts.views
from
unittest.mock
import
patch
class
SendLoginEmailViewTest
(
TestCase
):
class
SendLoginEmailViewTest
(
TestCase
):
def
test_redirects_to_home_page
(
self
):
response
=
self
.
client
.
post
(
'/accounts/send_login_email'
,
data
=
{
'email'
:
'edith@example.com'
})
response
=
self
.
client
.
post
(
'/accounts/send_login_email'
,
data
=
{
'email'
:
'edith@example.com'
})
self
.
assertRedirects
(
response
,
'/'
)
@
patch
(
'accounts.views.send_mail'
)
def
test_sends_mail_to_address_from_post
(
self
,
mock_send_mail
):
self
.
client
.
post
(
'/accounts/send_login_email'
,
data
=
{
'email'
:
'edith@example.com'
})
self
.
client
.
post
(
'/accounts/send_login_email'
,
data
=
{
'email'
:
'edith@example.com'
})
self
.
assertTrue
(
mock_send_mail
.
called
)
(
subject
,
body
,
from_email
,
to_list
),
kwargs
=
mock_send_mail
.
call_args
(
subject
,
_
,
from_email
,
to_list
),
_
=
mock_send_mail
.
call_args
self
.
assertEqual
(
subject
,
'Your login link for Superlists'
)
self
.
assertEqual
(
from_email
,
'noreply@superlists'
)
self
.
assertEqual
(
to_list
,
[
'edith@example.com'
])
\ No newline at end of file
self
.
assertEqual
(
to_list
,
[
'edith@example.com'
])
def
test_adds_success_message
(
self
):
response
=
self
.
client
.
post
(
'/accounts/send_login_email'
,
data
=
{
'email'
:
'edith@example.com'
},
follow
=
True
)
message
=
list
(
response
.
context
[
'messages'
])[
0
]
self
.
assertEqual
(
message
.
message
,
"Check your email, we've sent you a link you can use to log in."
)
self
.
assertEqual
(
message
.
tags
,
"success"
)
@
patch
(
'accounts.views.auth'
)
class
LoginViewTest
(
TestCase
):
def
test_calls_authenticate_with_uid_from_get_request
(
self
,
mock_auth
):
self
.
client
.
get
(
'/accounts/login?token=abcd123'
)
self
.
assertEqual
(
mock_auth
.
authenticate
.
call_args
,
call
(
uid
=
'abcd123'
))
def
test_calls_auth_login_with_user_if_there_is_one
(
self
,
mock_auth
):
response
=
self
.
client
.
get
(
'/accounts/login?token=abcd123'
)
self
.
assertEqual
(
mock_auth
.
login
.
call_args
,
call
(
response
.
wsgi_request
,
mock_auth
.
authenticate
.
return_value
))
def
test_does_not_login_if_user_is_not_authenticated
(
self
,
mock_auth
):
mock_auth
.
authenticate
.
return_value
=
None
self
.
client
.
get
(
'/accounts/login?token=abcd123'
)
self
.
assertEqual
(
mock_auth
.
login
.
called
,
False
)
accounts/urls.py
View file @
67ab144c
from
django.contrib.auth.views
import
logout
from
django.conf.urls
import
url
from
accounts
import
views
...
...
@@ -5,5 +6,5 @@ urlpatterns = [
url
(
r
'^send_email$'
,
views
.
send_login_email
,
name
=
'send_login_email'
),
url
(
r
'^send_login_email$'
,
views
.
send_login_email
,
name
=
'send_login_email'
),
url
(
r
'^login$'
,
views
.
login
,
name
=
'login'
),
url
(
r
'^logout$'
,
views
.
logout
,
name
=
'logout'
),
url
(
r
'^logout$'
,
logout
,
{
'next_page'
:
'/'
}
,
name
=
'logout'
),
]
\ No newline at end of file
accounts/views.py
View file @
67ab144c
import
uuid
import
sys
from
django.shortcuts
import
render
,
redirect
from
django.core.mail
import
send_mail
from
django.contrib.auth
import
login
as
auth_login
,
logout
as
auth_logout
from
django.contrib
import
auth
,
messages
from
unittest.mock
import
patch
from
django.core.mail
import
send_mail
from
django.shortcuts
import
redirect
from
accounts.models
import
Token
def
send_login_email
(
request
):
email
=
request
.
POST
[
'email'
]
token
=
Token
.
objects
.
create
(
email
=
email
)
...
...
@@ -16,23 +12,16 @@ def send_login_email(request):
'Your login link for Superlists'
,
# subject
f
'Use this link to log in:
\n\n
{
url
}
'
,
# body
'noreply@superlists'
,
# email.to
[
email
]
[
email
]
,
)
messages
.
success
(
request
,
"Check your email, we've sent you a link you can use to log in."
)
return
redirect
(
'/'
)
def
login
(
request
):
print
(
'login view'
,
file
=
sys
.
stderr
)
uid
=
request
.
GET
.
get
(
'uid'
)
user
=
authenticate
(
uid
=
uid
)
if
user
is
not
None
:
auth_login
(
request
,
user
)
return
redirect
(
'/'
)
def
logout
(
request
):
auth_logout
(
request
)
def
login
(
request
):
user
=
auth
.
authenticate
(
uid
=
request
.
GET
.
get
(
'token'
))
if
user
:
auth
.
login
(
request
,
user
)
return
redirect
(
'/'
)
lists/templates/base.html
View file @
67ab144c
...
...
@@ -16,7 +16,7 @@
<p>
Logged in as {{ user.email }}
</p>
<p><a
id=
"id_logout"
href=
"{% url 'logout' %}"
>
Log out
</a></p>
{% else %}
<form
method=
"POST"
action =
"{% url 'send_login_email' %}"
>
<form
class=
"navbar-form navbar-right"
method=
"POST"
action =
"{% url 'send_login_email' %}"
>
Enter email to log in:
<input
name=
"email"
type=
"text"
/>
{% csrf_token %}
</form>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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