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 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
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 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 78f8ff
Pierre-Yves Chibon bf2dfd
import pagure.docs_server
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 78f8ff
import tests
Pierre-Yves Chibon 27a73d
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 78f8ff
Pierre-Yves Chibon 78f8ff
Clement Verna 109c4b
class PagureFlaskDocstests(tests.SimplePagureTest):
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 73d120
        pagure.docs_server.APP.config["TESTING"] = True
Pierre-Yves Chibon bf2dfd
        pagure.docs_server.SESSION = self.session
Pierre-Yves Chibon bf2dfd
Pierre-Yves Chibon 73d120
        pagure.docs_server.APP.config["GIT_FOLDER"] = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        pagure.docs_server.APP.config["TICKETS_FOLDER"] = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "tickets"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        pagure.docs_server.APP.config["DOCS_FOLDER"] = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos", "docs"
Pierre-Yves Chibon 73d120
        )
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 73d120
        docrepo = os.path.join(self.path, "repos", "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 73d120
        with open(os.path.join(docrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon f88a86
        repo.index.write()
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon 73d120
        folderpart = os.path.join(docrepo, "folder1", "folder2")
Pierre-Yves Chibon f88a86
        os.makedirs(folderpart)
Pierre-Yves Chibon 73d120
        with open(os.path.join(folderpart, "test_file"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("row1\nrow2\nrow3")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon f88a86
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon f88a86
            author,
Pierre-Yves Chibon f88a86
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [],
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 73d120
            "origin", os.path.join(self.path, "repos", "docs", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f88a86
Pierre-Yves Chibon 73d120
        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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
            "

Documentation not found

" in output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon bf2dfd
Pierre-Yves Chibon 73d120
        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 73d120
            "

Documentation not found

" in output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        )
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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos", "docs"))
Pierre-Yves Chibon c25c8b
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/docs")
Pierre-Yves Chibon 832b32
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon c25c8b
Pierre-Yves Chibon 73d120
        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 73d120
        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 51cfa4
    def test_view_docs_empty_repo(self):
Pierre-Yves Chibon 51cfa4
        """ Test the view_docs endpoint when the git repo is empty. """
Pierre-Yves Chibon 51cfa4
        tests.create_projects(self.session)
Pierre-Yves Chibon 51cfa4
        repo = pygit2.init_repository(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "docs", "test.git"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 51cfa4
Pierre-Yves Chibon 51cfa4
        # Turn on the docs project since it's off by default
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        repo.settings = {"project_documentation": True}
Pierre-Yves Chibon 51cfa4
        self.session.add(repo)
Pierre-Yves Chibon 51cfa4
        self.session.commit()
Pierre-Yves Chibon 51cfa4
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/docs")
Pierre-Yves Chibon 51cfa4
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 51cfa4
        self.assertIn(
Pierre-Yves Chibon 73d120
            "

No content found in the repository, you may want to read "

Pierre-Yves Chibon 51cfa4
            'the 
Pierre-Yves Chibon 51cfa4
            'using_doc.html">Using the doc repository of your project '
Pierre-Yves Chibon 73d120
            "documentation.

",
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 51cfa4
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 73d120
            os.path.join(self.path, "repos", "docs", "test.git"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon 73d120
        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 73d120
        output = self.app.get("/test/docs")
Pierre-Yves Chibon 832b32
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/sources")
Pierre-Yves Chibon b4d58a
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        self.assertEqual("
foo\n bar
", output.get_data(as_text=True))
Pierre-Yves Chibon b4d58a
Pierre-Yves Chibon 73d120
        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 73d120
                in output.get_data(as_text=True)
    Pierre-Yves Chibon 73d120
            )
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 73d120
            output = self.app.get("/test/folder1/folder2/test_file")
    Pierre-Yves Chibon b4d58a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 73d120
            self.assertEqual(
    Pierre-Yves Chibon 73d120
                "
    row1\nrow2\nrow3
    ", output.get_data(as_text=True)
    Pierre-Yves Chibon 73d120
            )
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 73d120
            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 73d120
                in output.get_data(as_text=True)
    Pierre-Yves Chibon 73d120
            )
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 73d120
            output = self.app.get("/test/folder1/foo")
    Pierre-Yves Chibon 832b32
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon b4d58a
    Pierre-Yves Chibon 73d120
            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 73d120
            "pagure.lib.encoding_utils.decode",
    Pierre-Yves Chibon 73d120
            mock.MagicMock(side_effect=pagure.exceptions.PagureEncodingException),
    Pierre-Yves Chibon 73d120
        )
    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 73d120
                os.path.join(self.path, "repos", "docs", "test.git"), bare=True
    Pierre-Yves Chibon 73d120
            )
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon 73d120
            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 73d120
            output = self.app.get("/test/sources")
    Pierre-Yves Chibon f88a86
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 73d120
            self.assertEqual("foo\n bar", output.get_data(as_text=True))
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon 73d120
            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 73d120
                in output.get_data(as_text=True)
    Pierre-Yves Chibon 73d120
            )
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon f88a86
        @mock.patch(
    Pierre-Yves Chibon 73d120
            "pagure.lib.encoding_utils.decode", mock.MagicMock(side_effect=IOError)
    Pierre-Yves Chibon 73d120
        )
    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 73d120
                os.path.join(self.path, "repos", "docs", "test.git"), bare=True
    Pierre-Yves Chibon 73d120
            )
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon 73d120
            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 73d120
            output = self.app.get("/test/sources")
    Pierre-Yves Chibon f88a86
            self.assertEqual(output.status_code, 500)
    Pierre-Yves Chibon f88a86
    Pierre-Yves Chibon 73d120
            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 73d120
    if __name__ == "__main__":
    Pierre-Yves Chibon 393f31
        unittest.main(verbosity=2)