Fakultas Ilmu Komputer UI

Commit c59bb733 authored by Yumna Pratista Tastaftian's avatar Yumna Pratista Tastaftian
Browse files

Merge branch 'testinggoat/ex8' into 'master'

Testinggoat/ex8

See merge request !8
parents efa00ed2 02842153
Pipeline #26090 passed with stages
in 5 minutes and 52 seconds
<html>
<h1>Email sent</h1>
<p>Check your email, you'll find a message with a link that will log you into
the site.</p>
<html>
<h1>Email sent</h1>
<p>Check your email, you'll find a message with a link that will log you into
the site.</p>
</html>
\ No newline at end of file
from django.test import TestCase
from django.contrib.auth import get_user_model
User = get_user_model()
class UserModelTest(TestCase):
def test_user_is_valid_with_email_only(self):
user = User(email='a@b.com')
user.full_clean() # should not raise
\ No newline at end of file
from django.test import TestCase
from django.contrib.auth import get_user_model
from accounts.authentication import PasswordlessAuthenticationBackend
from accounts.models import Token, User
User = get_user_model()
class AuthenticateTest(TestCase):
def test_returns_None_if_no_such_token(self):
result = PasswordlessAuthenticationBackend().authenticate(
'no-such-token'
)
self.assertIsNone(result)
def test_returns_new_user_with_correct_email_if_token_exists(self):
email = 'edith@example.com'
token = Token.objects.create(email=email)
user = PasswordlessAuthenticationBackend().authenticate(token.uid)
new_user = User.objects.get(email=email)
self.assertEqual(user, new_user)
def test_returns_existing_user_with_correct_email_if_token_exists(self):
email = 'edith@example.com'
existing_user = User.objects.create(email=email)
token = Token.objects.create(email=email)
user = PasswordlessAuthenticationBackend().authenticate(token.uid)
self.assertEqual(user, existing_user)
\ No newline at end of file
from django.test import TestCase
from django.contrib.auth import get_user_model
from accounts.models import Token
User = get_user_model()
class UserModelTest(TestCase):
def test_user_is_valid_with_email_only(self):
user = User(email='a@b.com')
user.full_clean() # should not raise
def test_email_is_primary_key(self):
user = User(email='a@b.com')
self.assertEqual(user.pk, 'a@b.com')
class TokenModelTest(TestCase):
def test_links_user_with_auto_generated_uid(self):
token1 = Token.objects.create(email='a@b.com')
token2 = Token.objects.create(email='a@b.com')
from django.test import TestCase
from django.contrib.auth import get_user_model, login
from accounts.models import Token
User = get_user_model()
class UserModelTest(TestCase):
def test_user_is_valid_with_email_only(self):
user = User(email='a@b.com')
user.full_clean() # should not raise
def test_email_is_primary_key(self):
user = User(email='a@b.com')
self.assertEqual(user.pk, 'a@b.com')
def test_no_problem_with_auth_login(self):
user = User.objects.create(email='edith@example.com')
user.backend = ''
request = self.client.request().wsgi_request
login(request, user) # should not raise
class TokenModelTest(TestCase):
def test_links_user_with_auto_generated_uid(self):
token1 = Token.objects.create(email='a@b.com')
token2 = Token.objects.create(email='a@b.com')
self.assertNotEqual(token1.uid, token2.uid)
\ No newline at end of file
from django.test import TestCase
from unittest import skip
from unittest.mock import patch,call
import accounts.views
from accounts.models import Token
class SendLoginEmailViewTest(TestCase):
@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.assertEqual(mock_send_mail.called, True)
(subject, body, from_email, to_list), kwargs = 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'])
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")
def test_creates_token_associated_with_email(self):
self.client.post('/accounts/send_login_email', data={
'email': 'edith@example.com'
})
token = Token.objects.first()
self.assertEqual(token.email, 'edith@example.com')
@patch('accounts.views.send_mail')
def test_sends_link_to_login_using_token_uid(self, mock_send_mail):
self.client.post('/accounts/send_login_email', data={
'email': 'edith@example.com'
})
token = Token.objects.first()
expected_url = f'http://testserver/accounts/login?token={token.uid}'
(subject, body, from_email, to_list), kwargs = mock_send_mail.call_args
self.assertIn(expected_url, body)
@patch('accounts.views.auth')
class LoginViewTest(TestCase):
def test_redirects_to_home_page(self, mock_auth):
response = self.client.get('/accounts/login?token=abcd123')
self.assertRedirects(response, '/')
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')
)
args, kwargs = mock_auth.authenticate.call_args
# self.assertEqual(args, (,))
self.assertEqual(kwargs, {'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)
# @patch('accounts.views.messages')
# def test_adds_success_message_with_mocks(self, mock_messages):
# response = self.client.post('/accounts/send_login_email', data={
# 'email': 'edith@example.com'
# })
# expected = "Check your email, we've sent you a link you can use to log in."
# self.assertEqual(
# mock_messages.success.call_args,
# call(response.wsgi_request, expected),
# )
# def test_sends_mail_to_address_from_post(self):
# self.send_mail_called = False
# def fake_send_mail(subject, body, from_email, to_list):
# self.send_mail_called = True
# self.subject = subject
# self.body = body
# self.from_email = from_email
# self.to_list = to_list
# accounts.views.send_mail = fake_send_mail
# self.client.post('/accounts/send_login_email', data={
# 'email': 'edith@example.com'
# })
# self.assertTrue(self.send_mail_called)
# self.assertEqual(self.subject, 'Your login link for Superlists')
# self.assertEqual(self.from_email, 'noreply@superlists')
# self.assertEqual(self.to_list, ['edith@example.com'])
from django.conf.urls import url
from accounts import views
urlpatterns = [
url(r'^send_email$', views.send_login_email, name='send_login_email'),
url(r'^login$', views.login, name='login'),
url(r'^logout$', views.logout, name='logout'),
from django.conf.urls import url
from accounts import views
from django.contrib.auth import logout
urlpatterns = [
url(r'^send_login_email$', views.send_login_email, name='send_login_email'),
url(r'^login$', views.login, name='login'),
url(r'^logout$', logout, {'next_page':'/'}, name='logout'),
]
\ No newline at end of file
from django.shortcuts import render
# Create your views here.
from django.core.mail import send_mail
from django.shortcuts import redirect
from django.contrib import auth, messages
from .models import Token, User
from django.urls import reverse
import sys
def login(request):
user = auth.authenticate(uid=request.GET.get('token'))
print("LOGIN TOKEN : ", request.GET.get('token'))
if user :
print("LOGGED IN")
auth.login(request, user)
return redirect('/')
def send_login_email(request):
email = request.POST['email']
print(email)
token = Token.objects.create(email=email)
print(token.uid)
url = request.build_absolute_uri(
reverse('login') + '?token=' + str(token.uid)
)
message_body = f'Use this link to log in:\n\n{url}'
send_mail(
'Your login link for Superlists',
message_body,
'noreply@superlists',
[email]
)
messages.success(
request,
"Check your email, we've sent you a link you can use to log in."
)
return redirect('/')
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