9053.2.3
by Ursula Junque
Adding the basic class methods. |
1 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
3 |
||
4 |
"""Basic login to launchpad test."""
|
|
5 |
||
6 |
__metaclass__ = type |
|
7 |
__all__ = [] |
|
8 |
||
9 |
import re |
|
10 |
import threading |
|
11 |
import unittest |
|
12 |
||
13 |
from funkload.FunkLoadTestCase import FunkLoadTestCase |
|
14 |
||
15 |
||
16 |
class Auth(FunkLoadTestCase): |
|
17 |
||
18 |
def setUp(self): |
|
19 |
"""Setting up test."""
|
|
20 |
self.logd("setUp") |
|
21 |
self.server_url = self.conf_get('main', 'url') |
|
22 |
||
9053.2.4
by Ursula Junque
Adding the functions that actually do the test. |
23 |
_error_re = re.compile('(?s)class="error message">(.*?)</') |
24 |
||
25 |
def assertNoFormErrors(self, response): |
|
26 |
"""Checks if the page returned ok and the login was sucessful."""
|
|
27 |
self.assertEquals(response.code, 200) |
|
28 |
match = self._error_re.search(response.body) |
|
29 |
if match is not None: |
|
30 |
self.fail('Form contained error: %s' % match.group(1)) |
|
9053.2.3
by Ursula Junque
Adding the basic class methods. |
31 |
|
9053.2.5
by Ursula Junque
Added a method that asserts if the user is already logged in, not sure if this is needed though. |
32 |
def assertIsNotLoggedIn(self, response): |
9053.2.7
by Ursula Junque
Now getting the login and passwd from the test conf file. |
33 |
# Not sure if this checking is needed.
|
9053.2.5
by Ursula Junque
Added a method that asserts if the user is already logged in, not sure if this is needed though. |
34 |
self.assertEquals(response.code, 200) |
35 |
match = response.body.find('If this is not you, please' |
|
36 |
'<a href="/+logout">log out now</a>') |
|
37 |
if match != -1: |
|
38 |
self.fail('Test failed: this user is already logged in.') |
|
39 |
||
9053.2.3
by Ursula Junque
Adding the basic class methods. |
40 |
def test_auth(self): |
41 |
"""Runs the steps of a simple Launchpad login."""
|
|
9053.2.5
by Ursula Junque
Added a method that asserts if the user is already logged in, not sure if this is needed though. |
42 |
self.logd("test_auth") |
9053.2.4
by Ursula Junque
Adding the functions that actually do the test. |
43 |
|
44 |
server_url = self.server_url |
|
45 |
||
46 |
# Get the login page
|
|
47 |
response = self.get(server_url + "/+login", description="GET /+login") |
|
48 |
self.assertNoFormErrors(response) |
|
9053.2.5
by Ursula Junque
Added a method that asserts if the user is already logged in, not sure if this is needed though. |
49 |
self.assertIsNotLoggedIn(response) |
9053.2.4
by Ursula Junque
Adding the functions that actually do the test. |
50 |
|
51 |
# The credentials of foo user, the loginator
|
|
9053.2.7
by Ursula Junque
Now getting the login and passwd from the test conf file. |
52 |
email = self.conf_get('main', 'login') |
53 |
password = self.conf_get('main', 'password') |
|
9053.2.4
by Ursula Junque
Adding the functions that actually do the test. |
54 |
|
55 |
# Get only the first form of the page, the login one,
|
|
56 |
# and fill it
|
|
57 |
fields = response.extractForm(path=[('form',0)], include_submit=True) |
|
58 |
fields['loginpage_email'] = email |
|
59 |
fields['loginpage_password'] = password |
|
60 |
||
61 |
# Submit the login form.
|
|
62 |
response = self.post( |
|
63 |
self.absolute_url(response, '/+login'), |
|
64 |
fields, "POST /+login") |
|
65 |
self.assertNoFormErrors(response) |
|
9053.2.5
by Ursula Junque
Added a method that asserts if the user is already logged in, not sure if this is needed though. |
66 |
self.assertIsNotLoggedIn(response) |
67 |
self.logd("logged in sucessfully") |
|
9053.2.4
by Ursula Junque
Adding the functions that actually do the test. |
68 |
|
69 |
def absolute_url(self, response, path): |
|
70 |
"""Calculate an absolute URL using the response and the path."""
|
|
71 |
return '%s://%s:%s%s' % ( |
|
72 |
response.protocol, response.server, response.port, path) |
|
9053.2.3
by Ursula Junque
Adding the basic class methods. |
73 |
|
74 |
def tearDown(self): |
|
75 |
"""Finishes the test."""
|
|
9053.2.4
by Ursula Junque
Adding the functions that actually do the test. |
76 |
self.logd("tearDown.\n") |
9053.2.3
by Ursula Junque
Adding the basic class methods. |
77 |
|
78 |
||
79 |
if __name__ in ('main', '__main__'): |
|
80 |
unittest.main() |
|
81 |
||
82 |
||
83 |