Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to test via Resty httpmock #10

Closed
jeremypng opened this issue Nov 18, 2021 · 8 comments
Closed

Add ability to test via Resty httpmock #10

jeremypng opened this issue Nov 18, 2021 · 8 comments

Comments

@jeremypng
Copy link
Contributor

I need to write a test for code written using this SDK. Since it is based on Resty, I think the below is how one would go about testing code that uses a Resty client. However, since the first call is NewClient, there is not a way to inject the mock before the call is made to the destination server.

Could the resty class be something that is instantiated before the NewClient or NewClientWithOptions is called so that the mock could be applied and then initiate the connection to DNAC?

If I have misunderstood this please correct me.

Here is an example referenced by the Resty documentation:
https://github.com/jarcoal/httpmock#ginkgo--resty-example

Thanks!

@wastorga
Copy link
Collaborator

Hi, @jeremypng.

It looks like you only need access to the RestyClient used.

I have updated the new branches for DNAC versions 2.2.2.3 and 2.2.3.3.

@wastorga
Copy link
Collaborator

wastorga commented Nov 25, 2021

It looks like this

//examples/test/test_suite_test.go

package main_test

import (
	dnac "dnacenter-go-sdk/sdk"
	"testing"

	"github.com/jarcoal/httpmock"
	. "github.com/onsi/ginkgo"
)

var Client *dnac.Client

// ...
var _ = BeforeSuite(func() {
	// block all HTTP requests
	Client, _ = dnac.NewClientWithOptions("https://192.168.196.2/",
		"altus", "Altus123",
		"true", "false")

	httpmock.ActivateNonDefault(Client.RestyClient().GetClient())
})

var _ = BeforeEach(func() {
	// remove any mocks
	httpmock.Reset()
})

var _ = AfterSuite(func() {
	httpmock.DeactivateAndReset()
})

func TestBooks(t *testing.T) {
	RunSpecs(t, "ApplicationsCount")
}
//examples/test/applications_count_test.go
package main_test

import (
	"github.com/jarcoal/httpmock"
	. "github.com/onsi/ginkgo"
)

var _ = Describe("ApplicationsCount", func() {
	It("returns a count of applications", func() {
		fixture := `{
			"response": 1,
			"version": "1.0"
		}`
		responder := httpmock.NewStringResponder(200, fixture)
		fakeUrl := "https://192.168.196.2/dna/intent/api/v1/applications-count"
		httpmock.RegisterResponder("GET", fakeUrl, responder)
		// fetch the article into struct
		nResponse, _, err := Client.ApplicationPolicy.GetApplicationsCount()
		if err != nil {
			Fail("Failure reason 1")
		}
		if nResponse == nil {
			Fail("Failure reason 2")
		}

		// do stuff with the article object ...
	})
})

@wastorga
Copy link
Collaborator

The result

$ ginkgo
Running Suite: ApplicationsCount
================================
Random Seed: 1637798439
Will run 1 of 1 specs

•
Ran 1 of 1 Specs in 30.006 seconds
SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
2021/11/24 18:01:11.561450 DEBUG RESTY 
==============================================================================
~~~ REQUEST ~~~
GET  /dna/intent/api/v1/applications-count  HTTP/1.1
HOST   : 192.168.196.2
HEADERS:
        Accept: application/json
        Content-Type: application/json
        User-Agent: go-resty/2.6.0 (https://github.com/go-resty/resty)
BODY   :
***** NO CONTENT *****
------------------------------------------------------------------------------
~~~ RESPONSE ~~~
STATUS       : 200
PROTO        : 
RECEIVED AT  : 2021-11-24T18:01:11.561167-06:00
TIME DURATION: 16.917µs
HEADERS      :

BODY         :
{
                        "response": 1,
                        "version": "1.0"
                }
==============================================================================

Ginkgo ran 1 suite in 32.441645292s
Test Suite Passed

@wastorga
Copy link
Collaborator

For the current version it does not seem to work right now.
I'll look into it. I'll keep you updated.

@wastorga
Copy link
Collaborator

@jeremypng We decided to update the version compatible with Cisco DNA Center v2.1.2 to support this.
So, there is now a new release of the SDK, v1.0.0, with the changes.

@jeremypng
Copy link
Contributor Author

jeremypng commented Jan 7, 2022

@wastorga - This is failing in testing because line 166 in api_client.go tries to authenticate when instantiating the NewClient() function.

        result, response, err := c.Authentication.AuthenticationAPI(username, password)
	if err != nil {
		return c, err
	}
	if response.StatusCode() > 399 {
		error := response.Error()
		return c, fmt.Errorf("%s", error)

@jeremypng
Copy link
Contributor Author

Can I get a NewClient() function without the auth method. And then a separate Auth Method?

wastorga added a commit that referenced this issue Jan 13, 2022
@wastorga
Copy link
Collaborator

Hi, @jeremypng. Thanks for your contribution.
I just merged your PR and released the changes to v3.2.0.

wastorga pushed a commit that referenced this issue Jan 13, 2022
Separates Authentication in api_client.go into separate func allowing tests without connection to live server
wastorga pushed a commit that referenced this issue Jan 13, 2022
Separates Authentication in api_client.go into separate func allowing tests without connection to live server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants