Blame tests/test_pagure_flask_docs.py

Pierre-Yves Chibon 78f8ff
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
"""
Pierre-Yves Chibon f88a86
 (c) 2015-2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
 Authors:
Pierre-Yves Chibon 78f8ff
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
"""
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 78f8ff
import pkg_resources
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon b4d58a
import json
Pierre-Yves Chibon 78f8ff
import unittest
Pierre-Yves Chibon 78f8ff
import shutil
Pierre-Yves Chibon 78f8ff
import sys
Pierre-Yves Chibon 78f8ff
import os
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon f88a86
import mock
Pierre-Yves Chibon b4d58a
import pygit2
Pierre-Yves Chibon 78f8ff
from mock import patch
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 78f8ff
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon bf2dfd
import pagure.docs_server
Pierre-Yves Chibon fe5017
import pagure.lib
Pierre-Yves Chibon 78f8ff
import tests
Pierre-Yves Chibon 27a73d
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon fe5017
class PagureFlaskDocstests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for flask docs of pagure """
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
    def setUp(self):
Pierre-Yves Chibon 78f8ff
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon fe5017
        super(PagureFlaskDocstests, self).setUp()
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon c684eb
        pagure.docs_server.LOG.handlers = []
Pierre-Yves Chibon bf2dfd
        pagure.docs_server.APP.config['TESTING'] = True
Pierre-Yves Chibon bf2dfd
        pagure.docs_server.SESSION = self.session
Pierre-Yves Chibon bf2dfd
Pierre-Yves Chibon fe5017
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon fe5017
        pagure.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon 59394d
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 78f8ff
Jeremy Cline 20109f
        pagure.docs_server.APP.config['GIT_FOLDER'] = self.path
Pierre-Yves Chibon bf2dfd
        pagure.docs_server.APP.config['TICKETS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'tickets')
Pierre-Yves Chibon bf2dfd
        pagure.docs_server.APP.config['DOCS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'docs')
Pierre-Yves Chibon bf2dfd
Jeremy Cline 20109f
        pagure.APP.config['GIT_FOLDER'] = self.path
Pierre-Yves Chibon fe5017
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'tickets')
Pierre-Yves Chibon fe5017
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'docs')
Pierre-Yves Chibon bf2dfd
        self.app = pagure.docs_server.APP.test_client()
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon f88a86
    def _set_up_doc(self):
Pierre-Yves Chibon f88a86
        # forked doc repo
Pierre-Yves Chibon f88a86
        docrepo = os.path.join(self.path, 'docs', 'test', 'test.git')
Pierre-Yves Chibon f88a86
        repo = pygit2.init_repository(docrepo)
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon f88a86
        # Create files in that git repo
Pierre-Yves Chibon f88a86
        with open(os.path.join(docrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon f88a86
            stream.write('foo\n bar')
Pierre-Yves Chibon f88a86
        repo.index.add('sources')
Pierre-Yves Chibon f88a86
        repo.index.write()
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon f88a86
        folderpart = os.path.join(docrepo, 'folder1', 'folder2')
Pierre-Yves Chibon f88a86
        os.makedirs(folderpart)
Pierre-Yves Chibon f88a86
        with open(os.path.join(folderpart, 'test_file'), 'w') as stream:
Pierre-Yves Chibon f88a86
            stream.write('row1\nrow2\nrow3')
Pierre-Yves Chibon f88a86
        repo.index.add(os.path.join('folder1', 'folder2', 'test_file'))
Pierre-Yves Chibon f88a86
        repo.index.write()
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon f88a86
        # Commits the files added
Pierre-Yves Chibon f88a86
        tree = repo.index.write_tree()
Pierre-Yves Chibon f88a86
        author = pygit2.Signature(
Pierre-Yves Chibon f88a86
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon f88a86
        committer = pygit2.Signature(
Pierre-Yves Chibon f88a86
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon f88a86
        repo.create_commit(
Pierre-Yves Chibon f88a86
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon f88a86
            author,
Pierre-Yves Chibon f88a86
            committer,
Pierre-Yves Chibon f88a86
            'Add test files and folder',
Pierre-Yves Chibon f88a86
            # binary string representing the tree object ID
Pierre-Yves Chibon f88a86
            tree,
Pierre-Yves Chibon f88a86
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon f88a86
            []
Pierre-Yves Chibon f88a86
        )
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon f88a86
        # Push the changes to the bare repo
Pierre-Yves Chibon f88a86
        remote = repo.create_remote(
Pierre-Yves Chibon f88a86
            'origin', os.path.join(self.path, 'docs', 'test.git'))
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon f88a86
        PagureRepo.push(remote, 'refs/heads/master:refs/heads/master')
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon f88a86
        # Turn on the docs project since it's off by default
Pierre-Yves Chibon f88a86
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon f88a86
        repo.settings = {'project_documentation': True}
Pierre-Yves Chibon f88a86
        self.session.add(repo)
Pierre-Yves Chibon f88a86
        self.session.commit()
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon 78f8ff
    def test_view_docs_no_project(self):
Pierre-Yves Chibon 78f8ff
        """ Test the view_docs endpoint with no project. """
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
        output = self.app.get('/foo/docs')
Pierre-Yves Chibon 78f8ff
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon cb4ad3
    def test_view_docs_project_no_git(self):
Pierre-Yves Chibon cb4ad3
        """ Test the view_docs endpoint with a project that has no
