1 | from behave import when, then |
||
2 | from django.core import mail |
||
3 | from django.contrib.auth.models import User |
||
4 | from allauth.account.models import EmailConfirmationHMAC, EmailAddress |
||
5 | |||
6 | |||
7 | USERNAME = 'test' |
||
8 | EMAIL = '[email protected]' |
||
9 | PASSWORD = 'test123' |
||
10 | |||
11 | |||
12 | @given(r'an unconfirmed account') |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
13 | def step_impl(context): |
||
14 | context.user = User.objects.create(username=USERNAME, email=EMAIL) |
||
15 | context.user.set_password(PASSWORD) |
||
16 | context.user.save() |
||
17 | |||
18 | email = EmailAddress.objects.create(user=context.user, verified=False) |
||
19 | email.send_confirmation() |
||
20 | |||
21 | |||
22 | @given(r'a confirmed account') |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
23 | def step_impl(context): |
||
24 | context.user = User.objects.create(username=USERNAME, email=EMAIL) |
||
25 | context.user.set_password(PASSWORD) |
||
26 | context.user.save() |
||
27 | |||
28 | EmailAddress.objects.create(user=context.user, verified=True) |
||
29 | |||
30 | |||
31 | @when(r'I log in') |
||
32 | def step_impl(context): |
||
33 | browser = context.get_browser() |
||
34 | browser.visit(context.base_url) |
||
35 | browser.click_link_by_text('Log In') |
||
36 | browser.find_by_name('login').fill(USERNAME) |
||
37 | browser.find_by_name('password').fill(PASSWORD) |
||
38 | browser.find_by_value('Log In').click() |
||
39 | |||
40 | |||
41 | @when(r'I click the "{link}" link') |
||
42 | def step_impl(context, link): |
||
43 | context.get_browser().click_link_by_text(link) |
||
44 | |||
45 | |||
46 | @when(r'I fill out the sign up form') |
||
47 | def step_impl(context): |
||
48 | browser = context.get_browser() |
||
49 | |||
50 | browser.find_by_name('username').fill(USERNAME) |
||
51 | browser.find_by_name('email').fill(EMAIL) |
||
52 | browser.find_by_name('password1').fill(PASSWORD) |
||
53 | browser.find_by_name('password2').fill(PASSWORD) |
||
54 | |||
55 | |||
56 | @when(r'I fill out the sign up form without an email') |
||
57 | def step_impl(context): |
||
58 | browser = context.get_browser() |
||
59 | |||
60 | browser.find_by_name('username').fill(USERNAME) |
||
61 | browser.find_by_name('password1').fill(PASSWORD) |
||
62 | browser.find_by_name('password2').fill(PASSWORD) |
||
63 | |||
64 | |||
65 | @when(r'I click the "{button}" button') |
||
66 | def step_impl(context, button): |
||
67 | context.get_browser().find_by_value(button).click() |
||
68 | |||
69 | |||
70 | @when(r'I go to the email confirmation URL') |
||
71 | def step_impl(context): |
||
72 | email = EmailAddress.objects.get(user=context.user) |
||
73 | confirmation = EmailConfirmationHMAC(email) |
||
74 | context.get_browser().visit( |
||
75 | context.base_url + |
||
76 | '/accounts/confirm-email/{}/'.format(confirmation.key) |
||
77 | ) |
||
78 | |||
79 | |||
80 | @then(r'a new user should be created') |
||
81 | def step_impl(context): |
||
82 | assert User.objects.get(username=USERNAME) is not None |
||
83 | |||
84 | |||
85 | @then(r'no new user should be created') |
||
86 | def step_impl(context): |
||
87 | assert len(User.objects.all()) == 0 |
||
88 | |||
89 | |||
90 | @then(r'a confirmation email should be sent') |
||
91 | def step_impl(context): |
||
92 | assert len(mail.outbox) == 1 |
||
93 | |||
94 | |||
95 | @then(r'my email should be confirmed') |
||
96 | def step_impl(context): |
||
97 | EmailAddress.objects.filter(user=context.user, verified=True).exists() |
||
98 | |||
99 | |||
100 | @then(r'I should be taken to {url}') |
||
101 | def step_impl(context, url): |
||
102 | assert context.get_browser().url.endswith(url) |
||
103 | |||
104 | |||
105 | @then(r'I should see a message saying "{text}"') |
||
106 | def step_impl(context, text): |
||
107 | assert text in context.get_browser().find_by_id('messages').text |
||
108 |