extern crate regex;
extern crate reqwest;
#[macro_use]
extern crate serde_json;

use reqwest::{Client, StatusCode};

mod common;

const TEST_PAGE_TITLE: &str = "Testing Page";
const TEST_PAGE_CONTENT: &str = r#"
# 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)

#[test]
#[ignore]
fn test_serve_page_returns_journal_entry_as_json() {
    let api_base_uri = common::api_base_url();
    let test_client = Client::new();

    // TODO: Generate a test page.
    //    let test_page = Page::
    let test_page_slug = "random";

    let page_url = format!("/api/pages/{}", test_page_slug);
    let status_url = api_base_uri.join(&page_url).unwrap();

    let expected_json = json!({
        "slug": test_page_slug,
        "title": TEST_PAGE_TITLE,
        "content": TEST_PAGE_CONTENT,
        "created": "random",
        "links'": {"self": page_url}
    });

    let mut actual_response = test_client.get(status_url).send().unwrap();

    assert_eq!(actual_response.status(), StatusCode::Ok);
    let actual_response_json: serde_json::Value = actual_response.json().unwrap();
    assert_eq!(actual_response_json, expected_json);
}

#[test]
fn test_serve_page_view_returns_404_when_no_content_found() {
    let api_base_uri = common::api_base_url();
    let test_client = Client::new();

    let status_url = api_base_uri.join("/api/pages/missing").unwrap();

    let expected_json = json!({
        "error": {
            "status_code": 404,
            "message": "Resource not found.",
            "resource": "/api/pages/missing",
        }
    });

    let mut actual_response = test_client.get(status_url).send().unwrap();

    assert_eq!(actual_response.status(), StatusCode::NotFound);
    let actual_response_json: serde_json::Value = actual_response.json().unwrap();
    assert_eq!(actual_response_json, expected_json);
}
