diff --git a/apitester b/apitester new file mode 100755 index 0000000..5e03ef5 Binary files /dev/null and b/apitester differ diff --git a/check_results.go b/check_results.go index 0c99f27..8d54c5a 100644 --- a/check_results.go +++ b/check_results.go @@ -102,7 +102,7 @@ func BodyContains(testCase *TestCase) (bool, error) { // search through all the results to see if the expected value is there for i := range results { if strings.Contains(results[i].(string), v) { - fmt.Printf("Expected value of '%s' matches, success\n", v) + fmt.Printf("Expected value of '%s' matches\n", v) checkFound = true break } @@ -123,13 +123,25 @@ func BodyContains(testCase *TestCase) (bool, error) { func BodyHasKeys(testCase *TestCase) (bool, error) { for _, key := range testCase.Expect.Body.HasKeys { - // If the body response was json then we stored it already when running the test - if _, ok := testCase.ResultBodyMap[key]; ok { - //fmt.Printf("Confirmed body has key '%s'\n", key) + + results := findKey(key, testCase.ResultBodyMap) + + if len(results) > 0 { + fmt.Printf("Found key '%s' with values %v\n", key, results) } else { fmt.Printf("Body HasKeys test failed, expected key '%s' not found\n", key) return false, nil } + + /* + // If the body response was json then we stored it already when running the test + if _, ok := testCase.ResultBodyMap[key]; ok { + //fmt.Printf("Confirmed body has key '%s'\n", key) + } else { + fmt.Printf("Body HasKeys test failed, expected key '%s' not found\n", key) + return false, nil + } + */ } return true, nil diff --git a/group-test.json b/group-test.json new file mode 100644 index 0000000..e3bad13 --- /dev/null +++ b/group-test.json @@ -0,0 +1,57 @@ +{ + "name": "CBS group testing", + "testCases": { + "test1": { + "path": "/api/login", + "method": "POST", + "description": "Perform login", + "body": { + "username": "Administrator", + "password": "Password123" + }, + "expect": { + "header": { + "contains": { + "http_status": "200", + "Content-Type": "application/json" + } + }, + "body": { + "hasKeys": ["access_token"] + } + } + }, + "test2": { + "path": "/api/admin/groups", + "method": "GET", + "description": "get group listing", + "expect": { + "header": { + "contains": { + "http_status": "200" + } + }, + "body": { + "hasKeys": ["groupName"], + "contains": { + "message": "success" + } + } + } + } + }, + "url": "https://10.63.39.130:8443", + "insecure": true, + "header": { + "Content-Type": "application/json", + "Authorization": "Bearer %ACCESS_TOKEN%" + }, + "capture": { + "test1": { + "body": { + "access_token": "%ACCESS_TOKEN%" + } + } + } +} + \ No newline at end of file diff --git a/main.go b/main.go index 2091cd1..e269184 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ func main() { // Process command line arguments flag.StringVar(&inputJson, "input", "./tests.json", "Full path to input json test definition file") + flag.Parse() // Read the json input file if fileExists(inputJson) { @@ -51,6 +52,7 @@ func main() { } RunTest(&testDefinitions.TestCases[i]) + fmt.Printf("\nRunning checks on output\n") success, err := CheckResults(&testDefinitions.TestCases[i]) if err != nil { fmt.Printf("Error running test case : %s\n", err) @@ -60,11 +62,13 @@ func main() { fmt.Printf("Test result failed\n") os.Exit(1) } else { - fmt.Printf("Tests successful\n") + fmt.Printf("\nTest '%s' successful\n", testDefinitions.TestCases[i].Name) } } + fmt.Printf("\nCongratulations, all tests passed!\n\n") + // For debugging, just dump the output //fmt.Printf("%+v\n", testDefinitions) } diff --git a/run_tests.go b/run_tests.go index b24b602..e011a88 100644 --- a/run_tests.go +++ b/run_tests.go @@ -25,7 +25,7 @@ func RunTest(testCase *TestCase) error { } client := &http.Client{Transport: tr} - fmt.Printf("\nRunning %s : %s\n", testCase.Name, testCase.Description) + fmt.Printf("\nRunning %s : %s\n\n", testCase.Name, testCase.Description) // Determine URL if len(testDefinitions.BaseUrl) > 0 { diff --git a/tests.json b/tests.json index e99d452..0def9a2 100644 --- a/tests.json +++ b/tests.json @@ -99,7 +99,7 @@ }, "test10": { "path": "/api/admin/unlock", - "disabled": true, + "disabled": false, "method": "POST", "description": "provide unlock key", "body": { @@ -115,7 +115,7 @@ }, "test11": { "path": "/api/admin/unlock", - "disabled": true, + "disabled": false, "method": "POST", "description": "repeat provide unlock key", "body": { @@ -129,6 +129,26 @@ } } }, + "test12": { + "path": "/api/secret/store", + "disabled": true, + "method": "POST", + "description": "store first new secret", + "body": { + "safeId": 1, + "deviceName": "avcp01.cdc.home", + "deviceCategory": "appliance", + "userName": "service@cdc.home", + "secretValue": "TheWiggles" + }, + "expect": { + "header": { + "contains": { + "http_status": "200" + } + } + } + }, "test20": { "path": "/api/secret/list", "disabled": false, @@ -179,8 +199,30 @@ } } } - }, + }, "test23": { + "path": "/api/secret/retrieve", + "method": "POST", + "description": "get updated secret", + "body": { + "deviceName": "adcp01.cdc.home", + "deviceCategory": "windows-server", + "userName": "dummy@cdc.home" + }, + "expect": { + "header": { + "contains": { + "http_status": "200" + } + }, + "body": { + "contains": { + "deviceName": "adcp01.cdc.home" + } + } + } + }, + "test24": { "path": "/api/secret/delete", "method": "POST", "description": "delete existing secret", @@ -197,7 +239,7 @@ } } }, - "test24": { + "test25": { "path": "/api/secret/store", "method": "POST", "description": "store new secret", @@ -216,7 +258,7 @@ } } }, - "test25": { + "test26": { "path": "/api/secret/retrieve", "method": "POST", "description": "test retrieving the new secret",