Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application: add tests to ensure correct UID behavior. #357

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}