Skip to content

Commit

Permalink
Fix create_draft_update
Browse files Browse the repository at this point in the history
- Updated `create_draft_update` to ensure `name_draft` is not dependent on `new.profile_draft`.
  • Loading branch information
BLYKIM authored Aug 28, 2024
1 parent 50e5cc0 commit 3b4862c
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 7 deletions.
195 changes: 195 additions & 0 deletions src/graphql/node/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,199 @@ mod tests {
let res = schema.execute(r#"{nodeList{totalCount}}"#).await;
assert_eq!(res.data.to_string(), r#"{nodeList: {totalCount: 0}}"#);
}

#[tokio::test]
async fn update_node_name() {
let schema = TestSchema::new().await;

// check empty
let res = schema.execute(r#"{nodeList{totalCount}}"#).await;
assert_eq!(res.data.to_string(), r#"{nodeList: {totalCount: 0}}"#);

// insert node
let res = schema
.execute(
r#"mutation {
insertNode(
name: "admin node",
customerId: 0,
description: "This is the admin node running review.",
hostname: "admin.aice-security.com",
agents: [{
key: "reconverge@analysis"
kind: RECONVERGE
status: ENABLED
},
{
key: "piglet@collect"
kind: PIGLET
status: ENABLED
}]
giganto: null
)
}"#,
)
.await;
assert_eq!(res.data.to_string(), r#"{insertNode: "0"}"#);

// check node count after insert
let res = schema.execute(r#"{nodeList{totalCount}}"#).await;
assert_eq!(res.data.to_string(), r#"{nodeList: {totalCount: 1}}"#);

// check inserted node
let res = schema
.execute(
r#"{node(id: "0") {
id
name
nameDraft
profile {
customerId
description
hostname
}
profileDraft {
customerId
description
hostname
}
agents {
key
kind
status
}
giganto {
status
draft
}
}}"#,
)
.await;

assert_json_eq!(
res.data.into_json().unwrap(),
json!({
"node": {
"id": "0",
"name": "admin node",
"nameDraft": "admin node",
"profile": null,
"profileDraft": {
"customerId": "0",
"description": "This is the admin node running review.",
"hostname": "admin.aice-security.com",
},
"agents": [{
"key": "reconverge@analysis",
"kind": "RECONVERGE",
"status": "ENABLED",
},
{
"key": "piglet@collect",
"kind": "PIGLET",
"status": "ENABLED",
}],
"giganto": null
}
})
);

// update node (update name, update profile_draft to null)
let res = schema
.execute(
r#"mutation {
updateNodeDraft(
id: "0"
old: {
name: "admin node",
nameDraft: "admin node",
profile: null,
profileDraft: {
customerId: 0,
description: "This is the admin node running review.",
hostname: "admin.aice-security.com",
}
agents: [
{
key: "reconverge@analysis",
kind: "RECONVERGE",
status: "ENABLED",
config: null,
draft: null
},
{
key: "piglet@collect",
kind: "PIGLET",
status: "ENABLED",
config: null,
draft: null
}
],
giganto: null,
},
new: {
nameDraft: "AdminNode",
agents: [
{
key: "reconverge@analysis",
kind: "RECONVERGE",
status: "ENABLED",
config: null,
draft: null
},
{
key: "piglet@collect",
kind: "PIGLET",
status: "ENABLED",
config: null,
draft: null
}
],
giganto: null,
}
)
}"#,
)
.await;
assert_eq!(res.data.to_string(), r#"{updateNodeDraft: "0"}"#);

// check node count after update
let res = schema.execute(r#"{nodeList{totalCount}}"#).await;
assert_eq!(res.data.to_string(), r#"{nodeList: {totalCount: 1}}"#);

// check updated node
let res = schema
.execute(
r#"{node(id: "0") {
id
name
nameDraft
profile {
customerId
description
hostname
}
profileDraft {
customerId
description
hostname
}
}}"#,
)
.await;

assert_json_eq!(
res.data.into_json().unwrap(),
json!({
"node": {
"id": "0",
"name": "admin node", // stays the same
"nameDraft": "AdminNode", // updated
"profile": null,
"profileDraft": null, // updated
}
})
);
}
}
15 changes: 8 additions & 7 deletions src/graphql/node/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ pub(super) fn create_draft_update(
old: &NodeInput,
new: NodeDraftInput,
) -> Result<review_database::NodeUpdate> {
let (name_draft, profile_draft) = if let Some(draft) = new.profile_draft {
if new.name_draft.is_empty() {
return Err("missing name draft".into());
}
(Some(new.name_draft), Some(draft.try_into()?))
if new.name_draft.is_empty() {
return Err("missing name draft".into());
}

let profile_draft = if let Some(draft) = new.profile_draft {
Some(draft.try_into()?)
} else {
(None, None)
None
};

let agents: Vec<review_database::Agent> = if let Some(agents) = new.agents {
Expand All @@ -120,7 +121,7 @@ pub(super) fn create_draft_update(

Ok(review_database::NodeUpdate {
name: Some(old.name.clone()),
name_draft,
name_draft: Some(new.name_draft),
profile: old.profile.clone().map(TryInto::try_into).transpose()?,
profile_draft,
agents,
Expand Down

0 comments on commit 3b4862c

Please sign in to comment.