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

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

import http
from unittest import mock

import requests
import pytest

from tests import utils

TEST_PAGE_TITLE = 'Testing Page'
TEST_PAGE_CONTENT = '''
# Hello World
I'm a test page.
'''


@pytest.fixture(scope='module')
def test_page(db):
    return utils.generate_test_page(
        db, title=TEST_PAGE_TITLE, content=TEST_PAGE_CONTENT)


# TODO: Change these tests to be bdd style functional tests.
def test_serve_page_returns_journal_entry_as_json(api_base_uri, test_page):
    expected_info = {
        'slug': test_page.slug,
        'title': TEST_PAGE_TITLE,
        'content': TEST_PAGE_CONTENT,
        'created': mock.ANY,
        'links': {'self': f'/api/pages/{test_page.slug}'}
    }

    actual = requests.get(f'{api_base_uri}/api/pages/{test_page.slug}')

    assert actual.status_code == http.HTTPStatus.OK
    assert actual.json() == expected_info


def test_serve_page_view_returns_404_when_no_content_found(api_base_uri):
    expected_json = {
        'error': {
            'status_code': 404,
            'message': 'Resource not found.',
            'resource': '/api/pages/missing',
        }
    }

    actual = requests.get(f'{api_base_uri}/api/pages/missing')

    assert actual.status_code == http.HTTPStatus.NOT_FOUND
    assert actual.json() == expected_json
