/
gpg_key_integration_test.go
169 lines (131 loc) · 4.53 KB
/
gpg_key_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//go:build integration
// +build integration
package tfe
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGPGKeyCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
t.Cleanup(orgCleanup)
upgradeOrganizationSubscription(t, client, org)
provider, providerCleanup := createRegistryProvider(t, client, org, PrivateRegistry)
t.Cleanup(providerCleanup)
t.Run("with valid options", func(t *testing.T) {
opts := GPGKeyCreateOptions{
Namespace: provider.Organization.Name,
AsciiAmor: testGpgArmor,
}
gpgKey, err := client.GPGKeys.Create(ctx, opts)
require.NoError(t, err)
assert.NotEmpty(t, gpgKey.ID)
assert.Equal(t, gpgKey.AsciiAmor, opts.AsciiAmor)
assert.Equal(t, gpgKey.Namespace, opts.Namespace)
assert.NotEmpty(t, gpgKey.CreatedAt)
assert.NotEmpty(t, gpgKey.UpdatedAt)
// The default value for these two fields is an empty string
assert.Empty(t, gpgKey.Source)
assert.Empty(t, gpgKey.TrustSignature)
})
t.Run("with invalid options", func(t *testing.T) {
missingNamespaceOpts := GPGKeyCreateOptions{
Namespace: "",
AsciiAmor: testGpgArmor,
}
_, err := client.GPGKeys.Create(ctx, missingNamespaceOpts)
assert.ErrorIs(t, err, ErrInvalidNamespace)
missingAsciiArmorOpts := GPGKeyCreateOptions{
Namespace: provider.Organization.Name,
AsciiAmor: "",
}
_, err = client.GPGKeys.Create(ctx, missingAsciiArmorOpts)
assert.ErrorIs(t, err, ErrInvalidAsciiArmor)
})
}
func TestGPGKeyRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
t.Cleanup(orgCleanup)
upgradeOrganizationSubscription(t, client, org)
provider, providerCleanup := createRegistryProvider(t, client, org, PrivateRegistry)
t.Cleanup(providerCleanup)
gpgKey, gpgKeyCleanup := createGPGKey(t, client, org, provider)
t.Cleanup(gpgKeyCleanup)
t.Run("when the gpg key exists", func(t *testing.T) {
fetched, err := client.GPGKeys.Read(ctx, GPGKeyID{
Namespace: provider.Organization.Name,
KeyID: gpgKey.KeyID,
})
require.NoError(t, err)
assert.NotEmpty(t, gpgKey.ID)
assert.NotEmpty(t, gpgKey.KeyID)
assert.Greater(t, len(gpgKey.AsciiAmor), 0)
assert.Equal(t, fetched.Namespace, provider.Organization.Name)
})
t.Run("when the key does not exist", func(t *testing.T) {
_, err := client.GPGKeys.Read(ctx, GPGKeyID{
Namespace: provider.Organization.Name,
KeyID: "foobar",
})
assert.ErrorIs(t, err, ErrResourceNotFound)
})
}
func TestGPGKeyUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
t.Cleanup(orgCleanup)
upgradeOrganizationSubscription(t, client, org)
provider, providerCleanup := createRegistryProvider(t, client, org, PrivateRegistry)
t.Cleanup(providerCleanup)
// We won't use the cleanup method here as the namespace
// is used to identify a key and that will change due to the update
// call. We'll need to manually delete the key.
gpgKey, _ := createGPGKey(t, client, org, provider)
t.Run("when updating to a valid namespace", func(t *testing.T) {
// Create a new namespace to update the key with
org2, org2Cleanup := createOrganization(t, client)
t.Cleanup(org2Cleanup)
provider2, provider2Cleanup := createRegistryProvider(t, client, org2, PrivateRegistry)
t.Cleanup(provider2Cleanup)
keyID := GPGKeyID{
Namespace: provider.Organization.Name,
KeyID: gpgKey.KeyID,
}
opts := GPGKeyUpdateOptions{
Namespace: provider2.Organization.Name,
}
updatedKey, err := client.GPGKeys.Update(ctx, keyID, opts)
require.NoError(t, err)
assert.Equal(t, gpgKey.KeyID, updatedKey.KeyID)
assert.Equal(t, updatedKey.Namespace, provider2.Organization.Name)
// Cleanup
err = client.GPGKeys.Delete(ctx, GPGKeyID{
Namespace: provider2.Organization.Name,
KeyID: updatedKey.KeyID,
})
require.NoError(t, err)
})
}
func TestGPGKeyDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
org, orgCleanup := createOrganization(t, client)
t.Cleanup(orgCleanup)
upgradeOrganizationSubscription(t, client, org)
provider, providerCleanup := createRegistryProvider(t, client, org, PrivateRegistry)
t.Cleanup(providerCleanup)
gpgKey, _ := createGPGKey(t, client, org, provider)
t.Run("when a key exists", func(t *testing.T) {
err := client.GPGKeys.Delete(ctx, GPGKeyID{
Namespace: provider.Organization.Name,
KeyID: gpgKey.KeyID,
})
require.NoError(t, err)
})
}