From cc4638afe69aa3dcd557eac989bccbd7068d4b49 Mon Sep 17 00:00:00 2001 From: Adam Kol <93466081+AdamKol-up9@users.noreply.github.com> Date: Tue, 18 Jan 2022 17:32:50 +0200 Subject: [PATCH] Cypress: two new tests --> IgnoredUserAgents and RegexMasking (#663) --- .../integration/configurations/Default.json} | 16 ++-- .../integration/configurations/HugeMizu.json | 14 +++ .../integration/tests/IgnoredUserAgents.js | 33 +++++++ .../integration/tests/MultipleNamespaces.js | 1 - .../cypress/integration/tests/RegexMasking.js | 7 ++ acceptanceTests/tap_test.go | 87 ++----------------- 6 files changed, 71 insertions(+), 87 deletions(-) rename acceptanceTests/{cypress.json => cypress/integration/configurations/Default.json} (51%) create mode 100644 acceptanceTests/cypress/integration/configurations/HugeMizu.json create mode 100644 acceptanceTests/cypress/integration/tests/IgnoredUserAgents.js create mode 100644 acceptanceTests/cypress/integration/tests/RegexMasking.js diff --git a/acceptanceTests/cypress.json b/acceptanceTests/cypress/integration/configurations/Default.json similarity index 51% rename from acceptanceTests/cypress.json rename to acceptanceTests/cypress/integration/configurations/Default.json index a0fe7f827..68621f7ef 100644 --- a/acceptanceTests/cypress.json +++ b/acceptanceTests/cypress/integration/configurations/Default.json @@ -4,17 +4,19 @@ "viewportHeight": 1080, "video": false, "screenshotOnRunFailure": false, - - "testFiles": - ["tests/GuiPort.js", - "tests/MultipleNamespaces.js", - "tests/Redact.js", + "testFiles": [ + "tests/GuiPort.js", + "tests/MultipleNamespaces.js", + "tests/Redact.js", "tests/NoRedact.js", - "tests/Regex.js"], + "tests/Regex.js", + "tests/RegexMasking.js" + ], "env": { "testUrl": "http://localhost:8899/", "redactHeaderContent": "User-Header[REDACTED]", - "redactBodyContent": "{ \"User\": \"[REDACTED]\" }" + "redactBodyContent": "{ \"User\": \"[REDACTED]\" }", + "regexMaskingBodyContent": "[REDACTED]" } } diff --git a/acceptanceTests/cypress/integration/configurations/HugeMizu.json b/acceptanceTests/cypress/integration/configurations/HugeMizu.json new file mode 100644 index 000000000..969bbab10 --- /dev/null +++ b/acceptanceTests/cypress/integration/configurations/HugeMizu.json @@ -0,0 +1,14 @@ +{ + "watchForFileChanges":false, + "viewportWidth": 1920, + "viewportHeight": 3500, + "video": false, + "screenshotOnRunFailure": false, + "testFiles": [ + "tests/IgnoredUserAgents.js" + ], + + "env": { + "testUrl": "http://localhost:8899/" + } +} diff --git a/acceptanceTests/cypress/integration/tests/IgnoredUserAgents.js b/acceptanceTests/cypress/integration/tests/IgnoredUserAgents.js new file mode 100644 index 000000000..46b569efa --- /dev/null +++ b/acceptanceTests/cypress/integration/tests/IgnoredUserAgents.js @@ -0,0 +1,33 @@ +import {isValueExistsInElement} from "../testHelpers/TrafficHelper"; + +it('Loading Mizu', function () { + cy.visit(Cypress.env('testUrl')); +}); + +it('going through each entry', function () { + cy.get('#total-entries').then(number => { + const getNum = () => { + const numOfEntries = number.text(); + return parseInt(numOfEntries); + }; + cy.wrap({ there: getNum }).invoke('there').should('be.gte', 25); + + checkThatAllEntriesShown(); + + const entriesNum = getNum(); + [...Array(entriesNum).keys()].map(checkEntry); + }); +}); + +function checkThatAllEntriesShown() { + cy.get('#entries-length').then(number => { + if (number.text() === '1') + cy.get('[title="Fetch old records"]').click(); + }); +} + +function checkEntry(entryIndex) { + cy.get(`#entry-${entryIndex}`).click(); + cy.get('#tbody-Headers').should('be.visible'); + isValueExistsInElement(false, 'Ignored-User-Agent', '#tbody-Headers'); +} diff --git a/acceptanceTests/cypress/integration/tests/MultipleNamespaces.js b/acceptanceTests/cypress/integration/tests/MultipleNamespaces.js index 96b032e10..c27a3636e 100644 --- a/acceptanceTests/cypress/integration/tests/MultipleNamespaces.js +++ b/acceptanceTests/cypress/integration/tests/MultipleNamespaces.js @@ -15,4 +15,3 @@ function doItFunc(number) { findLineAndCheck(getExpectedDetailsDict(podName, namespace)); }); } - diff --git a/acceptanceTests/cypress/integration/tests/RegexMasking.js b/acceptanceTests/cypress/integration/tests/RegexMasking.js new file mode 100644 index 000000000..2eb620f64 --- /dev/null +++ b/acceptanceTests/cypress/integration/tests/RegexMasking.js @@ -0,0 +1,7 @@ +import {isValueExistsInElement} from "../testHelpers/TrafficHelper"; + +it('Loading Mizu', function () { + cy.visit(Cypress.env('testUrl')); +}) + +isValueExistsInElement(true, Cypress.env('regexMaskingBodyContent'), '.hljs'); diff --git a/acceptanceTests/tap_test.go b/acceptanceTests/tap_test.go index 4a61db1ee..fa33c4681 100644 --- a/acceptanceTests/tap_test.go +++ b/acceptanceTests/tap_test.go @@ -138,7 +138,7 @@ func TestTapGuiPort(t *testing.T) { return } - runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/GuiPort.js\" --env port=%d", guiPort)) + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/GuiPort.js\" --env port=%d --config-file cypress/integration/configurations/Default.json", guiPort)) }) } } @@ -184,7 +184,7 @@ func TestTapAllNamespaces(t *testing.T) { return } - runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/MultipleNamespaces.js\" --env name1=%v,name2=%v,name3=%v,namespace1=%v,namespace2=%v,namespace3=%v", + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/MultipleNamespaces.js\" --env name1=%v,name2=%v,name3=%v,namespace1=%v,namespace2=%v,namespace3=%v --config-file cypress/integration/configurations/Default.json", expectedPods[0].Name, expectedPods[1].Name, expectedPods[2].Name, expectedPods[0].Namespace, expectedPods[1].Namespace, expectedPods[2].Namespace)) } @@ -233,7 +233,7 @@ func TestTapMultipleNamespaces(t *testing.T) { return } - runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/MultipleNamespaces.js\" --env name1=%v,name2=%v,name3=%v,namespace1=%v,namespace2=%v,namespace3=%v", + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/MultipleNamespaces.js\" --env name1=%v,name2=%v,name3=%v,namespace1=%v,namespace2=%v,namespace3=%v --config-file cypress/integration/configurations/Default.json", expectedPods[0].Name, expectedPods[1].Name, expectedPods[2].Name, expectedPods[0].Namespace, expectedPods[1].Namespace, expectedPods[2].Namespace)) } @@ -279,7 +279,7 @@ func TestTapRegex(t *testing.T) { return } - runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/Regex.js\" --env name=%v,namespace=%v", + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/Regex.js\" --env name=%v,namespace=%v --config-file cypress/integration/configurations/Default.json", expectedPods[0].Name, expectedPods[0].Namespace)) } @@ -377,7 +377,7 @@ func TestTapRedact(t *testing.T) { } } - runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/Redact.js\"")) + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/Redact.js\" --config-file cypress/integration/configurations/Default.json")) } func TestTapNoRedact(t *testing.T) { @@ -429,7 +429,7 @@ func TestTapNoRedact(t *testing.T) { } } - runCypressTests(t, "npx cypress run --spec \"cypress/integration/tests/NoRedact.js\"") + runCypressTests(t, "npx cypress run --spec \"cypress/integration/tests/NoRedact.js\" --config-file cypress/integration/configurations/Default.json") } func TestTapRegexMasking(t *testing.T) { @@ -480,41 +480,8 @@ func TestTapRegexMasking(t *testing.T) { } } - redactCheckFunc := func() error { - timestamp := time.Now().UnixNano() / int64(time.Millisecond) + runCypressTests(t, "npx cypress run --spec \"cypress/integration/tests/RegexMasking.js\" --config-file cypress/integration/configurations/Default.json") - entries, err := getDBEntries(timestamp, defaultEntriesCount, 1*time.Second) - if err != nil { - return err - } - err = checkEntriesAtLeast(entries, 1) - if err != nil { - return err - } - firstEntry := entries[0] - - entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, firstEntry["id"]) - requestResult, requestErr := executeHttpGetRequest(entryUrl) - if requestErr != nil { - return fmt.Errorf("failed to get entry, err: %v", requestErr) - } - - entry := requestResult.(map[string]interface{})["data"].(map[string]interface{}) - request := entry["request"].(map[string]interface{}) - - postData := request["postData"].(map[string]interface{}) - textData := postData["text"].(string) - - if textData != "[REDACTED]" { - return fmt.Errorf("unexpected result - body is not redacted") - } - - return nil - } - if err := retriesExecute(shortRetriesCount, redactCheckFunc); err != nil { - t.Errorf("%v", err) - return - } } func TestTapIgnoredUserAgents(t *testing.T) { @@ -575,45 +542,7 @@ func TestTapIgnoredUserAgents(t *testing.T) { } } - ignoredUserAgentsCheckFunc := func() error { - timestamp := time.Now().UnixNano() / int64(time.Millisecond) - - entries, err := getDBEntries(timestamp, defaultEntriesCount, 1*time.Second) - if err != nil { - return err - } - err = checkEntriesAtLeast(entries, 1) - if err != nil { - return err - } - - for _, entryInterface := range entries { - entryUrl := fmt.Sprintf("%v/entries/%v", apiServerUrl, entryInterface["id"]) - requestResult, requestErr := executeHttpGetRequest(entryUrl) - if requestErr != nil { - return fmt.Errorf("failed to get entry, err: %v", requestErr) - } - - entry := requestResult.(map[string]interface{})["data"].(map[string]interface{}) - request := entry["request"].(map[string]interface{}) - - headers := request["_headers"].([]interface{}) - for _, headerInterface := range headers { - header := headerInterface.(map[string]interface{}) - if header["name"].(string) != ignoredUserAgentCustomHeader { - continue - } - - return fmt.Errorf("unexpected result - user agent is not ignored") - } - } - - return nil - } - if err := retriesExecute(shortRetriesCount, ignoredUserAgentsCheckFunc); err != nil { - t.Errorf("%v", err) - return - } + runCypressTests(t, "npx cypress run --spec \"cypress/integration/tests/IgnoredUserAgents.js\" --config-file cypress/integration/configurations/HugeMizu.json") } func TestTapDumpLogs(t *testing.T) {