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 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 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 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 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 b4d58a
        # forked doc repo
Jeremy Cline 20109f
        docrepo = os.path.join(self.path, '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(
Jeremy Cline 20109f
            'origin', os.path.join(self.path, 'docs', 'test.git'))
Pierre-Yves Chibon 97a6d1
Pierre-Yves Chibon 27a73d
        PagureRepo.push(remote, 'refs/heads/master:refs/heads/master')
Pierre-Yves Chibon b4d58a
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 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 832b32
                '
  • '
    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 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)