Blame tests/test_pagure_flask_api.py

Pierre-Yves Chibon 62f012
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
"""
Pierre-Yves Chibon 62f012
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
 Authors:
Pierre-Yves Chibon 62f012
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
"""
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 62f012
import unittest
Pierre-Yves Chibon 62f012
import shutil
Pierre-Yves Chibon 62f012
import sys
Pierre-Yves Chibon 62f012
import os
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
import json
Slavek Kabrda 92297a
from mock import patch, MagicMock
Pierre-Yves Chibon 62f012
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 62f012
Slavek Kabrda 727932
import pagure.api
Pierre-Yves Chibon b130e5
import pagure.flask_app
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 62f012
import tests
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
Clement Verna 109c4b
class PagureFlaskApitests(tests.SimplePagureTest):
Pierre-Yves Chibon fe5017
    """ Tests for flask API controller of pagure """
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 437cd0
    maxDiff = None
Pierre-Yves Chibon 437cd0
Pierre-Yves Chibon 674045
    def test_api_doc(self):
Pierre-Yves Chibon 674045
        """ Test the API documentation page. """
Pierre-Yves Chibon 930073
        print(dir(self.app))
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/")
Pierre-Yves Chibon 674045
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        self.assertIn("<title> API | pagure  - Pagure</title>\n", output_text)
Pierre-Yves Chibon 674045
        self.assertIn(
Pierre-Yves Chibon 73d120
            "  Pagure API Reference\n        \n", output_text
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 674045
Pierre-Yves Chibon 674045
    def test_api_doc_authenticated(self):
Pierre-Yves Chibon 674045
        """ Test the API documentation page. """
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 674045
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/")
Pierre-Yves Chibon 674045
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 674045
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title> API | pagure  - Pagure</title>\n", output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 674045
            self.assertIn(
Pierre-Yves Chibon 73d120
                "  Pagure API Reference\n        \n", output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 674045
Slavek Kabrda 727932
    def test_api_get_request_data(self):
Pierre-Yves Chibon 73d120
        data = {"foo": "bar"}
Slavek Kabrda 727932
        # test_request_context doesn't set flask.g, but some teardown
Slavek Kabrda 727932
        # functions try to use that, so let's exclude them
Slavek Kabrda 727932
        self._app.teardown_request_funcs = {}
Pierre-Yves Chibon b562bc
        with self._app.test_request_context(
Pierre-Yves Chibon 73d120
            "/api/0/version", method="POST", data=data
Pierre-Yves Chibon 73d120
        ):
Pierre-Yves Chibon 73d120
            self.assertEqual(pagure.api.get_request_data()["foo"], "bar")
Slavek Kabrda 727932
        data = json.dumps(data)
Pierre-Yves Chibon 73d120
        with self._app.test_request_context(
Pierre-Yves Chibon 73d120
            "/api/0/version", data=data, content_type="application/json"
Pierre-Yves Chibon 73d120
        ):
Pierre-Yves Chibon 73d120
            self.assertEqual(pagure.api.get_request_data()["foo"], "bar")
Slavek Kabrda 727932
Pierre-Yves Chibon 0b9b6b
    def test_api_version_old_url(self):
Pierre-Yves Chibon 62f012
        """ Test the api_version function.  """
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/version")
Pierre-Yves Chibon 62f012
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["version"], pagure.__api_version__)
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["version"])
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 0b9b6b
    def test_api_version_new_url(self):
Pierre-Yves Chibon 0b9b6b
        """ Test the api_version function at its new url.  """
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/-/version")
Pierre-Yves Chibon 0b9b6b
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0b9b6b
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["version"], pagure.__api_version__)
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["version"])
Pierre-Yves Chibon 0b9b6b
Pierre-Yves Chibon dbea26
    def test_api_project_tags(self):
Pierre-Yves Chibon dbea26
        """ Test the api_project_tags function.  """
Pierre-Yves Chibon dbea26
        tests.create_projects(self.session)
Pierre-Yves Chibon dbea26
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/foo/tags/")
Pierre-Yves Chibon dbea26
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(set(data.keys()), set(["output", "error"]))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["output"], "notok")
Pierre-Yves Chibon 73d120
        self.assertEqual(data["error"], "Project not found")
Pierre-Yves Chibon dbea26
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/tags/")
Pierre-Yves Chibon dbea26
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["tags", "total_tags"])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["tags"], [])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_tags"], 0)
Pierre-Yves Chibon dbea26
Pierre-Yves Chibon dbea26
        # Add an issue and tag it so that we can list them
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.Issue(
Pierre-Yves Chibon dbea26
            id=1,
Pierre-Yves Chibon 73d120
            uid="foobar",
Pierre-Yves Chibon dbea26
            project_id=1,
Pierre-Yves Chibon 73d120
            title="issue",
Pierre-Yves Chibon 73d120
            content="a bug report",
Pierre-Yves Chibon dbea26
            user_id=1,  # pingou
Pierre-Yves Chibon dbea26
        )
