Blame tests/test_pagure_flask_ui_repo_slash_name.py

Pierre-Yves Chibon 93120d
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
"""
Abhijeet Kasurde a2ea74
 (c) 2016-2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
 Authors:
Pierre-Yves Chibon 93120d
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
"""
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 93120d
import json
Pierre-Yves Chibon 93120d
import unittest
Pierre-Yves Chibon 93120d
import shutil
Pierre-Yves Chibon 93120d
import sys
Pierre-Yves Chibon 93120d
import tempfile
Pierre-Yves Chibon 93120d
import os
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
import pygit2
Pierre-Yves Chibon 93120d
from mock import patch
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 93120d
import tests
Pierre-Yves Chibon 93120d
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
Clement Verna 109c4b
class PagureFlaskSlashInNametests(tests.SimplePagureTest):
Pierre-Yves Chibon 93120d
    """ Tests for flask application when the project contains a '/'.
Pierre-Yves Chibon 93120d
    """
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
    def setUp(self):
Pierre-Yves Chibon 93120d
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 93120d
        super(PagureFlaskSlashInNametests, self).setUp()
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 73d120
    def set_up_git_repo(self, name="test"):
Pierre-Yves Chibon 06d8fa
        """ Set up the git repo to play with. """
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "%s.git" % name)
Pierre-Yves Chibon 06d8fa
        repo = pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 73d120
        newpath = tempfile.mkdtemp(prefix="pagure-other-test")
Pierre-Yves Chibon 73d120
        repopath = os.path.join(newpath, "test")
Pierre-Yves Chibon 06d8fa
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        clone_repo.index.add("sources")
Pierre-Yves Chibon 06d8fa
        clone_repo.index.write()
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Commits the files added
Pierre-Yves Chibon 06d8fa
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 06d8fa
        clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 06d8fa
            author,
Pierre-Yves Chibon 06d8fa
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Pierre-Yves Chibon 06d8fa
            # binary string representing the tree object ID
Pierre-Yves Chibon 06d8fa
            tree,
Pierre-Yves Chibon 06d8fa
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Pierre-Yves Chibon 06d8fa
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master"
Pierre-Yves Chibon 06d8fa
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 06d8fa
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 06d8fa
    def test_view_repo_empty(self, send_email):
Pierre-Yves Chibon 93120d
        """ Test the view_repo endpoint when the project has a slash in its
Pierre-Yves Chibon 93120d
        name.
Pierre-Yves Chibon 93120d
        """
Pierre-Yves Chibon 93120d
        send_email.return_value = True
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
        tests.create_projects(self.session)
Pierre-Yves Chibon 93120d
        # Non-existant git repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/test")
Pierre-Yves Chibon 93120d
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 93120d
        repo = pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
        # With git repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/test")
Pierre-Yves Chibon 93120d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 77bdcd
            '
Pierre-Yves Chibon 95e961
            'value="git://localhost.localdomain/test.git" readonly>',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 73d120
            "

The Project Creator has not pushed any code yet

",
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 0d4ec5
        # We can't create the project `forks/test` the normal way
Pierre-Yves Chibon 0d4ec5
        self.assertRaises(
Pierre-Yves Chibon 0d4ec5
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 930073
            pagure.lib.query.new_project,
Pierre-Yves Chibon 93120d
            self.session,
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            namespace="forks",
Patrick Uiterwijk 3f97f6
            repospanner_region=None,
Pierre-Yves Chibon 73d120
            description="test project forks/test",
Pierre-Yves Chibon 73d120
            url="",
Pierre-Yves Chibon 73d120
            avatar_email="",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            blacklist=pagure.config.config["BLACKLISTED_PROJECTS"],
Pierre-Yves Chibon 73d120
            allowed_prefix=pagure.config.config["ALLOWED_PREFIX"],
Pierre-Yves Chibon 93120d
        )
Pierre-Yves Chibon 0d4ec5
Pierre-Yves Chibon 0d4ec5
        # So just put it in the DB
Pierre-Yves Chibon 0d4ec5
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 0d4ec5
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            namespace="forks",
Pierre-Yves Chibon 73d120
            description="test project forks/test",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 0d4ec5
        )
