Skip to content

Commit

Permalink
feat(auth-domain): impl user_authenticate() with test
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Dec 8, 2024
1 parent c3a899b commit e5fbf09
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions crates/auth-domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ pub enum CreateUserError {
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
pub enum AuthenticationError {
/// Indicates that an error occurred while fetching users.
#[error(transparent)]
#[error("Failed to fetch user")]
FetchError(#[from] FetchModelError),
/// Indicates that an error occurred while fetching users by index.
#[error("Failed to fetch user by index")]
FetchByIndexError(#[from] FetchModelByIndexError),
}

/// The authentication service trait.
Expand Down Expand Up @@ -135,9 +138,14 @@ impl<

async fn user_authenticate(
&self,
_creds: UserAuthCredentials,
creds: UserAuthCredentials,
) -> Result<Option<User>, AuthenticationError> {
todo!()
let user = match creds {
UserAuthCredentials::EmailEntryOnly(email) => {
self.fetch_user_by_email(email).await?
}
};
Ok(user)
}
}

Expand Down Expand Up @@ -265,4 +273,29 @@ mod tests {
let user2 = service.user_signup(user_2_req).await;
assert!(matches!(user2, Err(CreateUserError::EmailAlreadyUsed(_))));
}

#[tokio::test]
async fn test_user_authenticate() {
let user_repo = MockModelRepository::<User, UserCreateRequest>::new();
let service = AuthDomainServiceCanonical::new(Arc::new(user_repo));

let email = EmailAddress::try_new("[email protected]").unwrap();
let user_1_req = UserCreateRequest {
email: email.clone(),
name: HumanName::try_new("Test User 1").unwrap(),
auth: UserAuthCredentials::EmailEntryOnly(email.clone()),
};
let user = service.user_signup(user_1_req).await.unwrap();
assert_eq!(user.email, email);

let creds = UserAuthCredentials::EmailEntryOnly(email.clone());
let auth_user = service.user_authenticate(creds).await.unwrap();
assert_eq!(auth_user, Some(user));

let creds = UserAuthCredentials::EmailEntryOnly(
EmailAddress::try_new("[email protected]").unwrap(),
);
let auth_user = service.user_authenticate(creds).await.unwrap();
assert_eq!(auth_user, None);
}
}

0 comments on commit e5fbf09

Please sign in to comment.