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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
package tfe
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOAuthTokensList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
otTest1, _ := createOAuthToken(t, client, orgTest)
otTest2, _ := createOAuthToken(t, client, orgTest)
t.Run("without list options", func(t *testing.T) {
options := OAuthTokenListOptions{}
otl, err := client.OAuthTokens.List(ctx, orgTest.Name, options)
require.NoError(t, err)
t.Run("the OAuth client relationship is decoded correcly", func(t *testing.T) {
for _, ot := range otl.Items {
assert.NotEmpty(t, ot.OAuthClient)
}
})
// We need to strip some fields before the next test.
for _, ot := range otl.Items {
ot.CreatedAt = time.Time{}
ot.ServiceProviderUser = ""
ot.OAuthClient = nil
}
assert.Contains(t, otl.Items, otTest1)
assert.Contains(t, otl.Items, otTest2)
t.Skip("paging not supported yet in API")
assert.Equal(t, 1, otl.CurrentPage)
assert.Equal(t, 2, otl.TotalCount)
})
t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
options := OAuthTokenListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
}
otl, err := client.OAuthTokens.List(ctx, orgTest.Name, options)
require.NoError(t, err)
assert.Empty(t, otl.Items)
assert.Equal(t, 999, otl.CurrentPage)
assert.Equal(t, 2, otl.TotalCount)
})
t.Run("without a valid organization", func(t *testing.T) {
options := OAuthTokenListOptions{}
otl, err := client.OAuthTokens.List(ctx, badIdentifier, options)
assert.Nil(t, otl)
assert.EqualError(t, err, "invalid value for organization")
})
}
func TestOAuthTokensRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
otTest, otTestCleanup := createOAuthToken(t, client, nil)
defer otTestCleanup()
t.Run("when the OAuth token exists", func(t *testing.T) {
ot, err := client.OAuthTokens.Read(ctx, otTest.ID)
require.NoError(t, err)
assert.Equal(t, otTest.ID, ot.ID)
assert.NotEmpty(t, ot.OAuthClient)
})
t.Run("when the OAuth token does not exist", func(t *testing.T) {
ot, err := client.OAuthTokens.Read(ctx, "nonexisting")
assert.Nil(t, ot)
assert.Equal(t, ErrResourceNotFound, err)
})
t.Run("without a valid OAuth token ID", func(t *testing.T) {
ot, err := client.OAuthTokens.Read(ctx, badIdentifier)
assert.Nil(t, ot)
assert.EqualError(t, err, "invalid value for OAuth token ID")
})
}
func TestOAuthTokensUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
otTest, otTestCleanup := createOAuthToken(t, client, nil)
defer otTestCleanup()
t.Run("before updating with an SSH key", func(t *testing.T) {
assert.False(t, otTest.HasSSHKey)
})
t.Run("without options", func(t *testing.T) {
ot, err := client.OAuthTokens.Update(ctx, otTest.ID, OAuthTokenUpdateOptions{})
require.NoError(t, err)
assert.False(t, ot.HasSSHKey)
})
t.Run("when updating with a valid SSH key", func(t *testing.T) {
dummyPrivateSSHKey := `-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDIF0s2yX7dSQQL1grdTbai1Mb7sEco6RIOz8iqrHTGqmESpu5n
d8imMkV5KadgVBJ/UvHsWpg446O3DAMYn0Y6f8dDlK7pmCEtiGVKTR1PaVRMpF8R
5Guvrmlru8Kex5ozh0pPMB15aGsIzSezCKgSs74Od9YL4smdgKyYwqsu3wIDAQAB
AoGBAKCs6+4j4icqYgBrMjBCHp4lRWCJTqtQdfrE6jv73o5F9Uu4FwupScwD5HwG
cezNtkjeP3zvxvsv+aCdGcNk60vSz4n9Nt6gEJveWFSpePYXKZ9cz/IjFLI7nSzc
1msLyE3DfUqB91s/A/aT5p0LiVDc8i4mCGDOga2OINIwqDGZAkEA/Vz8dkcqsAVW
CL1F000hWTrM6tu0V+x8Nm8CRx7wM/Gy/19PbV0t26wCVG0GXyLWsV2//huY7w5b
3AcSl5pfJQJBAMosYQXk5L4S+qivz2zmZdtyz+Ik6IZ3PwZoED32PxGSdW5rG8iP
V+iSJek5ESkS1zeXwDMnF4LeoBY9H07DiLMCQQCrHm1o2SIMpl34IxWQ4+wdHuid
yuuf4pn2Db2lGVE0VA8ICXBUtfUuA5vDN6tw/8+vFVmBn1QISVNjZOd6uwl9AkA+
jIRoAm0SsWSDlAEkvBN/VYIjgS+/il0haki8ItdYZGuYgeLSpiaYeb7o7RL2FjIn
rPd12/5WKvJ0buykvbIpAkEA5Uy3T8xQJkDGbp0+xA0yThoOYiB09lAok8I7Sv/5
dpIe8YOINN27XaojJvVpT5uBVCcZLF+G7kaMjSwCTlDx3Q==
-----END RSA PRIVATE KEY-----`
ot, err := client.OAuthTokens.Update(ctx, otTest.ID, OAuthTokenUpdateOptions{
PrivateSSHKey: String(dummyPrivateSSHKey),
})
require.NoError(t, err)
assert.Equal(t, otTest.ID, ot.ID)
assert.True(t, ot.HasSSHKey)
})
t.Run("when updating with an invalid SSH key", func(t *testing.T) {
ot, err := client.OAuthTokens.Update(ctx, otTest.ID, OAuthTokenUpdateOptions{
PrivateSSHKey: String(randomString(t)),
})
assert.Nil(t, ot)
assert.Contains(t, err.Error(), "Ssh key is invalid")
})
t.Run("without a valid policy ID", func(t *testing.T) {
ot, err := client.OAuthTokens.Update(ctx, badIdentifier, OAuthTokenUpdateOptions{})
assert.Nil(t, ot)
assert.EqualError(t, err, "invalid value for OAuth token ID")
})
}
func TestOAuthTokensDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
otTest, _ := createOAuthToken(t, client, orgTest)
t.Run("with valid options", func(t *testing.T) {
err := client.OAuthTokens.Delete(ctx, otTest.ID)
require.NoError(t, err)
// Try loading the OAuth token - it should fail.
_, err = client.OAuthTokens.Read(ctx, otTest.ID)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("when the OAuth token does not exist", func(t *testing.T) {
err := client.OAuthTokens.Delete(ctx, otTest.ID)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("when the OAuth token ID is invalid", func(t *testing.T) {
err := client.OAuthTokens.Delete(ctx, badIdentifier)
assert.EqualError(t, err, "invalid value for OAuth token ID")
})
}
|