Skip to content

Commit

Permalink
feat: fix basic actix api tests in an archive
Browse files Browse the repository at this point in the history
  • Loading branch information
n-dusan committed Dec 14, 2023
1 parent 6aa37da commit 7d6b6d2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 61 deletions.
60 changes: 32 additions & 28 deletions tests/api/basic_archive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,16 @@ async fn test_law_html_request_content_expect_html_document_retrieved() {
}
}

#[actix_web::test]
async fn test_resolve_law_xml_request_without_serve_prefix_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/c.xml").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_xml_request_content_expect_xml_document_retrieved() {
let archive_path =
common::initialize_archive(ArchiveType::Basic(Jurisdiction::Single)).unwrap();
let app = common::initialize_app(archive_path.path()).await;
for request_uri in &[
"/_xml/a/b/c.xml",
"/_xml/a/b/index.xml",
"/_xml/a/b/c/index.xml",
"/_xml/a/d/index.xml",
"/_xml/a/b/c.xml",
"/_xml/a/b/c/index.xml",
] {
let req = test::TestRequest::get().uri(request_uri).to_request();
let actual = test::call_and_read_body(&app, req).await;
Expand All @@ -90,16 +78,28 @@ async fn test_law_xml_request_content_expect_xml_document_retrieved() {
}
}

#[actix_web::test]
async fn test_resolve_law_xml_request_without_serve_prefix_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/c.xml").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_rdf_request_content_expect_rdf_document_retrieved() {
let archive_path =
common::initialize_archive(ArchiveType::Basic(Jurisdiction::Single)).unwrap();
let app = common::initialize_app(archive_path.path()).await;
for request_uri in &[
"/_rdf/a/b/c.xml",
"/_rdf/a/b/index.xml",
"/_rdf/a/b/c/index.xml",
"/_rdf/a/d/index.xml",
"/_rdf/a/b/c.rdf",
"/_rdf/a/b/index.rdf",
"/_rdf/a/d/index.rdf",
"/_rdf/a/b/c/index.rdf",
] {
let req = test::TestRequest::get().uri(request_uri).to_request();
let actual = test::call_and_read_body(&app, req).await;
Expand All @@ -112,7 +112,7 @@ async fn test_law_rdf_request_content_expect_rdf_document_retrieved() {
}

#[actix_web::test]
async fn test_law_other_data_fallback_request_content_expect_other_document_retrieved() {
async fn test_law_other_data_fallback_request_content_expect_document_retrieved() {
let archive_path =
common::initialize_archive(ArchiveType::Basic(Jurisdiction::Single)).unwrap();
let app = common::initialize_app(archive_path.path()).await;
Expand All @@ -130,13 +130,17 @@ async fn test_law_other_data_request_content_expect_other_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("/scope/_document/e/f/example.json")
.to_request();
let actual = test::call_and_read_body(&app, req).await;
let expected = "{ \"retrieved\": {\"json\": { \"key\": \"value\" } } }";
assert!(
common::blob_to_string(actual.to_vec()).starts_with(expected),
"doesn't start with {expected}"
);
for request_uri in &[
"/_prefix/a/index.html",
"/a/_doc/e/index.html",
"/a/e/_doc/f/index.html",
] {
let req = test::TestRequest::get().uri(request_uri).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}"
);
}
}
17 changes: 10 additions & 7 deletions tests/archive_testtools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use anyhow::Result;
use git2::{Commit, Error, Oid};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::{default, fmt};
use stelae::stelae::types::repositories::{Custom, Repositories, Repository};
use stelae::utils::paths::fix_unc_path;
pub use stelae::stelae::types::repositories::{Custom, Repositories, Repository};

pub enum ArchiveType {
Basic(Jurisdiction),
Expand Down Expand Up @@ -103,8 +101,9 @@ pub fn get_basic_test_data_repositories() -> Result<Vec<TestDataRepositoryContex
"./index.html",
"./a/index.html",
"./a/b/index.html",
"./a/b/c.html",
"./a/d/index.html",
"./a/b/c.html",
"./a/b/c/index.html",
],
TestDataRepositoryType::Html,
None,
Expand All @@ -118,6 +117,8 @@ pub fn get_basic_test_data_repositories() -> Result<Vec<TestDataRepositoryContex
"./a/index.rdf",
"./a/b/index.rdf",
"./a/d/index.rdf",
"./a/b/c.rdf",
"./a/b/c/index.rdf",
],
TestDataRepositoryType::Rdf,
Some("_rdf"),
Expand All @@ -130,6 +131,8 @@ pub fn get_basic_test_data_repositories() -> Result<Vec<TestDataRepositoryContex
"./index.xml",
"./a/index.xml",
"./a/b/index.xml",
"./a/b/c.xml",
"./a/b/c/index.xml",
"./a/d/index.xml",
],
TestDataRepositoryType::Xml,
Expand Down Expand Up @@ -180,8 +183,8 @@ pub fn get_basic_test_data_repositories() -> Result<Vec<TestDataRepositoryContex
])
}

