Add client library test example

main
nleiva 2022-05-07 16:40:31 -04:00
parent 773904b4f3
commit f7da9d44ed
3 changed files with 112 additions and 2 deletions

View File

@ -2,6 +2,13 @@ module github.com/nautobot/go-nautobot
go 1.17
require github.com/deepmap/oapi-codegen v1.10.1
require (
github.com/deepmap/oapi-codegen v1.10.1
github.com/tidwall/gjson v1.14.1
)
require github.com/google/uuid v1.3.0 // indirect
require (
github.com/google/uuid v1.3.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)

View File

@ -83,6 +83,12 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=

97
client/nautobot_test.go Normal file
View File

@ -0,0 +1,97 @@
package nautobot_test
import (
"context"
"fmt"
"net/http"
"os"
"testing"
nb "github.com/nautobot/go-nautobot"
"github.com/tidwall/gjson"
)
const (
deviceExist = "manufacturer with this name already exists."
)
func NewSecurityProviderNautobotToken(t string) (*SecurityProviderNautobotToken, error) {
return &SecurityProviderNautobotToken{
token: t,
}, nil
}
type SecurityProviderNautobotToken struct {
token string
}
func (s *SecurityProviderNautobotToken) Intercept(ctx context.Context, req *http.Request) error {
req.Header.Set("Authorization", fmt.Sprintf("Token %s", s.token))
return nil
}
func getenv(name string) (string, error) {
v := os.Getenv(name)
if v == "" {
return v, fmt.Errorf("%s environment variable not set", name)
}
return v, nil
}
func TestDcimManufacturersCreateWithResponse(t *testing.T) {
server, err := getenv("NAUTOBOT_URL")
if err != nil {
t.Fatal(err)
}
token, err := getenv("NAUTOBOT_TOKEN")
if err != nil {
t.Fatal(err)
}
tk, err := NewSecurityProviderNautobotToken(token)
if err != nil {
t.Fatalf("could not create a Nautobot token for %s", server)
}
c, err := nb.NewClientWithResponses(
server,
nb.WithRequestEditorFn(tk.Intercept),
)
if err != nil {
t.Fatalf("could not setup a client connection to %s", server)
}
// Test table
tt := []struct {
name string
vendor string
err string
}{
{name: "New Vendor", vendor: "new vendor 1"},
{name: "Existing Vendor", vendor: "Cisco", err: deviceExist},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var m nb.Manufacturer
m.Name = tc.vendor
rsp, err := c.DcimManufacturersCreateWithResponse(
context.Background(),
nb.DcimManufacturersCreateJSONRequestBody(m))
data := string(rsp.Body)
dataName := gjson.Get(data, "name.0")
if dataName.String() == deviceExist && tc.err == deviceExist {
return
}
if err != nil {
t.Fatalf("failed to create manufacturer %s on %s: %s", tc.vendor, server, err.Error())
}
})
}
}