Blame tests/test_pagure_flask_ui_pr_no_sources.py

Pierre-Yves Chibon ff9832
# -*- coding: utf-8 -*-
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
"""
Pierre-Yves Chibon 9071b6
 (c) 2015-2018 - Copyright Red Hat Inc
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
 Authors:
Pierre-Yves Chibon ff9832
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
"""
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon ff9832
import json
Pierre-Yves Chibon ff9832
import unittest
Pierre-Yves Chibon ff9832
import shutil
Pierre-Yves Chibon ff9832
import sys
Pierre-Yves Chibon ff9832
import tempfile
Pierre-Yves Chibon ff9832
import time
Pierre-Yves Chibon ff9832
import os
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
import pygit2
Pierre-Yves Chibon ff9832
from mock import patch, MagicMock
Pierre-Yves Chibon ff9832
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 ff9832
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon ff9832
import tests
Pierre-Yves Chibon ff9832
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
class PagureFlaskPrNoSourcestests(tests.Modeltests):
Pierre-Yves Chibon ff9832
    """ Tests PR in pagure when the source is gone """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    maxDiff = None
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.fedmsg_publish", MagicMock(return_value=True))
Pierre-Yves Chibon ff9832
    def setUp(self):
Pierre-Yves Chibon ff9832
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon ff9832
        super(PagureFlaskPrNoSourcestests, self).setUp()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Create foo's fork of pingou's test project
Pierre-Yves Chibon ff9832
        item = pagure.lib.model.Project(
Pierre-Yves Chibon ff9832
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbb",
Pierre-Yves Chibon ff9832
            is_fork=True,
Pierre-Yves Chibon ff9832
            parent_id=1,
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
        self.session.add(item)
Pierre-Yves Chibon ff9832
        self.session.commit()
Pierre-Yves Chibon ff9832
        # Create the fork's git repo
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos", item.path)
Pierre-Yves Chibon ff9832
        pygit2.init_repository(repo_path, bare=True)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        fork = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        self.set_up_git_repo(repo=project, fork=fork)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Ensure things got setup straight
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon ff9832
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 0fe9c9
        # wait for the worker to process the task
Pierre-Yves Chibon 0fe9c9
        path = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos", "test.git", "refs", "pull", "1", "head"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4babe0
        self.assertTrue(os.path.exists(path))
Pierre-Yves Chibon 0fe9c9
Pierre-Yves Chibon 73d120
    def set_up_git_repo(self, repo, fork, branch_from="feature"):
Pierre-Yves Chibon ff9832
        """ Set up the git repo and create the corresponding PullRequest
Pierre-Yves Chibon ff9832
        object.
Pierre-Yves Chibon ff9832
        """
Pierre-Yves Chibon ff9832
Julen Landa Alustiza 064be0
        req = tests.add_pull_request_git_repo(
Julen Landa Alustiza 064be0
            self.path, self.session, repo, fork, branch_from
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "PR from the %s branch" % branch_from)
Pierre-Yves Chibon ff9832
Julen Landa Alustiza 064be0
        tests.clean_pull_requests_path()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    def test_request_pull_reference(self):
Pierre-Yves Chibon ff9832
        """ Test if there is a reference created for a new PR. """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon ff9832
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon ff9832
        self.assertEqual(
Pierre-Yves Chibon ff9832
            list(repo.listall_references()),
Pierre-Yves Chibon 73d120
            ["refs/heads/master", "refs/pull/1/head"],
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    def test_request_pull_fork_reference(self):
Pierre-Yves Chibon ff9832
        """ Test if there the references created on the fork. """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 930073
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ff9832
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", project.path)
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon ff9832
        self.assertEqual(
Pierre-Yves Chibon 73d120
            list(repo.listall_references()), ["refs/heads/feature"]
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    def test_accessing_pr_fork_deleted(self):
Pierre-Yves Chibon ff9832
        """ Test accessing the PR if the fork has been deleted. """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Delete fork on disk
Pierre-Yves Chibon 930073
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos", project.path)
Pierre-Yves Chibon ff9832
        self.assertTrue(os.path.exists(repo_path))
Pierre-Yves Chibon ff9832
        shutil.rmtree(repo_path)
Pierre-Yves Chibon ff9832
        self.assertFalse(os.path.exists(repo_path))
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Delete fork in the DB
Pierre-Yves Chibon ff9832
        self.session.delete(project)
Pierre-Yves Chibon ff9832
        self.session.commit()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # View the pull-request
Pierre-Yves Chibon 73d120
        output2 = self.app.get("/test/pull-request/1")
Pierre-Yves Chibon ff9832
        self.assertEqual(output2.status_code, 200)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 5da9ba
    def test_accessing_pr_patch_fork_deleted(self):
Pierre-Yves Chibon 5da9ba
        """ Test accessing the PR's patch if the fork has been deleted. """
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Delete fork on disk
Pierre-Yves Chibon 930073
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos", project.path)
Pierre-Yves Chibon 5da9ba
        self.assertTrue(os.path.exists(repo_path))
Pierre-Yves Chibon 5da9ba
        shutil.rmtree(repo_path)
Pierre-Yves Chibon 5da9ba
        self.assertFalse(os.path.exists(repo_path))
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Delete fork in the DB
Pierre-Yves Chibon 5da9ba
        self.session.delete(project)
Pierre-Yves Chibon 5da9ba
        self.session.commit()
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # View the pull-request
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/pull-request/1.patch")
Aurélien Bompard 626417
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5da9ba
        self.assertIn(
Pierre-Yves Chibon 73d120
            "--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@",
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon ff9832
    def test_accessing_pr_branch_deleted(self):
Pierre-Yves Chibon ff9832
        """ Test accessing the PR if branch it originates from has been
Pierre-Yves Chibon ff9832
        deleted. """
Pierre-Yves Chibon 930073
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Check the branches before
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", project.path)
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon ff9832
        self.assertEqual(
Pierre-Yves Chibon 73d120
            list(repo.listall_references()), ["refs/heads/feature"]
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Delete branch of the fork
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon ff9832
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/fork/foo/test/b/feature/delete", follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon ff9832
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Check the branches after
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", project.path)
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 73d120
        self.assertEqual(list(repo.listall_references()), [])
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # View the pull-request
Pierre-Yves Chibon 73d120
        output2 = self.app.get("/test/pull-request/1")
Pierre-Yves Chibon ff9832
        self.assertEqual(output2.status_code, 200)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 5da9ba
    def test_accessing_pr_patch_branch_deleted(self):
Pierre-Yves Chibon 5da9ba
        """ Test accessing the PR's patch if branch it originates from has
Pierre-Yves Chibon 5da9ba
        been deleted. """
Pierre-Yves Chibon 930073
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Check the branches before
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", project.path)
Pierre-Yves Chibon 5da9ba
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 5da9ba
        self.assertEqual(
Pierre-Yves Chibon 73d120
            list(repo.listall_references()), ["refs/heads/feature"]
Pierre-Yves Chibon 5da9ba
        )
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Delete branch of the fork
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 5da9ba
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5da9ba
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/fork/foo/test/b/feature/delete", follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 5da9ba
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Check the branches after
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", project.path)
Pierre-Yves Chibon 5da9ba
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 73d120
        self.assertEqual(list(repo.listall_references()), [])
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # View the pull-request
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/pull-request/1.patch")
Aurélien Bompard 626417
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5da9ba
        self.assertIn(
Pierre-Yves Chibon 73d120
            "--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@",
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon ff9832
    unittest.main(verbosity=2)