Pierre-Yves Chibon 0d4ec5
        self.session.add(item)
Pierre-Yves Chibon 93120d
        self.session.commit()
Pierre-Yves Chibon 0d4ec5
Pierre-Yves Chibon 0d4ec5
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "forks/test.git")
Pierre-Yves Chibon 0d4ec5
        repo = pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 73d120
        output = self.app.get("/forks/test")
Pierre-Yves Chibon 93120d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 77bdcd
            '
Pierre-Yves Chibon 95e961
            'value="git://localhost.localdomain/forks/test.git" readonly>',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 73d120
            "

The Project Creator has not pushed any code yet

",
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 73d120
        output = self.app.get("/forks/test/issues")
Pierre-Yves Chibon 93120d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 73d120
            "<title>Issues - forks/test - Pagure</title>", output_text
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 93120d
        self.assertIn(
Ryan Lerch 5483e2
            'no open issues found\n',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 06d8fa
    def test_view_repo(self, send_email):
Pierre-Yves Chibon 06d8fa
        """ Test the view_repo endpoint when the project has a slash in its
Pierre-Yves Chibon 06d8fa
        name.
Pierre-Yves Chibon 06d8fa
        """
Pierre-Yves Chibon 06d8fa
        send_email.return_value = True
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        tests.create_projects(self.session)
Pierre-Yves Chibon 06d8fa
        # Non-existant git repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/test")
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        self.set_up_git_repo()
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # With git repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/test")
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 77bdcd
            '
Pierre-Yves Chibon 95e961
            'value="git://localhost.localdomain/test.git" readonly>',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # We can't create the project `forks/test` the normal way
Pierre-Yves Chibon 06d8fa
        self.assertRaises(
Pierre-Yves Chibon 06d8fa
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 930073
            pagure.lib.query.new_project,
Pierre-Yves Chibon 06d8fa
            self.session,
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            namespace="forks",
Patrick Uiterwijk 3f97f6
            repospanner_region=None,
Pierre-Yves Chibon 73d120
            description="test project forks/test",
Pierre-Yves Chibon 73d120
            url="",
Pierre-Yves Chibon 73d120
            avatar_email="",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            blacklist=pagure.config.config["BLACKLISTED_PROJECTS"],
Pierre-Yves Chibon 73d120
            allowed_prefix=pagure.config.config["ALLOWED_PREFIX"],
Pierre-Yves Chibon 06d8fa
        )
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # So just put it in the DB
Pierre-Yves Chibon 06d8fa
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 06d8fa
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            namespace="forks",
Pierre-Yves Chibon 73d120
            description="test project forks/test",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 06d8fa
        )
Pierre-Yves Chibon 06d8fa
        self.session.add(item)
Pierre-Yves Chibon 06d8fa
        self.session.commit()
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 73d120
        self.set_up_git_repo(name="forks/test")
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Front page shows fine
Pierre-Yves Chibon 73d120
        output = self.app.get("/forks/test")
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 77bdcd
            '
Pierre-Yves Chibon 95e961
            'value="git://localhost.localdomain/forks/test.git" readonly>',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 73d120
            "<title>Overview - forks/test - Pagure</title>", output_text
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Issues list shows fine
Pierre-Yves Chibon 73d120
        output = self.app.get("/forks/test/issues")
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 73d120
            "<title>Issues - forks/test - Pagure</title>", output_text
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Ryan Lerch 5483e2
            'no open issues found\n',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Try accessing the commit
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "forks/test.git")
Pierre-Yves Chibon 06d8fa
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 73d120
        master_branch = repo.lookup_branch("master")
Pierre-Yves Chibon 29ff0a
        first_commit = master_branch.peel().hex
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 73d120
        output = self.app.get("/forks/test/commits")
Pierre-Yves Chibon c68e92
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Aurélien Bompard 626417
        self.assertIn(first_commit, output_text)
Pierre-Yves Chibon c68e92
        self.assertIn(
Pierre-Yves Chibon c68e92
            '
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon c68e92
Pierre-Yves Chibon 73d120
        output = self.app.get("/forks/test/c/%s" % first_commit)
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        self.assertIn("<title>Commit - forks/test ", output_text)</title>
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)