Blame tests/test_pagure_flask_api_auth.py

Pierre-Yves Chibon f329bb
# -*- coding: utf-8 -*-
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon f329bb
"""
Pierre-Yves Chibon f329bb
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon f329bb
 Authors:
Pierre-Yves Chibon f329bb
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon f329bb
"""
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon f329bb
import unittest
Pierre-Yves Chibon f329bb
import shutil
Pierre-Yves Chibon f329bb
import sys
Pierre-Yves Chibon f329bb
import os
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon f329bb
import json
Pierre-Yves Chibon f329bb
from mock import patch
Pierre-Yves Chibon f329bb
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 f329bb
Jeremy Cline 099538
import pagure.api
Pierre-Yves Chibon f329bb
import pagure.lib
Pierre-Yves Chibon f329bb
import tests
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon f329bb
Clement Verna 109c4b
class PagureFlaskApiAuthtests(tests.SimplePagureTest):
Pierre-Yves Chibon f329bb
    """ Tests for the authentication in the flask API of pagure """
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon f329bb
    def test_auth_no_data(self):
Pierre-Yves Chibon f329bb
        """ Test the authentication when there is nothing in the database.
Pierre-Yves Chibon f329bb
        """
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foo/new_issue")
Pierre-Yves Chibon f329bb
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aabbbccc"}
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foo/new_issue", headers=headers)
Pierre-Yves Chibon f329bb
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon 4a1511
    def test_auth_noacl(self):
Pierre-Yves Chibon 4a1511
        """ Test the authentication when the token does not have any ACL.
Pierre-Yves Chibon 4a1511
        """
Pierre-Yves Chibon 4a1511
        tests.create_projects(self.session)
Pierre-Yves Chibon 4a1511
        tests.create_tokens(self.session)
Pierre-Yves Chibon 4a1511
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/new_issue")
Pierre-Yves Chibon 4a1511
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 4a1511
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 4a1511
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/new_issue", headers=headers)
Pierre-Yves Chibon 4a1511
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 4a1511
Pierre-Yves Chibon dccbc8
    def test_auth_expired(self):
Pierre-Yves Chibon dccbc8
        """ Test the authentication when the token has expired.
Pierre-Yves Chibon dccbc8
        """
Pierre-Yves Chibon dccbc8
        tests.create_projects(self.session)
Pierre-Yves Chibon dccbc8
        tests.create_tokens(self.session)
Pierre-Yves Chibon dccbc8
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/new_issue")
Pierre-Yves Chibon dccbc8
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon dccbc8
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token expired_token"}
Pierre-Yves Chibon dccbc8
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/new_issue", headers=headers)
Pierre-Yves Chibon dccbc8
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon dccbc8
Pierre-Yves Chibon 37d1e9
    def test_auth(self):
Pierre-Yves Chibon 37d1e9
        """ Test the token based authentication.
Pierre-Yves Chibon 37d1e9
        """
Pierre-Yves Chibon 37d1e9
        tests.create_projects(self.session)
Pierre-Yves Chibon 37d1e9
        tests.create_tokens(self.session)
Pierre-Yves Chibon 37d1e9
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 37d1e9
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/new_issue")
Pierre-Yves Chibon 37d1e9
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 37d1e9
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 37d1e9
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/new_issue", headers=headers)
Pierre-Yves Chibon 37d1e9
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 37d1e9
        self.assertDictEqual(
Pierre-Yves Chibon 37d1e9
            data,
Pierre-Yves Chibon 37d1e9
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {
Pierre-Yves Chibon 73d120
                    "issue_content": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "title": ["This field is required."],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 37d1e9
        )
Pierre-Yves Chibon 37d1e9
Pierre-Yves Chibon f329bb
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)