File: test_pagure_flask_ui_repo_slash_name.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 (259 lines) | stat: -rw-r--r-- 8,718 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
# -*- coding: utf-8 -*-

"""
 (c) 2016-2017 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

from __future__ import unicode_literals, absolute_import

import json
import unittest
import shutil
import sys
import tempfile
import os

import pygit2
from unittest.mock import patch

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

import pagure.lib.query
import tests
from pagure.lib.repo import PagureRepo


class PagureFlaskSlashInNametests(tests.SimplePagureTest):
    """Tests for flask application when the project contains a '/'."""

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskSlashInNametests, self).setUp()

    def set_up_git_repo(self, name="test"):
        """ Set up the git repo to play with. """

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, "repos", "%s.git" % name)
        repo = pygit2.init_repository(gitrepo, bare=True)

        newpath = tempfile.mkdtemp(prefix="pagure-other-test")
        repopath = os.path.join(newpath, "test")
        clone_repo = pygit2.clone_repository(gitrepo, repopath)

        # Create a file in that git repo
        with open(os.path.join(repopath, "sources"), "w") as stream:
            stream.write("foo\n bar")
        clone_repo.index.add("sources")
        clone_repo.index.write()

        # Commits the files added
        tree = clone_repo.index.write_tree()
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
        clone_repo.create_commit(
            "refs/heads/master",  # the name of the reference to update
            author,
            committer,
            "Add sources file for testing",
            # binary string representing the tree object ID
            tree,
            # list of binary strings representing parents of the new commit
            [],
        )
        refname = "refs/heads/master"
        ori_remote = clone_repo.remotes[0]
        PagureRepo.push(ori_remote, refname)

    @patch("pagure.lib.notify.send_email")
    def test_view_repo_empty(self, send_email):
        """Test the view_repo endpoint when the project has a slash in its
        name.
        """
        send_email.return_value = True

        tests.create_projects(self.session)
        # Non-existant git repo
        output = self.app.get("/test")
        self.assertEqual(output.status_code, 404)

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, "repos", "test.git")
        repo = pygit2.init_repository(gitrepo, bare=True)

        # With git repo
        output = self.app.get("/test")
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '<input class="form-control bg-white select-on-focus" type="text" '
            'value="git://localhost.localdomain/test.git" readonly>',
            output_text,
        )
        self.assertIn(
            "<p>The Project Creator has not pushed any code yet</p>",
            output_text,
        )

        # We can't create the project `forks/test` the normal way
        self.assertRaises(
            pagure.exceptions.PagureException,
            pagure.lib.query.new_project,
            self.session,
            name="test",
            namespace="forks",
            repospanner_region=None,
            description="test project forks/test",
            url="",
            avatar_email="",
            user="pingou",
            blacklist=pagure.config.config["BLACKLISTED_PROJECTS"],
            allowed_prefix=pagure.config.config["ALLOWED_PREFIX"],
        )

        # So just put it in the DB
        item = pagure.lib.model.Project(
            user_id=1,  # pingou
            name="test",
            namespace="forks",
            description="test project forks/test",
            hook_token="aaabbbcccddd",
        )
        self.session.add(item)
        self.session.commit()

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, "repos", "forks/test.git")
        repo = pygit2.init_repository(gitrepo, bare=True)

        output = self.app.get("/forks/test")
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '<input class="form-control bg-white select-on-focus" type="text" '
            'value="git://localhost.localdomain/forks/test.git" readonly>',
            output_text,
        )
        self.assertIn(
            "<p>The Project Creator has not pushed any code yet</p>",
            output_text,
        )

        output = self.app.get("/forks/test/issues")
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            "<title>Issues - forks/test - Pagure</title>", output_text
        )
        self.assertIn(
            '<span class="font-weight-bold">no open issues found</span>\n',
            output_text,
        )

    @patch("pagure.lib.notify.send_email")
    def test_view_repo(self, send_email):
        """Test the view_repo endpoint when the project has a slash in its
        name.
        """
        send_email.return_value = True

        tests.create_projects(self.session)
        # Non-existant git repo
        output = self.app.get("/test")
        self.assertEqual(output.status_code, 404)

        self.set_up_git_repo()

        # With git repo
        output = self.app.get("/test")
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '<input class="form-control bg-white select-on-focus" type="text" '
            'value="git://localhost.localdomain/test.git" readonly>',
            output_text,
        )

        # We can't create the project `forks/test` the normal way
        self.assertRaises(
            pagure.exceptions.PagureException,
            pagure.lib.query.new_project,
            self.session,
            name="test",
            namespace="forks",
            repospanner_region=None,
            description="test project forks/test",
            url="",
            avatar_email="",
            user="pingou",
            blacklist=pagure.config.config["BLACKLISTED_PROJECTS"],
            allowed_prefix=pagure.config.config["ALLOWED_PREFIX"],
        )

        # So just put it in the DB
        item = pagure.lib.model.Project(
            user_id=1,  # pingou
            name="test",
            namespace="forks",
            description="test project forks/test",
            hook_token="aaabbbcccddd",
        )
        self.session.add(item)
        self.session.commit()

        self.set_up_git_repo(name="forks/test")

        # Front page shows fine
        output = self.app.get("/forks/test")
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '<input class="form-control bg-white select-on-focus" type="text" '
            'value="git://localhost.localdomain/forks/test.git" readonly>',
            output_text,
        )
        self.assertIn(
            "<title>Overview - forks/test - Pagure</title>", output_text
        )

        # Issues list shows fine
        output = self.app.get("/forks/test/issues")
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            "<title>Issues - forks/test - Pagure</title>", output_text
        )
        self.assertIn(
            '<span class="font-weight-bold">no open issues found</span>\n',
            output_text,
        )

        # Try accessing the commit
        gitrepo = os.path.join(self.path, "repos", "forks/test.git")
        repo = pygit2.Repository(gitrepo)
        master_branch = repo.lookup_branch("master")
        first_commit = str(master_branch.peel().id)

        output = self.app.get("/forks/test/commits")
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn(first_commit, output_text)
        self.assertIn(
            '<a href="/forks/test/c/%s?branch=master"' % first_commit,
            output_text,
        )

        output = self.app.get("/forks/test/c/%s" % first_commit)
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertIn("<title>Commit - forks/test ", output_text)


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