Fakultas Ilmu Komputer UI

test_login.py 1.82 KB
Newer Older
1
2
3
import os
import re
from unittest import skipIf
Rani Lasma Uli's avatar
Rani Lasma Uli committed
4
5
from django.core import mail
from selenium.webdriver.common.keys import Keys
6
from functional_tests.base import FunctionalTest
Rani Lasma Uli's avatar
Rani Lasma Uli committed
7

8
TEST_EMAIL = 'hahaha@xmail.com'
Rani Lasma Uli's avatar
Rani Lasma Uli committed
9
10
11
12
SUBJECT = 'Your login link for Superlists'


class LoginTest(FunctionalTest):
13
14
    @skipIf(os.environ.get("HEROKU_HOST"),
            "Somehow always fails at Heroku, outgoing SMTP blocked by Heroku")
Rani Lasma Uli's avatar
Rani Lasma Uli committed
15
    def test_can_get_email_link_to_log_in(self):
Rani Lasma Uli's avatar
OK    
Rani Lasma Uli committed
16
        # Safira goes to the awesome superlists site
Rani Lasma Uli's avatar
Rani Lasma Uli committed
17
18
        # 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
Rani Lasma Uli's avatar
OK    
Rani Lasma Uli committed
19
        self.browser.get(self.BASE_URL)
Rani Lasma Uli's avatar
Rani Lasma Uli committed
20
21
22
23
24
25
        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',
Rani Lasma Uli's avatar
OK    
Rani Lasma Uli committed
26
            self.browser.find_element_by_tag_name('body').text))
Rani Lasma Uli's avatar
Rani Lasma Uli committed
27
28

        # She checks her email and finds a message
Rani Lasma Uli's avatar
OK    
Rani Lasma Uli committed
29
        email = mail.outbox[0]
Rani Lasma Uli's avatar
Rani Lasma Uli committed
30
31
32
33
34
35
36
37
38
        self.assertIn(TEST_EMAIL, email.to)
        self.assertEqual(email.subject, SUBJECT)

        # It has a url link in it
        self.assertIn('Use this link to log in', email.body)
        url_search = re.search(r'http://.+/.+$', email.body)
        if not url_search:
            self.fail(f'Could not find url in email body:\n{email.body}')
        url = url_search.group(0)
Rani Lasma Uli's avatar
OK    
Rani Lasma Uli committed
39
        self.assertIn(self.BASE_URL, url)
Rani Lasma Uli's avatar
Rani Lasma Uli committed
40
41
42
43
44

        # she clicks it
        self.browser.get(url)

        # she is logged in!
Rani Lasma Uli's avatar
Rani Lasma Uli committed
45
        self.wait_to_be_logged_in(email=TEST_EMAIL)
Rani Lasma Uli's avatar
OK    
Rani Lasma Uli committed
46

47
48
        # Now she logs out
        self.browser.find_element_by_link_text('Log out').click()
Rani Lasma Uli's avatar
OK    
Rani Lasma Uli committed
49

50
        # She is logged out
Rani Lasma Uli's avatar
Rani Lasma Uli committed
51
        self.wait_to_be_logged_out(email=TEST_EMAIL)