Skip to content

Commit

Permalink
Merge pull request #278 from ZeusWPI/update_deps
Browse files Browse the repository at this point in the history
Cargo update and nix flake update
  • Loading branch information
rien authored Jun 19, 2024
2 parents c1699dc + cdfc619 commit 9c2ad8c
Show file tree
Hide file tree
Showing 11 changed files with 590 additions and 531 deletions.
978 changes: 516 additions & 462 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/ephemeral/authorization_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ impl<'r> FromRequest<'r> for AuthorizationToken {
let headers: Vec<_> = request.headers().get("Authorization").collect();
if headers.is_empty() {
let msg = String::from("Authorization header missing");
return Outcome::Failure((Status::BadRequest, msg));
return Outcome::Error((Status::BadRequest, msg));
} else if headers.len() > 1 {
let msg = String::from("More than one authorization header");
return Outcome::Failure((Status::BadRequest, msg));
return Outcome::Error((Status::BadRequest, msg));
}

let auth_header = headers[0];
Expand All @@ -35,7 +35,7 @@ impl<'r> FromRequest<'r> for AuthorizationToken {
Outcome::Success(AuthorizationToken { username: token })
} else {
let msg = "Unable to parse tokenn".to_string();
Outcome::Failure((Status::BadRequest, msg))
Outcome::Error((Status::BadRequest, msg))
}
}
}
12 changes: 6 additions & 6 deletions src/ephemeral/from_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ where
.map(|v| Api {
inner: v.into_inner(),
})
.map_failure(|(s, e)| (s, ApiError::FormError(e)))
.map_error(|(s, e)| (s, ApiError::FormError(e)))
} else if request.content_type() == Some(&ContentType::JSON) {
Json::from_data(request, data)
.await
.map(|v| Api {
inner: v.into_inner(),
})
.map_failure(|(s, e)| (s, ApiError::JsonError(e)))
.map_error(|(s, e)| (s, ApiError::JsonError(e)))
} else {
Outcome::Failure((Status::NotAcceptable, ApiError::WasNeither))
Outcome::Error((Status::NotAcceptable, ApiError::WasNeither))
}
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ where
form_phantom: PhantomData,
json_phantom: PhantomData,
})
.map_failure(|(s, e)| (s, SplitApiError::FormError(e)))
.map_error(|(s, e)| (s, SplitApiError::FormError(e)))
} else if request.content_type() == Some(&ContentType::JSON) {
Json::from_data(request, data)
.await
Expand All @@ -123,9 +123,9 @@ where
form_phantom: PhantomData,
json_phantom: PhantomData,
})
.map_failure(|(s, e)| (s, SplitApiError::JsonError(e)))
.map_error(|(s, e)| (s, SplitApiError::JsonError(e)))
} else {
Outcome::Failure((Status::NotAcceptable, SplitApiError::WasNeither))
Outcome::Error((Status::NotAcceptable, SplitApiError::WasNeither))
}
}
}
28 changes: 14 additions & 14 deletions src/ephemeral/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'r> FromRequest<'r> for SessionCookie {
.map(|cookie| SessionCookie::from_str(cookie.value()));
match session {
Some(Ok(session)) => Outcome::Success(session),
_ => Outcome::Failure((Status::Unauthorized, "invalid session")),
_ => Outcome::Error((Status::Unauthorized, "invalid session")),
}
}
}
Expand All @@ -96,7 +96,7 @@ impl UserSession {
cookies: &CookieJar<'_>,
db: &DbConn,
) -> Result<()> {
cookies.remove_private(Cookie::named(SESSION_COOKIE));
cookies.remove_private(Cookie::from(SESSION_COOKIE));
self.session.invalidate(db).await?;
Ok(())
}
Expand All @@ -111,19 +111,19 @@ impl<'r> FromRequest<'r> for UserSession {
) -> Outcome<Self, Self::Error> {
let cookie = try_outcome!(request.guard::<SessionCookie>().await);
let db =
try_outcome!(request.guard::<DbConn>().await.map_failure(|_| {
try_outcome!(request.guard::<DbConn>().await.map_error(|_| {
(Status::InternalServerError, "could not connect to database")
}));

match Session::find_by_id(cookie.session_id, &db).await {
Ok(session) => match session.user(&db).await {
Ok(user) => Outcome::Success(UserSession { user, session }),
_ => Outcome::Failure((
_ => Outcome::Error((
Status::Unauthorized,
"user not found for database session",
)),
},
_ => Outcome::Failure((
_ => Outcome::Error((
Status::Unauthorized,
"session not found for valid cookie",
)),
Expand All @@ -148,7 +148,7 @@ impl<'r> FromRequest<'r> for AdminSession {
if user.admin {
Outcome::Success(AdminSession { admin: user })
} else {
Outcome::Failure((Status::Forbidden, "user is not an admin"))
Outcome::Error((Status::Forbidden, "user is not an admin"))
}
}
}
Expand All @@ -168,12 +168,12 @@ impl<'r> FromRequest<'r> for ClientSession {
) -> Outcome<Self, Self::Error> {
let headers: Vec<_> = request.headers().get("Authorization").collect();
if headers.is_empty() {
return Outcome::Failure((
return Outcome::Error((
Status::BadRequest,
"no authorization header found",
));
} else if headers.len() > 1 {
return Outcome::Failure((
return Outcome::Error((
Status::BadRequest,
"found more than one authorization header",
));
Expand All @@ -182,15 +182,15 @@ impl<'r> FromRequest<'r> for ClientSession {
let auth_header = headers[0];
let prefix = "Bearer ";
if !auth_header.starts_with(prefix) {
return Outcome::Failure((
return Outcome::Error((
Status::BadRequest,
"only support Bearer tokens are supported",
));
}
let key = &auth_header[prefix.len()..];

let db =
try_outcome!(request.guard::<DbConn>().await.map_failure(|_| {
try_outcome!(request.guard::<DbConn>().await.map_error(|_| {
(Status::InternalServerError, "could not connect to database")
}));

Expand All @@ -200,17 +200,17 @@ impl<'r> FromRequest<'r> for ClientSession {
Ok(Some(client)) => {
Outcome::Success(ClientSession { user, client })
},
_ => Outcome::Failure((
_ => Outcome::Error((
Status::Unauthorized,
"there is no client associated to this client session",
)),
},
_ => Outcome::Failure((
_ => Outcome::Error((
Status::Unauthorized,
"user not found for database session",
)),
},
_ => Outcome::Failure((
_ => Outcome::Error((
Status::Unauthorized,
"session not found for valid cookie",
)),
Expand Down Expand Up @@ -245,7 +245,7 @@ impl<'r> FromRequest<'r> for ClientOrUserSession {
client: Some(session.client),
})
},
_ => Outcome::Failure((
_ => Outcome::Error((
Status::Unauthorized,
"found neither a user session or client session",
)),
Expand Down
8 changes: 4 additions & 4 deletions src/http_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ impl<'r> FromRequest<'r> for BasicAuthentication {
) -> request::Outcome<Self, Self::Error> {
let headers: Vec<_> = request.headers().get("Authorization").collect();
if headers.is_empty() {
return Outcome::Failure((
return Outcome::Error((
Status::BadRequest,
String::from("Authorization header missing"),
));
} else if headers.len() > 1 {
return Outcome::Failure((
return Outcome::Error((
Status::BadRequest,
String::from("More than one authorization header"),
));
Expand All @@ -56,14 +56,14 @@ impl<'r> FromRequest<'r> for BasicAuthentication {
let auth_header = headers[0];
let prefix = "Basic ";
if !auth_header.starts_with(prefix) {
return Outcome::Failure((
return Outcome::Error((
Status::BadRequest,
String::from("We only support Basic Authentication"),
));
}
match BasicAuthentication::from_str(&auth_header[prefix.len()..]) {
Ok(credentials) => Outcome::Success(credentials),
Err(error_msg) => Outcome::Failure((Status::BadRequest, error_msg)),
Err(error_msg) => Outcome::Error((Status::BadRequest, error_msg)),
}
}
}
8 changes: 5 additions & 3 deletions tests/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
extern crate diesel;
extern crate rocket;

use common::HttpClient;
use rocket::http::Accept;
use rocket::http::ContentType;
use rocket::http::Status;
use zauth::models::user::User;

mod common;

Expand All @@ -15,7 +17,7 @@ use zauth::models::session::Session;

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

let client_form = format!("name={}", url(&client_name),);
Expand Down Expand Up @@ -75,7 +77,7 @@ async fn create_and_update_client() {

#[rocket::async_test]
async fn change_client_secret() {
common::as_admin(async move |http_client, db, _user| {
common::as_admin(async move |http_client: HttpClient, db, _user: User| {
let client = Client::create(
NewClient {
name: "test".to_string(),
Expand Down Expand Up @@ -104,7 +106,7 @@ async fn change_client_secret() {

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

let client_form = format!("name={}", url(&client_name),);
Expand Down
4 changes: 2 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
F: FnOnce(HttpClient, DbConn, User) -> R,
R: Future<Output = ()>,
{
as_visitor(async move |client, db| {
as_visitor(async move |client: HttpClient, db| {
let user = User::create(
NewUser {
username: String::from("username"),
Expand Down Expand Up @@ -140,7 +140,7 @@ where
F: FnOnce(HttpClient, DbConn, User) -> R,
R: Future<Output = ()>,
{
as_visitor(async move |client, db| {
as_visitor(async move |client: HttpClient, db| {
let mut user = User::create(
NewUser {
username: String::from("admin"),
Expand Down
11 changes: 6 additions & 5 deletions tests/mailinglist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate diesel;
extern crate rocket;

use common::HttpClient;
use rocket::http::{Accept, ContentType, Status};

use zauth::models::mail::NewMail;
Expand Down Expand Up @@ -60,7 +61,7 @@ async fn setup_test_users(db: &DbConn) {
/// new, subscribed user
#[rocket::async_test]
async fn mailinglist_workflow() {
common::as_admin(async move |http_client, db, admin| {
common::as_admin(async move |http_client: HttpClient, db, admin: User| {
setup_test_users(&db).await;

let subscribed_users = User::find_subscribed(&db).await.unwrap();
Expand Down Expand Up @@ -110,7 +111,7 @@ async fn mailinglist_workflow() {
/// Ensure that anyone can unsubscribe
#[rocket::async_test]
async fn visitor_can_unsubscribe() {
common::as_visitor(async move |http_client, db| {
common::as_visitor(async move |http_client: HttpClient, db| {
setup_test_users(&db).await;
let test_user = &User::find_subscribed(&db).await.unwrap()[0];
let test_token = &test_user.unsubscribe_token;
Expand Down Expand Up @@ -157,7 +158,7 @@ async fn visitor_can_unsubscribe() {
/// Ensure visitors cannot see mails pages
#[rocket::async_test]
async fn visitor_cannot_use_mailinglist() {
common::as_visitor(async move |http_client, _db| {
common::as_visitor(async move |http_client: HttpClient, _db| {
let mails_response = http_client.get("/mails").dispatch().await;
let new_mail_response = http_client.get("/mails/new").dispatch().await;
let specific_mail_response =
Expand Down Expand Up @@ -197,7 +198,7 @@ async fn visitor_cannot_use_mailinglist() {
/// Ensure users can see the mailinglist, but cannot create any mails
#[rocket::async_test]
async fn user_can_see_mailinglist() {
common::as_user(async move |http_client, db, user| {
common::as_user(async move |http_client: HttpClient, db, user: User| {
let test_mail = NewMail {
author: user.username,
subject: "foo".to_string(),
Expand Down Expand Up @@ -246,7 +247,7 @@ async fn user_can_see_mailinglist() {
/// Ensure admins can see mails pages and create new mails
#[rocket::async_test]
async fn admin_can_use_mailinglist() {
common::as_admin(async move |http_client, db, user| {
common::as_admin(async move |http_client: HttpClient, db, user: User| {
let test_mail = NewMail {
author: user.username,
subject: "foo".to_string(),
Expand Down
7 changes: 4 additions & 3 deletions tests/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ extern crate diesel;
extern crate rocket;

use chrono::{Duration, Utc};
use common::HttpClient;
use rocket::http::Status;
use zauth::models::session::*;

mod common;

#[rocket::async_test]
async fn valid_user_session() {
common::as_user(async move |http_client, _db, _user| {
common::as_user(async move |http_client: HttpClient, _db, _user| {
let response = http_client.get("/current_user").dispatch().await;
assert_eq!(response.status(), Status::Ok);
})
Expand All @@ -21,7 +22,7 @@ async fn valid_user_session() {

#[rocket::async_test]
async fn invalid_user_session() {
common::as_user(async move |http_client, db, _user| {
common::as_user(async move |http_client: HttpClient, db, _user| {
let mut session = Session::last(&db).await.expect("last session");
assert!(session.valid);

Expand All @@ -36,7 +37,7 @@ async fn invalid_user_session() {

#[rocket::async_test]
async fn expired_user_session() {
common::as_user(async move |http_client, db, _user| {
common::as_user(async move |http_client: HttpClient, db, _user| {
let mut session = Session::last(&db).await.expect("last session");
assert!(session.valid);

Expand Down
Loading

0 comments on commit 9c2ad8c

Please sign in to comment.