From 20d69228d3af9cd9ca7044dcecb0b5a935f7732d Mon Sep 17 00:00:00 2001 From: Adam Kol <93466081+AdamKol-up9@users.noreply.github.com> Date: Sun, 16 Jan 2022 09:57:14 +0200 Subject: [PATCH] Cypress: new Redact and NoRedact tests (#618) --- acceptanceTests/cypress.json | 3 + .../cypress/integration/tests/RedactTests.js | 23 ++++ acceptanceTests/tap_test.go | 109 +----------------- 3 files changed, 28 insertions(+), 107 deletions(-) create mode 100644 acceptanceTests/cypress/integration/tests/RedactTests.js diff --git a/acceptanceTests/cypress.json b/acceptanceTests/cypress.json index 48150bbb3..44ae98c89 100644 --- a/acceptanceTests/cypress.json +++ b/acceptanceTests/cypress.json @@ -4,10 +4,13 @@ "viewportHeight": 1080, "video": false, "screenshotOnRunFailure": false, + "testFiles": ["tests/GuiPort.js", "tests/MultipleNamespaces.js", + "tests/RedactTests.js", "tests/Regex.js"], + "env": { "testUrl": "http://localhost:8899/" } diff --git a/acceptanceTests/cypress/integration/tests/RedactTests.js b/acceptanceTests/cypress/integration/tests/RedactTests.js new file mode 100644 index 000000000..7005eeebf --- /dev/null +++ b/acceptanceTests/cypress/integration/tests/RedactTests.js @@ -0,0 +1,23 @@ +const inHeader = 'User-Header[REDACTED]'; +const inBody = '{ "User": "[REDACTED]" }'; +const shouldExist = Cypress.env('shouldExist'); + +it('Loading Mizu', function () { + cy.visit(Cypress.env('testUrl')); +}) + +it(`should ${shouldExist ? '' : 'not'} include ${inHeader}`, function () { + cy.get('.CollapsibleContainer', { timeout : 15 * 1000}).first().next().then(headerElements => { //TODO change the path and refactor the body and head functions + const allText = headerElements.text(); + if (allText.includes(inHeader) !== shouldExist) + throw new Error(`The headers panel doesnt include ${inHeader}`); + }); +}); + +it(`should ${shouldExist ? '' : 'not'} include ${inBody}`, function () { + cy.get('.hljs').then(bodyElement => { + const line = bodyElement.text(); + if (line.includes(inBody) !== shouldExist) + throw new Error(`The body panel doesnt include ${inBody}`); + }); +}); diff --git a/acceptanceTests/tap_test.go b/acceptanceTests/tap_test.go index d8f309d11..381c5ed0f 100644 --- a/acceptanceTests/tap_test.go +++ b/acceptanceTests/tap_test.go @@ -3,7 +3,6 @@ package acceptanceTests import ( "archive/zip" "bytes" - "encoding/json" "fmt" "io/ioutil" "net/http" @@ -378,59 +377,7 @@ func TestTapRedact(t *testing.T) { } } - redactCheckFunc := 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 - } - 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{}) - - headers := request["_headers"].([]interface{}) - for _, headerInterface := range headers { - header := headerInterface.(map[string]interface{}) - if header["name"].(string) != "User-Header" { - continue - } - - userHeader := header["value"].(string) - if userHeader != "[REDACTED]" { - return fmt.Errorf("unexpected result - user agent is not redacted") - } - } - - postData := request["postData"].(map[string]interface{}) - textDataStr := postData["text"].(string) - - var textData map[string]string - if parseErr := json.Unmarshal([]byte(textDataStr), &textData); parseErr != nil { - return fmt.Errorf("failed to parse text data, err: %v", parseErr) - } - - if textData["User"] != "[REDACTED]" { - return fmt.Errorf("unexpected result - user in body is not redacted") - } - - return nil - } - if err := retriesExecute(shortRetriesCount, redactCheckFunc); err != nil { - t.Errorf("%v", err) - return - } + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/RedactTests.js\" --env shouldExist=true")) } func TestTapNoRedact(t *testing.T) { @@ -482,59 +429,7 @@ func TestTapNoRedact(t *testing.T) { } } - redactCheckFunc := 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 - } - 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{}) - - headers := request["_headers"].([]interface{}) - for _, headerInterface := range headers { - header := headerInterface.(map[string]interface{}) - if header["name"].(string) != "User-Header" { - continue - } - - userHeader := header["value"].(string) - if userHeader == "[REDACTED]" { - return fmt.Errorf("unexpected result - user agent is redacted") - } - } - - postData := request["postData"].(map[string]interface{}) - textDataStr := postData["text"].(string) - - var textData map[string]string - if parseErr := json.Unmarshal([]byte(textDataStr), &textData); parseErr != nil { - return fmt.Errorf("failed to parse text data, err: %v", parseErr) - } - - if textData["User"] == "[REDACTED]" { - return fmt.Errorf("unexpected result - user in body is redacted") - } - - return nil - } - if err := retriesExecute(shortRetriesCount, redactCheckFunc); err != nil { - t.Errorf("%v", err) - return - } + runCypressTests(t, "npx cypress run --spec \"cypress/integration/tests/RedactTests.js\" --env shouldExist=false") } func TestTapRegexMasking(t *testing.T) {