~vila/canonical-identity-provider/redirect-loop

« back to all changes in this revision

Viewing changes to identityprovider/tests/acceptance/helpers.py

  • Committer: Tarmac
  • Author(s): Ricardo Kirkner
  • Date: 2011-12-22 11:38:36 UTC
  • mfrom: (242.3.13 sso-sst)
  • Revision ID: tarmac@199959-20111222113836-1egowi5i2avr1s1o
[r=ricardokirkner] Include acceptance (SST) tests as part of the code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import quopri
 
4
import re
 
5
 
 
6
from django.conf import settings
 
7
from canonical.isd.tests.sst import mail
 
8
from sst.actions import *
 
9
 
 
10
 
 
11
def register_account(email_address, displayname="My Name",
 
12
                     password="Admin007", fetch_email=True, verify=True):
 
13
    """Register an account and verify the token by default."""
 
14
    vcode = None
 
15
    go_to('/+new_account')
 
16
    wait_for(assert_title, 'Create account')
 
17
    write_textfield('id_displayname', displayname)
 
18
    write_textfield('id_email', email_address)
 
19
    write_textfield('id_password', password)
 
20
    write_textfield('id_passwordconfirm', password)
 
21
    # Even though the recaptcha field is ignored for our tests, we do
 
22
    # want to verify that it is on the page.
 
23
    write_textfield('recaptcha_response_field', 'ignored')
 
24
    click_button(get_element(name='continue'))
 
25
    wait_for(assert_url, '/+email-sent')
 
26
    if fetch_email:
 
27
        vcode = get_verification_code_for_address(email_address)
 
28
        if verify:
 
29
            write_textfield(get_element(name='confirmation_code'), vcode)
 
30
            click_button(get_element(css_class='btn'))
 
31
    return vcode
 
32
 
 
33
 
 
34
def add_email(address, verify=False):
 
35
    go_to('/+emails')
 
36
    wait_for(assert_title_contains, "'s email addresses")
 
37
    write_textfield('id_newemail', address)
 
38
    click_button(get_element(name='continue'))
 
39
    code = get_verification_code_for_address(address)
 
40
    if verify:
 
41
        go_to('/+enter_token')
 
42
        wait_for(write_textfield, 'id_confirmation_code', code)
 
43
        write_textfield('id_email', address)
 
44
        click_button(get_element(name='continue'))
 
45
        assert_title('Complete email address validation')
 
46
        click_button(get_element(name='continue'))
 
47
    return code
 
48
 
 
49
 
 
50
def delete_email():
 
51
    go_to('/+emails')
 
52
    remove_link = get_element(tag='a', text='Remove')
 
53
    click_link(remove_link)
 
54
    confirm_button = get_element(tag='button', text='Yes, remove')
 
55
    click_button(confirm_button)
 
56
 
 
57
 
 
58
def try_to_validate_email(address, code):
 
59
    go_to('/+enter_token')
 
60
    wait_for(write_textfield, 'id_confirmation_code', code)
 
61
    write_textfield('id_email', address)
 
62
    click_button(get_element(name='continue'))
 
63
 
 
64
 
 
65
def login(email, password):
 
66
    go_to('/')
 
67
    write_textfield('id_email', email)
 
68
    write_textfield('id_password', password)
 
69
    click_button(get_element(name='continue'))
 
70
 
 
71
 
 
72
def logout():
 
73
    go_to('%s/+logout' % get_base_url())
 
74
 
 
75
 
 
76
def _get_verification_data_for_address(email_address):
 
77
    """A private helper for public helpers below.
 
78
 
 
79
    Note: We have two different public helpers here for verification
 
80
    code and link so that functional tests don't need to deal with
 
81
    idioms like:
 
82
        vcode, ignored = get_verification_for_address(email_address).
 
83
    """
 
84
    email_msg = mail.get_latest_email_sent_to(email_address)
 
85
    vcode = link = None
 
86
    if email_msg:
 
87
        # The body is encoded as quoted-printable.  This affects any line longer
 
88
        # than a certain length.  Decode now to not have to worry about it in
 
89
        # the regexen.
 
90
        body = quopri.decodestring(email_msg.get_payload())
 
91
        match = re.search(
 
92
            'Here is your confirmation code:(.*)(Enter|If you made)',
 
93
            body, re.S)
 
94
        if match:
 
95
            vcode = match.group(1).strip()
 
96
        else:
 
97
            raise AssertionError(
 
98
                "No verification code found in email.")
 
99
        match = re.search(
 
100
            'confirm your (?:account|email address|reset):(.*)If',
 
101
            body, re.S)
 
102
        if match:
 
103
            link = match.group(1).strip()
 
104
        else:
 
105
            raise AssertionError(
 
106
                "No verification link found in email.")
 
107
    return vcode, link
 
108
 
 
109
 
 
110
def get_verification_code_for_address(email_address):
 
111
    print("Retrieving verification code for %s." % email_address)
 
112
    vcode, link = _get_verification_data_for_address(email_address)
 
113
    print("Verification code retrieved: %s." % vcode)
 
114
    return vcode
 
115
 
 
116
 
 
117
def get_verification_link_for_address(email_address):
 
118
    print("Retrieving verification link for %s." % email_address)
 
119
    vcode, link = _get_verification_data_for_address(email_address)
 
120
    print("Verification link retrieved: %s." % link)
 
121
    return link
 
122
 
 
123
 
 
124
def request_password_reset(email_address):
 
125
    logout()
 
126
    go_to('/+forgot_password')
 
127
    write_textfield('id_email', email_address)
 
128
    # Even though the recaptcha field is ignored for our tests, we do
 
129
    # want to verify that it is on the page.
 
130
    write_textfield('recaptcha_response_field', 'ignored')
 
131
    click_button(get_element(name='continue'))
 
132
 
 
133
 
 
134
def login_to_test_account():
 
135
    from sst import actions
 
136
 
 
137
    login(settings.TEST_ACCOUNT_EMAIL, settings.TEST_ACCOUNT_PASSWORD)
 
138
    # Wait for the reload
 
139
    wait_for(exists_element, id='ubuntu-header')
 
140
 
 
141
    if exists_element(tag='span', css_class='error', text="Password didn't match."):
 
142
        if get_base_url() == 'https://login.ubuntu.com/':
 
143
            actions._raise("This test requires a test account to be present")
 
144
 
 
145
        register_account(settings.TEST_ACCOUNT_EMAIL, "Test Account",
 
146
            settings.TEST_ACCOUNT_PASSWORD)
 
147
 
 
148
 
 
149
def login_from_redirect(email=settings.QA_ACCOUNT_EMAIL,
 
150
        password=settings.QA_ACCOUNT_PASSWORD):
 
151
    wait_for(assert_title, 'Log in')
 
152
    write_textfield('id_email', email)
 
153
    write_textfield('id_password', password)
 
154
    click_button(get_element(name='continue'))
 
155
    wait_for(assert_title_contains, 'Authenticate to')
 
156
    click_button(get_element(name='yes'))
 
157