Pierre-Yves Chibon cb4ad3
        corresponding git repo.
Pierre-Yves Chibon cb4ad3
        """
Pierre-Yves Chibon cb4ad3
        tests.create_projects(self.session)
Pierre-Yves Chibon cb4ad3
Pierre-Yves Chibon e994c4
        # Turn on the docs project since it's off by default
Pierre-Yves Chibon e994c4
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon e994c4
        repo.settings = {'project_documentation': True}
Pierre-Yves Chibon e994c4
        self.session.add(repo)
Pierre-Yves Chibon e994c4
        self.session.commit()
Pierre-Yves Chibon e994c4
Pierre-Yves Chibon cb4ad3
        output = self.app.get('/test/docs', follow_redirects=True)
Pierre-Yves Chibon cb4ad3
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon cb4ad3
        self.assertTrue(
Pierre-Yves Chibon bf2dfd
            '

Documentation not found

' in output.data)
Pierre-Yves Chibon bf2dfd
Pierre-Yves Chibon bf2dfd
        output = self.app.get('/test', follow_redirects=True)
Pierre-Yves Chibon bf2dfd
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon bf2dfd
        self.assertTrue(
Pierre-Yves Chibon bf2dfd
            '

Documentation not found

' in output.data)
Pierre-Yves Chibon cb4ad3
Pierre-Yves Chibon c25c8b
    def test_view_docs_project_no_docs(self):
Pierre-Yves Chibon c25c8b
        """ Test the view_docs endpoint with a project that disabled the
Pierre-Yves Chibon c25c8b
        docs.
