Skip to content

Commit

Permalink
feat: Added get blok route
Browse files Browse the repository at this point in the history
  • Loading branch information
leo91000 committed Jun 3, 2022
1 parent 8a33dcd commit f944ce8
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 3 deletions.
4 changes: 2 additions & 2 deletions crates/server/src/services/blok/mod.rs
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)
}
30 changes: 29 additions & 1 deletion crates/server/src/services/blok/routes.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
use crate::middlewares::api_key::ApiKey;
use crate::{
errors::{utils::db_err_into_api_err, ApiError},
middlewares::api_key::WriteApiKey,
server::AppState,
services::blok::models::{BlokInput, BlokOutput},
};
use actix_web::{post, web, Error as ActixError, HttpResponse};
use actix_web::{get, post, web, Error as ActixError, HttpResponse};
use entity::blok::{Column, Entity, Model};
use entity::page::{Column as PageColumn, Entity as PageEntity};
use sea_orm::prelude::*;

#[get("/{id}")]
pub async fn get_blok(
data: web::Data<AppState>,
path_id: web::Path<i32>,
api_key: ApiKey,
) -> Result<HttpResponse, ActixError> {
let id = path_id.into_inner();

let blok: Model = Entity::find()
.find_also_related(PageEntity)
.filter(Column::Id.eq(id))
.one(data.conn())
.await
.map_err(db_err_into_api_err)?
.and_then(|(blok, page)| page.map(|p| (blok, p)))
.and_then(|(blok, page)| {
if &page.namespace == api_key.namespace() {
return Some(blok);
}
None
})
.ok_or(ApiError::NotFound)?;

Ok(HttpResponse::Ok().json(BlokOutput::from(blok)))
}

#[post("")]
pub async fn create_blok(
data: web::Data<AppState>,
Expand Down
1 change: 1 addition & 0 deletions crates/server/tests/api/services/blok/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod create;
mod read;
110 changes: 110 additions & 0 deletions crates/server/tests/api/services/blok/read.rs
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()));
}

0 comments on commit f944ce8

Please sign in to comment.