Pierre-Yves Chibon dbea26
        self.session.add(item)
Pierre-Yves Chibon dbea26
        self.session.commit()
Pierre-Yves Chibon a8f682
        item = pagure.lib.model.TagColored(
Pierre-Yves Chibon 73d120
            tag="tag1", tag_color="DeepBlueSky", project_id=1
Pierre-Yves Chibon dbea26
        )
Pierre-Yves Chibon dbea26
        self.session.add(item)
Pierre-Yves Chibon dbea26
        self.session.commit()
Pierre-Yves Chibon a8f682
        item = pagure.lib.model.TagIssueColored(
Pierre-Yves Chibon 73d120
            issue_uid="foobar", tag_id=item.id
Pierre-Yves Chibon dbea26
        )
Pierre-Yves Chibon dbea26
        self.session.add(item)
Pierre-Yves Chibon dbea26
        self.session.commit()
Pierre-Yves Chibon dbea26
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/tags/")
Pierre-Yves Chibon dbea26
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["tags", "total_tags"])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["tags"], ["tag1"])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_tags"], 1)
Pierre-Yves Chibon dbea26
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/tags/?pattern=t")
Pierre-Yves Chibon dbea26
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["tags", "total_tags"])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["tags"], ["tag1"])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_tags"], 1)
Pierre-Yves Chibon dbea26
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/tags/?pattern=p")
Pierre-Yves Chibon dbea26
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["tags", "total_tags"])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["tags"], [])
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_tags"], 0)
Pierre-Yves Chibon dbea26
Aurélien Bompard 626417
    def test_api_groups(self):
Aurélien Bompard 626417
        """ Test the api_groups function.  """
Aurélien Bompard 626417
Aurélien Bompard 626417
        # Add a couple of groups so that we can list them
Aurélien Bompard 626417
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
            group_name="group1",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            display_name="User group",
Aurélien Bompard 626417
            user_id=1,  # pingou
Aurélien Bompard 626417
        )
Aurélien Bompard 626417
        self.session.add(item)
Aurélien Bompard 626417
Aurélien Bompard 626417
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
            group_name="rel-eng",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            display_name="Release engineering group",
Aurélien Bompard 626417
            user_id=1,  # pingou
Aurélien Bompard 626417
        )
Aurélien Bompard 626417
        self.session.add(item)
Aurélien Bompard 626417
        self.session.commit()
Aurélien Bompard 626417
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/groups")
Aurélien Bompard 626417
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["groups"], ["group1", "rel-eng"])
Pierre-Yves Chibon 75809f
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(data.keys()), ["groups", "pagination", "total_groups"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_groups"], 2)
Aurélien Bompard 626417
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/groups?pattern=re")
Aurélien Bompard 626417
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["groups"], ["rel-eng"])
Pierre-Yves Chibon 75809f
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(data.keys()), ["groups", "pagination", "total_groups"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_groups"], 1)
Aurélien Bompard 626417
Pierre-Yves Chibon 48ad71
    def test_api_whoami_unauth(self):
Pierre-Yves Chibon 48ad71
        """ Test the api_whoami function. """
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/-/whoami")
Pierre-Yves Chibon 48ad71
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 48ad71
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 48ad71
        self.assertEqual(
Pierre-Yves Chibon 48ad71
            data,
Pierre-Yves Chibon 48ad71
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Pierre-Yves Chibon 73d120
                "http://localhost.localdomain/settings#api-keys to get or "
Pierre-Yves Chibon 73d120
                "renew your API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 48ad71
        )
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 48ad71
    def test_api_whoami_invalid_auth(self):
Pierre-Yves Chibon 48ad71
        """ Test the api_whoami function with an invalid token. """
Pierre-Yves Chibon 48ad71
        tests.create_projects(self.session)
Pierre-Yves Chibon 48ad71
        tests.create_tokens(self.session)
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token invalid"}
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/-/whoami", headers=headers)
Pierre-Yves Chibon 48ad71
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 48ad71
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 48ad71
        self.assertEqual(
Pierre-Yves Chibon 48ad71
            data,
Pierre-Yves Chibon 48ad71
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Pierre-Yves Chibon 73d120
                "http://localhost.localdomain/settings#api-keys to get or "
Pierre-Yves Chibon 73d120
                "renew your API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
                "errors": "Invalid token",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 48ad71
        )
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 48ad71
    def test_api_whoami_auth(self):
Pierre-Yves Chibon 48ad71
        """ Test the api_whoami function with a valid token. """
Pierre-Yves Chibon 48ad71
        tests.create_projects(self.session)
Pierre-Yves Chibon 48ad71
        tests.create_tokens(self.session)
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/-/whoami", headers=headers)
Pierre-Yves Chibon 48ad71
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 48ad71
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data, {"username": "pingou"})
Pierre-Yves Chibon 48ad71
Pierre-Yves Chibon 437cd0
    def test_api_error_codes(self):
