summaryrefslogtreecommitdiff
path: root/src/login/routes
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-19 00:57:20 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-19 00:57:20 -0400
commitad00b553d845dba8af7b0e9fa2930209aee1dd62 (patch)
tree0a91c3c0113b2618730d3160f311c083e95b0581 /src/login/routes
parent635d92eb4ffc5a1c94cba784a2a4f18e1cb5effc (diff)
Make the responses for various data creation requests more consistent.
In general: * If the client can only assume the response is immediately valid (mostly, login creation, where the client cannot monitor the event stream), then 200 Okay, with data describing the server's view of the request. * If the client can monitor for completion by watching the event stream, then 202 Accepted, with data describing the server's view of the request. This comes on the heels of a comment I made on Discord: > hrm > > creating a login: 204 No Content, no body > sending a message: 202 Accepted, no body > creating a channel: 200 Okay, has a body > > past me, what were you on There wasn't any principled reason for this inconsistency; it happened as the endpoints were written at different times and with different states of mind.
Diffstat (limited to 'src/login/routes')
-rw-r--r--src/login/routes/login/post.rs10
-rw-r--r--src/login/routes/login/test.rs22
2 files changed, 16 insertions, 16 deletions
diff --git a/src/login/routes/login/post.rs b/src/login/routes/login/post.rs
index e33acad..67eaa6d 100644
--- a/src/login/routes/login/post.rs
+++ b/src/login/routes/login/post.rs
@@ -8,7 +8,7 @@ use crate::{
app::App,
clock::RequestedAt,
error::Internal,
- login::Password,
+ login::{Login, Password},
token::{app, extract::IdentityToken},
};
@@ -17,14 +17,14 @@ pub async fn handler(
RequestedAt(now): RequestedAt,
identity: IdentityToken,
Json(request): Json<Request>,
-) -> Result<(IdentityToken, StatusCode), Error> {
- let token = app
+) -> Result<(IdentityToken, Json<Login>), Error> {
+ let (login, secret) = app
.tokens()
.login(&request.name, &request.password, &now)
.await
.map_err(Error)?;
- let identity = identity.set(token);
- Ok((identity, StatusCode::NO_CONTENT))
+ let identity = identity.set(secret);
+ Ok((identity, Json(login)))
}
#[derive(serde::Deserialize)]
diff --git a/src/login/routes/login/test.rs b/src/login/routes/login/test.rs
index d431612..c94f14c 100644
--- a/src/login/routes/login/test.rs
+++ b/src/login/routes/login/test.rs
@@ -1,7 +1,4 @@
-use axum::{
- extract::{Json, State},
- http::StatusCode,
-};
+use axum::extract::{Json, State};
use super::post;
use crate::{test::fixtures, token::app};
@@ -11,24 +8,24 @@ async fn correct_credentials() {
// Set up the environment
let app = fixtures::scratch_app().await;
- let (name, password) = fixtures::login::create_with_password(&app, &fixtures::now()).await;
+ let (login, password) = fixtures::login::create_with_password(&app, &fixtures::now()).await;
// Call the endpoint
let identity = fixtures::identity::not_logged_in();
let logged_in_at = fixtures::now();
let request = post::Request {
- name: name.clone(),
+ name: login.name.clone(),
password,
};
- let (identity, status) =
+ let (identity, Json(response)) =
post::handler(State(app.clone()), logged_in_at, identity, Json(request))
.await
.expect("logged in with valid credentials");
// Verify the return value's basic structure
- assert_eq!(StatusCode::NO_CONTENT, status);
+ assert_eq!(login, response);
let secret = identity.secret().expect("logged in with valid credentials");
// Verify the semantics
@@ -40,7 +37,7 @@ async fn correct_credentials() {
.await
.expect("identity secret is valid");
- assert_eq!(name, validated_login.name);
+ assert_eq!(login, validated_login);
}
#[tokio::test]
@@ -98,13 +95,16 @@ async fn token_expires() {
// Set up the environment
let app = fixtures::scratch_app().await;
- let (name, password) = fixtures::login::create_with_password(&app, &fixtures::now()).await;
+ let (login, password) = fixtures::login::create_with_password(&app, &fixtures::now()).await;
// Call the endpoint
let logged_in_at = fixtures::ancient();
let identity = fixtures::identity::not_logged_in();
- let request = post::Request { name, password };
+ let request = post::Request {
+ name: login.name.clone(),
+ password,
+ };
let (identity, _) = post::handler(State(app.clone()), logged_in_at, identity, Json(request))
.await
.expect("logged in with valid credentials");