Skip to content

Commit

Permalink
working on assets testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Sep 25, 2024
1 parent c6a99bf commit 7533263
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 128 deletions.
143 changes: 34 additions & 109 deletions cli/src/commands/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ use crate::{
error::{DaikokuCliError, DaikokuResult},
logger,
},
utils::{absolute_path, frame_to_bytes_body},
utils::absolute_path,
AssetsCommands,
};

use async_recursion::async_recursion;
use bytes::Bytes;

use http_body_util::{Empty, Full};
use hyper::{header, Request};
use hyper_util::rt::TokioIo;
use hyper::header;
use serde::{Deserialize, Serialize};
use tokio::net::TcpStream;
use walkdir::WalkDir;

use super::{
Expand Down Expand Up @@ -77,7 +73,7 @@ async fn exists(filename: String) -> DaikokuResult<()> {
let apikey = read_apikey_from_secrets(true)?;

let url: String = format!(
"{}/cms-api/tenant-assets/{}",
"{}/tenant-assets/{}",
environment.server,
slug::slugify(filename.clone()),
);
Expand Down Expand Up @@ -144,7 +140,9 @@ async fn add(
.read_to_end(&mut contents)
.map_err(|err| DaikokuCliError::FileSystem(err.to_string()));

let _ = daikoku_cms_api_post(&url, Bytes::from(contents)).await?;
let _ = daikoku_cms_api_post(&url, Bytes::from(contents), false).await?;

logger::success("New asset has been pushed".to_string());

Ok(())
}
Expand All @@ -167,40 +165,15 @@ async fn remove(filename: String, path: Option<String>, slug: Option<String>) ->
slug.unwrap_or(slug::slugify(filename.clone()))
);

let req = Request::delete(&url)
.header(header::HOST, &host)
let req = reqwest::Client::new()
.delete(url)
.header(header::HOST, host)
.header(header::AUTHORIZATION, format!("Basic {}", apikey))
.body(Empty::<Bytes>::new())
.expect("failed to build a request");

let stream = TcpStream::connect(&host).await.map_err(|err| {
DaikokuCliError::DaikokuErrorWithMessage("failed to join the server".to_string(), err)
})?;
let io = TokioIo::new(stream);

let (mut sender, conn) = hyper::client::conn::http1::handshake(io)
.await
.map_err(|err| DaikokuCliError::HyperError(err))?;

tokio::task::spawn(async move {
if let Err(err) = conn.await {
logger::error(format!("Connection error {:?}", err));
}
});

let upstream_resp = sender
.send_request(req)
.send()
.await
.map_err(|err| DaikokuCliError::ParsingError(err.to_string()))?;

let (
hyper::http::response::Parts {
headers: _, status, ..
},
_body,
) = upstream_resp.into_parts();
.map_err(|err| DaikokuCliError::DaikokuStrError(err.to_string()))?;

let status = status.as_u16();
let status = req.status().as_u16();

if status == 200 {
let project = get_default_project()?;
Expand Down Expand Up @@ -235,46 +208,23 @@ async fn list() -> DaikokuResult<()> {

let url: String = format!("{}/cms-api/tenant-assets/slugified", environment.server);

let req = Request::get(&url)
.header(header::HOST, &host)
// .header(header::COOKIE, cookie)
let req = reqwest::Client::new()
.get(url)
.header(header::HOST, host)
.header(header::AUTHORIZATION, format!("Basic {}", apikey))
.body(Empty::<Bytes>::new())
.expect("failed to build a request");

let stream = TcpStream::connect(&host).await.map_err(|err| {
DaikokuCliError::DaikokuErrorWithMessage("failed to join the server".to_string(), err)
})?;
let io = TokioIo::new(stream);

let (mut sender, conn) = hyper::client::conn::http1::handshake(io)
.await
.map_err(|err| DaikokuCliError::HyperError(err))?;

tokio::task::spawn(async move {
if let Err(err) = conn.await {
logger::error(format!("Connection error {:?}", err));
}
});

let upstream_resp = sender
.send_request(req)
.send()
.await
.map_err(|err| DaikokuCliError::ParsingError(err.to_string()))?;

let (
hyper::http::response::Parts {
headers: _, status, ..
},
body,
) = upstream_resp.into_parts();
.map_err(|err| DaikokuCliError::DaikokuStrError(err.to_string()))?;

let status = status.as_u16();
let status = req.status().as_u16();

if status == 200 {
let bytes = frame_to_bytes_body(body).await;
let bytes = req
.bytes()
.await
.map_err(|err| DaikokuCliError::DaikokuStrError(err.to_string()))?;

let assets: String = String::from_utf8(bytes).map_err(|_err| {
let assets: String = String::from_utf8(bytes.to_vec()).map_err(|_err| {
DaikokuCliError::ParsingError("failed to convert assets body".to_string())
})?;

Expand Down Expand Up @@ -340,7 +290,7 @@ async fn sync() -> DaikokuResult<()> {

let apikey = read_apikey_from_secrets(true)?;

let url: String = format!("{}/cms-api/tenant-assets/bulk", environment.server);
let url: String = format!("{}/tenant-assets/bulk", environment.server);

let files = pages
.iter()
Expand All @@ -362,45 +312,20 @@ async fn sync() -> DaikokuResult<()> {
})
.collect::<Vec<Asset>>();

let req = Request::post(&url)
.header(header::HOST, &host)
let req = reqwest::Client::new()
.post(url)
.header(header::HOST, host)
.header(header::AUTHORIZATION, format!("Basic {}", apikey))
.body(Full::new(Bytes::from(
serde_json::to_string(&contents).map_err(|_err| {
.body(Bytes::from(serde_json::to_string(&contents).map_err(
|_err| {
DaikokuCliError::ParsingError("failed to convert assets to json array".to_string())
})?,
)))
.expect("failed to build a request");

let stream = TcpStream::connect(&host).await.map_err(|err| {
DaikokuCliError::DaikokuErrorWithMessage("failed to join the server".to_string(), err)
})?;
let io = TokioIo::new(stream);

let (mut sender, conn) = hyper::client::conn::http1::handshake(io)
.await
.map_err(|err| DaikokuCliError::HyperError(err))?;

tokio::task::spawn(async move {
if let Err(err) = conn.await {
logger::error(format!("Connection error {:?}", err));
}
});

let upstream_resp = sender
.send_request(req)
},
)?))
.send()
.await
.map_err(|err| DaikokuCliError::ParsingError(err.to_string()))?;

let (
hyper::http::response::Parts {
headers: _, status, ..
},
_body,
) = upstream_resp.into_parts();

let status = status.as_u16();
.map_err(|err| DaikokuCliError::DaikokuStrError(err.to_string()))?;

let status = req.status().as_u16();
if status == 200 {
logger::success("synchronization done".to_string());
list().await
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn synchronization(body: &mut Vec<CmsFile>, dry_run: bool) -> DaikokuResul
);

if !dry_run {
daikoku_cms_api_post("/sync", body).await?;
daikoku_cms_api_post("/sync", body, true).await?;
}

Ok(())
Expand Down
22 changes: 13 additions & 9 deletions cli/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub(crate) fn bytes_to_vec_of_struct<T: for<'a> Deserialize<'a>>(
pub(crate) async fn daikoku_cms_api_post<T: Buf + std::marker::Send + 'static>(
path: &str,
body: T,
is_json_content: bool,
) -> DaikokuResult<Vec<u8>>
where
reqwest::Body: From<T>,
Expand All @@ -59,15 +60,18 @@ where

let url: String = format!("{}/cms-api{}", environment.server, &path);

let resp = reqwest::Client::new()
.post(url)
.header(header::HOST, host)
// .header(header::CONTENT_TYPE, "application/json")
.header(header::AUTHORIZATION, format!("Basic {}", apikey))
.body(body)
.send()
.await
.map_err(|err| DaikokuCliError::DaikokuStrError(err.to_string()))?;
let builder = reqwest::Client::new().post(url).header(header::HOST, host);

let resp = if is_json_content {
builder.header(header::CONTENT_TYPE, "application/json")
} else {
builder
}
.header(header::AUTHORIZATION, format!("Basic {}", apikey))
.body(body)
.send()
.await
.map_err(|err| DaikokuCliError::DaikokuStrError(err.to_string()))?;

let status = resp.status().as_u16();

Expand Down
26 changes: 20 additions & 6 deletions cli/tests/cli/commands/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ffi;
use std::path::PathBuf;
use std::{ffi, fs};

use assert_cmd::{
assert::{Assert, OutputAssertExt},
Expand Down Expand Up @@ -69,14 +69,26 @@ impl CLI {
}

pub(crate) async fn start_with_s3() -> Result<CLI, Box<dyn std::error::Error + 'static>> {
let mut state_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
state_path.push("tests/resources/s3");

let s3 = GenericImage::new("scireum/s3-ninja", "latest")
.with_wait_for(WaitFor::message_on_stdout("System is UP and RUNNING"))
.with_mapped_port(9000, 9000.tcp());
.with_mapped_port(9002, 9000.tcp())
.with_mount(Mount::bind_mount(
state_path.into_os_string().into_string().unwrap(),
"/home/sirius/data",
))
.with_network("daikoku");

let s3_container = s3.start().await?;

let mut cli = CLI::start().await?;
cli.s3_container = Some(s3_container);
let (postgres_container, daikoku_container) = Self::start_containers().await?;
let cli = CLI {
postgres_container: postgres_container,
daikoku_container: daikoku_container,
s3_container: Some(s3_container),
};
Ok(cli)
}

Expand All @@ -91,7 +103,8 @@ impl CLI {
.with_mapped_port(5432, 5432.tcp())
.with_env_var("POSTGRES_USER", "postgres")
.with_env_var("POSTGRES_PASSWORD", "postgres")
.with_env_var("POSTGRES_DB", "daikoku");
.with_env_var("POSTGRES_DB", "daikoku")
.with_network("daikoku");

let postgres_container = postgres.start().await?;
let host = postgres_container
Expand All @@ -114,7 +127,8 @@ impl CLI {
.with_mount(Mount::bind_mount(
state_path.into_os_string().into_string().unwrap(),
"/tmp",
));
))
.with_network("daikoku");

let daikoku_container = daikoku.start().await?;

Expand Down
2 changes: 1 addition & 1 deletion cli/tests/resources/daikoku-state.ndjson
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"type":"tenants","payload":{"_id":"default","name":"Daikoku Default Tenant","style":{"js":"","css":"","logo":"/assets/images/daikoku.svg","jsUrl":null,"title":"Daikoku Default Tenant","cssUrl":null,"footer":null,"cacheTTL":60000,"colorTheme":":root {\n --error-color: #ff6347;\n --error-color: #ffa494;\n --success-color: #4F8A10;\n --success-color: #76cf18;\n\n --link-color: #7f96af;\n --link--hover-color: #8fa6bf;\n\n --body-bg-color: #fff;\n --body-text-color: #212529;\n --navbar-bg-color: #7f96af;\n --navbar-brand-color: #fff;\n --menu-bg-color: #fff;\n --menu-text-color: #212529;\n --menu-text-hover-bg-color: #9bb0c5;\n --menu-text-hover-color: #fff;\n --section-bg-color: #f8f9fa;\n --section-text-color: #6c757d;\n --section-bottom-color: #eee;\n --addContent-bg-color: #e9ecef;\n --addContent-text-color: #000;\n --sidebar-bg-color: #f8f9fa;\n\n --btn-bg-color: #fff;\n --btn-text-color: #495057;\n --btn-border-color: #97b0c7;\n\n --badge-tags-bg-color: #ffc107;\n --badge-tags-bg-color: #ffe1a7;\n --badge-tags-text-color: #212529;\n\n --pagination-text-color: #586069;\n --pagination-border-color: #586069;\n\n --table-bg-color: #f8f9fa;\n\n --apicard-visibility-color: #586069;\n --apicard-visibility-border-color: rgba(27,31,35,.15);\n --modal-selection-bg-color: rgba(27,31,35,.15);\n}","faviconUrl":null,"description":"A new organization to host very fine APIs","homeCmsPage":"aaWYws62IbbyHGqHKDMMnko0yk66LUTq","unloggedHome":"","fontFamilyUrl":null,"homePageVisible":true,"notFoundCmsPage":"aaWYws62IbbyHGqHKDMMnko0yk66LUTq","cmsHistoryLength":10,"authenticatedCmsPage":"aaWYws62IbbyHGqHKDMMnko0yk66LUTq"},"domain":"localhost","contact":"contact@foo.bar","display":"default","enabled":true,"_deleted":false,"adminApi":"admin-api-tenant-default","robotTxt":null,"isPrivate":false,"tenantMode":"Default","authProvider":"Local","environments":[],"bucketSettings":{"access":"AKIAIOSFODNN7EXAMPLE","bucket":"daikoku-bucket","region":"eu-west-1","secret":"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY","v4auth":true,"endpoint":"http://localhost:9000","chunkSize":8388608},"defaultMessage":null,"mailerSettings":{"type":"console","template":"Bienvenue chez Ollyncare!\n{{email}}"},"defaultLanguage":"En","_humanReadableId":"daikoku-default-tenant","auditTrailConfig":{"kafkaConfig":null,"alertsEmails":[],"auditWebhooks":[],"elasticConfigs":null},"creationSecurity":false,"otoroshiSettings":[],"adminSubscriptions":["wKmlBKvIpD6RMF4ahGtwdNgwVVnX8feZ","cIYrbHDhwvhwDpO0A4dx9w6FhBlaydBb"],"authProviderSettings":{"sessionMaxAge":86400},"subscriptionSecurity":false,"apiReferenceHideForGuest":true,"thirdPartyPaymentSettings":[],"aggregationApiKeysSecurity":false}}
{"type":"tenants","payload":{"_id":"default","name":"Daikoku Default Tenant","style":{"js":"","css":"","logo":"/assets/images/daikoku.svg","jsUrl":null,"title":"Daikoku Default Tenant","cssUrl":null,"footer":null,"cacheTTL":60000,"colorTheme":":root {\n --error-color: #ff6347;\n --error-color: #ffa494;\n --success-color: #4F8A10;\n --success-color: #76cf18;\n\n --link-color: #7f96af;\n --link--hover-color: #8fa6bf;\n\n --body-bg-color: #fff;\n --body-text-color: #212529;\n --navbar-bg-color: #7f96af;\n --navbar-brand-color: #fff;\n --menu-bg-color: #fff;\n --menu-text-color: #212529;\n --menu-text-hover-bg-color: #9bb0c5;\n --menu-text-hover-color: #fff;\n --section-bg-color: #f8f9fa;\n --section-text-color: #6c757d;\n --section-bottom-color: #eee;\n --addContent-bg-color: #e9ecef;\n --addContent-text-color: #000;\n --sidebar-bg-color: #f8f9fa;\n\n --btn-bg-color: #fff;\n --btn-text-color: #495057;\n --btn-border-color: #97b0c7;\n\n --badge-tags-bg-color: #ffc107;\n --badge-tags-bg-color: #ffe1a7;\n --badge-tags-text-color: #212529;\n\n --pagination-text-color: #586069;\n --pagination-border-color: #586069;\n\n --table-bg-color: #f8f9fa;\n\n --apicard-visibility-color: #586069;\n --apicard-visibility-border-color: rgba(27,31,35,.15);\n --modal-selection-bg-color: rgba(27,31,35,.15);\n}","faviconUrl":null,"description":"A new organization to host very fine APIs","homeCmsPage":"aaWYws62IbbyHGqHKDMMnko0yk66LUTq","unloggedHome":"","fontFamilyUrl":null,"homePageVisible":true,"notFoundCmsPage":"aaWYws62IbbyHGqHKDMMnko0yk66LUTq","cmsHistoryLength":10,"authenticatedCmsPage":"aaWYws62IbbyHGqHKDMMnko0yk66LUTq"},"domain":"localhost","contact":"contact@foo.bar","display":"default","enabled":true,"_deleted":false,"adminApi":"admin-api-tenant-default","robotTxt":null,"isPrivate":false,"tenantMode":"Default","authProvider":"Local","environments":[],"bucketSettings":{"access":"AKIAIOSFODNN7EXAMPLE","bucket":"daikoku-bucket","region":"eu-west-1","secret":"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY","v4auth":true,"endpoint":"http://host.docker.internal:9002/daikoku-bucket","chunkSize":8388608},"defaultMessage":null,"mailerSettings":{"type":"console","template":"Bienvenue !\n{{email}}"},"defaultLanguage":"En","_humanReadableId":"daikoku-default-tenant","auditTrailConfig":{"kafkaConfig":null,"alertsEmails":[],"auditWebhooks":[],"elasticConfigs":null},"creationSecurity":false,"otoroshiSettings":[],"adminSubscriptions":["wKmlBKvIpD6RMF4ahGtwdNgwVVnX8feZ","cIYrbHDhwvhwDpO0A4dx9w6FhBlaydBb"],"authProviderSettings":{"sessionMaxAge":86400},"subscriptionSecurity":false,"apiReferenceHideForGuest":true,"thirdPartyPaymentSettings":[],"aggregationApiKeysSecurity":false}}
{"type":"users","payload":{"_id":"oxrtSfdDVXe7b0jEYFaziUzATuFuWsAX","name":"Daikoku admin","email":"admin@daikoku.io","isGuest":false,"origins":["Otoroshi"],"picture":"https://www.gravatar.com/avatar/35486a5583d99e3b2773814f359e4631?size=128&d=robohash","tenants":["default"],"_deleted":false,"metadata":{},"password":"$2a$10$RIZnvDZNZbmKBVkIJU1toOjjhfyW6d1OePboAcksYaB7A2EDNYOWe","invitation":null,"lastTenant":"default","starredApis":[],"personalToken":"2pAuww5Onlfn8po79jBCdsyB3lqWYRu8","isDaikokuAdmin":true,"defaultLanguage":null,"_humanReadableId":"admin-daikoku.io","pictureFromProvider":true,"twoFactorAuthentication":null,"hardwareKeyRegistrations":[]}}
{"type":"user_sessions","payload":{"_id":"KDF7uHaE2csoj4k4XPmqqIpQ4s47KU0W","ttl":86400000,"userId":"oxrtSfdDVXe7b0jEYFaziUzATuFuWsAX","created":1722521844604,"expires":1726821444735,"userName":"Daikoku admin","sessionId":"aWTVLkEg9jPwsK5hqDdxXR78wepinfe8kLbxsaE81gjKK7qY6ipOSNYSsMubrMFe","userEmail":"admin@daikoku.io","impersonatorId":null,"impersonatorName":null,"impersonatorEmail":null,"impersonatorSessionId":null}}
{"type":"evolutions","payload":{"_id":"JLJNk2wsTjTty1rHM4obDrPfSqO2J9Iq","date":1718877359581,"applied":true,"version":"1.0.2"}}
Expand Down
Binary file modified cli/tests/resources/hsperfdata_demiourgos728/1
Binary file not shown.
Empty file added cli/tests/resources/s3/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
#Tue Sep 24 16:53:46 CEST 2024
Content-Type=image/svg+xml
ETag=d7c6f4a4b59af83bb01d8d9f3ddf8e56
x-amz-acl=private
x-amz-content-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
x-amz-date=20240924T145346Z
x-amz-meta-asset=364542420-2320-4ac7-ae70-c8d893a2a0bc
x-amz-meta-content-type=image/svg+xml
x-amz-meta-desc=--
x-amz-meta-filename=otoroshi-logo.svg
x-amz-meta-tenant=default
x-amz-meta-title=otoroshi_logo
1 change: 1 addition & 0 deletions cli/tests/resources/s3/daikoku-bucket/$version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Loading

0 comments on commit 7533263

Please sign in to comment.