Refactor tests to improve response parsing and validation
- Introduced a new common module with response helper functions to streamline response parsing across tests. - Updated role controller tests to assert success messages in responses for create, update, and delete operations. - Enhanced role service tests to validate response structures and ensure proper error handling. - Modified team controller and service tests to check for success messages and validate response data formats. - Improved user controller and service tests to include success message assertions and error handling for invalid inputs. - Ensured all tests utilize the new response parsing functions for consistency and maintainability.
This commit is contained in:
@@ -115,13 +115,9 @@ async fn test_admin_team_endpoints_sensitive_data_exposure() {
|
||||
|
||||
assert!(response.status().is_success(), "Admin team list should return success");
|
||||
|
||||
let response_body = match response.into_body().into_string().await {
|
||||
Ok(body) => body,
|
||||
Err(e) => panic!("Failed to read response body: {}", e),
|
||||
};
|
||||
|
||||
let response_json: ResponseListSuccessDto<Vec<AdminTeamsListItemDto>> =
|
||||
serde_json::from_str(&response_body).unwrap();
|
||||
let v = crate::common::response_helpers::parse_response_value(response, 8192).await;
|
||||
let response_json: ResponseListSuccessDto<Vec<AdminTeamsListItemDto>> =
|
||||
serde_json::from_value(v).unwrap();
|
||||
|
||||
// Verify sensitive fields are present in admin response
|
||||
assert!(response_json.data.iter().any(|team| {
|
||||
@@ -140,14 +136,9 @@ async fn test_admin_team_endpoints_sensitive_data_exposure() {
|
||||
|
||||
assert!(response.status().is_success(), "Admin team detail should return success");
|
||||
|
||||
let response_body = match response.into_body().into_string().await {
|
||||
Ok(body) => body,
|
||||
Err(e) => panic!("Failed to read response body: {}", e),
|
||||
};
|
||||
|
||||
let response_json: ResponseSuccessDto<AdminTeamsDetailItemDto> =
|
||||
serde_json::from_str(&response_body).unwrap();
|
||||
|
||||
let v = crate::common::response_helpers::parse_response_value(response, 8192).await;
|
||||
let response_json: ResponseSuccessDto<AdminTeamsDetailItemDto> =
|
||||
serde_json::from_value(v).unwrap();
|
||||
let admin_team = response_json.data;
|
||||
|
||||
// Verify sensitive fields are present
|
||||
@@ -175,14 +166,9 @@ async fn test_admin_team_endpoints_sensitive_data_exposure() {
|
||||
|
||||
assert!(response.status().is_success(), "Admin team members should return success");
|
||||
|
||||
let response_body = match response.into_body().into_string().await {
|
||||
Ok(body) => body,
|
||||
Err(e) => panic!("Failed to read response body: {}", e),
|
||||
};
|
||||
|
||||
let response_json: ResponseSuccessDto<Vec<TeamMemberDto>> =
|
||||
serde_json::from_str(&response_body).unwrap();
|
||||
|
||||
let v = crate::common::response_helpers::parse_response_value(response, 8192).await;
|
||||
let response_json: ResponseSuccessDto<Vec<TeamMemberDto>> =
|
||||
serde_json::from_value(v).unwrap();
|
||||
let admin_members = response_json.data;
|
||||
|
||||
// Verify all members have sensitive info
|
||||
@@ -264,6 +250,12 @@ async fn test_admin_team_endpoints_permission_guard() {
|
||||
).await;
|
||||
|
||||
assert_eq!(response.status().as_u16(), 403, "Regular user should get forbidden for admin endpoints");
|
||||
// Also assert response body contains a permission/forbidden message
|
||||
let v = crate::common::response_helpers::parse_response_value(response, 1024).await;
|
||||
let msg = v.get("message").and_then(|m| m.as_str()).unwrap_or("");
|
||||
let msg_l = msg.to_lowercase();
|
||||
assert!(msg_l.contains("forbidden") || msg_l.contains("permission") || msg_l.contains("not authorized") || msg_l.contains("unauthorized"),
|
||||
"permission guard response should include a forbidden/permission message");
|
||||
|
||||
// Clean up
|
||||
let _ = repo.query_delete_team(team_id).await;
|
||||
|
||||
Reference in New Issue
Block a user