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 93120d
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 93120d
import pkg_resources
Pierre-Yves Chibon 93120d
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 93120d
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 93120d
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
import pagure.lib
Pierre-Yves Chibon 93120d
import tests
Pierre-Yves Chibon 93120d
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
class PagureFlaskSlashInNametests(tests.Modeltests):
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 93120d
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 93120d
        pagure.SESSION = self.session
Pierre-Yves Chibon 93120d
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon 93120d
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon 93120d
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon 93120d
        pagure.ui.fork.SESSION = self.session
Pierre-Yves Chibon 93120d
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 93120d
        pagure.ui.issues.SESSION = self.session
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 06d8fa
    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
Jeremy Cline 20109f
        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 06d8fa
        newpath = tempfile.mkdtemp(prefix='pagure-other-test')
Pierre-Yves Chibon 06d8fa
        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 06d8fa
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 06d8fa
            stream.write('foo\n bar')
Pierre-Yves Chibon 06d8fa
        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 06d8fa
        author = pygit2.Signature(
Pierre-Yves Chibon 06d8fa
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 06d8fa
        committer = pygit2.Signature(
Pierre-Yves Chibon 06d8fa
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 06d8fa
        clone_repo.create_commit(
Pierre-Yves Chibon 06d8fa
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 06d8fa
            author,
Pierre-Yves Chibon 06d8fa
            committer,
Pierre-Yves Chibon 06d8fa
            '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 06d8fa
            []
Pierre-Yves Chibon 06d8fa
        )
Pierre-Yves Chibon 06d8fa
        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 93120d
    @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 93120d
        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
Jeremy Cline 20109f
        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 93120d
        output = self.app.get('/test')
Pierre-Yves Chibon 93120d
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 93120d
            '
\n '
Pierre-Yves Chibon 058fe8
            '
Contributors
', output.data)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 93120d
            '

The Project Creator has not pushed any code yet

',
Pierre-Yves Chibon 93120d
            output.data)
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 0d4ec5
            pagure.lib.new_project,
Pierre-Yves Chibon 93120d
            self.session,
Pierre-Yves Chibon 8544f4
            name='test',
Pierre-Yves Chibon 8544f4
            namespace='forks',
Pierre-Yves Chibon 93120d
            description='test project forks/test',
Pierre-Yves Chibon 93120d
            url='',
Pierre-Yves Chibon 93120d
            avatar_email='',
Pierre-Yves Chibon 93120d
            user='pingou',
Pierre-Yves Chibon 93120d
            blacklist=pagure.APP.config['BLACKLISTED_PROJECTS'],
Pierre-Yves Chibon 0d4ec5
            allowed_prefix=pagure.APP.config['ALLOWED_PREFIX'],
Pierre-Yves Chibon 93120d
            gitfolder=pagure.APP.config['GIT_FOLDER'],
Pierre-Yves Chibon 93120d
            docfolder=pagure.APP.config['DOCS_FOLDER'],
Pierre-Yves Chibon 93120d
            ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
Pierre-Yves Chibon 93120d
            requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
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 8544f4
            name='test',
Pierre-Yves Chibon 8544f4
            namespace='forks',
Pierre-Yves Chibon 0d4ec5
            description='test project forks/test',
Pierre-Yves Chibon 0d4ec5
            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
Jeremy Cline 20109f
        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 93120d
        output = self.app.get('/forks/test')
Pierre-Yves Chibon 93120d
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 93120d
            '
\n '
Pierre-Yves Chibon 058fe8
            '
Contributors
', output.data)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 93120d
            '

The Project Creator has not pushed any code yet

',
Pierre-Yves Chibon 93120d
            output.data)
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
        output = self.app.get('/forks/test/issues')
Pierre-Yves Chibon 93120d
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 93120d
        self.assertIn(
Pierre-Yves Chibon 93120d
            '<title>Issues - forks/test - Pagure</title>', output.data)
