Fakultas Ilmu Komputer UI

Commit 5dcbea0d authored by Rani Lasma Uli's avatar Rani Lasma Uli
Browse files

OK

parent 2cee6bfe
......@@ -44,9 +44,8 @@ FunctionalTest:
- apt-get update -qq && apt-get install -y -qq unzip
- apt-get install -y google-chrome-stable
- apt-get install -y xvfb
- wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz
- sh -c 'tar -x geckodriver -zf geckodriver-v0.23.0-linux64.tar.gz -O > /usr/bin/geckodriver'
- chmod chmod +x /usr/bin/geckodriver
- wget https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip
- unzip chromedriver_linux64.zip
- pip install -r requirements.txt
when: on_success
script:
......
from django.test import TestCase
# Create your tests here.
from django.test import TestCase
import accounts.views
from unittest.mock import patch
class SendLoginEmailViewTest(TestCase):
def test_redirects_to_home_page(self):
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.assertTrue(mock_send_mail.called)
(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'])
\ No newline at end of file
......@@ -3,6 +3,7 @@ from accounts import views
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'),
]
\ No newline at end of file
import uuid
import sys
from django.shortcuts import render
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 accounts.models import Token
def send_login_email(request):
email = request.POST['email']
uid = str(uuid.uuid4())
Token.objects.create(email=email, uid=uid)
print('saving uid', uid, 'for email', email, file=sys.stderr)
url = request.build_absolute_uri(f'/accounts/login?uid={uid}')
token = Token.objects.create(email=email)
url = request.build_absolute_uri(f'/accounts/login?token={token.uid}')
send_mail(
'Your login link for Superlists',
f'Use this link to log in:\n\n{url}',
'noreply@superlists',
[email],
'Your login link for Superlists', # subject
f'Use this link to log in:\n\n{url}', # body
'noreply@superlists', # email.to
[email]
)
return render(request, 'login_email_sent.html')
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)
......
......@@ -11,21 +11,20 @@ SUBJECT = 'Your login link for Superlists'
class LoginTest(FunctionalTest):
def test_can_get_email_link_to_log_in(self):
# Edith goes to the awesome superlists site
# Safira goes to the awesome superlists site
# and notices a "Log in" section in the navbar for the first time
# It's telling her to enter her email address, so she does
self.browser.get(self.live_server_url)
self.browser.get(self.BASE_URL)
self.browser.find_element_by_name('email').send_keys(TEST_EMAIL)
self.browser.find_element_by_name('email').send_keys(Keys.ENTER)
# A message appears telling her an email has been sent
self.wait_for(lambda: self.assertIn(
'Check your email',
self.browser.find_element_by_tag_name('body').text
))
self.browser.find_element_by_tag_name('body').text))
# She checks her email and finds a message
email = mail.outbox[0]
email = mail.outbox[0]
self.assertIn(TEST_EMAIL, email.to)
self.assertEqual(email.subject, SUBJECT)
......@@ -35,14 +34,21 @@ class LoginTest(FunctionalTest):
if not url_search:
self.fail(f'Could not find url in email body:\n{email.body}')
url = url_search.group(0)
self.assertIn(self.live_server_url, url)
self.assertIn(self.BASE_URL, url)
# she clicks it
self.browser.get(url)
# she is logged in!
self.wait_for(
lambda: self.browser.find_element_by_link_text('Log out')
)
lambda: self.browser.find_element_by_link_text('Log out'))
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertIn(TEST_EMAIL, navbar.text)
\ No newline at end of file
self.assertIn(TEST_EMAIL, navbar.text)
# # Now she logs out
# self.browser.find_element_by_link_text('Log out').click()
# # She is logged out
# self.wait_for(lambda: self.browser.find_element_by_name('email'))
# navbar = self.browser.find_element_by_css_selector('.navbar')
# self.assertNotIn(TEST_EMAIL, navbar.text)
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