Skip to content

Commit 43c1a6d

Browse files
committed
refine tests, add data sanitization test
1 parent 7039b06 commit 43c1a6d

File tree

5 files changed

+84
-17
lines changed

5 files changed

+84
-17
lines changed

plugins/wpgraphql-logging/tests/e2e/constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ export const GET_POSTS_QUERY = `
88
id
99
title
1010
date
11+
excerpt
12+
author {
13+
node {
14+
id
15+
name
16+
}
17+
}
1118
}
1219
}
1320
}

plugins/wpgraphql-logging/tests/e2e/specs/basic-usage.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ test.describe("Configure WPGraphQL Logging Plugin and Verify Logging Works", ()
3030

3131
await expect(page.locator(".notice.notice-success")).toBeVisible();
3232

33-
// Execute a GraphQL query
3433
const response = await executeGraphQLQuery(request, GET_POSTS_QUERY);
3534
expect(response.ok()).toBeTruthy();
3635

plugins/wpgraphql-logging/tests/e2e/specs/data-cleanup.spec.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
configureDataManagement,
44
goToLoggingSettingsPage,
55
resetPluginSettings,
6+
switchToSettingsTab,
67
} from "../utils";
78

89
test.describe("Data Management - Configure Automatic Cleanup", () => {
@@ -29,10 +30,7 @@ test.describe("Data Management - Configure Automatic Cleanup", () => {
2930
// Reload the page to verify settings persisted
3031
await page.reload({ waitUntil: "networkidle" });
3132

32-
await page
33-
.locator("#wpbody-content")
34-
.getByRole("link", { name: "Data Management" })
35-
.click();
33+
await switchToSettingsTab(page, "Data Management");
3634

3735
const deletionCheckbox = page.locator(
3836
'input[name="wpgraphql_logging_settings[data_management][data_deletion_enabled]"]'

plugins/wpgraphql-logging/tests/e2e/specs/exclude-and-sanitize.spec.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
goToLoggingSettingsPage,
44
goToLogsListPage,
55
configureLogging,
6+
configureDataManagement,
67
executeGraphQLQuery,
78
resetPluginSettings,
89
} from "../utils";
@@ -44,36 +45,58 @@ test.describe("Exclude Sensitive Queries from Logging", () => {
4445
).toBeVisible();
4546
});
4647

47-
test("should log queries when not excluded", async ({
48+
test("should sanitize sensitive data in logs when sanitization is enabled", async ({
4849
page,
4950
admin,
5051
request,
5152
}) => {
52-
// Set up logging without excluded queries
53+
// Set up logging settings and execute a GraphQL query
5354
await goToLoggingSettingsPage(admin);
55+
await expect(page.locator("h1")).toHaveText("WPGraphQL Logging Settings");
5456

5557
await configureLogging(page, {
5658
enabled: true,
5759
dataSampling: "100",
5860
eventLogSelection: ["graphql_request_results"],
59-
excludeQueries: "",
6061
});
6162

62-
await expect(page.locator(".notice.notice-success")).toBeVisible();
63+
await goToLoggingSettingsPage(admin);
64+
await configureDataManagement(page, {
65+
dataSanitizationEnabled: true,
66+
dataSanitizationMethod: "custom",
67+
dataSanitizationCustomFieldAnonymize: "request.app_context.viewer",
68+
});
6369

64-
// Execute query
70+
// Navigate to log details page
6571
await executeGraphQLQuery(request, GET_POSTS_QUERY);
6672

67-
// Navigate to logs and verify query is logged
6873
await goToLogsListPage(admin);
6974
await expect(page.locator("h1")).toContainText("WPGraphQL Logs");
7075

71-
// Verify GetPosts query is logged
72-
const getPostsLog = page
76+
const logRow = page
7377
.locator("#the-list tr")
74-
.filter({ hasText: "GetPosts" });
75-
await expect(getPostsLog).toBeVisible({ timeout: 10000 });
76-
});
78+
.filter({ hasText: "GetPosts" })
79+
.first();
80+
await expect(logRow).toBeVisible({ timeout: 10000 });
81+
82+
const viewLink = logRow.locator(".row-actions .view a");
83+
await expect(viewLink).toBeVisible();
84+
await viewLink.focus();
85+
await viewLink.click();
7786

78-
// TODO add sanitization tests here
87+
await expect(page.locator("h1")).toContainText("Log Entry");
88+
89+
const logTable = page.locator(".widefat.striped");
90+
const contextRow = logTable
91+
.locator("tr")
92+
.filter({ has: page.locator("th", { hasText: "Context" }) });
93+
94+
await expect(contextRow).toBeVisible();
95+
96+
// Verify sanitization in the content
97+
const contextContent = await contextRow.locator("td pre").textContent();
98+
99+
expect(contextContent).toBeTruthy();
100+
expect(contextContent).toContain('"viewer": "***"');
101+
});
79102
});

plugins/wpgraphql-logging/tests/e2e/utils.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ export async function getLogDetails(page, logId) {
130130
return details;
131131
}
132132

133+
/**
134+
* Switch to a settings tab
135+
*/
136+
export async function switchToSettingsTab(page, tabName) {
137+
await page
138+
.locator("#wpbody-content")
139+
.getByRole("link", { name: "Data Management" })
140+
.click();
141+
}
142+
133143
/**
134144
* Configure data management settings
135145
*/
@@ -139,6 +149,9 @@ export async function configureDataManagement(page, settings = {}) {
139149
dataRetentionDays = "30",
140150
dataSanitizationEnabled = false,
141151
dataSanitizationMethod = "recommended",
152+
dataSanitizationCustomFieldAnonymize = "",
153+
dataSanitizationCustomFieldRemove = "",
154+
dataSanitizationCustomFieldTruncate = "",
142155
} = settings;
143156

144157
// Switch to Data Management tab
@@ -178,6 +191,33 @@ export async function configureDataManagement(page, settings = {}) {
178191
)
179192
.selectOption(dataSanitizationMethod);
180193

194+
// Set custom field anonymize (if provided)
195+
if (dataSanitizationCustomFieldAnonymize) {
196+
await page
197+
.locator(
198+
'input[name="wpgraphql_logging_settings[data_management][data_sanitization_custom_field_anonymize]"]'
199+
)
200+
.fill(dataSanitizationCustomFieldAnonymize);
201+
}
202+
203+
// Set custom field remove (if provided)
204+
if (dataSanitizationCustomFieldRemove) {
205+
await page
206+
.locator(
207+
'input[name="wpgraphql_logging_settings[data_management][data_sanitization_custom_field_remove]"]'
208+
)
209+
.fill(dataSanitizationCustomFieldRemove);
210+
}
211+
212+
// Set custom field truncate (if provided)
213+
if (dataSanitizationCustomFieldTruncate) {
214+
await page
215+
.locator(
216+
'input[name="wpgraphql_logging_settings[data_management][data_sanitization_custom_field_truncate]"]'
217+
)
218+
.fill(dataSanitizationCustomFieldTruncate);
219+
}
220+
181221
await page.getByRole("button", { name: "Save Changes" }).click();
182222
await page.waitForSelector(".notice.notice-success");
183223
}

0 commit comments

Comments
 (0)