Pierre-Yves Chibon 93120d
        self.assertIn(
Abhijeet Kasurde a2ea74
            'No issues found',
Pierre-Yves Chibon 93120d
            output.data)
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 06d8fa
    @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 06d8fa
        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 06d8fa
        output = self.app.get('/test')
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 06d8fa
            '
\n '
Pierre-Yves Chibon 058fe8
            '
Contributors
', output.data)
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 06d8fa
            pagure.lib.new_project,
Pierre-Yves Chibon 06d8fa
            self.session,
Pierre-Yves Chibon 8544f4
            name='test',
Pierre-Yves Chibon 8544f4
            namespace='forks',
Pierre-Yves Chibon 06d8fa
            description='test project forks/test',
Pierre-Yves Chibon 06d8fa
            url='',
Pierre-Yves Chibon 06d8fa
            avatar_email='',
Pierre-Yves Chibon 06d8fa
            user='pingou',
Pierre-Yves Chibon 06d8fa
            blacklist=pagure.APP.config['BLACKLISTED_PROJECTS'],
Pierre-Yves Chibon 06d8fa
            allowed_prefix=pagure.APP.config['ALLOWED_PREFIX'],
Pierre-Yves Chibon 06d8fa
            gitfolder=pagure.APP.config['GIT_FOLDER'],
Pierre-Yves Chibon 06d8fa
            docfolder=pagure.APP.config['DOCS_FOLDER'],
Pierre-Yves Chibon 06d8fa
            ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
Pierre-Yves Chibon 06d8fa
            requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
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 8544f4
            name='test',
Pierre-Yves Chibon 8544f4
            namespace='forks',
Pierre-Yves Chibon 06d8fa
            description='test project forks/test',
Pierre-Yves Chibon 06d8fa
            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 06d8fa
        self.set_up_git_repo(name='forks/test')
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Front page shows fine
Pierre-Yves Chibon 06d8fa
        output = self.app.get('/forks/test')
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 06d8fa
            '
\n '
Pierre-Yves Chibon 058fe8
            '
Contributors
', output.data)
Pierre-Yves Chibon 06d8fa
        self.assertIn('Add sources file for testing', output.data)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 06d8fa
            '<title>Overview - forks/test - Pagure</title>', output.data)
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Issues list shows fine
Pierre-Yves Chibon 06d8fa
        output = self.app.get('/forks/test/issues')
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 06d8fa
            '<title>Issues - forks/test - Pagure</title>', output.data)
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Abhijeet Kasurde a2ea74
            'No issues found',
Pierre-Yves Chibon 06d8fa
            output.data)
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 06d8fa
        # Try accessing the commit
Jeremy Cline 20109f
        gitrepo = os.path.join(self.path, 'repos', 'forks/test.git')
Pierre-Yves Chibon 06d8fa
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 06d8fa
        master_branch = repo.lookup_branch('master')
Pierre-Yves Chibon 06d8fa
        first_commit = master_branch.get_object().hex
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon c68e92
        output = self.app.get('/forks/test/commits')
Pierre-Yves Chibon c68e92
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon c68e92
        self.assertIn(first_commit, output.data)
Pierre-Yves Chibon c68e92
        self.assertIn(
Pierre-Yves Chibon c68e92
            '
Pierre-Yves Chibon c68e92
            output.data)
Pierre-Yves Chibon c68e92
Pierre-Yves Chibon 06d8fa
        output = self.app.get('/forks/test/c/%s' % first_commit)
Pierre-Yves Chibon 06d8fa
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 06d8fa
        self.assertIn('<title>Commit - forks/test ', output.data)</title>
Pierre-Yves Chibon 06d8fa
        self.assertIn(
Pierre-Yves Chibon 06d8fa
            '+2          ',
Pierre-Yves Chibon 06d8fa
            output.data)
Pierre-Yves Chibon 06d8fa
Pierre-Yves Chibon 93120d
Pierre-Yves Chibon 93120d
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)