Skip to content

Commit

Permalink
feat: add invalid_credentials_test
Browse files Browse the repository at this point in the history
  • Loading branch information
raimundo-henriques authored and zeitgeist committed Oct 31, 2023
1 parent b3b99bd commit d76b9a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion endpoint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ pub(crate) struct AccessDenied {
}

/// RFC 6749 OAuth 2.0 Error Response
#[serde(crate = "rocket::serde")]
#[derive(Serialize, JsonSchema, PartialEq, Debug)]
#[serde(crate = "rocket::serde")]
pub(crate) struct OAuth2ErrorMessage {
pub(crate) error: &'static str,
pub(crate) error_description: &'static str,
}


#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Debug)]
#[serde(crate = "rocket::serde")]
/// Response with an error code of `BadRequest`. See Chapter "Error Codes" of the Tech Specs for mor details.
Expand Down
34 changes: 34 additions & 0 deletions endpoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ use std::cmp::min;
use auth::UserToken;
use chrono::{DateTime, Utc};
use either::Either;

use lambda_web::{is_running_on_lambda, launch_rocket_on_lambda, LambdaError};
use okapi::openapi3::{Object, Parameter, ParameterValue};
use rocket::catch;
use rocket::form::Form;
use rocket::request::FromRequest;

use rocket::serde::json::Json;
use rocket_okapi::rapidoc::{
make_rapidoc, GeneralConfig, HideShowConfig, RapiDocConfig, Theme, UiConfig,
Expand Down Expand Up @@ -447,6 +449,38 @@ async fn main() -> Result<(), LambdaError> {
#[cfg(test)]
const EXAMPLE_HOST: &str = "api.pathfinder.sine.dev";

#[test]
fn invalid_credentials_test() {
let auth_uri = "/2/auth/token";

let credentials = base64::encode("hello:wrong_password");
let basic_auth = format!("Basic {credentials}");
let client = &Client::tracked(create_server()).unwrap();

let resp = client
.post(auth_uri)
.header(rocket::http::Header::new("Host", EXAMPLE_HOST))
.header(rocket::http::Header::new("Authorization", basic_auth))
.header(rocket::http::Header::new(
"Content-Type",
"application/x-www-form-urlencoded",
))
.body("grant_type=client_credentials")
.dispatch();

let error_response: std::collections::HashMap<String, String> = resp.into_json().unwrap();

println!("error_response = {error_response:#?}");
assert_eq!(
error_response.get("error"),
Some(&"unauthorized_client".to_string())
);
assert_eq!(
error_response.get("error_description"),
Some(&"Invalid client credentials".to_string())
);
}

#[test]
fn get_list_test() {
let token = UserToken {
Expand Down

0 comments on commit d76b9a8

Please sign in to comment.