"""
Unit tests for testing serving of page content.

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

import http
import pathlib

import pytest

import rookeries
from tests import utils


@pytest.fixture
def flask_test_app():
    return rookeries.make_rookeries_app().test_client()


@pytest.fixture
def expected_json_content():
    with pathlib.Path('docs/about.md').open('r', encoding='UTF-8') as markdown_data:
        content = markdown_data.read()
    return content


def test_serve_page_returns_journal_entry_as_json(flask_test_app, expected_json_content):
    expected_info = {
        'slug': 'about',
        'title': 'About Rookeries',
        'content': expected_json_content,
    }

    actual = flask_test_app.get('/api/pages/about')

    assert actual.status_code == http.HTTPStatus.OK
    assert utils.convert_response_into_json(actual) == expected_info


def test_serve_landing_page_view_returns_about_page(flask_test_app, expected_json_content):
    expected_info = {
        'slug': 'about',
        'title': 'About Rookeries',
        'content': expected_json_content,
    }

    actual = flask_test_app.get('/api/pages')

    assert actual.status_code == http.HTTPStatus.OK
    assert utils.convert_response_into_json(actual) == expected_info


def test_serve_page_view_returns_404_when_no_content_found(flask_test_app):
    expect_json = {
        'error': {
            'code': 404,
            'message': 'Not Found'
        }
    }

    actual = flask_test_app.get('/api/pages/missing')

    assert actual.status_code == http.HTTPStatus.NOT_FOUND
    assert utils.convert_response_into_json(actual) == expect_json
