~ursinha/launchpad/add-all-fl-tests

« back to all changes in this revision

Viewing changes to benchmarks/test_browsebug.py

  • Committer: Ursula Junque
  • Date: 2009-08-20 21:46:15 UTC
  • Revision ID: ursinha@canonical.com-20090820214615-17bktuq6qu7e1qbw
Adding fl tests: soyuz package queue and repository size 'applet' and browsing a bug flow.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
"""Load the bug subscribers portlet."""
 
5
 
 
6
__metaclass__ = type
 
7
__all__ = []
 
8
 
 
9
import cgi
 
10
import re
 
11
import threading
 
12
import unittest
 
13
import urllib
 
14
import urllib2
 
15
from random import randint
 
16
 
 
17
from funkload.FunkLoadTestCase import FunkLoadTestCase
 
18
 
 
19
counter = 1
 
20
counter_lock = threading.Lock()
 
21
 
 
22
 
 
23
class BrowseBug(FunkLoadTestCase):
 
24
 
 
25
    def setUp(self):
 
26
        """Setting up test."""
 
27
        self.logd("setUp")
 
28
 
 
29
        self.server_url = self.conf_get('main', 'url')
 
30
        self.username_pattern = self.conf_get("main", "login")
 
31
        self.password = self.conf_get("main", "password")
 
32
 
 
33
        global counter
 
34
        counter_lock.acquire()
 
35
        self.counter = counter
 
36
        counter = counter + 1
 
37
        if counter == 10000:
 
38
            counter = 1
 
39
        counter_lock.release()
 
40
 
 
41
        self.username = self.username_pattern % self.counter
 
42
 
 
43
    _error_re = re.compile('(?s)class="error message">(.*?)</')
 
44
 
 
45
 
 
46
    def assertNoFormErrors(self, response):
 
47
        match = self._error_re.search(response.body)
 
48
        if match is not None:
 
49
            self.fail('Form contained error "%s"' % match.group(1))
 
50
 
 
51
    def assertLoggedUser(self, response):
 
52
        match = response.body.find('value="Log Out"')
 
53
        if match == -1:
 
54
            self.fail('This user is not logged in.')
 
55
 
 
56
    def absolute_url(self, response, path):
 
57
        """Calculate an absolute URL using the response and the path."""
 
58
        return '%s://%s:%s%s' % (
 
59
            response.protocol, response.server, response.port, path)
 
60
 
 
61
    def login(self):
 
62
        self.logd("logging in as user %s" % self.username)
 
63
 
 
64
        server_url = self.server_url
 
65
 
 
66
        # Get the login page
 
67
        response = self.get(server_url + "/+login", description="GET /")
 
68
        self.assertNoFormErrors(response)
 
69
 
 
70
        # User posts form with username/password.
 
71
        fields = response.extractForm(path=[('form',0)], include_submit=True)
 
72
        fields['loginpage_email'] = self.username
 
73
        fields['loginpage_password'] = self.password
 
74
        response = self.post(
 
75
            self.absolute_url(response, '/+login'),
 
76
            fields, "POST /+login")
 
77
        self.assertNoFormErrors(response)
 
78
 
 
79
        self.assertLoggedUser(response)
 
80
        self.logd("Logged in sucessfully")
 
81
 
 
82
    def test_browsebug(self):
 
83
        self.logd("access bug page started")
 
84
 
 
85
        server_url = self.server_url
 
86
 
 
87
        self.login()
 
88
 
 
89
        # Choose randomly a bug number
 
90
        while True:
 
91
            bug_number = str(randint(100000, 400000))
 
92
            try:
 
93
                response = self.get(server_url + "/bugs/" + bug_number,
 
94
                    description="GET /bugs/" + bug_number)
 
95
                self.assertNoFormErrors(response)
 
96
                break
 
97
            except Exception, e:
 
98
                if "Response 403:" in e.message:
 
99
                    continue
 
100
                if response.code not in [200, 303, 403, 404]:
 
101
                    self.fail('ERROR: ' + response.code)
 
102
 
 
103
 
 
104
 
 
105
        self.logd("access bug page completed successfully")
 
106
 
 
107
    def tearDown(self):
 
108
        """Setting up test."""
 
109
        self.logd("tearDown.\n")
 
110
 
 
111
 
 
112
if __name__ in ('main', '__main__'):
 
113
    unittest.main()
 
114
 
 
115
 
 
116
#            try:
 
117
#                response = self.get(server_url + "/bugs/" + bug_number,
 
118
#                        description="GET /bugs/" + bug_number)
 
119
#                self.assertNoFormErrors(response)
 
120
#                break
 
121
#            except Exception, e:
 
122
#                if "HTTP Response 403: Forbidden" in e.message:
 
123
#                    # This bug is private, trying another one
 
124
#                    self.logd("ERROR: This bug is private. Won't log the"
 
125
#                            "failure.")
 
126
#                    continue
 
127
#                if "404:" in e.message:
 
128
#                    self.logd("ERROR: This bug doesn't exist, maybe it was"
 
129
#                            "removed?")
 
130
#                    continue
 
131
#                self.fail(e)
 
132
 
 
133