From 673ec7404648d938ae458ecad98eaba982095da4 Mon Sep 17 00:00:00 2001 From: Przemyslaw Swierczek Date: Thu, 29 Oct 2020 14:55:28 +0100 Subject: [PATCH] Added Mocking For Tokens Endpoint in Tests aligning tests with DoLogin changes in: bef8ffb1bdc611e6c3785ad00ffa785a7589a386 Signed-off-by: Przemyslaw Swierczek --- sdk/mock_data_test.go | 6 ++++ sdk/vra7_sdk_test.go | 45 ++++++++++++++++++++++++--- vra7/mock_data_test.go | 6 ++-- vra7/resource_vra7_deployment_test.go | 4 +++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/sdk/mock_data_test.go b/sdk/mock_data_test.go index 2ea76c9d..a29ca1d8 100644 --- a/sdk/mock_data_test.go +++ b/sdk/mock_data_test.go @@ -1,6 +1,12 @@ package sdk var ( + validAuthResponse = `{ + "expires":"2019-02-26T03:32:35.000Z", + "id":"MTU1MTEyMzE1NTc5ODpiYTZkYjdhNjZlNGNkYjZmZTBiMjp0ZW5hbnQ6cWV1c2VybmFtZTpmcml0ekBjb2tlLnNxYS1ob3Jpem9uLmxvY2FsZXhwaXJhdGlvbjoxNTUxMTUxOTU1MDAwOmMyNGVjNTFiNzE1OTJhZDZjNTljMTUwMDkxMjcyNzUyZDkzNzQ0ODRkMTVlZGFhNWM0MDhjYmQ3YTM2MTljZGNiNjM3MjM1NmY1MzZlYTk1YzUyMGZiZDVjMTkzMzg3YjQzZmMwNmNlMGI5YjJkZmIwNzhlZGU2NzdiNTk3MWFk", + "tenant":"qe" + }` + requestTemplateResponse = `{ "type":"com.vmware.vcac.catalog.domain.request.CatalogItemProvisioningRequest", "catalogItemId":"feaedf73-560c-4612-a573-41667e017691", diff --git a/sdk/vra7_sdk_test.go b/sdk/vra7_sdk_test.go index 92fa531c..4265ef40 100644 --- a/sdk/vra7_sdk_test.go +++ b/sdk/vra7_sdk_test.go @@ -28,12 +28,15 @@ func init() { func TestGetCatalogItemRequestTemplate(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) catalogItemID := "feaedf73-560c-4612-a573-41667e017691" path := fmt.Sprintf(RequestTemplateAPI, catalogItemID) url := client.BuildEncodedURL(path, nil) - httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, requestTemplateResponse)) @@ -59,11 +62,14 @@ func TestReadCatalogItemNameByID(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) catalogItemID := "e5dd4fba-45ed-4943-b1fc-7f96239286be" path := fmt.Sprintf(EntitledCatalogItems+"/"+"%s", catalogItemID) url := client.BuildEncodedURL(path, nil) - httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, catalogItemResp)) @@ -79,10 +85,13 @@ func TestReadCatalogItemNameByID(t *testing.T) { func TestReadCatalogItemByName(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) url := client.BuildEncodedURL(EntitledCatalogItemViewsAPI, map[string]string{ "page": "1"}) - httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, entitledCatalogItemViewsResponse)) @@ -98,10 +107,13 @@ func TestGetBusinessGroupID(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) path := Tenants + "/" + mockTenant + "/subtenants" url := client.BuildEncodedURL(path, nil) - httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, subTenantsResponse)) id, err := client.GetBusinessGroupID("Development", mockTenant) @@ -112,10 +124,15 @@ func TestGetBusinessGroupID(t *testing.T) { func TestGetRequestStatus(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) mockRequestID := "adca9535-4a35-4981-8864-28643bd990b0" path := fmt.Sprintf(ConsumerRequests+"/"+"%s", mockRequestID) url := client.BuildEncodedURL(path, nil) + httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, requestStatusResponse)) requestStatus, err := client.GetRequestStatus(mockRequestID) @@ -136,10 +153,15 @@ func TestGetRequestStatus(t *testing.T) { func TestGetRequestResourceView(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) mockRequestID := "594bf7ec-c8d2-4a0d-8477-553ed987aa48" path := fmt.Sprintf(GetRequestResourceViewAPI, mockRequestID) url := client.BuildEncodedURL(path, nil) + httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, deploymentStateResponse)) resourceView, err := client.GetRequestResourceView(mockRequestID, 1) @@ -160,6 +182,10 @@ func TestGetRequestResources(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //auth + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) mockRequestID := "6ec160e5-41c5-4b1d-8ddc-e89c426957c6" path := fmt.Sprintf(GetRequestResourcesAPI, mockRequestID) @@ -183,13 +209,19 @@ func TestGetRequestResources(t *testing.T) { func TestGetResourceActionTemplate(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) mockResourceID := "0786a919-3545-408d-9091-cc8fe24e7790" mockActionID := "1a22752b-31a9-462e-a38a-e42b60c08a78" - // test for delete action template getActionTemplatePath := fmt.Sprintf(GetActionTemplateAPI, mockResourceID, mockActionID) url := client.BuildEncodedURL(getActionTemplatePath, nil) + httpmock.RegisterResponder("POST", tokens_url, + httpmock.NewStringResponder(200, validAuthResponse)) + httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, deleteActionTemplateResponse)) actionTemplte, err := client.GetResourceActionTemplate(mockResourceID, mockActionID) @@ -198,6 +230,7 @@ func TestGetResourceActionTemplate(t *testing.T) { //test for reconfigure action tenplate httpmock.Reset() + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, reconfigureActionTemplateResponse)) actionTemplte, err = client.GetResourceActionTemplate(mockResourceID, mockActionID) utils.AssertNilError(t, err) @@ -206,6 +239,7 @@ func TestGetResourceActionTemplate(t *testing.T) { // invalid resource id mockResourceID = "gd78tegd-0e737egd-jhdg" httpmock.Reset() + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(10101, invalidResourceErrorResponse)) actionTemplte, err = client.GetResourceActionTemplate(mockResourceID, mockActionID) @@ -215,6 +249,7 @@ func TestGetResourceActionTemplate(t *testing.T) { //invalid action id mockActionID = "7364g-8736eg-87736" httpmock.Reset() + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(50505, systemExceptionResponse)) actionTemplte, err = client.GetResourceActionTemplate(mockResourceID, mockActionID) diff --git a/vra7/mock_data_test.go b/vra7/mock_data_test.go index cda98061..d3e20bce 100644 --- a/vra7/mock_data_test.go +++ b/vra7/mock_data_test.go @@ -1,14 +1,14 @@ package vra7 var ( - validAuthResponse = `{ + validAuthResponse = `{ "expires":"2019-02-26T03:32:35.000Z", "id":"MTU1MTEyMzE1NTc5ODpiYTZkYjdhNjZlNGNkYjZmZTBiMjp0ZW5hbnQ6cWV1c2VybmFtZTpmcml0ekBjb2tlLnNxYS1ob3Jpem9uLmxvY2FsZXhwaXJhdGlvbjoxNTUxMTUxOTU1MDAwOmMyNGVjNTFiNzE1OTJhZDZjNTljMTUwMDkxMjcyNzUyZDkzNzQ0ODRkMTVlZGFhNWM0MDhjYmQ3YTM2MTljZGNiNjM3MjM1NmY1MzZlYTk1YzUyMGZiZDVjMTkzMzg3YjQzZmMwNmNlMGI5YjJkZmIwNzhlZGU2NzdiNTk3MWFk", "tenant":"qe" }` - errorAuthResponse = `{ - "errors":[ + errorAuthResponse = `{ + "errors":[ { "code":90135, "source":null, diff --git a/vra7/resource_vra7_deployment_test.go b/vra7/resource_vra7_deployment_test.go index 600e623f..e62e992f 100644 --- a/vra7/resource_vra7_deployment_test.go +++ b/vra7/resource_vra7_deployment_test.go @@ -25,6 +25,10 @@ func TestConfigValidityFunction(t *testing.T) { httpmock.ActivateNonDefault(client.Client) defer httpmock.DeactivateAndReset() + //register tokens mock + tokens_path := sdk.Tokens + tokens_url := client.BuildEncodedURL(tokens_path, nil) + httpmock.RegisterResponder("POST", tokens_url, httpmock.NewStringResponder(200, validAuthResponse)) catalogItemID := "dhbh-jhdv-ghdv-dhvdd"