Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
n-dusan committed Dec 1, 2023
1 parent 2a76dab commit 299a30a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 52 deletions.
72 changes: 38 additions & 34 deletions tests/api/basic_archive_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::archive_testtools::{ArchiveType, Jurisdiction};
use crate::common;
use crate::common::ArchiveType;
use actix_web::test;
use actix_web::web::Bytes;
use serde_json::Value;

#[actix_web::test]
async fn test_resolve_law_html_request_with_full_path_expect_success() {
let archive_path = common::initialize_archive(ArchiveType::Basic).unwrap();
let archive_path =
common::initialize_archive(ArchiveType::Basic(Jurisdiction::Single)).unwrap();
let app = common::initialize_app(archive_path.path()).await;
let req = test::TestRequest::get().uri("/a/b/c.html").to_request();
let resp = test::call_service(&app, req).await;
Expand All @@ -15,37 +16,40 @@ async fn test_resolve_law_html_request_with_full_path_expect_success() {
assert_eq!(actual, expected);
}

#[actix_web::test]
async fn test_resolve_law_html_request_with_empty_path_expect_success() {
let archive_path = common::initialize_archive(ArchiveType::Basic).unwrap();
let app = common::initialize_app(archive_path.path()).await;
let req = test::TestRequest::get().uri("/").to_request();
let resp = test::call_service(&app, req).await;
let actual = resp.status().is_success();
let expected = true;
assert_eq!(actual, expected);
}
// #[actix_web::test]
// async fn test_resolve_law_html_request_with_empty_path_expect_success() {
// let archive_path =
// common::initialize_archive(ArchiveType::Basic(Jurisdiction::Single)).unwrap();
// let app = common::initialize_app(archive_path.path()).await;
// let req = test::TestRequest::get().uri("/").to_request();
// let resp = test::call_service(&app, req).await;
// let actual = resp.status().is_success();
// let expected = true;
// assert_eq!(actual, expected);
// }

#[actix_web::test]
async fn test_resolve_request_with_incorrect_path_expect_client_error() {
let archive_path = common::initialize_archive(ArchiveType::Basic).unwrap();
let app = common::initialize_app(archive_path.path()).await;
let req = test::TestRequest::get().uri("/a/b/x").to_request();
let resp = test::call_service(&app, req).await;
let actual = resp.status().is_client_error();
let expected = true;
assert_eq!(actual, expected);
}
// #[actix_web::test]
// async fn test_resolve_request_with_incorrect_path_expect_client_error() {
// let archive_path =
// common::initialize_archive(ArchiveType::Basic(Jurisdiction::Single)).unwrap();
// let app = common::initialize_app(archive_path.path()).await;
// let req = test::TestRequest::get().uri("/a/b/x").to_request();
// let resp = test::call_service(&app, req).await;
// let actual = resp.status().is_client_error();
// let expected = true;
// assert_eq!(actual, expected);
// }

#[actix_web::test]
async fn test_law_html_request_content_expect_html_document_retrieved() {
let archive_path = common::initialize_archive(ArchiveType::Basic).unwrap();
let app = common::initialize_app(archive_path.path()).await;
let req = test::TestRequest::get().uri("/a/b/c.html").to_request();
let actual = test::call_and_read_body(&app, req).await;
let expected = "<!DOCTYPE html>";
assert!(
common::blob_to_string(actual.to_vec()).starts_with(expected),
"doesn't start with {expected}"
);
}
// #[actix_web::test]
// async fn test_law_html_request_content_expect_html_document_retrieved() {
// let archive_path =
// common::initialize_archive(ArchiveType::Basic(Jurisdiction::Single)).unwrap();
// let app = common::initialize_app(archive_path.path()).await;
// let req = test::TestRequest::get().uri("/a/b/c.html").to_request();
// let actual = test::call_and_read_body(&app, req).await;
// let expected = "<!DOCTYPE html>";
// assert!(
// common::blob_to_string(actual.to_vec()).starts_with(expected),
// "doesn't start with {expected}"
// );
// }
20 changes: 20 additions & 0 deletions tests/archive_testtools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ use lazy_static::lazy_static;
use std::path::{Path, PathBuf};
use stelae::utils::paths::fix_unc_path;

pub enum Jurisdiction {
Single,
Multi,
}

pub enum ArchiveType {
Basic(Jurisdiction),
Multihost(Jurisdiction),
}

type Route = String;

pub enum DataRepository {
Html(String, Vec<Route>),
Rdf(String, Vec<Route>),
Xml(String, Vec<Route>),
Pdf(String, Vec<Route>),
Other(String, Vec<Route>),
}

lazy_static! {
static ref SCRIPT_PATH: PathBuf = {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down
105 changes: 87 additions & 18 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::archive_testtools;
use crate::archive_testtools::{self, ArchiveType, DataRepository, Jurisdiction};
use actix_http::Request;
use actix_service::Service;
use actix_web::{
Expand All @@ -7,10 +7,11 @@ use actix_web::{
Error,
};
use anyhow::Result;
use std::path::{Path, PathBuf};
use git2;
use std::{path::{Path, PathBuf}, fs::create_dir_all};
use std::sync::Once;
use tempfile::{Builder, TempDir};

use tracing_subscriber::registry::Data;
static INIT: Once = Once::new();

use actix_http::body::MessageBody;
Expand All @@ -20,12 +21,6 @@ use stelae::stelae::archive::{self, Archive};

pub const BASIC_MODULE_NAME: &str = "basic";

pub enum ArchiveType {
Basic,
Multijurisdiction,
Multihost,
}

pub fn blob_to_string(blob: Vec<u8>) -> String {
core::str::from_utf8(blob.as_slice()).unwrap().into()
}
Expand Down Expand Up @@ -71,26 +66,36 @@ pub fn initialize_archive(archive_type: ArchiveType) -> Result<tempfile::TempDir

fn initialize_archive_inner(archive_type: ArchiveType, td: &TempDir) -> Result<()> {
match archive_type {
ArchiveType::Basic => initialize_archive_basic(td),
ArchiveType::Multijurisdiction => initialize_archive_multijurisdiction(td),
ArchiveType::Multihost => initialize_archive_multihost(td),
ArchiveType::Basic(Jurisdiction::Single) => initialize_archive_basic(td),
ArchiveType::Basic(Jurisdiction::Multi) => initialize_archive_multijurisdiction(td),
ArchiveType::Multihost(_) => initialize_archive_multihost(td),
}
}

fn initialize_archive_basic(td: &TempDir) -> Result<()> {
let mut path = td.path().to_owned();
path.push("test");
std::fs::create_dir_all(&path).unwrap();
let org_name = "test_org";

archive::init(
td.path().to_owned(),
"law".into(),
"test".into(),
org_name.into(),
None,
false,
);
let law = make_repository("make-law-repo.sh", &path).unwrap();
let law_html = make_repository("make-law-html-repo.sh", &path).unwrap();
let stele = initialize_stele(
td.path().to_path_buf(),
org_name,
vec![
// DataRepository::Html("law-html".into()),
// DataRepository::Rdf("law-rdf".into()),
// DataRepository::Xml("law-xml".into()),
// DataRepository::Pdf("law-docs".into()),
],
)
.unwrap();
// let law = make_repository("make-law-repo.sh", &path).unwrap();
// let law_html = make_repository("make-law-html-repo.sh", &path).unwrap();
anyhow::bail!("Something went wrong!");
Ok(())
}

Expand All @@ -102,6 +107,70 @@ fn initialize_archive_multihost(td: &TempDir) -> Result<()> {
unimplemented!()
}

pub fn initialize_stele(
path: PathBuf,
org_name: &str,
data_repositories: Vec<DataRepository>,
) -> Result<()> {
init_auth_repository(&path, org_name)?;

// for repository in data_repositories {
// init_data_repository(&path, repository)?;
// }
// archive_testtools::execute_script("make-stele.sh", path)?;
Ok(())
}

pub fn init_auth_repository(path: &Path, org_name: &str) -> Result<git2::Repository> {
let mut path = path.to_path_buf();
path.push(format!("{org_name}/law"));
std::fs::create_dir_all(&path).unwrap();

let repo = git2::Repository::init(&path).unwrap();
add_repositories_json(&repo, &path)?;
Ok(repo)
}

fn add_repositories_json(repo: &git2::Repository, path: &Path) -> Result<()> {
let mut index = repo.index().unwrap();
let mut path = path.to_path_buf();
path.push("targets/repositories.json");
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let path_str = "targets/repositories.json";
let content = r#"{
"law": {
"custom": {
"type": "data",
"allow-unauthenticated-commits": true,
"serve": "law",
"routes": [
"law/{path:.*}"
],
"serve-prefix": "law",
"is-fallback": true
}
}
}"#;
std::fs::write(path, content).unwrap();
index.add_path(&PathBuf::from(path_str)).unwrap();
index.write().unwrap();
let tree_id = index.write_tree().unwrap();
let tree = repo.find_tree(tree_id).unwrap();
let sig = repo.signature().unwrap();
let parent_commit = repo
.find_commit(repo.head().unwrap().target().unwrap())
.unwrap();
repo.commit(
Some("HEAD"),
&sig,
&sig,
"Add repositories.json",
&tree,
&[&parent_commit],
)
.unwrap();
Ok(())
}
pub fn make_repository(script_name: &str, path: &Path) -> Result<()> {
archive_testtools::execute_script(script_name, path.canonicalize()?)?;
// TODO: return repository
Expand Down

0 comments on commit 299a30a

Please sign in to comment.