Blame tests/test_progit_flask_ui_docs.py

Pierre-Yves Chibon 78f8ff
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
"""
Pierre-Yves Chibon 78f8ff
 (c) 2015 - 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 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 78f8ff
import progit.lib
Pierre-Yves Chibon 78f8ff
import tests
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
class ProgitFlaskDocstests(tests.Modeltests):
Pierre-Yves Chibon 78f8ff
    """ Tests for flask docs of progit """
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 78f8ff
        super(ProgitFlaskDocstests, self).setUp()
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
        progit.APP.config['TESTING'] = True
Pierre-Yves Chibon 78f8ff
        progit.SESSION = self.session
Pierre-Yves Chibon 78f8ff
        progit.ui.SESSION = self.session
Pierre-Yves Chibon 78f8ff
        progit.ui.app.SESSION = self.session
Pierre-Yves Chibon 78f8ff
        progit.ui.docs.SESSION = self.session
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
        progit.APP.config['GIT_FOLDER'] = tests.HERE
Pierre-Yves Chibon 78f8ff
        progit.APP.config['FORK_FOLDER'] = os.path.join(
Pierre-Yves Chibon 78f8ff
            tests.HERE, 'forks')
Pierre-Yves Chibon 78f8ff
        progit.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 78f8ff
            tests.HERE, 'tickets')
Pierre-Yves Chibon 78f8ff
        progit.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 78f8ff
            tests.HERE, 'docs')
Pierre-Yves Chibon 78f8ff
        self.app = progit.APP.test_client()
Pierre-Yves Chibon 78f8ff
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 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 cb4ad3
            '
  • No docs repository could be found, please '
  • Pierre-Yves Chibon cb4ad3
                'contact an admin' 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 c25c8b
            repo = progit.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon c25c8b
            tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
    Pierre-Yves Chibon c25c8b
    Pierre-Yves Chibon c25c8b
            output = self.app.get('/test/docs')
    Pierre-Yves Chibon c25c8b
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon c25c8b
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon c25c8b
            self.assertTrue('

    This repo is brand new!

    ' in output.data)
    Pierre-Yves Chibon c25c8b
            self.assertTrue(
    Pierre-Yves Chibon c25c8b
                'git clone git@progit.fedorahosted.org:docs/test.git'
    Pierre-Yves Chibon c25c8b
                in output.data)
    Pierre-Yves Chibon c25c8b
    Pierre-Yves Chibon c25c8b
            repo.project_docs = 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(
    Pierre-Yves Chibon b4d58a
                os.path.join(tests.HERE, 'docs', 'test.git'), bare=True)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            output = self.app.get('/test/docs')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    This repo is brand new!

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                'git clone git@progit.fedorahosted.org:docs/test.git'
    Pierre-Yves Chibon b4d58a
                in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            # forked doc repo
    Pierre-Yves Chibon b4d58a
            docrepo = os.path.join(tests.HERE, 'docs', 'test', 'test.git')
    Pierre-Yves Chibon b4d58a
            repo = pygit2.init_repository(docrepo)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            # Create files in that git repo
    Pierre-Yves Chibon b4d58a
            with open(os.path.join(docrepo, 'sources'), 'w') as stream:
    Pierre-Yves Chibon b4d58a
                stream.write('foo\n bar')
    Pierre-Yves Chibon b4d58a
            repo.index.add('sources')
    Pierre-Yves Chibon b4d58a
            repo.index.write()
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            folderpart = os.path.join(docrepo, 'folder1', 'folder2')
    Pierre-Yves Chibon b4d58a
            os.makedirs(folderpart)
    Pierre-Yves Chibon b4d58a
            with open(os.path.join(folderpart, 'test_file'), 'w') as stream:
    Pierre-Yves Chibon b4d58a
                stream.write('row1\nrow2\nrow3')
    Pierre-Yves Chibon b4d58a
            repo.index.add(os.path.join('folder1', 'folder2', 'test_file'))
    Pierre-Yves Chibon b4d58a
            repo.index.write()
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            # Commits the files added
    Pierre-Yves Chibon b4d58a
            tree = repo.index.write_tree()
    Pierre-Yves Chibon b4d58a
            author = pygit2.Signature(
    Pierre-Yves Chibon b4d58a
                'Alice Author', 'alice@authors.tld')
    Pierre-Yves Chibon b4d58a
            committer = pygit2.Signature(
    Pierre-Yves Chibon b4d58a
                'Cecil Committer', 'cecil@committers.tld')
    Pierre-Yves Chibon b4d58a
            repo.create_commit(
    Pierre-Yves Chibon b4d58a
                'refs/heads/master',  # the name of the reference to update
    Pierre-Yves Chibon b4d58a
                author,
    Pierre-Yves Chibon b4d58a
                committer,
    Pierre-Yves Chibon b4d58a
                'Add test files and folder',
    Pierre-Yves Chibon b4d58a
                # binary string representing the tree object ID
    Pierre-Yves Chibon b4d58a
                tree,
    Pierre-Yves Chibon b4d58a
                # list of binary strings representing parents of the new commit
    Pierre-Yves Chibon b4d58a
                []
    Pierre-Yves Chibon b4d58a
            )
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            # Push the changes to the bare repo
    Pierre-Yves Chibon b4d58a
            remote = repo.create_remote(
    Pierre-Yves Chibon b4d58a
                'origin', os.path.join(tests.HERE, 'docs', 'test.git'))
    Pierre-Yves Chibon b4d58a
            remote.push('refs/heads/master:refs/heads/master')
    Pierre-Yves Chibon b4d58a
    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 b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertFalse('

    This repo is brand new!

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                '' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                '' in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            output = self.app.get('/test/docs/master/sources')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('<section class="docs_content">' in output.data)</section>
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            output = self.app.get('/test/docs/master/folder1/folder2')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                '
  • \n '
  • Pierre-Yves Chibon b4d58a
                ''
    Pierre-Yves Chibon b4d58a
                in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            output = self.app.get('/test/docs/master/folder1/folder2/test_file')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                '  <section class="docs_content">\n    row1\nrow2\n'</section>
    Pierre-Yves Chibon b4d58a
                'row3\n  ' in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            output = self.app.get('/test/docs/master/folder1')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                '
  • \n '
  • Pierre-Yves Chibon b4d58a
                ''
    Pierre-Yves Chibon b4d58a
                in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            output = self.app.get('/test/docs/master/folder1/foo')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue('

    Docs

    ' in output.data)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                '
  • File folder1/foo not found
  • '
    Pierre-Yves Chibon b4d58a
                in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon b4d58a
            output = self.app.get('/test/docs/master/folder1/foo/folder2')
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b4d58a
            self.assertTrue(
    Pierre-Yves Chibon b4d58a
                '
  • File folder1/foo/folder2 not found
  • '
    Pierre-Yves Chibon b4d58a
                in output.data)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 78f8ff
    Pierre-Yves Chibon 78f8ff
    if __name__ == '__main__':
    Pierre-Yves Chibon 78f8ff
        SUITE = unittest.TestLoader().loadTestsFromTestCase(ProgitFlaskDocstests)
    Pierre-Yves Chibon 78f8ff
        unittest.TextTestRunner(verbosity=2).run(SUITE)