~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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Provides mixins for visibility tests in messages."""

__metaclass__ = type

__all__ = [
    'TestHideMessageControlMixin',
    'TestMessageVisibilityMixin',
    ]


from lp.testing.pages import find_tag_by_id


class TestMessageVisibilityMixin:

    comment_text = "You can't see me."

    def makeHiddenMessage(self):
        """To be overwridden by subclasses.

        This method must create and return a message bearing object
        (e.g. bug or question) with a hidden message/comment.
        """
        raise NotImplementedError

    def getView(self, context, user=None, no_login=False):
        """To be overwridden by subclasses.

        This method returns a view object rendered on the context
        obtained from makeHiddenMessage.
        """
        raise NotImplementedError

    def test_admin_can_see_comments(self):
        context = self.makeHiddenMessage()
        admin = self.factory.makeAdministrator()
        view = self.getView(context=context, user=admin)
        self.assertIn(self.comment_text, view.contents)

    def test_registry_can_see_comments(self):
        context = self.makeHiddenMessage()
        registry_expert = self.factory.makeRegistryExpert()
        view = self.getView(context=context, user=registry_expert)
        self.assertIn(self.comment_text, view.contents)

    def test_anon_cannot_see_comments(self):
        context = self.makeHiddenMessage()
        view = self.getView(context=context, no_login=True)
        self.assertNotIn(self.comment_text, view.contents)

    def test_random_cannot_see_comments(self):
        context = self.makeHiddenMessage()
        view = self.getView(context=context)
        self.assertNotIn(self.comment_text, view.contents)


class TestHideMessageControlMixin:

    control_text = 'mark-spam-1'

    def getContext(self, comment_owner=None):
        """To be overwridden by subclasses.

        This method must create and return a message bearing object
        (e.g. bug or question) with a hidden message/comment.
        """
        raise NotImplementedError

    def getView(self, context, user=None, no_login=False):
        """To be overwridden by subclasses.

        This method returns a view object rendered on the context
        obtained from makeHiddenMessage.
        """
        raise NotImplementedError

    def test_admin_sees_hide_control(self):
        context = self.getContext()
        administrator = self.factory.makeAdministrator()
        view = self.getView(context=context, user=administrator)
        hide_link = find_tag_by_id(view.contents, self.control_text)
        self.assertIsNot(None, hide_link)

    def test_registry_sees_hide_control(self):
        context = self.getContext()
        registry_expert = self.factory.makeRegistryExpert()
        view = self.getView(context=context, user=registry_expert)
        hide_link = find_tag_by_id(view.contents, self.control_text)
        self.assertIsNot(None, hide_link)

    def test_anon_doesnt_see_hide_control(self):
        context = self.getContext()
        view = self.getView(context=context, no_login=True)
        hide_link = find_tag_by_id(view.contents, self.control_text)
        self.assertIs(None, hide_link)

    def test_random_doesnt_see_hide_control(self):
        context = self.getContext()
        view = self.getView(context=context)
        hide_link = find_tag_by_id(view.contents, self.control_text)
        self.assertIs(None, hide_link)