Skip to content

Commit d7813f2

Browse files
authored
test: adding some json standard tests for CMD and CMS (#197)
1 parent 762ff64 commit d7813f2

27 files changed

+813
-32
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integrationos-api/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "integrationos-api"
3-
version = "0.1.0"
3+
version = "1.0.0"
44
edition = "2021"
55

66
[dependencies]

integrationos-api/src/logic/connection.rs

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub struct DatabaseConnectionSecret {
7575
pub value: DatabaseConnectionConfig,
7676
pub namespace: String,
7777
pub service_name: String,
78+
pub connection_id: Id,
7879
}
7980

8081
async fn test_connection(
@@ -373,6 +374,7 @@ async fn generate_k8s_specs_and_secret(
373374
connection_config: &ConnectionDefinition,
374375
payload: &CreateConnectionPayload,
375376
auth_form_data: &Value,
377+
// emit_url: &str,
376378
) -> Result<
377379
(
378380
Value,
@@ -391,6 +393,8 @@ async fn generate_k8s_specs_and_secret(
391393
.chain(vec![
392394
("WORKER_THREADS".into(), "1".into()),
393395
("INTERNAL_SERVER_ADDRESS".into(), "0.0.0.0:5005".into()),
396+
("CONNECTION_ID".into(), connection_id.to_string()),
397+
("EMIT_URL".into(), state.config.emit_url.clone()),
394398
(
395399
"DATABASE_CONNECTION_TYPE".into(),
396400
connection_config.platform.clone(),
@@ -419,6 +423,7 @@ async fn generate_k8s_specs_and_secret(
419423
value: database_connection_config,
420424
service_name: service_name.to_string(),
421425
namespace: namespace.to_string(),
426+
connection_id: *connection_id,
422427
};
423428

424429
let service = ServiceSpecParams {

integrationos-api/src/logic/utils.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use axum::{extract::Path, Json};
1+
use axum::{extract::Path, response::IntoResponse, Json};
22
use http::StatusCode;
33
use integrationos_domain::{prefix::IdPrefix, Id, IntegrationOSError};
44
use serde::Serialize;
5+
use serde_json::json;
56

67
#[derive(Serialize)]
78
pub struct GenerateIdResponse {
@@ -22,3 +23,10 @@ pub async fn generate_id(
2223
Json(GenerateIdResponse { id: id.to_string() }),
2324
))
2425
}
26+
27+
pub async fn get_version() -> impl IntoResponse {
28+
(
29+
StatusCode::OK,
30+
Json(json!({"version": env!("CARGO_PKG_VERSION")})),
31+
)
32+
}

integrationos-api/src/router/public.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,6 @@ use tower_http::trace::TraceLayer;
2424

2525
pub fn get_router(state: &Arc<AppState>) -> Router<Arc<AppState>> {
2626
Router::new()
27-
.route(
28-
"/event-access/default",
29-
post(create_event_access_for_new_user).layer(from_fn_with_state(
30-
Arc::new(JwtState::from_state(state)),
31-
jwt_auth::jwt_auth_middleware,
32-
)),
33-
)
34-
.route(
35-
"/connection-definitions",
36-
get(read::<connection_definition::CreateRequest, ConnectionDefinition>),
37-
)
38-
.nest("/schemas", schema_generator::get_router())
39-
.route(
40-
"/connection-oauth-definition-schema",
41-
get(read::<
42-
connection_oauth_definition::FrontendOauthConnectionDefinition,
43-
connection_oauth_definition::FrontendOauthConnectionDefinition,
44-
>),
45-
)
46-
.route("/openapi", get(openapi::get_openapi))
47-
.route("/openapi/yaml", get(openapi::get_openapi_yaml))
48-
.route(
49-
"/connection-data/models/:platform_name",
50-
get(connection_model_schema::get_platform_models),
51-
)
5227
.nest(
5328
"/sdk",
5429
Router::new()
@@ -61,15 +36,41 @@ pub fn get_router(state: &Arc<AppState>) -> Router<Arc<AppState>> {
6136
get(read::<common_enum::GetRequest, CommonEnum>),
6237
),
6338
)
39+
.route(
40+
"/connection-data",
41+
get(read::<GetPublicConnectionDetailsRequest, PublicConnectionDetails>),
42+
)
6443
.route(
6544
"/connection-data/:model/:platform_name",
6645
get(connection_definition::public_get_connection_details),
6746
)
6847
.route(
69-
"/connection-data",
70-
get(read::<GetPublicConnectionDetailsRequest, PublicConnectionDetails>),
48+
"/connection-data/models/:platform_name",
49+
get(connection_model_schema::get_platform_models),
50+
)
51+
.route(
52+
"/connection-definitions",
53+
get(read::<connection_definition::CreateRequest, ConnectionDefinition>),
54+
)
55+
.nest("/schemas", schema_generator::get_router())
56+
.route(
57+
"/connection-oauth-definition-schema",
58+
get(read::<
59+
connection_oauth_definition::FrontendOauthConnectionDefinition,
60+
connection_oauth_definition::FrontendOauthConnectionDefinition,
61+
>),
62+
)
63+
.route(
64+
"/event-access/default",
65+
post(create_event_access_for_new_user).layer(from_fn_with_state(
66+
Arc::new(JwtState::from_state(state)),
67+
jwt_auth::jwt_auth_middleware,
68+
)),
7169
)
7270
.route("/generate-id/:prefix", get(utils::generate_id))
71+
.route("/openapi", get(openapi::get_openapi))
72+
.route("/openapi/yaml", get(openapi::get_openapi_yaml))
73+
.route("/version", get(utils::get_version))
7374
.layer(from_fn(log_request_middleware))
7475
.layer(TraceLayer::new_for_http())
7576
}

integrationos-api/tests/checker.rs

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
use std::{
2+
fs::File,
3+
io::{BufReader, Read},
4+
};
5+
6+
use serde::{de::DeserializeOwned, Serialize};
7+
use serde_json::Value;
8+
9+
pub enum CheckType {
10+
Json,
11+
Bson,
12+
}
13+
14+
pub trait JsonChecker {
15+
/// Check if the Struct passed can be serialized and deserialized using a file
16+
/// as a reference
17+
fn check<T: Serialize + DeserializeOwned + PartialEq>(
18+
&self,
19+
value: &T,
20+
r#type: CheckType,
21+
) -> bool;
22+
23+
/// The location where the checker will look for the file. If the folder doesn't exist, it will
24+
/// be created. It is always in relation to the current directory.
25+
fn location(&self) -> String {
26+
let current_dir = std::env::current_dir().expect("Failed to get current directory");
27+
28+
let json_checks_dir = current_dir.join("tests").join("resource");
29+
30+
if !json_checks_dir.exists() {
31+
std::fs::create_dir(&json_checks_dir).expect("Failed to create json_checks directory");
32+
}
33+
34+
json_checks_dir
35+
.to_str()
36+
.expect("Failed to convert path to string")
37+
.to_string()
38+
}
39+
}
40+
41+
#[derive(Debug, Clone, Default)]
42+
pub struct JsonCheckerImpl {
43+
/// If true, the checker will override the file if it exists
44+
/// or create a new file if it doesn't exist
45+
r#override: bool,
46+
}
47+
48+
impl JsonCheckerImpl {
49+
pub fn r#override(mut self) -> Self {
50+
self.r#override = true;
51+
self
52+
}
53+
}
54+
55+
impl JsonChecker for JsonCheckerImpl {
56+
fn check<T: Serialize + DeserializeOwned + PartialEq>(
57+
&self,
58+
value: &T,
59+
r#type: CheckType,
60+
) -> bool {
61+
let type_name = std::any::type_name::<T>().to_string();
62+
let file_path = match r#type {
63+
CheckType::Json => self.location() + &format!("/{}.json", type_name),
64+
CheckType::Bson => self.location() + &format!("/{}.bson", type_name),
65+
};
66+
67+
match r#type {
68+
CheckType::Json => {
69+
let serialized =
70+
serde_json::to_string_pretty(value).expect("Failed to serialize value");
71+
72+
let file = File::open(file_path.clone());
73+
74+
if self.r#override {
75+
std::fs::write(file_path, serialized).expect("Failed to write to file");
76+
panic!("Override flag is enabled, remember to disable and commit the changes");
77+
}
78+
79+
if file.is_err() {
80+
return false;
81+
}
82+
83+
let file = file.expect("Failed to open file");
84+
let mut file = BufReader::new(file);
85+
86+
let mut contents = String::new();
87+
file.read_to_string(&mut contents)
88+
.expect("Failed to read file contents");
89+
90+
let expected = serde_json::from_str::<Value>(&contents)
91+
.expect("Failed to deserialize expect value");
92+
93+
let actual = serde_json::from_str::<Value>(&serialized)
94+
.expect("Failed to deserialize actual value");
95+
96+
expected == actual
97+
}
98+
CheckType::Bson => {
99+
let serialized = bson::to_vec(value).expect("Failed to serialize value");
100+
101+
let file = File::open(file_path.clone());
102+
103+
if self.r#override {
104+
std::fs::write(file_path, serialized).expect("Failed to write to file");
105+
panic!("Override flag is enabled, remember to disable and commit the changes");
106+
}
107+
108+
if file.is_err() {
109+
return false;
110+
}
111+
112+
let file = file.expect("Failed to open file");
113+
let mut file = BufReader::new(file);
114+
115+
let mut contents: Vec<u8> = vec![];
116+
file.read_to_end(&mut contents)
117+
.expect("Failed to read file");
118+
119+
let expected =
120+
bson::from_slice::<T>(&contents).expect("Failed to deserialize expect value");
121+
122+
let actual =
123+
bson::from_slice::<T>(&serialized).expect("Failed to deserialize actual value");
124+
125+
expected == actual
126+
}
127+
}
128+
}
129+
}

integrationos-api/tests/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub mod checker;
12
pub mod context;
23
pub mod http;
4+
pub mod standard;
35
pub mod worker;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"_id": "conn::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
3+
"platformVersion": "platform-version",
4+
"connectionDefinitionId": "conn_def::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
5+
"type": {
6+
"api": {}
7+
},
8+
"key": "key",
9+
"group": "group",
10+
"name": "name",
11+
"environment": "live",
12+
"platform": "platform",
13+
"secretsServiceId": "secrets-service-id",
14+
"eventAccessId": "evt_ac::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
15+
"accessKey": "access-key",
16+
"identity": "identity",
17+
"identityType": "user",
18+
"settings": {
19+
"parseWebhookBody": true,
20+
"showSecret": false,
21+
"allowCustomEvents": false,
22+
"oauth": false
23+
},
24+
"throughput": {
25+
"key": "throughput-key",
26+
"limit": 100
27+
},
28+
"ownership": {
29+
"buildableId": "owner-id",
30+
"clientId": "client-id",
31+
"organizationId": "organization-id",
32+
"projectId": "project-id",
33+
"userId": "user-id"
34+
},
35+
"oauth": {
36+
"enabled": {
37+
"connection_oauth_definition_id": "conn_oauth_def::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA",
38+
"expires_in": 100,
39+
"expires_at": 100
40+
}
41+
},
42+
"createdAt": 0,
43+
"updatedAt": 0,
44+
"updated": false,
45+
"version": "1.0.0",
46+
"lastModifiedBy": "system",
47+
"deleted": false,
48+
"active": true,
49+
"deprecated": false
50+
}

0 commit comments

Comments
 (0)