~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Basic login to launchpad test."""

__metaclass__ = type
__all__ = []

import re
import threading
import unittest

from funkload.FunkLoadTestCase import FunkLoadTestCase


class Auth(FunkLoadTestCase):

    def setUp(self):
        """Setting up test."""
        self.logd("setUp")
        self.server_url = self.conf_get('main', 'url')

    _error_re = re.compile('(?s)class="error message">(.*?)</')

    def assertNoFormErrors(self, response):
        """Checks if the page returned ok and the login was sucessful."""
        self.assertEquals(response.code, 200)
        match = self._error_re.search(response.body)
        if match is not None:
            self.fail('Form contained error: %s' % match.group(1))

    def assertIsNotLoggedIn(self, response):
        # Not sure if this checking is needed.
        self.assertEquals(response.code, 200)
        match = response.body.find('If this is not you, please'
            '<a href="/+logout">log out now</a>')
        if match != -1:
            self.fail('Test failed: this user is already logged in.')

    def test_auth(self):
        """Runs the steps of a simple Launchpad login."""
        self.logd("test_auth")

        server_url = self.server_url

        # Get the login page
        response = self.get(server_url + "/+login", description="GET /+login")
        self.assertNoFormErrors(response)
        self.assertIsNotLoggedIn(response)

        # The credentials of foo user, the loginator
        email = self.conf_get('main', 'login')
        password = self.conf_get('main', 'password')

        # Get only the first form of the page, the login one,
        # and fill it
        fields = response.extractForm(path=[('form',0)], include_submit=True)
        fields['loginpage_email'] = email
        fields['loginpage_password'] = password

        # Submit the login form.
        response = self.post(
            self.absolute_url(response, '/+login'),
            fields, "POST /+login")
        self.assertNoFormErrors(response)
        self.assertIsNotLoggedIn(response)
        self.logd("logged in sucessfully")

    def absolute_url(self, response, path):
        """Calculate an absolute URL using the response and the path."""
        return '%s://%s:%s%s' % (
            response.protocol, response.server, response.port, path)

    def tearDown(self):
        """Finishes the test."""
        self.logd("tearDown.\n")


if __name__ in ('main', '__main__'):
    unittest.main()