|
1 | 1 | package tfe
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
| 6 | + "encoding/json" |
| 7 | + "github.com/hashicorp/go-retryablehttp" |
5 | 8 | "io/ioutil"
|
6 | 9 | "strings"
|
7 | 10 | "testing"
|
| 11 | + "time" |
8 | 12 |
|
9 | 13 | "github.com/stretchr/testify/assert"
|
10 | 14 | "github.com/stretchr/testify/require"
|
@@ -804,3 +808,86 @@ func TestWorkspacesUnassignSSHKey(t *testing.T) {
|
804 | 808 | assert.EqualError(t, err, ErrInvalidWorkspaceID.Error())
|
805 | 809 | })
|
806 | 810 | }
|
| 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