Fakultas Ilmu Komputer UI

Commit 67ab144c authored by Rani Lasma Uli's avatar Rani Lasma Uli
Browse files

trying to pass test

parent 5dcbea0d
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)
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
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('/')
......@@ -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>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment