Skip to content

Commit fdcbf1a

Browse files
authored
Merge pull request #204 from hashicorp/radditude/fix-tests
fix marshalling nested structs in requests
2 parents 94c58eb + 094a293 commit fdcbf1a

4 files changed

+95
-8
lines changed

registry_module.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ func (o RegistryModuleCreateWithVCSConnectionOptions) valid() error {
241241
}
242242

243243
type RegistryModuleVCSRepoOptions struct {
244-
Identifier *string `jsonapi:"attr,identifier"`
245-
OAuthTokenID *string `jsonapi:"attr,oauth-token-id"`
246-
DisplayIdentifier *string `jsonapi:"attr,display-identifier"`
244+
Identifier *string `json:"identifier"`
245+
OAuthTokenID *string `json:"oauth-token-id"`
246+
DisplayIdentifier *string `json:"display-identifier"`
247247
}
248248

249249
func (o RegistryModuleVCSRepoOptions) valid() error {

registry_module_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ func TestRegistryCreateOptions_Marshal(t *testing.T) {
607607
bodyBytes, err := req.BodyBytes()
608608
require.NoError(t, err)
609609

610-
expectedBody := `{"data":{"type":"registry-modules","attributes":{"vcs-repo":{"Identifier":"id","OAuthTokenID":"token","DisplayIdentifier":"display-id"}}}}
610+
expectedBody := `{"data":{"type":"registry-modules","attributes":{"vcs-repo":{"identifier":"id","oauth-token-id":"token","display-identifier":"display-id"}}}}
611611
`
612612
assert.Equal(t, expectedBody, string(bodyBytes))
613613
}

workspace.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ type WorkspaceCreateOptions struct {
271271
// TODO: move this struct out. VCSRepoOptions is used by workspaces, policy sets, and registry modules
272272
// VCSRepoOptions represents the configuration options of a VCS integration.
273273
type VCSRepoOptions struct {
274-
Branch *string `jsonapi:"attr,branch,omitempty"`
275-
Identifier *string `jsonapi:"attr,identifier,omitempty"`
276-
IngressSubmodules *bool `jsonapi:"attr,ingress-submodules,omitempty"`
277-
OAuthTokenID *string `jsonapi:"attr,oauth-token-id,omitempty"`
274+
Branch *string `json:"branch,omitempty"`
275+
Identifier *string `json:"identifier,omitempty"`
276+
IngressSubmodules *bool `json:"ingress-submodules,omitempty"`
277+
OAuthTokenID *string `json:"oauth-token-id,omitempty"`
278278
}
279279

280280
func (o WorkspaceCreateOptions) valid() error {

workspace_test.go

+87
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package tfe
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/json"
7+
"github.com/hashicorp/go-retryablehttp"
58
"io/ioutil"
69
"strings"
710
"testing"
11+
"time"
812

913
"github.com/stretchr/testify/assert"
1014
"github.com/stretchr/testify/require"
@@ -804,3 +808,86 @@ func TestWorkspacesUnassignSSHKey(t *testing.T) {
804808
assert.EqualError(t, err, ErrInvalidWorkspaceID.Error())
805809
})
806810
}
811+
812+
func TestWorkspace_Unmarshal(t *testing.T) {
813+
data := map[string]interface{}{
814+
"data": map[string]interface{}{
815+
"type": "workspaces",
816+
"id": "ws-1234",
817+
"attributes": map[string]interface{}{
818+
"name": "my-workspace",
819+
"auto-apply": true,
820+
"created-at": "2020-07-15T23:38:43.821Z",
821+
"resource-count": 2,
822+
"permissions": map[string]interface{}{
823+
"can-update": true,
824+
"can-lock": true,
825+
},
826+
"vcs-repo": map[string]interface{}{
827+
"branch": "main",
828+
"display-identifier": "repo-name",
829+
"identifier": "hashicorp/repo-name",
830+
"ingress-submodules": true,
831+
"oauth-token-id": "token",
832+
"repository-http-url": "github.com",
833+
"service-provider": "github",
834+
},
835+
"actions": map[string]interface{}{
836+
"is-destroyable": true,
837+
},
838+
"trigger-prefixes": []string{"prefix-"},
839+
},
840+
},
841+
}
842+
843+
byteData, err := json.Marshal(data)
844+
require.NoError(t, err)
845+
846+
responseBody := bytes.NewReader(byteData)
847+
ws := &Workspace{}
848+
err = unmarshalResponse(responseBody, ws)
849+
require.NoError(t, err)
850+
851+
iso8601TimeFormat := "2006-01-02T15:04:05Z"
852+
parsedTime, err := time.Parse(iso8601TimeFormat, "2020-07-15T23:38:43.821Z")
853+
854+
assert.Equal(t, ws.ID, "ws-1234")
855+
assert.Equal(t, ws.Name, "my-workspace")
856+
assert.Equal(t, ws.AutoApply, true)
857+
assert.Equal(t, ws.CreatedAt, parsedTime)
858+
assert.Equal(t, ws.ResourceCount, 2)
859+
assert.Equal(t, ws.Permissions.CanUpdate, true)
860+
assert.Equal(t, ws.Permissions.CanLock, true)
861+
assert.Equal(t, ws.VCSRepo.Branch, "main")
862+
assert.Equal(t, ws.VCSRepo.DisplayIdentifier, "repo-name")
863+
assert.Equal(t, ws.VCSRepo.Identifier, "hashicorp/repo-name")
864+
assert.Equal(t, ws.VCSRepo.IngressSubmodules, true)
865+
assert.Equal(t, ws.VCSRepo.OAuthTokenID, "token")
866+
assert.Equal(t, ws.VCSRepo.RepositoryHTTPURL, "github.com")
867+
assert.Equal(t, ws.VCSRepo.ServiceProvider, "github")
868+
assert.Equal(t, ws.Actions.IsDestroyable, true)
869+
assert.Equal(t, ws.TriggerPrefixes, []string{"prefix-"})
870+
}
871+
872+
func TestWorkspaceCreateOptions_Marshal(t *testing.T) {
873+
opts := WorkspaceCreateOptions{
874+
AllowDestroyPlan: Bool(true),
875+
Name: String("my-workspace"),
876+
TriggerPrefixes: []string{"prefix-"},
877+
VCSRepo: &VCSRepoOptions{
878+
Identifier: String("id"),
879+
OAuthTokenID: String("token"),
880+
},
881+
}
882+
883+
reqBody, err := serializeRequestBody(&opts)
884+
require.NoError(t, err)
885+
req, err := retryablehttp.NewRequest("POST", "url", reqBody)
886+
require.NoError(t, err)
887+
bodyBytes, err := req.BodyBytes()
888+
require.NoError(t, err)
889+
890+
expectedBody := `{"data":{"type":"workspaces","attributes":{"allow-destroy-plan":true,"name":"my-workspace","trigger-prefixes":["prefix-"],"vcs-repo":{"identifier":"id","oauth-token-id":"token"}}}}
891+
`
892+
assert.Equal(t, expectedBody, string(bodyBytes))
893+
}

0 commit comments

Comments
 (0)