~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/windmill/tests/test_branch_broken_links.py

[r=allenap][ui=none][bug=665407] Add invalid-link style to invalid lp
        branch short links and prevent click through

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
"""Test for links between branches and bugs or specs."""
 
5
 
 
6
__metaclass__ = type
 
7
__all__ = []
 
8
 
 
9
import unittest
 
10
 
 
11
import transaction
 
12
import windmill
 
13
from zope.security.proxy import removeSecurityProxy
 
14
 
 
15
from canonical.launchpad.windmill.testing import lpuser
 
16
from canonical.launchpad.windmill.testing.constants import SLEEP
 
17
from lp.code.windmill.testing import CodeWindmillLayer
 
18
from lp.testing import WindmillTestCase
 
19
 
 
20
 
 
21
ADD_COMMENT_BUTTON = (
 
22
    u'//input[@id="field.actions.save" and contains(@class, "button")]')
 
23
 
 
24
 
 
25
class TestBranchLinks(WindmillTestCase):
 
26
    """Test the rendering of broken branch links."""
 
27
 
 
28
    layer = CodeWindmillLayer
 
29
    suite_name = "Broken branch links"
 
30
 
 
31
    BUG_TEXT_TEMPLATE = u"""
 
32
    Here is the bug. Which branches are valid?
 
33
    Valid: %s
 
34
    Invalid %s
 
35
    """
 
36
 
 
37
    BRANCH_URL_TEMPLATE = "lp:%s"
 
38
 
 
39
    def make_product_and_valid_links(self):
 
40
        branch = self.factory.makeProductBranch()
 
41
        valid_branch_url = self.BRANCH_URL_TEMPLATE % branch.unique_name
 
42
        product = self.factory.makeProduct()
 
43
        product_branch = self.factory.makeProductBranch(product=product)
 
44
        naked_product = removeSecurityProxy(product)
 
45
        naked_product.development_focus.branch = product_branch
 
46
        valid_product_url = self.BRANCH_URL_TEMPLATE % product.name
 
47
 
 
48
        return (naked_product, [
 
49
            valid_branch_url,
 
50
            valid_product_url,
 
51
        ])
 
52
 
 
53
    def make_invalid_links(self):
 
54
        return [
 
55
            self.BRANCH_URL_TEMPLATE % 'foo',
 
56
            self.BRANCH_URL_TEMPLATE % 'bar',
 
57
            ]
 
58
 
 
59
    def test_invalid_url_rendering(self):
 
60
        """Link a bug from the branch page."""
 
61
        client = self.client
 
62
 
 
63
        lpuser.FOO_BAR.ensure_login(client)
 
64
 
 
65
        naked_product, valid_links = self.make_product_and_valid_links()
 
66
        invalid_links = self.make_invalid_links()
 
67
        bug_description = self.BUG_TEXT_TEMPLATE % (
 
68
            ', '.join(valid_links), ', '.join(invalid_links))
 
69
        bug = self.factory.makeBug(product=naked_product,
 
70
                                        title="The meaning of life is broken",
 
71
                                        description=bug_description)
 
72
        transaction.commit()
 
73
 
 
74
        bug_url = (
 
75
            windmill.settings['TEST_URL'] + '%s/+bug/%s'
 
76
            % (naked_product.name, bug.id))
 
77
        client.open(url=bug_url)
 
78
        client.waits.forElement(xpath=ADD_COMMENT_BUTTON)
 
79
 
 
80
        # Let the Ajax call run
 
81
        client.waits.sleep(milliseconds=SLEEP)
 
82
 
 
83
        code = """
 
84
            var good_a = windmill.testWin().document.getElementsByClassName(
 
85
                            'branch-short-link', 'a');
 
86
            var good_links = [];
 
87
            for( i=0; i<good_a.length; i++ ) {
 
88
                good_links.push(good_a[i].innerHTML);
 
89
            }
 
90
 
 
91
            var bad_a = windmill.testWin().document.getElementsByClassName(
 
92
                            'invalid-link', 'a');
 
93
            var bad_links = [];
 
94
            for( i=0; i<bad_a.length; i++ ) {
 
95
                bad_links.push(bad_a[i].innerHTML);
 
96
            }
 
97
 
 
98
 
 
99
            var result = {};
 
100
            result.good = good_links;
 
101
            result.bad = bad_links;
 
102
            result
 
103
        """
 
104
        raw_result = self.client.commands.execJS(js=code)
 
105
        result = raw_result['result']
 
106
        result_valid_links = result['good']
 
107
        result_invalid_links = result['bad']
 
108
        self.assertEqual(set(invalid_links), set(result_invalid_links))
 
109
        self.assertEqual(set(valid_links), set(result_valid_links))
 
110
 
 
111
 
 
112
def test_suite():
 
113
    return unittest.TestLoader().loadTestsFromName(__name__)