use openlark_core::{
SDKResult,
api::{ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
use crate::common::api_endpoints::DriveApi;
#[derive(Debug, Clone)]
pub struct AuthPermissionMemberRequest {
config: Config,
pub token: String,
pub file_type: String,
pub action: String,
}
impl AuthPermissionMemberRequest {
pub fn new(
config: Config,
token: impl Into<String>,
file_type: impl Into<String>,
action: impl Into<String>,
) -> Self {
Self {
config,
token: token.into(),
file_type: file_type.into(),
action: action.into(),
}
}
pub async fn execute(self) -> SDKResult<AuthPermissionMemberResponse> {
self.execute_with_options(openlark_core::req_option::RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
option: openlark_core::req_option::RequestOption,
) -> SDKResult<AuthPermissionMemberResponse> {
validate_required!(self.token, "token 不能为空");
validate_required!(self.file_type, "file_type 不能为空");
validate_required!(self.action, "action 不能为空");
match self.action.as_str() {
"view" | "edit" | "share" | "comment" | "export" | "copy" | "print"
| "manage_public" => {}
_ => {
return Err(openlark_core::error::validation_error(
"action",
"action 必须为 view/edit/share/comment/export/copy/print/manage_public",
));
}
}
let api_endpoint = DriveApi::AuthPermissionMember(self.token.clone());
let api_request = api_endpoint
.to_request::<AuthPermissionMemberResponse>()
.query("type", &self.file_type)
.query("action", &self.action);
Transport::request_typed(api_request, &self.config, Some(option), "授权").await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthPermissionMemberResponse {
pub auth_result: bool,
}
impl ApiResponseTrait for AuthPermissionMemberResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_permission_member_request_builder() {
let config = Config::default();
let request = AuthPermissionMemberRequest::new(config, "file_token", "docx", "view");
assert_eq!(request.token, "file_token");
assert_eq!(request.file_type, "docx");
assert_eq!(request.action, "view");
}
#[test]
fn test_response_trait() {
assert_eq!(
AuthPermissionMemberResponse::data_format(),
ResponseFormat::Data
);
}
#[test]
fn test_supported_actions() {
let config = Config::default();
for action in [
"view",
"edit",
"share",
"comment",
"export",
"copy",
"print",
"manage_public",
] {
let request = AuthPermissionMemberRequest::new(
config.clone(),
"token",
"docx",
action.to_string(),
);
assert_eq!(request.action, action);
}
}
#[test]
fn test_response_structure() {
let response = AuthPermissionMemberResponse { auth_result: true };
assert!(response.auth_result);
let response2 = AuthPermissionMemberResponse { auth_result: false };
assert!(!response2.auth_result);
}
#[tokio::test]
async fn test_auth_permission_member_returns_data_on_success() {
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path(
"/open-apis/drive/v1/permissions/file_token_001/members/auth",
))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0,
"msg": "success",
"data": {
"auth_result": true
}
})))
.mount(&server)
.await;
let config = Config::builder()
.app_id("ci_app_id")
.app_secret("ci_app_secret")
.base_url(server.uri())
.enable_token_cache(false)
.build();
let resp = AuthPermissionMemberRequest::new(config, "file_token_001", "docx", "view")
.execute()
.await
.expect("权限判断应成功");
assert!(resp.auth_result);
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/drive/v1/permissions/file_token_001/members/auth"
);
let query = received[0].url.query().unwrap_or("");
assert!(
query.contains("type=docx"),
"query 应携带 type=docx,实际:{query}"
);
assert!(
query.contains("action=view"),
"query 应携带 action=view,实际:{query}"
);
}
}