Repository: Netflix/consoleme
File: tests/handlers/v2/test_requests.py (Python)

Code before:
import ujson as json
from mock import patch
from tornado.testing import AsyncHTTPTestCase
from consoleme.config import config

class TestRequestsHandler(AsyncHTTPTestCase):
    def get_app(self):
        from consoleme.routes import make_app

        return make_app(jwt_validator=lambda x: {})
    
    def test_get(self):
        headers = {
            config.get("auth.user_header_name"): "user@github.com",
            config.get("auth.groups_header_name"): "groupa,groupb,groupc",
        }
        response = self.fetch("/api/v2/requests", method="GET", headers=headers)
        self.assertEqual(response.code, 200)
        self.assertIn(b"501: OK", response.body)

    def test_post(self):
        headers = {
            config.get("auth.user_header_name"): "user@github.com",
            config.get("auth.groups_header_name"): "groupa,groupb,groupc",
        }
        response = self.fetch( "/api/v2/requests", method="POST", headers=headers, body="{}")
        self.assertEqual(response.code, 200)
        self.assertIn(b"501: OK", response.body)


class TestRequestDetailHandler(AsyncHTTPTestCase):
    def get_app(self):
        from consoleme.routes import make_app

        return make_app(jwt_validator=lambda x: {})

    def test_get(self):
        headers = {
            config.get("auth.user_header_name"): "user@github.com",
            config.get("auth.groups_header_name"): "groupa,groupb,groupc",
        }
        response = self.fetch(
            "/api/v2/requests/16fd2706-8baf-433b-82eb-8c7fada847da",

Review comment (suggestion):
These endpoints should be returning a `501 Not Implemented` status until they're implemented. It looks like this changed because `write_error()` is implemented differently for `BaseAPIHandler` than it is for `BaseJSONHandler`. We don't have to keep the same logic, but it would be nice to have an easy and consistent way to return HTTP errors.

Code after:
import ujson as json
from mock import patch
from tornado.testing import AsyncHTTPTestCase
from consoleme.config import config

class TestRequestsHandler(AsyncHTTPTestCase):
    def get_app(self):
        from consoleme.routes import make_app

        return make_app(jwt_validator=lambda x: {})
    
    def test_get(self):
        expected = {
            "status": 501,
            "title": "Not Implemented",
            "message": "Get requests",
        }
        headers = {
            config.get("auth.user_header_name"): "user@github.com",
            config.get("auth.groups_header_name"): "groupa,groupb,groupc",
        }
        response = self.fetch("/api/v2/requests", method="GET", headers=headers)
        self.assertEqual(response.code, 501)
        self.assertDictEqual(json.loads(response.body), expected)

    def test_post(self):
        expected = {
            "status": 501,
            "title": "Not Implemented",
            "message": "Create request",
        }
        headers = {
            config.get("auth.user_header_name"): "user@github.com",
            config.get("auth.groups_header_name"): "groupa,groupb,groupc",
        }
        response = self.fetch("/api/v2/requests", method="POST", headers=headers, body="{}")
        self.assertEqual(response.code, 501)
        self.assertDictEqual(json.loads(response.body), expected)


class TestRequestDetailHandler(AsyncHTTPTestCase):
    def get_app(self):
        from consoleme.routes import make_app