132 lines
3.9 KiB
Rust
132 lines
3.9 KiB
Rust
use axum::{body::{to_bytes, Body}, http::{Request, StatusCode}};
|
|
use hermes_backend::{app_state::AppState, build_router, config::AppConfig};
|
|
use serde_json as json;
|
|
use tower::ServiceExt;
|
|
|
|
#[tokio::test]
|
|
async fn health_returns_ok() {
|
|
let app = build_router(AppState::new(AppConfig::default(), None, None));
|
|
|
|
let response = app
|
|
.oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn session_start_and_me_work() {
|
|
let app = build_router(AppState::new(AppConfig::default(), None, None));
|
|
|
|
let response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/session/start")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from("{}"))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::CREATED);
|
|
|
|
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
let json: json::Value = json::from_slice(&body).unwrap();
|
|
assert_eq!(json["locale_code"], "en");
|
|
|
|
let response = app
|
|
.oneshot(Request::builder().uri("/api/v1/session/me").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn participant_ref_reuses_the_same_user() {
|
|
let app = build_router(AppState::new(AppConfig::default(), None, None));
|
|
|
|
let body = json::json!({
|
|
"external_ref": "participant-001",
|
|
"locale_code": "sv"
|
|
})
|
|
.to_string();
|
|
|
|
let first = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/session/start")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from(body.clone()))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let first_body = to_bytes(first.into_body(), usize::MAX).await.unwrap();
|
|
let first_json: json::Value = json::from_slice(&first_body).unwrap();
|
|
|
|
let second = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/api/v1/session/start")
|
|
.header("content-type", "application/json")
|
|
.body(Body::from(body))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let second_body = to_bytes(second.into_body(), usize::MAX).await.unwrap();
|
|
let second_json: json::Value = json::from_slice(&second_body).unwrap();
|
|
|
|
assert_eq!(first_json["user_id"], second_json["user_id"]);
|
|
assert_eq!(first_json["locale_code"], "sv");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn feed_next_returns_a_manifestable_event() {
|
|
let app = build_router(AppState::new(AppConfig::default(), None, None));
|
|
|
|
let response = app
|
|
.clone()
|
|
.oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
let event: json::Value = json::from_slice(&body).unwrap();
|
|
let event_id = event["id"].as_str().unwrap().to_string();
|
|
|
|
assert_eq!(event["status"], "prefetch_ready");
|
|
|
|
let response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri(format!("/api/v1/events/{event_id}/manifest"))
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
let manifest: json::Value = json::from_slice(&body).unwrap();
|
|
|
|
assert_eq!(manifest["event"]["id"], event_id);
|
|
assert!(manifest["media"].as_array().unwrap().len() >= 1);
|
|
assert!(manifest["markets"].as_array().unwrap().len() >= 1);
|
|
}
|