test_forgot_password_flow()   F
last analyzed

Complexity

Conditions 12

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
c 0
b 0
f 0
dl 0
loc 40
rs 2.7855

How to fix   Complexity   

Complexity

Complex classes like test_forgot_password_flow() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import re
2
from .. import factories as f
3
4
5
def test_forgot_password_flow(base_url, browser, outbox):
6
    f.create_usertype(slug='tutor', display_name='tutor')
7
    user = f.UserFactory()
8
9
    # Forgot password link must be present on login page
10
    url = base_url + '/accounts/login/'
11
    browser.visit(url)
12
    forgot_pass_link = browser.find_by_text('Forgot Password?')[0]
13
    assert forgot_pass_link
14
15
    # When clicking on the link it should open a page and prompt for email
16
    forgot_pass_link.click()
17
    assert 'Password Reset' in browser.title
18
    browser.fill('email', '[email protected]')
19
    browser.find_by_css('[type=submit]')[0].click()
20
    assert browser.is_text_present(
21
        'The e-mail address is not assigned to any user account')
22
    assert len(outbox) == 0
23
24
    # Now, enter a valid email
25
    browser.fill('email', user.email)
26
    browser.find_by_css('button[type=submit]')[0].click()
27
    assert browser.is_text_present('We have sent you an e-mail.')
28
    assert len(outbox) == 1
29
    mail = outbox[0]
30
    reset_link = re.findall(r'http.*/reset/.*/', mail.body)
31
    assert reset_link
32
33
    browser.visit(reset_link[0])
34
    assert "Change Password" in browser.title
35
    assert browser.is_text_present('Change Password')
36
    browser.fill('password1', 'mynewpassword')
37
    browser.fill('password2', 'mynewpassword_wrong')
38
    browser.find_by_css('[type=submit]')[0].click()
39
    assert browser.is_text_present(
40
        'You must type the same password each time.')
41
    browser.fill('password1', 'mynewpassword')
42
    browser.fill('password2', 'mynewpassword')
43
    browser.find_by_css('[type=submit]')[0].click()
44
    assert browser.is_text_present('Your password is now changed.')
45