From 31903fea771e65ff1840844f5634ef6486177e68 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 1 Apr 2022 16:56:24 +0300 Subject: [PATCH] Application: add tests to ensure correct UID behavior. (#357) --- server/svix-server/tests/e2e_application.rs | 199 +++++++++++++++++++- 1 file changed, 198 insertions(+), 1 deletion(-) diff --git a/server/svix-server/tests/e2e_application.rs b/server/svix-server/tests/e2e_application.rs index 0602444f6..7d2e8c921 100644 --- a/server/svix-server/tests/e2e_application.rs +++ b/server/svix-server/tests/e2e_application.rs @@ -3,7 +3,11 @@ use reqwest::StatusCode; -use svix_server::{v1::endpoints::application::ApplicationOut, v1::utils::ListResponse}; +use svix_server::{ + core::types::ApplicationUid, + v1::endpoints::application::ApplicationOut, + v1::{endpoints::application::ApplicationIn, utils::ListResponse}, +}; mod utils; @@ -150,3 +154,196 @@ async fn test_list() { assert_eq!(&app_1, list.data.get(0).unwrap()); assert_eq!(&app_2, list.data.get(1).unwrap()); } + +#[tokio::test] +async fn test_uid() { + let (client, _jh) = start_svix_server(); + + let app: ApplicationOut = client + .post( + "api/v1/app/", + ApplicationIn { + name: "App 1".to_owned(), + uid: Some(ApplicationUid("app1".to_owned())), + ..Default::default() + }, + StatusCode::CREATED, + ) + .await + .unwrap(); + + assert_ne!(app.id.0, app.uid.unwrap().0); + + // Can't create another app with the same uid twice + let _: IgnoredResponse = client + .post( + "api/v1/app/", + ApplicationIn { + name: "App 1".to_owned(), + uid: Some(ApplicationUid("app1".to_owned())), + ..Default::default() + }, + StatusCode::CONFLICT, + ) + .await + .unwrap(); + + // Can't update an app to an existing uid (when we have no uid) + let app2: ApplicationOut = client + .post( + "api/v1/app/", + ApplicationIn { + name: "App 2".to_owned(), + ..Default::default() + }, + StatusCode::CREATED, + ) + .await + .unwrap(); + + let _: IgnoredResponse = client + .put( + &format!("api/v1/app/{}/", app2.id), + ApplicationIn { + name: "App 2".to_owned(), + uid: Some(ApplicationUid("app1".to_owned())), + ..Default::default() + }, + StatusCode::CONFLICT, + ) + .await + .unwrap(); + + // Can't update an app to an existing uid (when we have a uid) + let app2: ApplicationOut = client + .post( + "api/v1/app/", + ApplicationIn { + name: "App 2".to_owned(), + uid: Some(ApplicationUid("app2".to_owned())), + ..Default::default() + }, + StatusCode::CREATED, + ) + .await + .unwrap(); + + let _: IgnoredResponse = client + .put( + &format!("api/v1/app/{}/", app2.id), + ApplicationIn { + name: "App 2".to_owned(), + uid: Some(ApplicationUid("app1".to_owned())), + ..Default::default() + }, + StatusCode::CONFLICT, + ) + .await + .unwrap(); + + // Delete app1 + let _: IgnoredResponse = client + .delete(&format!("api/v1/app/{}/", app.id), StatusCode::NO_CONTENT) + .await + .unwrap(); + + // Update to a now deleted uid + let app2: ApplicationOut = client + .put( + &format!("api/v1/app/{}/", app2.id), + ApplicationIn { + name: "App 2".to_owned(), + uid: Some(ApplicationUid("app1".to_owned())), + ..Default::default() + }, + StatusCode::OK, + ) + .await + .unwrap(); + + let _: IgnoredResponse = client + .delete( + &format!("api/v1/app/{}/", app2.uid.unwrap()), + StatusCode::NO_CONTENT, + ) + .await + .unwrap(); + + // Create an app with the same UID again (after it was deleted) + let app: ApplicationOut = client + .post( + "api/v1/app/", + ApplicationIn { + name: "App 1".to_owned(), + uid: Some(ApplicationUid("app1".to_owned())), + ..Default::default() + }, + StatusCode::CREATED, + ) + .await + .unwrap(); + + // Can update an app with a UID + let _: IgnoredResponse = client + .put( + &format!("api/v1/app/{}/", app.id), + ApplicationIn { + name: "App 1".to_owned(), + uid: Some(ApplicationUid("app1".to_owned())), + ..Default::default() + }, + StatusCode::OK, + ) + .await + .unwrap(); + + // Can update the UID + let app: ApplicationOut = client + .put( + &format!("api/v1/app/{}/", app.id), + ApplicationIn { + name: "App 1".to_owned(), + uid: Some(ApplicationUid("app3".to_owned())), + ..Default::default() + }, + StatusCode::OK, + ) + .await + .unwrap(); + + let app2: ApplicationOut = client + .get(&format!("api/v1/app/{}/", app.id), StatusCode::OK) + .await + .unwrap(); + + assert_eq!(app.id, app2.id); + assert_eq!(app.uid, app2.uid); + assert_eq!(app2.uid.unwrap().0, "app3"); + + // Remove the uid + let app: ApplicationOut = client + .put( + &format!("api/v1/app/{}/", app.id), + ApplicationIn { + name: "App 1".to_owned(), + ..Default::default() + }, + StatusCode::OK, + ) + .await + .unwrap(); + + let app2: ApplicationOut = client + .get(&format!("api/v1/app/{}/", app.id), StatusCode::OK) + .await + .unwrap(); + + assert_eq!(app.id, app2.id); + assert!(app2.uid.is_none()); + + // Make sure we can't fetch by the old UID + let _: IgnoredResponse = client + .get(&format!("api/v1/app/{}/", "app3"), StatusCode::NOT_FOUND) + .await + .unwrap(); +}