This is an automated email from the ASF dual-hosted git repository. mssun pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git
commit 87f3668b6ee28cef936ad1df0a75e3da9b67028a Author: Mingshen Sun <[email protected]> AuthorDate: Sun Feb 2 11:37:18 2020 -0800 [proto] Provide constructor (new) for simple messages --- services/authentication/enclave/src/api_service.rs | 20 ++----- services/frontend/enclave/src/service.rs | 2 +- .../proto/src/teaclave_authentication_service.rs | 54 +++++++++++++++++- services/proto/src/teaclave_common.rs | 10 +++- .../enclave/src/teaclave_authentication_service.rs | 64 +++++----------------- 5 files changed, 78 insertions(+), 72 deletions(-) diff --git a/services/authentication/enclave/src/api_service.rs b/services/authentication/enclave/src/api_service.rs index da5eb66..ae425df 100644 --- a/services/authentication/enclave/src/api_service.rs +++ b/services/authentication/enclave/src/api_service.rs @@ -111,10 +111,7 @@ pub mod tests { } pub fn test_user_register() { - let request = UserRegisterRequest { - id: "test_register_id".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_register_id", "test_password"); let request = Request::new(request); let service = get_mock_service(); assert!(service.user_register(request).is_ok()); @@ -122,16 +119,10 @@ pub mod tests { pub fn test_user_login() { let service = get_mock_service(); - let request = UserRegisterRequest { - id: "test_login_id".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_login_id", "test_password"); let request = Request::new(request); assert!(service.user_register(request).is_ok()); - let request = UserLoginRequest { - id: "test_login_id".to_string(), - password: "test_password".to_string(), - }; + let request = UserLoginRequest::new("test_login_id", "test_password"); let request = Request::new(request); let response = service.user_login(request); assert!(response.is_ok()); @@ -140,10 +131,7 @@ pub mod tests { assert!(user.validate_token(&service.jwt_secret, &token)); info!("saved user_info: {:?}", user); - let request = UserLoginRequest { - id: "test_login_id".to_string(), - password: "test_password1".to_string(), - }; + let request = UserLoginRequest::new("test_login_id", "test_password1"); let request = Request::new(request); assert!(service.user_login(request).is_err()); } diff --git a/services/frontend/enclave/src/service.rs b/services/frontend/enclave/src/service.rs index 86d9ba1..7ffd390 100644 --- a/services/frontend/enclave/src/service.rs +++ b/services/frontend/enclave/src/service.rs @@ -85,7 +85,7 @@ impl TeaclaveFrontendService { .metadata .get("token") .ok_or_else(|| anyhow!("Missing credential"))?; - let credential = UserCredential::new(&id, &token); + let credential = UserCredential::new(id, token); let auth_request = UserAuthenticateRequest { credential }; let auth_response = self .authentication_client diff --git a/services/proto/src/teaclave_authentication_service.rs b/services/proto/src/teaclave_authentication_service.rs index dd38701..e2ae30b 100644 --- a/services/proto/src/teaclave_authentication_service.rs +++ b/services/proto/src/teaclave_authentication_service.rs @@ -1,6 +1,7 @@ use anyhow::anyhow; use anyhow::{Error, Result}; use core::convert::TryInto; +use std::prelude::v1::*; use crate::teaclave_authentication_service_proto as proto; use crate::teaclave_common; @@ -19,8 +20,21 @@ pub struct UserRegisterRequest { pub password: std::string::String, } -#[derive(Debug)] -pub struct UserRegisterResponse {} +impl UserRegisterRequest { + pub fn new<S, T>(id: S, password: T) -> Self + where + S: Into<String>, + T: Into<String>, + { + Self { + id: id.into(), + password: password.into(), + } + } +} + +#[derive(Debug, Default)] +pub struct UserRegisterResponse; #[derive(Debug)] pub struct UserLoginRequest { @@ -28,21 +42,57 @@ pub struct UserLoginRequest { pub password: std::string::String, } +impl UserLoginRequest { + pub fn new<S, T>(id: S, password: T) -> Self + where + S: Into<String>, + T: Into<String>, + { + Self { + id: id.into(), + password: password.into(), + } + } +} + #[derive(Debug)] pub struct UserLoginResponse { pub token: std::string::String, } +impl UserLoginResponse { + pub fn new<S>(token: S) -> Self + where + S: Into<String>, + { + Self { + token: token.into(), + } + } +} + #[derive(Debug)] pub struct UserAuthenticateRequest { pub credential: teaclave_common::UserCredential, } +impl UserAuthenticateRequest { + pub fn new(credential: teaclave_common::UserCredential) -> Self { + Self { credential } + } +} + #[derive(Debug)] pub struct UserAuthenticateResponse { pub accept: bool, } +impl UserAuthenticateResponse { + pub fn new(accept: bool) -> Self { + Self { accept } + } +} + impl std::convert::TryFrom<proto::UserRegisterRequest> for UserRegisterRequest { type Error = Error; diff --git a/services/proto/src/teaclave_common.rs b/services/proto/src/teaclave_common.rs index fe98ec5..8cdbe4c 100644 --- a/services/proto/src/teaclave_common.rs +++ b/services/proto/src/teaclave_common.rs @@ -16,10 +16,14 @@ pub struct UserCredential { } impl UserCredential { - pub fn new(id: &str, token: &str) -> Self { + pub fn new<S, T>(id: S, token: T) -> Self + where + S: Into<String>, + T: Into<String>, + { Self { - id: id.to_owned(), - token: token.to_owned(), + id: id.into(), + token: token.into(), } } } diff --git a/tests/functional_tests/enclave/src/teaclave_authentication_service.rs b/tests/functional_tests/enclave/src/teaclave_authentication_service.rs index fc39059..443f6b2 100644 --- a/tests/functional_tests/enclave/src/teaclave_authentication_service.rs +++ b/tests/functional_tests/enclave/src/teaclave_authentication_service.rs @@ -68,17 +68,11 @@ fn get_internal_client() -> TeaclaveAuthenticationInternalClient { fn test_login_success() { let mut client = get_api_client(); - let request = UserRegisterRequest { - id: "test_login_id1".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_login_id1", "test_password"); let response_result = client.user_register(request); assert!(response_result.is_ok()); - let request = UserLoginRequest { - id: "test_login_id1".to_string(), - password: "test_password".to_string(), - }; + let request = UserLoginRequest::new("test_login_id1", "test_password"); let response_result = client.user_login(request); info!("{:?}", response_result); assert!(response_result.is_ok()); @@ -86,17 +80,11 @@ fn test_login_success() { fn test_login_fail() { let mut client = get_api_client(); - let request = UserRegisterRequest { - id: "test_login_id2".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_login_id2", "test_password"); let response_result = client.user_register(request); assert!(response_result.is_ok()); - let request = UserLoginRequest { - id: "test_login_id2".to_string(), - password: "wrong_password".to_string(), - }; + let request = UserLoginRequest::new("test_login_id2", "wrong_password"); let response_result = client.user_login(request); info!("{:?}", response_result); assert!(response_result.is_err()); @@ -105,24 +93,15 @@ fn test_login_fail() { fn test_authenticate_success() { let mut api_client = get_api_client(); let mut internal_client = get_internal_client(); - let request = UserRegisterRequest { - id: "test_authenticate_id1".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_authenticate_id1", "test_password"); let response_result = api_client.user_register(request); assert!(response_result.is_ok()); - let request = UserLoginRequest { - id: "test_authenticate_id1".to_string(), - password: "test_password".to_string(), - }; + let request = UserLoginRequest::new("test_authenticate_id1", "test_password"); let response_result = api_client.user_login(request); assert!(response_result.is_ok()); - let credential = UserCredential { - id: "test_authenticate_id1".to_string(), - token: response_result.unwrap().token, - }; - let request = UserAuthenticateRequest { credential }; + let credential = UserCredential::new("test_authenticate_id1", response_result.unwrap().token); + let request = UserAuthenticateRequest::new(credential); let response_result = internal_client.user_authenticate(request); info!("{:?}", response_result); assert!(response_result.unwrap().accept); @@ -132,18 +111,12 @@ fn test_authenticate_fail() { let mut api_client = get_api_client(); let mut internal_client = get_internal_client(); - let request = UserRegisterRequest { - id: "test_authenticate_id2".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_authenticate_id2", "test_password"); let response_result = api_client.user_register(request); assert!(response_result.is_ok()); - let credential = UserCredential { - id: "test_authenticate_id2".to_string(), - token: "wrong_token".to_string(), - }; - let request = UserAuthenticateRequest { credential }; + let credential = UserCredential::new("test_authenticate_id2", "wrong_token"); + let request = UserAuthenticateRequest::new(credential); let response_result = internal_client.user_authenticate(request); info!("{:?}", response_result); assert!(!response_result.unwrap().accept); @@ -151,10 +124,7 @@ fn test_authenticate_fail() { fn test_register_success() { let mut client = get_api_client(); - let request = UserRegisterRequest { - id: "test_register_id1".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_register_id1", "test_password"); let response_result = client.user_register(request); info!("{:?}", response_result); assert!(response_result.is_ok()); @@ -162,16 +132,10 @@ fn test_register_success() { fn test_register_fail() { let mut client = get_api_client(); - let request = UserRegisterRequest { - id: "test_register_id2".to_string(), - password: "test_password".to_string(), - }; + let request = UserRegisterRequest::new("test_register_id2", "test_password"); let response_result = client.user_register(request); assert!(response_result.is_ok()); - let request = UserRegisterRequest { - id: "test_register_id2".to_string(), - password: "test_password2".to_string(), - }; + let request = UserRegisterRequest::new("test_register_id2", "test_password"); let response_result = client.user_register(request); info!("{:?}", response_result); assert!(response_result.is_err()); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
