pub struct TestUser {
pub id: Uuid,
pub username: String,
pub email: String,
pub permissions: Vec<String>,
pub roles: Vec<String>,
pub is_authenticated: bool,
pub attributes: HashMap<String, Value>,
}Expand description
A test user for simulating authentication in tests.
This struct represents a user that can be injected into the test context to simulate authenticated requests.
§Example
ⓘ
// Anonymous user
let anon = TestUser::anonymous();
// Simple authenticated user
let user = TestUser::authenticated("alice");
// Admin user with full permissions
let admin = TestUser::admin();
// Custom user with specific attributes
let custom = TestUser::authenticated("bob")
.with_email("bob@example.com")
.with_permission("admin:read")
.with_role("moderator");Fields§
§id: UuidUnique user identifier.
username: StringUsername.
email: StringEmail address.
permissions: Vec<String>List of permissions granted to this user.
roles: Vec<String>List of roles assigned to this user.
is_authenticated: boolWhether the user is authenticated.
attributes: HashMap<String, Value>Additional custom attributes.
Implementations§
Source§impl TestUser
impl TestUser
Sourcepub fn authenticated(username: impl Into<String>) -> Self
pub fn authenticated(username: impl Into<String>) -> Self
Create an authenticated user with the given username.
Sourcepub fn with_email(self, email: impl Into<String>) -> Self
pub fn with_email(self, email: impl Into<String>) -> Self
Set the email address.
Sourcepub fn with_permission(self, permission: impl Into<String>) -> Self
pub fn with_permission(self, permission: impl Into<String>) -> Self
Add a permission.
Sourcepub fn with_permissions<S: Into<String>>(
self,
permissions: impl IntoIterator<Item = S>,
) -> Self
pub fn with_permissions<S: Into<String>>( self, permissions: impl IntoIterator<Item = S>, ) -> Self
Add multiple permissions.
Sourcepub fn with_roles<S: Into<String>>(
self,
roles: impl IntoIterator<Item = S>,
) -> Self
pub fn with_roles<S: Into<String>>( self, roles: impl IntoIterator<Item = S>, ) -> Self
Add multiple roles.
Sourcepub fn with_attribute(
self,
key: impl Into<String>,
value: impl Into<Value>,
) -> Self
pub fn with_attribute( self, key: impl Into<String>, value: impl Into<Value>, ) -> Self
Add a custom attribute.
Sourcepub fn has_permission(&self, permission: &str) -> bool
pub fn has_permission(&self, permission: &str) -> bool
Check if the user has a specific permission.
Also checks for wildcard (*) permission.
Sourcepub fn has_any_permission(&self, permissions: &[&str]) -> bool
pub fn has_any_permission(&self, permissions: &[&str]) -> bool
Check if the user has any of the given permissions.
Sourcepub fn has_all_permissions(&self, permissions: &[&str]) -> bool
pub fn has_all_permissions(&self, permissions: &[&str]) -> bool
Check if the user has all of the given permissions.
Sourcepub fn get_attribute(&self, key: &str) -> Option<&Value>
pub fn get_attribute(&self, key: &str) -> Option<&Value>
Get a custom attribute.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for TestUser
impl<'de> Deserialize<'de> for TestUser
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for TestUser
impl RefUnwindSafe for TestUser
impl Send for TestUser
impl Sync for TestUser
impl Unpin for TestUser
impl UnsafeUnpin for TestUser
impl UnwindSafe for TestUser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().Source§impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
Source§fn should_contain_message(&self, expected: &str)where
E: Display,
fn should_contain_message(&self, expected: &str)where
E: Display,
Assert that the error message contains the specified text.
Source§fn should_have_message(&self, expected: &str)where
E: Display,
fn should_have_message(&self, expected: &str)where
E: Display,
Assert that the error message matches exactly.