Start crafting site tests.

This commit is contained in:
Dorian 2018-08-13 17:54:08 -04:00
parent c4e07dafe7
commit c2d03bc216
2 changed files with 227 additions and 6 deletions

View File

@ -109,19 +109,36 @@ pub struct Site {
#[serde(rename = "type", skip_serializing_if = "Option::is_none")] #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
type_: Option<String>, type_: Option<String>,
name: String, pub name: String,
#[serde(rename = "landingPage")] #[serde(rename = "landingPage")]
landing_page: String, pub landing_page: String,
header: String, pub header: String,
footer: String, pub footer: String,
menu: Vec<MenuItems>, pub menu: Vec<MenuItems>,
config: HashMap<String, Value>, pub config: HashMap<String, Value>,
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
links: HashMap<String, Value>, links: HashMap<String, Value>,
} }
impl Default for Site {
fn default() -> Site {
Site {
id: None,
revision: None,
type_: Some(String::from("site")),
name: Default::default(),
landing_page: Default::default(),
header: Default::default(),
footer: Default::default(),
menu: Default::default(),
config: Default::default(),
links: HashMap::new(),
}
}
}
pub trait RookeriesDomain { pub trait RookeriesDomain {
fn id(&self) -> Option<String>; fn id(&self) -> Option<String>;
fn remove_couchdb_details(&self) -> Self; fn remove_couchdb_details(&self) -> Self;

View File

@ -0,0 +1,204 @@
// TODO: Rework site management and testing.
// TODO: Rework modification to only allow changes to layout and children...
// TODO: Add a test if the site does not exist for some reason.
extern crate reqwest;
extern crate rookeries;
#[macro_use]
extern crate serde_json;
use reqwest::{Client, Response, StatusCode};
use rookeries::couchdb::Server;
use rookeries::Site;
use serde_json::Value;
use std::collections::HashMap;
use std::str::FromStr;
mod common;
fn auth_token_headers() -> String {
let (user, password) = common::valid_user();
let api_base_uri = common::api_base_url();
let test_client = Client::new();
let auth_url = api_base_uri.join("/auth").unwrap();
let credentials = json!({"username": user.username, "password": password});
let mut auth_response = test_client
.post(auth_url)
.json(&credentials)
.send()
.unwrap();
// TODO: Improve setup of test site.
let token = auth_response.json::<Value>().unwrap()["access_token"]
.as_str()
.unwrap()
.to_string();
format!("JWT {}", token)
}
fn test_site() -> Site {
let mut test_config = HashMap::new();
test_config.insert(
String::from("tagline"),
Value::from_str("Simple Site Scaffolding").unwrap(),
);
test_config.insert(
String::from("favicon"),
Value::from_str("/static/images/mr-penguin-amber-favicon.ico").unwrap(),
);
test_config.insert(
String::from("logo"),
Value::from_str("/static/images/mr-penguin-amber.svg").unwrap(),
);
test_config.insert(
String::from("footer"),
Value::from_str("Footer here.").unwrap(),
);
let mut test_site = Site::default();
test_site.name = String::from("Test Site");
test_site.menu = vec![];
test_site.config = test_config;
test_site.landing_page = String::from("/blah");
let couchdb = Server::configure_from_env().unwrap();
couchdb.update(test_site.clone()).unwrap();
test_site
}
#[test]
#[ignore]
fn test_visitor_can_access_site() {
test_site();
let api_base_uri = common::api_base_url();
// response = requests.get(
// url=f'{api_base_uri}/api/site',
// headers={},
// )
// assert_site_response(response)
}
#[test]
#[ignore]
fn test_authenticated_user_can_access_site() {
// auth_token_headers, api_base_uri, test_site):
// response = requests.get(
// url=f'{api_base_uri}/api/site',
// headers=auth_token_headers,
// )
// assert_site_response(response)
}
#[test]
#[ignore]
fn test_authenticated_user_cannot_modify_site_using_bad_request() {
// auth_token_headers, api_base_uri, test_site):
// response = requests.put(
// url=f'{api_base_uri}/api/site',
// data='',
// headers=auth_token_headers,
// )
// assert_bad_request_response(response)
}
#[test]
#[ignore]
fn test_authenticated_user_cannot_modify_site_using_empty_request() {
// auth_token_headers, api_base_uri, test_site):
// response = requests.put(
// url=f'{api_base_uri}/api/site',
// json={},
// headers=auth_token_headers,
// )
// assert_bad_request_response(response)
}
#[test]
#[ignore]
fn test_authenticated_user_can_modify_site_using_proper_request() {
// auth_token_headers, api_base_uri, test_site, db):
//
// original_site = get_site_request(auth_token_headers, api_base_uri)
// modify_site = copy.deepcopy(original_site)
// modify_site['name'] = 'Modified Test Site'
//
// response = requests.put(
// url=f'{api_base_uri}/api/site',
// json=modify_site,
// headers=auth_token_headers,
// )
// assert_updated_site_response(test_site, response, db)
}
//def get_site_request(auth_token_headers, api_base_uri):
// return requests.get(
// url=f'{api_base_uri}/api/site',
// headers=auth_token_headers,
// ).json()
//
//
//def assert_site_response(response):
// assert response.status_code == http.HTTPStatus.OK
//
// expected_json = {
// 'name': 'Test Site',
// 'links': {'self': '/api/site'},
// 'config': mock.ANY,
// 'landingPage': mock.ANY,
// 'menu': mock.ANY,
// 'header': '',
// 'footer': '',
// }
// assert expected_json == response.json()
//
//
//def assert_unauthorized_response(expected_url, response):
// expected_response_json = {
// 'error': {
// 'status_code': http.HTTPStatus.UNAUTHORIZED.value,
// 'message': 'Not authorized to access this resource.',
// 'resource': expected_url,
// }
// }
//
// assert response.status_code == http.HTTPStatus.UNAUTHORIZED
// assert response.json() == expected_response_json
//
//
//def assert_bad_request_response(response):
// expected_response_json = {
// 'error': {
// 'status_code': http.HTTPStatus.BAD_REQUEST.value,
// 'message': mock.ANY,
// }
// }
//
// assert response.json() == expected_response_json
// assert response.status_code == http.HTTPStatus.BAD_REQUEST
//
//
//def assert_updated_site_response(test_site, api_response, db):
// actual_response = utils.extract_response(api_response)
// assert actual_response.status_code == http.HTTPStatus.OK
//
// expected_json = test_site.to_api_json()
// expected_json['name'] = 'Modified Test Site'
// assert expected_json == actual_response.json()
// assert '/api/site' == actual_response.json()['links']['self']
//
// # TODO: Remember to reset the state of the site to its original here.
// updated_site = models.Site.find_in_db(db, {})
// updated_site.name = test_site.name
// updated_site.config = test_site.config
// updated_site.menu = test_site.menu
// updated_site.landing_page = test_site.landing_page
// db.create_or_update_document(
// updated_site.to_db_json(), updated_site._id, updated_site._rev)