Skip to content

Commit

Permalink
Application: add tests to ensure correct UID behavior. (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
tasn authored Apr 1, 2022
1 parent ddb1ff8 commit 31903fe
Showing 1 changed file with 198 additions and 1 deletion.
199 changes: 198 additions & 1 deletion server/svix-server/tests/e2e_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

0 comments on commit 31903fe

Please sign in to comment.