Skip to content

Commit

Permalink
Add tests for edge cases + remove unnecessary copier
Browse files Browse the repository at this point in the history
  • Loading branch information
RLRabinowitz committed Aug 30, 2021
1 parent 8c83fd3 commit 1865bbd
Showing 1 changed file with 70 additions and 44 deletions.
114 changes: 70 additions & 44 deletions client/team_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client_test

import (
"errors"
. "github.com/env0/terraform-provider-env0/client"
"github.com/golang/mock/gomock"
"github.com/jinzhu/copier"
Expand Down Expand Up @@ -63,41 +64,57 @@ var _ = Describe("Teams Client", func() {
})

Describe("TeamCreate", func() {
var createdTeam Team
var err error
Describe("Success", func() {
var createdTeam Team
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
BeforeEach(func() {
mockOrganizationIdCall(organizationId)

createTeamPayload := TeamCreatePayload{}
copier.Copy(&createTeamPayload, &mockTeam)
createTeamPayload := TeamCreatePayload{}
copier.Copy(&createTeamPayload, &mockTeam)

expectedCreateRequest := createTeamPayload
expectedCreateRequest.OrganizationId = organizationId
expectedCreateRequest := createTeamPayload
expectedCreateRequest.OrganizationId = organizationId

httpCall = mockHttpClient.EXPECT().
Post("/teams", expectedCreateRequest, gomock.Any()).
Do(func(path string, request interface{}, response *Team) {
*response = mockTeam
})
httpCall = mockHttpClient.EXPECT().
Post("/teams", expectedCreateRequest, gomock.Any()).
Do(func(path string, request interface{}, response *Team) {
*response = mockTeam
})

createdTeam, err = apiClient.TeamCreate(createTeamPayload)
})
createdTeam, err = apiClient.TeamCreate(createTeamPayload)
})

It("Should get organization id", func() {
organizationIdCall.Times(1)
})
It("Should get organization id", func() {
organizationIdCall.Times(1)
})

It("Should send POST request with params", func() {
httpCall.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 not return error", func() {
Expect(err).To(BeNil())
})

It("Should return created team", func() {
Expect(createdTeam).To(Equal(mockTeam))
})
})

It("Should return created team", func() {
Expect(createdTeam).To(Equal(mockTeam))
Describe("Failure", func() {
It("Should fail when team has no name", func() {
teamWithoutNamePayload := TeamCreatePayload{Description: "team-without-name"}
_, err := apiClient.TeamCreate(teamWithoutNamePayload)
Expect(err).To(BeEquivalentTo(errors.New("Must specify team name on creation")))
})

It("Should fail if request includes organizationId (should be inferred automatically)", func() {
payloadWithOrgId := TeamCreatePayload{Name: "team-name", OrganizationId: "org-id"}
_, err := apiClient.TeamCreate(payloadWithOrgId)
Expect(err).To(BeEquivalentTo(errors.New("Must not specify organizationId")))
})
})
})

Expand All @@ -113,32 +130,41 @@ var _ = Describe("Teams Client", func() {
})

Describe("TeamUpdate", func() {
var updatedTeam Team
var err error
Describe("Success", func() {
var updatedTeam Team
var err error

BeforeEach(func() {
updateTeamPayload := TeamUpdatePayload{}
copier.Copy(&updateTeamPayload, &mockTeam)
BeforeEach(func() {
updateTeamPayload := TeamUpdatePayload{Name: "updated-name"}

httpCall = mockHttpClient.EXPECT().
Put("/teams/"+mockTeam.Id, updateTeamPayload, gomock.Any()).
Do(func(path string, request interface{}, response *Team) {
*response = mockTeam
})
httpCall = mockHttpClient.EXPECT().
Put("/teams/"+mockTeam.Id, updateTeamPayload, gomock.Any()).
Do(func(path string, request interface{}, response *Team) {
*response = mockTeam
})

updatedTeam, err = apiClient.TeamUpdate(mockTeam.Id, updateTeamPayload)
})
updatedTeam, err = apiClient.TeamUpdate(mockTeam.Id, updateTeamPayload)
})

It("Should send Put request with expected payload", func() {
httpCall.Times(1)
})
It("Should send Put request with expected payload", func() {
httpCall.Times(1)
})

It("Should not return an error", func() {
Expect(err).To(BeNil())
})

It("Should not return an error", func() {
Expect(err).To(BeNil())
It("Should return team received from API", func() {
Expect(updatedTeam).To(Equal(mockTeam))
})
})

It("Should return team received from API", func() {
Expect(updatedTeam).To(Equal(mockTeam))
Describe("Failure", func() {
It("Should fail if team has no name", func() {
payloadWithNoName := TeamUpdatePayload{Description: "team-without-name"}
_, err := apiClient.TeamUpdate(mockTeam.Id, payloadWithNoName)
Expect(err).To(BeEquivalentTo(errors.New("Must specify team name on update")))
})
})
})
})

0 comments on commit 1865bbd

Please sign in to comment.