~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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Test the peoplemerge browser module."""

__metaclass__ = type

from zope.component import getUtility

from canonical.testing.layers import DatabaseFunctionalLayer
from lp.registry.interfaces.person import (
    IPersonSet,
    TeamSubscriptionPolicy,
    )
from lp.registry.interfaces.persontransferjob import IPersonMergeJobSource
from lp.testing import (
    login_celebrity,
    login_person,
    person_logged_in,
    TestCaseWithFactory,
    )
from lp.testing.views import (
    create_initialized_view,
    create_view,
    )


class TestValidatingMergeView(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestValidatingMergeView, self).setUp()
        self.person_set = getUtility(IPersonSet)
        self.dupe = self.factory.makePerson(name='dupe')
        self.target = self.factory.makePerson(name='target')

    def getForm(self, dupe_name=None):
        if dupe_name is None:
            dupe_name = self.dupe.name
        return {
            'field.dupe_person': dupe_name,
            'field.target_person': self.target.name,
            'field.actions.continue': 'Continue',
            }

    def test_cannot_merge_person_with_ppas(self):
        # A team with a PPA cannot be merged.
        login_celebrity('admin')
        archive = self.dupe.createPPA()
        login_celebrity('registry_experts')
        view = create_initialized_view(
            self.person_set, '+requestmerge', form=self.getForm())
        self.assertEqual(
            [u"dupe has a PPA that must be deleted before it can be "
              "merged. It may take ten minutes to remove the deleted PPA's "
              "files."],
            view.errors)

    def test_cannot_merge_person_with_itself(self):
        # A IPerson cannot be merged with itself.
        login_person(self.target)
        form = self.getForm(dupe_name=self.target.name)
        view = create_initialized_view(
            self.person_set, '+requestmerge', form=form)
        self.assertEqual(
            ["You can't merge target into itself."], view.errors)

    def test_cannot_merge_dupe_person_with_an_existing_merge_job(self):
        # A merge cannot be requested for an IPerson if it there is a job
        # queued to merge it into another IPerson.
        job_source = getUtility(IPersonMergeJobSource)
        duplicate_job = job_source.create(
            from_person=self.dupe, to_person=self.target)
        login_person(self.target)
        view = create_initialized_view(
            self.person_set, '+requestmerge', form=self.getForm())
        self.assertEqual(
            ["dupe is already queued for merging."], view.errors)

    def test_cannot_merge_target_person_with_an_existing_merge_job(self):
        # A merge cannot be requested for an IPerson if it there is a job
        # queued to merge it into another IPerson.
        job_source = getUtility(IPersonMergeJobSource)
        duplicate_job = job_source.create(
            from_person=self.target, to_person=self.dupe)
        login_person(self.target)
        view = create_initialized_view(
            self.person_set, '+requestmerge', form=self.getForm())
        self.assertEqual(
            ["target is already queued for merging."], view.errors)


class TestRequestPeopleMergeMultipleEmailsView(TestCaseWithFactory):
    """Test the RequestPeopleMergeMultipleEmailsView rules."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestRequestPeopleMergeMultipleEmailsView, self).setUp()
        self.personset = getUtility(IPersonSet)
        self.dupe_user = self.factory.makePerson()
        self.email_2 = self.factory.makeEmail(
            'dupe@place.dom', self.dupe_user)
        self.original_user = self.factory.makePerson()
        login_person(self.original_user)

    def verify_user_must_reselect_email_addresses(self, view):
        self.assertFalse(view.form_processed)
        self.assertEqual(0, len(view.notified_addresses))
        self.assertEqual(1, len(view.request.notifications))
        message = view.request.notifications[0].message
        self.assertTrue(message.endswith('Select again.'))

    def test_removed_email(self):
        # When the duplicate user deletes an email addres while the merge
        # form is being complete, the view must abort and ask the user
        # to restart the merge request.
        form = {
            'dupe': self.dupe_user.id,
            }
        view = create_view(
            self.personset, name='+requestmerge-multiple', form=form)
        view.processForm()
        dupe_emails = [address for address in view.dupeemails]
        form['selected'] = [address.email for address in dupe_emails]
        with person_logged_in(self.dupe_user):
            dupe_emails.remove(self.email_2)
            self.email_2.destroySelf()
        view = create_view(
            self.personset, name='+requestmerge-multiple', form=form,
            method='POST')
        view.processForm()
        self.verify_user_must_reselect_email_addresses(view)

    def test_email_address_cannot_be_substituted(self):
        # A person cannot hack the form to use another user's email address
        # to take control of a profile.
        controlled_user = self.factory.makePerson()
        form = {
            'dupe': self.dupe_user.id,
            'selected': [controlled_user.preferredemail.email],
            }
        view = create_view(
            self.personset, name='+requestmerge-multiple', form=form,
            method='POST')
        view.processForm()
        self.verify_user_must_reselect_email_addresses(view)


class TestAdminTeamMergeView(TestCaseWithFactory):
    """Test the AdminTeamMergeView rules."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestAdminTeamMergeView, self).setUp()
        self.person_set = getUtility(IPersonSet)
        self.dupe_team = self.factory.makeTeam(name='dupe-team')
        self.target_team = self.factory.makeTeam(name='target-team')
        login_celebrity('registry_experts')

    def getView(self, form=None):
        if form is None:
            form = {
                'field.dupe_person': self.dupe_team.name,
                'field.target_person': self.target_team.name,
                'field.actions.deactivate_members_and_merge': 'Merge',
                }
        return create_initialized_view(
            self.person_set, '+adminteammerge', form=form)

    def test_cannot_merge_team_with_ppa(self):
        # A team with a PPA cannot be merged.
        login_celebrity('admin')
        self.dupe_team.subscriptionpolicy = TeamSubscriptionPolicy.MODERATED
        archive = self.dupe_team.createPPA()
        login_celebrity('registry_experts')
        view = self.getView()
        self.assertEqual(
            [u"dupe-team has a PPA that must be deleted before it can be "
              "merged. It may take ten minutes to remove the deleted PPA's "
              "files."],
            view.errors)


class TestAdminPeopleMergeView(TestCaseWithFactory):
    """Test the AdminPeopleMergeView rules."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestAdminPeopleMergeView, self).setUp()
        self.person_set = getUtility(IPersonSet)
        self.dupe_person = self.factory.makePerson(name='dupe-person')
        self.target_person = self.factory.makePerson()
        login_celebrity('registry_experts')

    def getView(self, form=None):
        if form is None:
            form = {
                'field.dupe_person': self.dupe_person.name,
                'field.target_person': self.target_person.name,
                'field.actions.reassign_emails_and_merge':
                    'Reassign E-mails and Merge',
                }
        return create_initialized_view(
            self.person_set, '+adminpeoplemerge', form=form)

    def test_cannot_merge_person_with_ppa(self):
        # A person with a PPA cannot be merged.
        login_celebrity('admin')
        archive = self.dupe_person.createPPA()
        view = self.getView()
        self.assertEqual(
            [u"dupe-person has a PPA that must be deleted before it can be "
              "merged. It may take ten minutes to remove the deleted PPA's "
              "files."],
            view.errors)