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

Fix deleting clients #260

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions migrations/2023-10-26-0942_session_delete_cascade/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- This file should undo anything in `up.sql`

ALTER TABLE sessions
DROP CONSTRAINT sessions_client_id_fkey,
ADD CONSTRAINT sessions_client_id_fkey
FOREIGN KEY (client_id)
REFERENCES clients(id)
ON DELETE NO ACTION;
8 changes: 8 additions & 0 deletions migrations/2023-10-26-0942_session_delete_cascade/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Your SQL goes here

ALTER TABLE sessions
DROP CONSTRAINT sessions_client_id_fkey,
ADD CONSTRAINT sessions_client_id_fkey
FOREIGN KEY (client_id)
REFERENCES clients(id)
ON DELETE CASCADE;
42 changes: 41 additions & 1 deletion tests/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use rocket::http::Status;

mod common;

use crate::common::url;
use crate::common::{config, url};
use zauth::models::client::{Client, NewClient};
use zauth::models::session::Session;

#[rocket::async_test]
async fn create_and_update_client() {
Expand Down Expand Up @@ -100,3 +101,42 @@ async fn change_client_secret() {
})
.await;
}

#[rocket::async_test]
async fn delete_client_with_session() {
common::as_admin(async move |http_client, db, user| {
let client_name = "test";

let client_form = format!("name={}", url(&client_name),);

let create = http_client
.post("/clients")
.body(client_form)
.header(ContentType::Form)
.header(Accept::JSON)
.dispatch()
.await;

assert_eq!(create.status(), Status::Created);
let client = Client::find_by_name(client_name.to_owned(), &db)
.await
.unwrap();

let session =
Session::create_client_session(&user, &client, &config(), &db)
.await
.unwrap();

let delete = http_client
.delete(format!("/clients/{}", &client.id))
.header(ContentType::Form)
.header(Accept::JSON)
.dispatch()
.await;

assert_eq!(delete.status(), Status::NoContent);
assert!(Client::find(client.id, &db).await.is_err());
assert!(Session::find_by_id(session.id, &db).await.is_err());
})
.await;
}
Loading