Skip to content

Commit

Permalink
test: adding some json standard tests for CMD and CMS (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez authored Nov 29, 2024
1 parent 762ff64 commit d7813f2
Show file tree
Hide file tree
Showing 27 changed files with 813 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion integrationos-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "integrationos-api"
version = "0.1.0"
version = "1.0.0"
edition = "2021"

[dependencies]
Expand Down
5 changes: 5 additions & 0 deletions integrationos-api/src/logic/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct DatabaseConnectionSecret {
pub value: DatabaseConnectionConfig,
pub namespace: String,
pub service_name: String,
pub connection_id: Id,
}

async fn test_connection(
Expand Down Expand Up @@ -373,6 +374,7 @@ async fn generate_k8s_specs_and_secret(
connection_config: &ConnectionDefinition,
payload: &CreateConnectionPayload,
auth_form_data: &Value,
// emit_url: &str,
) -> Result<
(
Value,
Expand All @@ -391,6 +393,8 @@ async fn generate_k8s_specs_and_secret(
.chain(vec![
("WORKER_THREADS".into(), "1".into()),
("INTERNAL_SERVER_ADDRESS".into(), "0.0.0.0:5005".into()),
("CONNECTION_ID".into(), connection_id.to_string()),
("EMIT_URL".into(), state.config.emit_url.clone()),
(
"DATABASE_CONNECTION_TYPE".into(),
connection_config.platform.clone(),
Expand Down Expand Up @@ -419,6 +423,7 @@ async fn generate_k8s_specs_and_secret(
value: database_connection_config,
service_name: service_name.to_string(),
namespace: namespace.to_string(),
connection_id: *connection_id,
};

let service = ServiceSpecParams {
Expand Down
10 changes: 9 additions & 1 deletion integrationos-api/src/logic/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use axum::{extract::Path, Json};
use axum::{extract::Path, response::IntoResponse, Json};
use http::StatusCode;
use integrationos_domain::{prefix::IdPrefix, Id, IntegrationOSError};
use serde::Serialize;
use serde_json::json;

#[derive(Serialize)]
pub struct GenerateIdResponse {
Expand All @@ -22,3 +23,10 @@ pub async fn generate_id(
Json(GenerateIdResponse { id: id.to_string() }),
))
}

pub async fn get_version() -> impl IntoResponse {
(
StatusCode::OK,
Json(json!({"version": env!("CARGO_PKG_VERSION")})),
)
}
55 changes: 28 additions & 27 deletions integrationos-api/src/router/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,6 @@ use tower_http::trace::TraceLayer;

pub fn get_router(state: &Arc<AppState>) -> Router<Arc<AppState>> {
Router::new()
.route(
"/event-access/default",
post(create_event_access_for_new_user).layer(from_fn_with_state(
Arc::new(JwtState::from_state(state)),
jwt_auth::jwt_auth_middleware,
)),
)
.route(
"/connection-definitions",
get(read::<connection_definition::CreateRequest, ConnectionDefinition>),
)
.nest("/schemas", schema_generator::get_router())
.route(
"/connection-oauth-definition-schema",
get(read::<
connection_oauth_definition::FrontendOauthConnectionDefinition,
connection_oauth_definition::FrontendOauthConnectionDefinition,
>),
)
.route("/openapi", get(openapi::get_openapi))
.route("/openapi/yaml", get(openapi::get_openapi_yaml))
.route(
"/connection-data/models/:platform_name",
get(connection_model_schema::get_platform_models),
)
.nest(
"/sdk",
Router::new()
Expand All @@ -61,15 +36,41 @@ pub fn get_router(state: &Arc<AppState>) -> Router<Arc<AppState>> {
get(read::<common_enum::GetRequest, CommonEnum>),
),
)
.route(
"/connection-data",
get(read::<GetPublicConnectionDetailsRequest, PublicConnectionDetails>),
)
.route(
"/connection-data/:model/:platform_name",
get(connection_definition::public_get_connection_details),
)
.route(
"/connection-data",
get(read::<GetPublicConnectionDetailsRequest, PublicConnectionDetails>),
"/connection-data/models/:platform_name",
get(connection_model_schema::get_platform_models),
)
.route(
"/connection-definitions",
get(read::<connection_definition::CreateRequest, ConnectionDefinition>),
)
.nest("/schemas", schema_generator::get_router())
.route(
"/connection-oauth-definition-schema",
get(read::<
connection_oauth_definition::FrontendOauthConnectionDefinition,
connection_oauth_definition::FrontendOauthConnectionDefinition,
>),
)
.route(
"/event-access/default",
post(create_event_access_for_new_user).layer(from_fn_with_state(
Arc::new(JwtState::from_state(state)),
jwt_auth::jwt_auth_middleware,
)),
)
.route("/generate-id/:prefix", get(utils::generate_id))
.route("/openapi", get(openapi::get_openapi))
.route("/openapi/yaml", get(openapi::get_openapi_yaml))
.route("/version", get(utils::get_version))
.layer(from_fn(log_request_middleware))
.layer(TraceLayer::new_for_http())
}
129 changes: 129 additions & 0 deletions integrationos-api/tests/checker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use std::{
fs::File,
io::{BufReader, Read},
};

use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;

pub enum CheckType {
Json,
Bson,
}

pub trait JsonChecker {
/// Check if the Struct passed can be serialized and deserialized using a file
/// as a reference
fn check<T: Serialize + DeserializeOwned + PartialEq>(
&self,
value: &T,
r#type: CheckType,
) -> bool;

/// The location where the checker will look for the file. If the folder doesn't exist, it will
/// be created. It is always in relation to the current directory.
fn location(&self) -> String {
let current_dir = std::env::current_dir().expect("Failed to get current directory");

let json_checks_dir = current_dir.join("tests").join("resource");

if !json_checks_dir.exists() {
std::fs::create_dir(&json_checks_dir).expect("Failed to create json_checks directory");
}

json_checks_dir
.to_str()
.expect("Failed to convert path to string")
.to_string()
}
}

#[derive(Debug, Clone, Default)]
pub struct JsonCheckerImpl {
/// If true, the checker will override the file if it exists
/// or create a new file if it doesn't exist
r#override: bool,
}

impl JsonCheckerImpl {
pub fn r#override(mut self) -> Self {
self.r#override = true;
self
}
}

impl JsonChecker for JsonCheckerImpl {
fn check<T: Serialize + DeserializeOwned + PartialEq>(
&self,
value: &T,
r#type: CheckType,
) -> bool {
let type_name = std::any::type_name::<T>().to_string();
let file_path = match r#type {
CheckType::Json => self.location() + &format!("/{}.json", type_name),
CheckType::Bson => self.location() + &format!("/{}.bson", type_name),
};

match r#type {
CheckType::Json => {
let serialized =
serde_json::to_string_pretty(value).expect("Failed to serialize value");

let file = File::open(file_path.clone());

if self.r#override {
std::fs::write(file_path, serialized).expect("Failed to write to file");
panic!("Override flag is enabled, remember to disable and commit the changes");
}

if file.is_err() {
return false;
}

let file = file.expect("Failed to open file");
let mut file = BufReader::new(file);

let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Failed to read file contents");

let expected = serde_json::from_str::<Value>(&contents)
.expect("Failed to deserialize expect value");

let actual = serde_json::from_str::<Value>(&serialized)
.expect("Failed to deserialize actual value");

expected == actual
}
CheckType::Bson => {
let serialized = bson::to_vec(value).expect("Failed to serialize value");

let file = File::open(file_path.clone());

if self.r#override {
std::fs::write(file_path, serialized).expect("Failed to write to file");
panic!("Override flag is enabled, remember to disable and commit the changes");
}

if file.is_err() {
return false;
}

let file = file.expect("Failed to open file");
let mut file = BufReader::new(file);

let mut contents: Vec<u8> = vec![];
file.read_to_end(&mut contents)
.expect("Failed to read file");

let expected =
bson::from_slice::<T>(&contents).expect("Failed to deserialize expect value");

let actual =
bson::from_slice::<T>(&serialized).expect("Failed to deserialize actual value");

expected == actual
}
}
}
}
2 changes: 2 additions & 0 deletions integrationos-api/tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod checker;
pub mod context;
pub mod http;
pub mod standard;
pub mod worker;
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"_id": "conn::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
"platformVersion": "platform-version",
"connectionDefinitionId": "conn_def::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
"type": {
"api": {}
},
"key": "key",
"group": "group",
"name": "name",
"environment": "live",
"platform": "platform",
"secretsServiceId": "secrets-service-id",
"eventAccessId": "evt_ac::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
"accessKey": "access-key",
"identity": "identity",
"identityType": "user",
"settings": {
"parseWebhookBody": true,
"showSecret": false,
"allowCustomEvents": false,
"oauth": false
},
"throughput": {
"key": "throughput-key",
"limit": 100
},
"ownership": {
"buildableId": "owner-id",
"clientId": "client-id",
"organizationId": "organization-id",
"projectId": "project-id",
"userId": "user-id"
},
"oauth": {
"enabled": {
"connection_oauth_definition_id": "conn_oauth_def::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
"expires_in": 100,
"expires_at": 100
}
},
"createdAt": 0,
"updatedAt": 0,
"updated": false,
"version": "1.0.0",
"lastModifiedBy": "system",
"deleted": false,
"active": true,
"deprecated": false
}
Binary file not shown.
Loading

0 comments on commit d7813f2

Please sign in to comment.