Skip to content

Commit

Permalink
Add updateTrustedDomain GraphQL API
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
henry0715-dev committed Dec 3, 2024
1 parent dd41e12 commit 9b07b68
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Added the `updateTrustedDomain` GraphQL API, allowing you to modify trusted
domains.

### Fixed

- Resolved an issue in the `applyNode` GraphQL API, where configuration values
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tokio = "1"
tower-http = { version = "0.6", features = ["fs", "trace"] }
tracing = "0.1"
vinum = { git = "https://github.com/vinesystems/vinum.git", tag = "1.0.3" }
regex = "1"

[dev-dependencies]
assert-json-diff = "2"
Expand Down
1 change: 1 addition & 0 deletions src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ mod tests {
#[tokio::test]
async fn unimplemented_agent_manager() {
let agent_manager = super::MockAgentManager {};
assert!(agent_manager.broadcast_trusted_domains().await.is_ok());
assert!(agent_manager
.broadcast_trusted_user_agent_list(&[])
.await
Expand Down
88 changes: 86 additions & 2 deletions src/graphql/trusted_domain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_graphql::{
connection::{Connection, EmptyFields},
Context, Object, Result, SimpleObject,
Context, InputObject, Object, Result, SimpleObject,
};

use super::{AgentManager, BoxedAgentManager, Role, RoleGuard};
Expand Down Expand Up @@ -62,6 +62,29 @@ impl TrustedDomainMutation {
Ok(name)
}

/// Update a trusted domain, returning the new value.
#[graphql(guard = "RoleGuard::new(Role::SystemAdministrator)
.or(RoleGuard::new(Role::SecurityAdministrator))")]
async fn update_trusted_domain(
&self,
ctx: &Context<'_>,
old: TrustedDomainInput,
new: TrustedDomainInput,
) -> Result<String> {
let name = {
let store = crate::graphql::get_store(ctx).await?;
let map = store.trusted_domain_map();
let old = review_database::TrustedDomain::from(old);
let new = review_database::TrustedDomain::from(new);
map.update(&old, &new)?;
new.name
};

let agent_manager = ctx.data::<BoxedAgentManager>()?;
agent_manager.broadcast_trusted_domains().await?;
Ok(name)
}

/// Removes a trusted domain, returning the old value if it existed.
#[graphql(
guard = "RoleGuard::new(Role::SystemAdministrator).or(RoleGuard::new(Role::SecurityAdministrator))"
Expand Down Expand Up @@ -94,6 +117,21 @@ impl From<review_database::TrustedDomain> for TrustedDomain {
}
}

#[derive(InputObject)]
pub(super) struct TrustedDomainInput {
name: String,
remarks: String,
}

impl From<TrustedDomainInput> for review_database::TrustedDomain {
fn from(input: TrustedDomainInput) -> Self {
Self {
name: input.name,
remarks: input.remarks,
}
}
}

async fn load(
ctx: &Context<'_>,
after: Option<String>,
Expand All @@ -108,7 +146,9 @@ async fn load(

#[cfg(test)]
mod tests {
use crate::graphql::TestSchema;
use std::net::SocketAddr;

use crate::graphql::{BoxedAgentManager, MockAgentManager, TestSchema};

#[tokio::test]
async fn trusted_domain_list() {
Expand Down Expand Up @@ -157,4 +197,48 @@ mod tests {
r#"{trustedDomainList: {edges: [{node: {name: "example2.org"}}]}}"#
);
}

#[tokio::test]
async fn update_trusted_domain() {
let agent_manager: BoxedAgentManager = Box::new(MockAgentManager {});
let test_addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
let schema = TestSchema::new_with(agent_manager, Some(test_addr)).await;
let insert_query = r#"
mutation {
insertTrustedDomain(
name: "test.com"
remarks: "origin_remarks"
)
}
"#;
let update_query = r#"
mutation {
updateTrustedDomain(
old: {
name: "test.com"
remarks: "origin_remarks"
}
new: {
name: "test2.com"
remarks: "updated_remarks"
}
)
}
"#;

let res = schema.execute(update_query).await;
assert_eq!(
res.errors.first().unwrap().message,
"no such entry".to_string()
);

let res = schema.execute(insert_query).await;
assert_eq!(res.data.to_string(), r#"{insertTrustedDomain: "test.com"}"#);

let res = schema.execute(update_query).await;
assert_eq!(
res.data.to_string(),
r#"{updateTrustedDomain: "test2.com"}"#
);
}
}

0 comments on commit 9b07b68

Please sign in to comment.