File: test_pagure_flask_ui_archives.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 (419 lines) | stat: -rw-r--r-- 13,806 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
# -*- coding: utf-8 -*-

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

 Authors:
   Clement Verna <cverna@tutanota.com>

"""

from __future__ import unicode_literals, absolute_import

import unittest
import sys
import os
from zipfile import ZipFile

import unittest.mock as mock
import pygit2

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

import pagure.lib.git
import pagure.lib.query
import tests
from tests.test_pagure_lib_git_get_tags_objects import add_repo_tag


class PagureFlaskUiArchivesTest(tests.Modeltests):
    """ Tests checking the archiving mechanism. """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskUiArchivesTest, self).setUp()
        tests.create_projects(self.session)
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
        project = pagure.lib.query._get_project(self.session, "test")

        # test has both commits and tags
        repopath = os.path.join(self.path, "repos", "test.git")
        tests.add_readme_git_repo(repopath)
        repo = pygit2.Repository(repopath)
        add_repo_tag(self.path, repo, ["v1.0", "v1.1"], "test.git")

        # test2 has only commits
        tests.add_readme_git_repo(
            os.path.join(self.path, "repos", "test2.git")
        )

        # somenamespace/test3 has neither commits nor tags

        # Create the archive folder:
        self.archive_path = os.path.join(self.path, "archives")
        os.mkdir(self.archive_path)

    def test_project_no_conf(self):
        """ Test getting the archive when pagure isn't configured. """
        output = self.app.get(
            "/somenamespace/test3/archive/tag1/test3-tag1.zip",
            follow_redirects=True,
        )

        self.assertEqual(output.status_code, 404)
        self.assertIn(
            "This pagure instance isn&#39;t configured to support "
            "this feature",
            output.get_data(as_text=True),
        )

        self.assertEqual(os.listdir(self.archive_path), [])

    def test_project_invalid_conf(self):
        """ Test getting the archive when pagure is wrongly configured. """
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "invalid")},
        ):
            output = self.app.get(
                "/somenamespace/test3/archive/tag1/test3-tag1.zip",
                follow_redirects=True,
            )

            self.assertEqual(output.status_code, 500)
            self.assertIn(
                "Incorrect configuration, please contact your admin",
                output.get_data(as_text=True),
            )

        self.assertEqual(os.listdir(self.archive_path), [])

    def test_project_invalid_format(self):
        """ Test getting the archive when the format provided is invalid. """
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/somenamespace/test3/archive/tag1/test3-tag1.unzip",
                follow_redirects=True,
            )

            self.assertEqual(output.status_code, 404)

        self.assertEqual(os.listdir(self.archive_path), [])

    def test_project_no_commit(self):
        """ Test getting the archive of an empty project. """
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/somenamespace/test3/archive/tag1/test3-tag1.zip",
                follow_redirects=True,
            )

            self.assertEqual(output.status_code, 404)
            self.assertIn(
                "<p>Invalid commit provided</p>", output.get_data(as_text=True)
            )

        self.assertEqual(os.listdir(self.archive_path), [])

    # All tests against test3 repo complete, re-use repo for symlink test
    def test_project_with_symlink_zip(self):
        """Test that symlink rather than target file gets added to zip"""
        filename = "os-release"
        symlinkfile_target = "/etc/os-release"
        directory = "etc"
        symlinkdir_target = "/etc"
        readme = "README.rst"
        archivename = "test3.zip"
        namespace = "somenamespace"
        reponame = "test3"
        repopath = os.path.join(
            self.path, "repos", namespace, "%s.git" % reponame
        )
        repo = pygit2.Repository(repopath)
        archivepath = os.path.join(self.archive_path, namespace, reponame)
        tests.add_commit_git_repo(
            repopath,
            ncommits=1,
            filename=filename,
            symlink_to=symlinkfile_target,
        )
        tests.add_commit_git_repo(
            repopath,
            ncommits=1,
            filename=directory,
            symlink_to=symlinkdir_target,
        )
        tests.add_readme_git_repo(repopath)
        commit = str(repo.head.target)

        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/%s/%s/archive/%s/%s"
                % (namespace, reponame, commit, archivename),
                follow_redirects=True,
            )

            self.assertEqual(output.status_code, 200)

        # Was a subfolder for the correct commit created?
        self.assertEqual(os.listdir(archivepath), [commit])

        archivepath_commit = os.path.join(archivepath, commit)
        # Does the subfolder contain the expected zip file?
        self.assertEqual(os.listdir(archivepath_commit), [archivename])

        archivepath_zip = os.path.join(archivepath_commit, archivename)
        # Is the test file in the zip archive a symlink?
        with ZipFile(os.path.join(archivepath_zip)) as testzip:
            self.assertIn(
                "lrwxrwxrwx",
                str(testzip.getinfo("%s/%s" % (reponame, filename))),
            )

        # Is the test directory in the zip archive a symlink?
        with ZipFile(os.path.join(archivepath_zip)) as testzip:
            self.assertIn(
                "lrwxrwxrwx",
                str(testzip.getinfo("%s/%s" % (reponame, directory))),
            )

        # Is the readme still an actual file in the zip archive?
        with ZipFile(os.path.join(archivepath_zip)) as testzip:
            self.assertIn(
                "-rw-r--r--",
                str(testzip.getinfo("%s/%s" % (reponame, readme))),
            )

    def test_project_no_tag(self):
        """Test getting the archive of a non-empty project but without
        tags."""
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/test2/archive/tag1/test2-tag1.zip", follow_redirects=True
            )

            self.assertEqual(output.status_code, 404)
            self.assertIn(
                "<p>Invalid commit provided</p>", output.get_data(as_text=True)
            )

        self.assertEqual(os.listdir(self.archive_path), [])

    def test_project_no_tag(self):
        """ Test getting the archive of an empty project. """
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/test2/archive/tag1/test2-tag1.zip", follow_redirects=True
            )

            self.assertEqual(output.status_code, 404)
            self.assertIn(
                "<p>Invalid commit provided</p>", output.get_data(as_text=True)
            )

        self.assertEqual(os.listdir(self.archive_path), [])

    def test_project_w_tag_zip(self):
        """ Test getting the archive from a tag. """
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/test/archive/v1.0/test-v1.0.zip", follow_redirects=True
            )

            self.assertEqual(output.status_code, 200)

        self.assertEqual(os.listdir(self.archive_path), ["test"])
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
        )
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test", "tags")),
            ["v1.0"],
        )

        self.assertEqual(
            len(
                os.listdir(
                    os.path.join(self.archive_path, "test", "tags", "v1.0")
                )
            ),
            1,
        )

        files = os.listdir(
            os.path.join(self.archive_path, "test", "tags", "v1.0")
        )
        self.assertEqual(
            os.listdir(
                os.path.join(
                    self.archive_path, "test", "tags", "v1.0", files[0]
                )
            ),
            ["test-v1.0.zip"],
        )

    def test_project_w_tag_tar(self):
        """ Test getting the archive from a tag. """
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/test/archive/v1.0/test-v1.0.tar", follow_redirects=True
            )

            self.assertEqual(output.status_code, 200)

        self.assertEqual(os.listdir(self.archive_path), ["test"])
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
        )
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test", "tags")),
            ["v1.0"],
        )

        self.assertEqual(
            len(
                os.listdir(
                    os.path.join(self.archive_path, "test", "tags", "v1.0")
                )
            ),
            1,
        )

        files = os.listdir(
            os.path.join(self.archive_path, "test", "tags", "v1.0")
        )
        self.assertEqual(
            os.listdir(
                os.path.join(
                    self.archive_path, "test", "tags", "v1.0", files[0]
                )
            ),
            ["test-v1.0.tar"],
        )

    def test_project_w_tag_tar_gz(self):
        """ Test getting the archive from a tag. """
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/test/archive/v1.0/test-v1.0.tar.gz", follow_redirects=True
            )

            self.assertEqual(output.status_code, 200)

        self.assertEqual(os.listdir(self.archive_path), ["test"])
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
        )
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test", "tags")),
            ["v1.0"],
        )

        self.assertEqual(
            len(
                os.listdir(
                    os.path.join(self.archive_path, "test", "tags", "v1.0")
                )
            ),
            1,
        )

        files = os.listdir(
            os.path.join(self.archive_path, "test", "tags", "v1.0")
        )
        self.assertEqual(
            os.listdir(
                os.path.join(
                    self.archive_path, "test", "tags", "v1.0", files[0]
                )
            ),
            ["test-v1.0.tar.gz"],
        )

    def test_project_w_commit_tar_gz(self):
        """ Test getting the archive from a commit. """
        repopath = os.path.join(self.path, "repos", "test.git")
        repo = pygit2.Repository(repopath)
        commit = str(repo.head.target)
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/test/archive/%s/test-v1.0.tar.gz" % commit,
                follow_redirects=True,
            )

            self.assertEqual(output.status_code, 200)

        self.assertEqual(os.listdir(self.archive_path), ["test"])
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test")), [commit]
        )
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test", commit)),
            ["test-v1.0.tar.gz"],
        )

    def test_project_w_commit_tar_gz_twice(self):
        """Test getting the archive from a commit twice, so we hit the
        disk cache."""
        repopath = os.path.join(self.path, "repos", "test.git")
        repo = pygit2.Repository(repopath)
        commit = str(repo.head.target)
        with mock.patch.dict(
            "pagure.config.config",
            {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
        ):
            output = self.app.get(
                "/test/archive/%s/test-v1.0.tar.gz" % commit,
                follow_redirects=True,
            )

            self.assertEqual(output.status_code, 200)

            output = self.app.get(
                "/test/archive/%s/test-v1.0.tar.gz" % commit,
                follow_redirects=True,
            )

            self.assertEqual(output.status_code, 200)

        self.assertEqual(os.listdir(self.archive_path), ["test"])
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test")), [commit]
        )
        self.assertEqual(
            os.listdir(os.path.join(self.archive_path, "test", commit)),
            ["test-v1.0.tar.gz"],
        )


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