Skip to content

Commit

Permalink
test: add tests of auth request validation
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <[email protected]>
  • Loading branch information
bsbds committed Jul 21, 2023
1 parent e729852 commit f364e59
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion xline/src/request_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl From<ValidationError> for ExecuteError {
#[cfg(test)]
mod test {
use super::*;
use crate::rpc::{Request, RequestOp};
use crate::rpc::{Request, RequestOp, UserAddOptions};

#[test]
fn txn_check() {
Expand Down Expand Up @@ -460,4 +460,60 @@ mod test {
.to_string();
assert_eq!(message, expected_err_message);
}

#[test]
fn test_user_add_invalid() {
let user_add_request_with_empty_name = AuthUserAddRequest {
name: String::new(),
password: "pwd".to_owned(),
..Default::default()
};
let expected_err_message = ValidationError::new("User name is empty").to_string();
let message = user_add_request_with_empty_name
.validation(())
.unwrap_err()
.to_string();
assert_eq!(message, expected_err_message);

let user_add_request_with_no_password = AuthUserAddRequest {
name: "user".to_owned(),
password: String::new(),
options: Some(UserAddOptions { no_password: false }),
..Default::default()
};
let expected_err_message =
ValidationError::new("Password is required but not provided").to_string();
let message = user_add_request_with_no_password
.validation(())
.unwrap_err()
.to_string();
assert_eq!(message, expected_err_message);
}

#[test]
fn test_role_add_invalid() {
let role_add_request_with_empty_name = AuthRoleAddRequest {
name: String::new(),
};
let expected_err_message = ValidationError::new("Role name is empty").to_string();
let message = role_add_request_with_empty_name
.validation(())
.unwrap_err()
.to_string();
assert_eq!(message, expected_err_message);
}

#[test]
fn test_role_grant_perm_invalid() {
let role_grant_perm_without_perm = AuthRoleGrantPermissionRequest {
name: "role".to_owned(),
perm: None,
};
let expected_err_message = ValidationError::new("Permission not given").to_string();
let message = role_grant_perm_without_perm
.validation(())
.unwrap_err()
.to_string();
assert_eq!(message, expected_err_message);
}
}

0 comments on commit f364e59

Please sign in to comment.