Pierre-Yves Chibon c25c8b
        """
Pierre-Yves Chibon c25c8b
        tests.create_projects(self.session)
Pierre-Yves Chibon fe5017
        repo = pagure.lib.get_project(self.session, 'test')
Jeremy Cline 20109f
        tests.create_projects_git(os.path.join(self.path, 'docs'))
Pierre-Yves Chibon c25c8b
Pierre-Yves Chibon c25c8b
        output = self.app.get('/test/docs')
Pierre-Yves Chibon 832b32
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon c25c8b
Pierre-Yves Chibon 932d90
        repo.settings = {'project_documentation': False}
Pierre-Yves Chibon c25c8b
        self.session.add(repo)
Pierre-Yves Chibon c25c8b
        self.session.commit()
Pierre-Yves Chibon c25c8b
Pierre-Yves Chibon c25c8b
        output = self.app.get('/test/docs', follow_redirects=True)
Pierre-Yves Chibon c25c8b
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon c25c8b
Pierre-Yves Chibon b4d58a
    def test_view_docs(self):
Pierre-Yves Chibon b4d58a
        """ Test the view_docs endpoint. """
Pierre-Yves Chibon b4d58a
        tests.create_projects(self.session)
Pierre-Yves Chibon b4d58a
        repo = pygit2.init_repository(
Jeremy Cline 20109f
            os.path.join(self.path, 'docs', 'test.git'), bare=True)
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon b4d58a
        output = self.app.get('/test/docs')
Pierre-Yves Chibon 832b32
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon f88a86
        self._set_up_doc()
Pierre-Yves Chibon e994c4
Pierre-Yves Chibon b4d58a
        # Now check the UI
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon b4d58a
        output = self.app.get('/test/docs')
Pierre-Yves Chibon 832b32
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon 832b32
        output = self.app.get('/test/sources')
Pierre-Yves Chibon b4d58a
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 832b32
        self.assertEqual('
foo\n bar
', output.data)
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon 832b32
        output = self.app.get('/test/folder1/folder2')
Pierre-Yves Chibon b4d58a
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b4d58a
        self.assertTrue(
Pierre-Yves Chibon 832b32
            '
  • '
    Pierre-Yves Chibon b4d58a
                in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 832b32
            output = self.app.get('/test/folder1/folder2/test_file')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 832b32
            self.assertEqual('
    row1\nrow2\nrow3
    ', output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 832b32
            output = self.app.get('/test/folder1')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon 9677a4
                '
  • '
    Pierre-Yves Chibon b4d58a
                in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 832b32
            output = self.app.get('/test/folder1/foo')
    Pierre-Yves Chibon 832b32
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 832b32
            output = self.app.get('/test/folder1/foo/folder2')
    Pierre-Yves Chibon 832b32
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon f88a86
        @mock.patch(
    Pierre-Yves Chibon f88a86
            'pagure.lib.encoding_utils.decode',
    Pierre-Yves Chibon 0183b4
            mock.MagicMock(side_effect=pagure.exceptions.PagureEncodingException))
    Pierre-Yves Chibon f88a86
        def test_view_docs_encoding_error(self):
    Pierre-Yves Chibon f88a86
            """ Test viewing a file of which we cannot find the encoding. """
    Pierre-Yves Chibon f88a86
            tests.create_projects(self.session)
    Pierre-Yves Chibon f88a86
            repo = pygit2.init_repository(
    Pierre-Yves Chibon f88a86
                os.path.join(self.path, 'docs', 'test.git'), bare=True)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            output = self.app.get('/test/docs')
    Pierre-Yves Chibon f88a86
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            self._set_up_doc()
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            output = self.app.get('/test/sources')
    Pierre-Yves Chibon f88a86
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f88a86
            self.assertEqual('foo\n bar', output.data)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            output = self.app.get('/test/folder1')
    Pierre-Yves Chibon f88a86
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f88a86
            self.assertTrue(
    Pierre-Yves Chibon f88a86
                '
  • '
    Pierre-Yves Chibon f88a86
                in output.data)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
        @mock.patch(
    Pierre-Yves Chibon f88a86
            'pagure.lib.encoding_utils.decode',
    Pierre-Yves Chibon f88a86
            mock.MagicMock(side_effect=IOError))
    Pierre-Yves Chibon f88a86
        def test_view_docs_unknown_error(self):
    Pierre-Yves Chibon f88a86
            """ Test viewing a file of which we cannot find the encoding. """
    Pierre-Yves Chibon f88a86
            tests.create_projects(self.session)
    Pierre-Yves Chibon f88a86
            repo = pygit2.init_repository(
    Pierre-Yves Chibon f88a86
                os.path.join(self.path, 'docs', 'test.git'), bare=True)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            output = self.app.get('/test/docs')
    Pierre-Yves Chibon f88a86
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            self._set_up_doc()
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            output = self.app.get('/test/sources')
    Pierre-Yves Chibon f88a86
            self.assertEqual(output.status_code, 500)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
            output = self.app.get('/test/folder1')
    Pierre-Yves Chibon 7f3b76
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon 78f8ff
    Pierre-Yves Chibon 78f8ff
    if __name__ == '__main__':
    Pierre-Yves Chibon fe5017
        SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskDocstests)
    Pierre-Yves Chibon 78f8ff
        unittest.TextTestRunner(verbosity=2).run(SUITE)