From c4c06a941a08305f2bad6b3a8e5fa6f541c44594 Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Mon, 31 May 2021 13:54:06 +0300 Subject: [PATCH] adding tests for template client --- client/template_test.go | 151 ++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +- go.sum | 2 + 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 client/template_test.go diff --git a/client/template_test.go b/client/template_test.go new file mode 100644 index 00000000..3c7c15bf --- /dev/null +++ b/client/template_test.go @@ -0,0 +1,151 @@ +package client_test + +import ( + . "github.com/env0/terraform-provider-env0/client" + "github.com/golang/mock/gomock" + "github.com/jinzhu/copier" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Templates Client", func() { + mockTemplate := Template{ + Id: "template-id", + Name: "template-name", + Repository: "https://re.po", + } + + Describe("Template", func() { + var returnedTemplate Template + + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT(). + Get("/blueprints/"+mockTemplate.Id, gomock.Nil(), gomock.Any()). + Do(func(path string, request interface{}, response *Template) { + *response = mockTemplate + }) + returnedTemplate, _ = apiClient.Template(mockTemplate.Id) + }) + + It("Should send GET request", func() { + httpCall.Times(1) + }) + + It("Should return template", func() { + Expect(returnedTemplate).To(Equal(mockTemplate)) + }) + }) + + Describe("Templates", func() { + var returnedTemplates []Template + mockTemplates := []Template{mockTemplate} + + BeforeEach(func() { + mockOrganizationIdCall(organizationId) + expectedPayload := map[string]string{"organizationId": organizationId} + httpCall = mockHttpClient.EXPECT(). + Get("/blueprints", expectedPayload, gomock.Any()). + Do(func(path string, request interface{}, response *[]Template) { + *response = mockTemplates + }) + returnedTemplates, _ = apiClient.Templates() + }) + + It("Should get organization id", func() { + organizationIdCall.Times(1) + }) + + It("Should send GET request", func() { + httpCall.Times(1) + }) + + It("Should return template", func() { + Expect(returnedTemplates).To(Equal(mockTemplates)) + }) + }) + + Describe("TemplateCreate", func() { + var createdTemplate Template + var err error + + BeforeEach(func() { + mockOrganizationIdCall(organizationId) + + createTemplatePayload := TemplateCreatePayload{} + copier.Copy(&createTemplatePayload, &mockTemplate) + + expectedCreateRequest := createTemplatePayload + expectedCreateRequest.OrganizationId = organizationId + + httpCall = mockHttpClient.EXPECT(). + Post("/blueprints", expectedCreateRequest, gomock.Any()). + Do(func(path string, request interface{}, response *Template) { + *response = mockTemplate + }) + + createdTemplate, err = apiClient.TemplateCreate(createTemplatePayload) + }) + + It("Should get organization id", func() { + organizationIdCall.Times(1) + }) + + It("Should send POST request with params", func() { + httpCall.Times(1) + }) + + It("Should not return error", func() { + Expect(err).To(BeNil()) + }) + + It("Should return created configuration variable", func() { + Expect(createdTemplate).To(Equal(mockTemplate)) + }) + }) + + Describe("TemplateDelete", func() { + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT().Delete("/blueprints/" + mockTemplate.Id) + apiClient.TemplateDelete(mockTemplate.Id) + }) + + It("Should send DELETE request with template id", func() { + httpCall.Times(1) + }) + }) + + Describe("TemplateUpdate", func() { + var updatedTemplate Template + var err error + + BeforeEach(func() { + mockOrganizationIdCall(organizationId) + + updateTemplatePayload := TemplateCreatePayload{} + copier.Copy(&updateTemplatePayload, &mockTemplate) + + expectedUpdateRequest := updateTemplatePayload + expectedUpdateRequest.OrganizationId = organizationId + + httpCall = mockHttpClient.EXPECT(). + Put("/blueprints/"+mockTemplate.Id, expectedUpdateRequest, gomock.Any()). + Do(func(path string, request interface{}, response *Template) { + *response = mockTemplate + }) + + updatedTemplate, err = apiClient.TemplateUpdate(mockTemplate.Id, updateTemplatePayload) + }) + + It("Should send POST request with expected payload", func() { + httpCall.Times(1) + }) + + It("Should not return an error", func() { + Expect(err).To(BeNil()) + }) + + It("Should return configuration value received from API", func() { + Expect(updatedTemplate).To(Equal(mockTemplate)) + }) + }) +}) diff --git a/go.mod b/go.mod index e11e43fd..9ba0b6dc 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,11 @@ go 1.14 require ( github.com/go-resty/resty/v2 v2.6.0 - github.com/hashicorp/terraform-plugin-docs v0.4.0 // indirect github.com/golang/mock v1.4.3 github.com/google/uuid v1.2.0 + github.com/hashicorp/terraform-plugin-docs v0.4.0 // indirect github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.4 + github.com/jinzhu/copier v0.3.2 github.com/onsi/ginkgo v1.16.2 github.com/onsi/gomega v1.12.0 ) diff --git a/go.sum b/go.sum index 535b2a10..a8e45cbd 100644 --- a/go.sum +++ b/go.sum @@ -261,6 +261,8 @@ github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGAR github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE= github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= +github.com/jinzhu/copier v0.3.2 h1:QdBOCbaouLDYaIPFfi1bKv5F5tPpeTwXe4sD0jqtz5w= +github.com/jinzhu/copier v0.3.2/go.mod h1:24xnZezI2Yqac9J61UC6/dG/k76ttpq0DdJI3QmUvro= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=