File: test_pagure_flask_ui_app_userdash.py

package info (click to toggle)
pagure 5.14.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 30,904 kB
  • sloc: python: 125,288; javascript: 22,012; makefile: 209; sh: 191
file content (428 lines) | stat: -rw-r--r-- 16,194 bytes parent folder | download | duplicates (2)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# -*- coding: utf-8 -*-

"""
 (c) 2018 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>
   Ryan Lerch <rlerch@redhat.com>
"""

from __future__ import unicode_literals, absolute_import

import datetime
import unittest
import shutil
import sys
import os

import six
import json
import pygit2
from unittest.mock import patch, MagicMock

sys.path.insert(
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
)

import pagure.lib.query
import tests


class PagureFlaskAppUserdashTests(tests.Modeltests):
    """ Tests for the index page of flask app controller of pagure """

    def test_index_commit_access_while_admin(self):
        """Test the index endpoint filter for commit access only when user
        is an admin."""
        tests.create_projects(self.session)

        # Add a 3rd project just for foo
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            # Before
            output = self.app.get("/dashboard/projects?acl=commit")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<span class="btn btn-outline-secondary disabled opacity-100'
                ' border-0 ml-auto font-weight-bold">1 Projects</span>',
                output_text,
            )
            self.assertIn(
                '<div class="text-center">No Projects match this filter</div>',
                output_text,
            )

            # Add foo to test with admin level
            project = pagure.lib.query._get_project(self.session, "test")
            msg = pagure.lib.query.add_user_to_project(
                self.session,
                project=project,
                new_user="foo",
                user="pingou",
                access="admin",
            )
            self.session.commit()
            self.assertEqual(msg, "User added")

            # After
            self.assertIn(
                '<span class="btn btn-outline-secondary disabled opacity-100'
                ' border-0 ml-auto font-weight-bold">1 Projects</span>',
                output_text,
            )
            self.assertIn(
                '<div class="text-center">No Projects match this filter</div>',
                output_text,
            )

    def test_index_commit_access_while_commit(self):
        """Test the index endpoint filter for commit access only when user
        is an committer."""
        tests.create_projects(self.session)

        # Add a 3rd project just for foo
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            # Before
            output = self.app.get("/dashboard/projects?acl=commit")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
                output_text,
            )

            # Add foo to test with commit level
            project = pagure.lib.query._get_project(self.session, "test")
            msg = pagure.lib.query.add_user_to_project(
                self.session,
                project=project,
                new_user="foo",
                user="pingou",
                access="commit",
            )
            self.session.commit()
            self.assertEqual(msg, "User added")

            # After
            output = self.app.get("/dashboard/projects?acl=commit")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
                output_text,
            )

    def test_index_commit_access_while_ticket(self):
        """Test the index endpoint filter for commit access only when user
        is has ticket access."""
        tests.create_projects(self.session)

        # Add a 3rd project just for foo
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            # Before
            output = self.app.get("/dashboard/projects?acl=ticket")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
                output_text,
            )

            # Add foo to test with ticket level
            project = pagure.lib.query._get_project(self.session, "test")
            msg = pagure.lib.query.add_user_to_project(
                self.session,
                project=project,
                new_user="foo",
                user="pingou",
                access="ticket",
            )
            self.session.commit()
            self.assertEqual(msg, "User added")

            # After  -  projects with ticket access aren't shown
            output = self.app.get("/dashboard/projects?acl=ticket")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
                output_text,
            )

    def test_index_admin_access_while_admin(self):
        """Test the index endpoint filter for admin access only when user
        is an admin."""
        tests.create_projects(self.session)

        # Add a 3rd project just for foo
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            # Before
            output = self.app.get("/dashboard/projects?acl=admin")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
                output_text,
            )

            # Add foo to test with admin level
            project = pagure.lib.query._get_project(self.session, "test")
            msg = pagure.lib.query.add_user_to_project(
                self.session,
                project=project,
                new_user="foo",
                user="pingou",
                access="admin",
            )
            self.session.commit()
            self.assertEqual(msg, "User added")

            # After
            output = self.app.get("/dashboard/projects?acl=admin")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
                output_text,
            )

    def test_index_admin_access_while_commit(self):
        """Test the index endpoint filter for admin access only when user
        is an committer."""
        tests.create_projects(self.session)

        # Add a 3rd project just for foo
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            # Before
            output = self.app.get("/dashboard/projects?acl=commit")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
                output_text,
            )

            # Add foo to test with commit level
            project = pagure.lib.query._get_project(self.session, "test")
            msg = pagure.lib.query.add_user_to_project(
                self.session,
                project=project,
                new_user="foo",
                user="pingou",
                access="commit",
            )
            self.session.commit()
            self.assertEqual(msg, "User added")

            # After
            output = self.app.get("/dashboard/projects?acl=commit")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            # The total number no longer changes
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
                output_text,
            )

    def test_index_main_admin_access_while_commit(self):
        """Test the index endpoint filter for main admin access only when
        user is an committer."""
        tests.create_projects(self.session)

        # Add a 3rd project just for foo
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            # Before
            output = self.app.get("/dashboard/projects?acl=main admin")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
                output_text,
            )

            # Add foo to test with commit level
            project = pagure.lib.query._get_project(self.session, "test")
            msg = pagure.lib.query.add_user_to_project(
                self.session,
                project=project,
                new_user="foo",
                user="pingou",
                access="commit",
            )
            self.session.commit()
            self.assertEqual(msg, "User added")

            # After
            output = self.app.get("/dashboard/projects?acl=main admin")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
                output_text,
            )

    @patch.dict("pagure.config.config", {"PRIVATE_PROJECTS": True})
    def test_index_logged_in_private_project(self):
        """ Test the index endpoint when logged in with a private project. """
        tests.create_projects(self.session)

        # Add a 3rd project with a long description
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
            private=True,
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            output = self.app.get("/dashboard/projects")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
                output_text,
            )
            self.assertIn(
                '<span title="Private project" class="text-danger '
                'fa fa-fw fa-lock"></span>',
                output_text,
            )
            self.assertEqual(output_text.count('title="Private project"'), 1)

    def test_index_logged_in_non_user_group(self):
        """
        Test the index endpoint when logged in with a member of non-user type
        group.
        """
        tests.create_projects(self.session)

        # Add a 3rd project just for foo
        item = pagure.lib.model.Project(
            user_id=2,  # foo
            name="test3",
            description="test project #3 with a very long description",
            hook_token="aaabbbeeefff",
        )
        self.session.add(item)
        self.session.commit()

        # Add admin group
        item = pagure.lib.model.PagureGroup(
            group_name="admin",
            group_type="admin",
            user_id=1,  # pingou
            display_name="admin",
            description="Admin Group",
        )
        self.session.add(item)
        self.session.commit()

        # Add foo to admin group
        item = pagure.lib.model.PagureUserGroup(
            user_id=2, group_id=1  # foo  # admin group
        )
        self.session.add(item)
        self.session.commit()

        user = tests.FakeUser(username="foo")
        with tests.user_set(self.app.application, user):
            output = self.app.get("/dashboard/projects")
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
                '          <span class="btn btn-outline-secondary disabled'
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
                output_text,
            )


if __name__ == "__main__":
    unittest.main(verbosity=2)