"""
Unit tests for the missing api endpoint view.

:copyright: Copyright 2013-2017, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""

import http

import requests


def assert_method_not_allowed_response(actual_response):
    expected_response_json = {
        'error': {
            'status_code': http.HTTPStatus.METHOD_NOT_ALLOWED.value,
            'message': 'Specified method is invalid for this resource',
        },
    }
    assert actual_response.status_code == http.HTTPStatus.METHOD_NOT_ALLOWED
    assert actual_response.json() == expected_response_json


def test_calling_get_on_missing_api_endpoint_returns_404(api_base_uri):
    missing_api_url = f'{api_base_uri}/api/missing/resource'
    expected_response_json = {
        'error': {
            'status_code': http.HTTPStatus.NOT_FOUND.value,
            'message': 'Resource not found.',
            'resource': '/api/missing/resource',
        },
    }

    actual_response = requests.get(missing_api_url)

    assert actual_response.status_code == http.HTTPStatus.NOT_FOUND
    assert actual_response.json() == expected_response_json


def test_calling_post_on_missing_api_endpoint_returns_405(api_base_uri):
    missing_api_url = f'{api_base_uri}/api/missing/resource'
    actual_response = requests.post(missing_api_url, json={'invalid': 'data'})
    assert_method_not_allowed_response(actual_response)


def test_calling_put_on_missing_api_endpoint_returns_405(api_base_uri):
    missing_api_url = f'{api_base_uri}/api/missing/resource'
    actual_response = requests.put(missing_api_url, json={'invalid': 'data'})
    assert_method_not_allowed_response(actual_response)


def test_calling_delete_on_missing_api_endpoint_returns_405(api_base_uri):
    missing_api_url = f'{api_base_uri}/api/missing/resource'
    actual_response = requests.delete(missing_api_url)
    assert_method_not_allowed_response(actual_response)
