forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_set_version_integration_test.go
147 lines (114 loc) · 3.52 KB
/
policy_set_version_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//go:build integration
// +build integration
package tfe
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const waitForPolicySetVersionUpload = 500 * time.Millisecond
func TestPolicySetVersionsCreate(t *testing.T) {
skipIfFreeOnly(t)
client := testClient(t)
ctx := context.Background()
psTest, psTestCleanup := createPolicySet(t, client, nil, nil, nil)
defer psTestCleanup()
t.Run("with valid identifier", func(t *testing.T) {
psv, err := client.PolicySetVersions.Create(ctx, psTest.ID)
require.NoError(t, err)
assert.NotEmpty(t, psv.ID)
assert.Equal(t, psv.Source, PolicySetVersionSourceAPI)
assert.Equal(t, psv.PolicySet.ID, psTest.ID)
})
t.Run("with invalid identifier", func(t *testing.T) {
_, err := client.PolicySetVersions.Create(ctx, badIdentifier)
assert.Equal(t, err, ErrInvalidPolicySetID)
})
}
func TestPolicySetVersionsRead(t *testing.T) {
skipIfFreeOnly(t)
client := testClient(t)
ctx := context.Background()
psTest, psTestCleanup := createPolicySet(t, client, nil, nil, nil)
defer psTestCleanup()
origPSV, err := client.PolicySetVersions.Create(ctx, psTest.ID)
require.NoError(t, err)
t.Run("with valid id", func(t *testing.T) {
psv, err := client.PolicySetVersions.Read(ctx, origPSV.ID)
require.NoError(t, err)
assert.Equal(t, psv.Source, origPSV.Source)
assert.Equal(t, psv.Status, origPSV.Status)
})
t.Run("with invalid id", func(t *testing.T) {
_, err := client.PolicySetVersions.Read(ctx, randomString(t))
require.Error(t, err)
})
}
func TestPolicySetVersionsUpload(t *testing.T) {
skipIfFreeOnly(t)
client := testClient(t)
ctx := context.Background()
psv, psvCleanup := createPolicySetVersion(t, client, nil)
defer psvCleanup()
t.Run("with valid upload URL", func(t *testing.T) {
psv, err := client.PolicySetVersions.Read(ctx, psv.ID)
require.NoError(t, err)
assert.Equal(t, psv.Status, PolicySetVersionPending)
err = client.PolicySetVersions.Upload(
ctx,
*psv,
"test-fixtures/policy-set-version",
)
require.NoError(t, err)
// give TFC soe time to process uploading the
// policy set version before reaeding..
time.Sleep(waitForPolicySetVersionUpload)
psv, err = client.PolicySetVersions.Read(ctx, psv.ID)
require.NoError(t, err)
assert.Equal(t, PolicySetVersionReady, psv.Status)
})
t.Run("with missing upload URL", func(t *testing.T) {
delete(psv.Links, "upload")
err := client.PolicySetVersions.Upload(
ctx,
*psv,
"test-fixtures/policy-set-version",
)
assert.EqualError(t, err, "the Policy Set Version does not contain an upload link")
})
}
func TestPolicySetVersionsUploadURL(t *testing.T) {
t.Run("successfully returns upload link", func(t *testing.T) {
links := map[string]interface{}{
"upload": "example.com",
}
psv := PolicySetVersion{
Links: links,
}
uploadURL, err := psv.uploadURL()
require.NoError(t, err)
assert.Equal(t, uploadURL, "example.com")
})
t.Run("errors when there is no upload key in the Links", func(t *testing.T) {
links := map[string]interface{}{
"bad-field": "example.com",
}
psv := PolicySetVersion{
Links: links,
}
_, err := psv.uploadURL()
assert.EqualError(t, err, "the Policy Set Version does not contain an upload link")
})
t.Run("errors when the upload link is empty", func(t *testing.T) {
links := map[string]interface{}{
"upload": "",
}
psv := PolicySetVersion{
Links: links,
}
_, err := psv.uploadURL()
assert.EqualError(t, err, "the Policy Set Version upload URL is empty")
})
}