impl From<TestDataRepositoryContext<'_>> for Repository {
fn from(context: TestDataRepositoryContext) -> Self {
impl From<&TestDataRepositoryContext<'_>> for Repository {
fn from(context: &TestDataRepositoryContext) -> Self {
let mut custom = Custom::default();
custom.repository_type = Some(match context.kind {
TestDataRepositoryType::Html => "html".to_string(),
Expand All @@ -194,6 +197,7 @@ impl From<TestDataRepositoryContext<'_>> for Repository {
custom.scope = context.serve_prefix.map(|s| s.to_string());
custom.routes = context
.route_glob_patterns
.as_ref()
.map(|r| r.iter().map(|s| s.to_string()).collect());
custom.is_fallback = Some(context.is_fallback);
Self {
Expand Down Expand Up @@ -241,7 +245,6 @@ impl GitRepository {
.unwrap_or_default();
let parent_commits: Vec<&Commit> = binding.iter().collect();

dbg!(&parent_commits);
self.repo
.commit(Some("HEAD"), &sig, &sig, commit_msg, &tree, &parent_commits)
}
Expand Down
47 changes: 21 additions & 26 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::archive_testtools::{
self, get_basic_test_data_repositories, ArchiveType, GitRepository, Jurisdiction,
TestDataRepositoryContext, TestDataRepositoryType,
copy_file, get_basic_test_data_repositories, get_default_static_filename, ArchiveType,
GitRepository, Jurisdiction, Repositories, Repository, TestDataRepositoryContext,
};
use actix_http::Request;
use actix_service::Service;
Expand All @@ -10,22 +10,15 @@ use actix_web::{
Error,
};
use anyhow::Result;
use git2::{Commit, Repository};
use std::path::{Path, PathBuf};
use std::sync::Once;
use std::{
fs::create_dir_all,
path::{Path, PathBuf},
};
use tempfile::{Builder, TempDir};
static INIT: Once = Once::new();

use actix_http::body::MessageBody;

use stelae::server::publish::{init_app, init_shared_app_state, AppState};
use stelae::stelae::archive::{self, Archive};
use stelae::{
server::publish::{init_app, init_shared_app_state, AppState},
stelae::types::repositories::Repositories,
};

pub const BASIC_MODULE_NAME: &str = "basic";

Expand All @@ -38,11 +31,12 @@ pub fn blob_to_string(blob: Vec<u8>) -> String {
// then we can manually inspect the state of the test environment

// to manually inspect state of test environment at present,
// we use anyhow::bail!() which aborts the entire test suite, which is not ideal.
// we use anyhow::bail!() which aborts the entire test suite.

pub async fn initialize_app(
archive_path: &Path,
) -> impl Service<Request, Response = ServiceResponse<impl MessageBody>, Error = Error> {
// dbg!(&archive_path);
let archive = Archive::parse(archive_path.to_path_buf(), archive_path, false).unwrap();
let state = AppState { archive };
let root = state.archive.get_root().unwrap();
Expand Down Expand Up @@ -134,20 +128,21 @@ pub fn init_auth_repository(

path.push("targets");

let content = r#"{
"repositories": {
"test_org/law-html": {
"custom": {
"type": "html",
"serve": "historical",
"location_regex": "/",
"routes": [".*"]
}
}
}
}"#;

repo.add_file(&path, "repositories.json", content).unwrap();
let repositories: Repositories =
data_repositories
.iter()
.fold(Repositories::default(), |mut repositories, data_repo| {
let mut repository = Repository::from(data_repo);
repository.name = format!("{}/{}", org_name, repository.name);
repositories
.repositories
.entry(repository.name.clone())
.or_insert(repository);
repositories
});
let content = serde_json::to_string_pretty(&repositories).unwrap();

repo.add_file(&path, "repositories.json", &content).unwrap();
repo.commit(Some("targets/repositories.json"), "Add repositories.json")
.unwrap();
Ok(repo)
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/static_files/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "retrieved": {"json": { "key": "value" } } }

0 comments on commit 7d6b6d2

Please sign in to comment.