Pierre-Yves Chibon 437cd0
        """ Test the api_error_codes endpoint. """
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/-/error_codes")
Pierre-Yves Chibon 437cd0
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 437cd0
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b2d897
        self.assertEqual(len(data), 37)
Pierre-Yves Chibon 437cd0
        self.assertEqual(
Pierre-Yves Chibon 437cd0
            sorted(data.keys()),
Pierre-Yves Chibon 73d120
            sorted(
Pierre-Yves Chibon 73d120
                [
Pierre-Yves Chibon 73d120
                    "EDATETIME",
Pierre-Yves Chibon 73d120
                    "EDBERROR",
Pierre-Yves Chibon 73d120
                    "EGITERROR",
Pierre-Yves Chibon 73d120
                    "EINVALIDISSUEFIELD",
Pierre-Yves Chibon 73d120
                    "EINVALIDISSUEFIELD_LINK",
Pierre-Yves Chibon 73d120
                    "EINVALIDPERPAGEVALUE",
Pierre-Yves Chibon 73d120
                    "EINVALIDPRIORITY",
Pierre-Yves Chibon 73d120
                    "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                    "EINVALIDTOK",
Pierre-Yves Chibon 73d120
                    "EISSUENOTALLOWED",
Pierre-Yves Chibon 73d120
                    "EMODIFYPROJECTNOTALLOWED",
Pierre-Yves Chibon 73d120
                    "ENEWPROJECTDISABLED",
Pierre-Yves Chibon 73d120
                    "ENOCODE",
Pierre-Yves Chibon 73d120
                    "ENOCOMMENT",
Pierre-Yves Chibon 73d120
                    "ENOCOMMIT",
Pierre-Yves Chibon 73d120
                    "ENOGROUP",
Pierre-Yves Chibon 73d120
                    "ENOISSUE",
Pierre-Yves Chibon 73d120
                    "ENOPRCLOSE",
Pierre-Yves Chibon 73d120
                    "ENOPROJECT",
Pierre-Yves Chibon 73d120
                    "ENOPROJECTS",
Pierre-Yves Chibon 73d120
                    "ENOPRSTATS",
Pierre-Yves Chibon 73d120
                    "ENOREQ",
Pierre-Yves Chibon 73d120
                    "ENOSIGNEDOFF",
Pierre-Yves Chibon 73d120
                    "ENOTASSIGNED",
Pierre-Yves Chibon 73d120
                    "ENOTASSIGNEE",
Pierre-Yves Chibon 73d120
                    "ENOTHIGHENOUGH",
Pierre-Yves Chibon 73d120
                    "ENOTMAINADMIN",
Pierre-Yves Chibon 73d120
                    "ENOUSER",
Pierre-Yves Chibon 73d120
                    "EPRCONFLICTS",
Pierre-Yves Chibon 73d120
                    "EPRNOTALLOWED",
Pierre-Yves Chibon 73d120
                    "EPRSCORE",
Pierre-Yves Chibon 73d120
                    "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
                    "ETIMESTAMP",
Pierre-Yves Chibon 73d120
                    "ETRACKERDISABLED",
Pierre-Yves Chibon 73d120
                    "ETRACKERREADONLY",
Pierre-Yves Chibon 73d120
                    "EUBLOCKED",
Pierre-Yves Chibon b2d897
                    "EREBASENOTALLOWED",
Pierre-Yves Chibon 73d120
                ]
Pierre-Yves Chibon 73d120
            ),
Pierre-Yves Chibon 437cd0
        )
Pierre-Yves Chibon 437cd0
Slavek Kabrda 92297a
    @patch("pagure.lib.tasks.get_result")
Slavek Kabrda 92297a
    def test_api_task_status(self, get_result):
Slavek Kabrda 92297a
        async_result = MagicMock()
Slavek Kabrda 92297a
        async_result.status = "running"
Slavek Kabrda 92297a
        async_result.ready.return_value = False
Slavek Kabrda 92297a
        get_result.return_value = async_result
Slavek Kabrda 92297a
        output = self.app.get("/api/0/task/123abc/status/")
Slavek Kabrda 92297a
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 92297a
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 92297a
        self.assertEqual(data, {"ready": False, "status": "running"})
Slavek Kabrda 92297a
Slavek Kabrda 92297a
        async_result = MagicMock()
Slavek Kabrda 92297a
        async_result.status = "finished"
Slavek Kabrda 92297a
        async_result.ready.return_value = True
Slavek Kabrda 92297a
        async_result.successful.return_value = True
Slavek Kabrda 92297a
        get_result.return_value = async_result
Slavek Kabrda 92297a
        output = self.app.get("/api/0/task/123abc/status/")
Slavek Kabrda 92297a
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 92297a
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 92297a
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"ready": True, "status": "finished", "successful": True}
Slavek Kabrda 92297a
        )
Slavek Kabrda 92297a
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)