Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Added Mocking For Tokens Endpoint in Tests
Browse files Browse the repository at this point in the history
aligning tests with DoLogin changes in: bef8ffb

Signed-off-by: Przemyslaw Swierczek <swierq@gmail.com>
  • Loading branch information
swierq committed Oct 29, 2020
1 parent c0eb58e commit 673ec74
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
6 changes: 6 additions & 0 deletions sdk/mock_data_test.go
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
45 changes: 40 additions & 5 deletions sdk/vra7_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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))

Expand All @@ -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))

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions vra7/mock_data_test.go
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 4 additions & 0 deletions vra7/resource_vra7_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 673ec74

Please sign in to comment.