Skip to content

Commit

Permalink
Merge pull request #61 from env0/chore-add-ut-for-template-client
Browse files Browse the repository at this point in the history
Chore: Adding tests for Template client
  • Loading branch information
avnerenv0 committed May 31, 2021
2 parents f6ae019 + c4c06a9 commit b06e0f2
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
151 changes: 151 additions & 0 deletions client/template_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit b06e0f2

Please sign in to comment.