summaryrefslogtreecommitdiff
path: root/src/user/routes/logout
diff options
context:
space:
mode:
authorojacobson <ojacobson@noreply.codeberg.org>2025-06-21 04:22:52 +0200
committerojacobson <ojacobson@noreply.codeberg.org>2025-06-21 04:22:52 +0200
commitcd1dc0dab4b46bc5712070812192d5ce34071470 (patch)
treec94f5a42f7e734b81892c1289a1d2b566706ba7c /src/user/routes/logout
parentd84ba5cd09b713fac2f193d5c05af7415ea6742d (diff)
parent4e3d5ccac99b24934c972e088cd7eb02bb95df06 (diff)
Reorganize and consolidate HTTP routes.
HTTP routes are now defined in a single, unified module, pulling them out of the topical modules they were formerly part of. This is intended to improve the navigability of the codebase. Previously, finding the handler corresponding to a specific endpoint required prior familiarity, though in practice you could usually guess from topic area. Now, all routes are defined in one place; if you know the path, you can read down the list to find the handler. Handlers themselves live with the domain they are most appropriately "part of," generally (in this version, universally) in a `handlers` submodule. The handlers themselves have been flattened down; rather than representing a path and a method, they now represent a named operation (which is suspiciously similar to the path in most cases). This means that we no longer have constructs like `crate::ui::routes::ch::channel` - it's now `crate::ui::handlers::channel` instead. ## Disclaimer I Solemnly Swear I Didn't Change Any Handlers. ## Prior art I've inadvertently reinvented Django's `urls.py` convention, and I've opted to lean into that. Merges flatter-routes-reorg into main.
Diffstat (limited to 'src/user/routes/logout')
-rw-r--r--src/user/routes/logout/mod.rs4
-rw-r--r--src/user/routes/logout/post.rs50
-rw-r--r--src/user/routes/logout/test.rs79
3 files changed, 0 insertions, 133 deletions
diff --git a/src/user/routes/logout/mod.rs b/src/user/routes/logout/mod.rs
deleted file mode 100644
index 36b384e..0000000
--- a/src/user/routes/logout/mod.rs
+++ /dev/null
@@ -1,4 +0,0 @@
-pub mod post;
-
-#[cfg(test)]
-mod test;
diff --git a/src/user/routes/logout/post.rs b/src/user/routes/logout/post.rs
deleted file mode 100644
index 0ac663e..0000000
--- a/src/user/routes/logout/post.rs
+++ /dev/null
@@ -1,50 +0,0 @@
-use axum::{
- extract::{Json, State},
- http::StatusCode,
- response::{IntoResponse, Response},
-};
-
-use crate::{
- app::App,
- clock::RequestedAt,
- error::{Internal, Unauthorized},
- token::{app, extract::IdentityCookie},
-};
-
-pub async fn handler(
- State(app): State<App>,
- RequestedAt(now): RequestedAt,
- identity: IdentityCookie,
- Json(_): Json<Request>,
-) -> Result<(IdentityCookie, StatusCode), Error> {
- if let Some(secret) = identity.secret() {
- let (token, _) = app.tokens().validate(&secret, &now).await?;
- app.tokens().logout(&token).await?;
- }
-
- let identity = identity.clear();
- Ok((identity, StatusCode::NO_CONTENT))
-}
-
-// This forces the only valid request to be `{}`, and not the infinite
-// variation allowed when there's no body extractor.
-#[derive(Default, serde::Deserialize)]
-pub struct Request {}
-
-#[derive(Debug, thiserror::Error)]
-#[error(transparent)]
-pub struct Error(#[from] pub app::ValidateError);
-
-impl IntoResponse for Error {
- fn into_response(self) -> Response {
- let Self(error) = self;
- match error {
- app::ValidateError::InvalidToken | app::ValidateError::LoginDeleted => {
- Unauthorized.into_response()
- }
- app::ValidateError::Name(_) | app::ValidateError::Database(_) => {
- Internal::from(error).into_response()
- }
- }
- }
-}
diff --git a/src/user/routes/logout/test.rs b/src/user/routes/logout/test.rs
deleted file mode 100644
index ce93760..0000000
--- a/src/user/routes/logout/test.rs
+++ /dev/null
@@ -1,79 +0,0 @@
-use axum::{
- extract::{Json, State},
- http::StatusCode,
-};
-
-use super::post;
-use crate::{test::fixtures, token::app};
-
-#[tokio::test]
-async fn successful() {
- // Set up the environment
-
- let app = fixtures::scratch_app().await;
- let now = fixtures::now();
- let creds = fixtures::user::create_with_password(&app, &fixtures::now()).await;
- let identity = fixtures::cookie::logged_in(&app, &creds, &now).await;
- let secret = fixtures::cookie::secret(&identity);
-
- // Call the endpoint
-
- let (response_identity, response_status) = post::handler(
- State(app.clone()),
- fixtures::now(),
- identity.clone(),
- Json::default(),
- )
- .await
- .expect("logged out with a valid token");
-
- // Verify the return value's basic structure
-
- assert!(response_identity.secret().is_none());
- assert_eq!(StatusCode::NO_CONTENT, response_status);
-
- // Verify the semantics
- let error = app
- .tokens()
- .validate(&secret, &now)
- .await
- .expect_err("secret is invalid");
- assert!(matches!(error, app::ValidateError::InvalidToken));
-}
-
-#[tokio::test]
-async fn no_identity() {
- // Set up the environment
-
- let app = fixtures::scratch_app().await;
-
- // Call the endpoint
-
- let identity = fixtures::cookie::not_logged_in();
- let (identity, status) = post::handler(State(app), fixtures::now(), identity, Json::default())
- .await
- .expect("logged out with no token succeeds");
-
- // Verify the return value's basic structure
-
- assert!(identity.secret().is_none());
- assert_eq!(StatusCode::NO_CONTENT, status);
-}
-
-#[tokio::test]
-async fn invalid_token() {
- // Set up the environment
-
- let app = fixtures::scratch_app().await;
-
- // Call the endpoint
-
- let identity = fixtures::cookie::fictitious();
- let post::Error(error) = post::handler(State(app), fixtures::now(), identity, Json::default())
- .await
- .expect_err("logged out with an invalid token fails");
-
- // Verify the return value's basic structure
-
- assert!(matches!(error, app::ValidateError::InvalidToken));
-}