-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
use crate::services::blok::routes::create_blok; | ||
use crate::services::blok::routes::{create_blok, get_blok}; | ||
use actix_web::{web::scope, Scope}; | ||
|
||
mod models; | ||
mod routes; | ||
|
||
pub fn blok_service() -> Scope { | ||
scope("/blok").service(create_blok) | ||
scope("/blok").service(get_blok).service(create_blok) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod create; | ||
mod read; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use crate::helpers::TestApp; | ||
use crate::services::blok::create::create_blok; | ||
use crate::services::page::create::create_page; | ||
use actix_web::http::StatusCode; | ||
use serde_json::{json, Value}; | ||
use std::collections::HashSet; | ||
use test_context::test_context; | ||
|
||
#[test_context(TestApp)] | ||
#[tokio::test] | ||
async fn get_one_blok_should_work(ctx: &mut TestApp) { | ||
ctx.create_api_key("namespace", false).await; | ||
|
||
let page = create_page( | ||
ctx, | ||
&json!({ | ||
"path": "/about/me", | ||
"title": "A propos !", | ||
"description": "Qui suis-je ? Telle est la question !" | ||
}), | ||
) | ||
.await; | ||
|
||
let blok = create_blok( | ||
ctx, | ||
&json!({ | ||
"pageId": page.get("id").and_then(|v| v.as_i64()).expect("Expected ID"), | ||
"componentId": "Hero", | ||
"props": { | ||
"backgroundImage": "https://picsum.photos/200/300" | ||
} | ||
}), | ||
) | ||
.await; | ||
|
||
let response = ctx | ||
.get(format!("/blok/{}", blok.get("id").expect("Expected ID"))) | ||
.await; | ||
|
||
let json = response | ||
.json::<Value>() | ||
.await | ||
.ok() | ||
.and_then(|v| v.as_object().cloned()) | ||
.expect("Expected value"); | ||
|
||
assert_eq!( | ||
HashSet::from([ | ||
"id", | ||
"pageId", | ||
"componentId", | ||
"props", | ||
"priority", | ||
"createdAt", | ||
"updatedAt" | ||
]), | ||
json.keys().map(|v| v.as_str()).collect(), | ||
); | ||
assert_eq!(page.get("id"), json.get("pageId")); | ||
assert_eq!(blok.get("componentId"), json.get("componentId")); | ||
assert_eq!(blok.get("props"), json.get("props")); | ||
assert!(json.get("id").and_then(|v| v.as_i64()).is_some()); | ||
assert!(json.get("createdAt").and_then(|v| v.as_str()).is_some()); | ||
assert!(json.get("updatedAt").and_then(|v| v.as_str()).is_some()); | ||
} | ||
|
||
#[test_context(TestApp)] | ||
#[tokio::test] | ||
async fn get_one_blok_from_another_namespace_should_be_denied(ctx: &mut TestApp) { | ||
ctx.create_api_key("other", false).await; | ||
|
||
let page = create_page( | ||
ctx, | ||
&json!({ | ||
"path": "/about/me", | ||
"title": "A propos !", | ||
"description": "Qui suis-je ? Telle est la question !" | ||
}), | ||
) | ||
.await; | ||
|
||
let blok = create_blok( | ||
ctx, | ||
&json!({ | ||
"pageId": page.get("id").and_then(|v| v.as_i64()).expect("Expected ID"), | ||
"componentId": "Hero", | ||
"props": { | ||
"backgroundImage": "https://picsum.photos/200/300" | ||
} | ||
}), | ||
) | ||
.await; | ||
|
||
ctx.create_api_key("namespace", false).await; | ||
|
||
let response = ctx | ||
.get(format!("/blok/{}", blok.get("id").expect("Expected ID"))) | ||
.await; | ||
|
||
assert_eq!(StatusCode::NOT_FOUND, response.status()); | ||
|
||
let json = response | ||
.json::<Value>() | ||
.await | ||
.ok() | ||
.and_then(|v| v.as_object().cloned()) | ||
.expect("Expected value"); | ||
|
||
assert_eq!(Some("NTFND"), json.get("code").and_then(|v| v.as_str())); | ||
} |