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
|
package unit
import (
"context"
"fmt"
"testing"
"github.com/jarcoal/httpmock"
"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
)
func TestAccountOauthClient_List(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_oauth_client_list")
assert.NoError(t, err)
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)
base.MockGet("account/oauth-clients", fixtureData)
oauthClients, err := base.Client.ListOAuthClients(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err)
// Assertions on the returned data
assert.Len(t, oauthClients, 1, "Expected one OAuth client")
client := oauthClients[0]
assert.Equal(t, "2737bf16b39ab5d7b4a1", client.ID, "Unexpected ID")
assert.Equal(t, "Test_Client_1", client.Label, "Unexpected Label")
assert.False(t, client.Public, "Unexpected Public value")
assert.Equal(t, "https://example.org/oauth/callback", client.RedirectURI, "Unexpected Redirect URI")
assert.Equal(t, linodego.OAuthClientStatus("active"), client.Status, "Unexpected Status")
assert.Equal(t, "https://api.linode.com/v4/account/clients/2737bf16b39ab5d7b4a1/thumbnail", *client.ThumbnailURL, "Unexpected Thumbnail URL")
}
func TestAccountOauthClient_Get(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_oauth_client_get")
assert.NoError(t, err)
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)
clientID := "2737bf16b39ab5d7b4a1"
base.MockGet(fmt.Sprintf("account/oauth-clients/%s", clientID), fixtureData)
oauthClient, err := base.Client.GetOAuthClient(context.Background(), clientID)
assert.NoError(t, err)
// Assertions on the returned data
assert.Equal(t, "2737bf16b39ab5d7b4a1", oauthClient.ID, "Unexpected ID")
assert.Equal(t, "Test_Client_1", oauthClient.Label, "Unexpected Label")
assert.False(t, oauthClient.Public, "Unexpected Public value")
assert.Equal(t, "https://example.org/oauth/callback", oauthClient.RedirectURI, "Unexpected Redirect URI")
assert.Equal(t, linodego.OAuthClientStatus("active"), oauthClient.Status, "Unexpected Status")
assert.Equal(t, "https://api.linode.com/v4/account/clients/2737bf16b39ab5d7b4a1/thumbnail", *oauthClient.ThumbnailURL, "Unexpected Thumbnail URL")
}
func TestAccountOauthClient_Create(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_oauth_client_create")
assert.NoError(t, err)
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)
requestData := linodego.OAuthClientCreateOptions{
Label: "Test_Client_1",
RedirectURI: "https://example.org/oauth/callback",
}
base.MockPost("account/oauth-clients", fixtureData)
oauthClient, err := base.Client.CreateOAuthClient(context.Background(), requestData)
assert.NoError(t, err)
// Assertions on the returned data
assert.Equal(t, "2737bf16b39ab5d7b4a1", oauthClient.ID, "Unexpected ID")
assert.Equal(t, "Test_Client_1", oauthClient.Label, "Unexpected Label")
assert.False(t, oauthClient.Public, "Unexpected Public value")
assert.Equal(t, "https://example.org/oauth/callback", oauthClient.RedirectURI, "Unexpected Redirect URI")
assert.Equal(t, linodego.OAuthClientStatus("active"), oauthClient.Status, "Unexpected Status")
assert.Equal(t, "https://api.linode.com/v4/account/clients/2737bf16b39ab5d7b4a1/thumbnail", *oauthClient.ThumbnailURL, "Unexpected Thumbnail URL")
}
func TestAccountOauthClient_Update(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_oauth_client_update")
assert.NoError(t, err)
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)
requestData := linodego.OAuthClientUpdateOptions{
Label: "Test_Client_1_Updated",
RedirectURI: "https://example_updated.org/oauth/callback",
Public: true,
}
clientID := "2737bf16b39ab5d7b4a1"
base.MockPut(fmt.Sprintf("account/oauth-clients/%s", clientID), fixtureData)
oauthClient, err := base.Client.UpdateOAuthClient(context.Background(), clientID, requestData)
assert.NoError(t, err)
// Assertions on the updated data
assert.Equal(t, "2737bf16b39ab5d7b4a1", oauthClient.ID, "Unexpected ID")
assert.Equal(t, "Test_Client_1_Updated", oauthClient.Label, "Unexpected Label")
assert.True(t, oauthClient.Public, "Unexpected Public value")
assert.Equal(t, "https://example_updated.org/oauth/callback", oauthClient.RedirectURI, "Unexpected Redirect URI")
assert.Equal(t, linodego.OAuthClientStatus("active"), oauthClient.Status, "Unexpected Status")
assert.Equal(t, "https://api.linode.com/v4/account/clients/2737bf16b39ab5d7b4a1/thumbnail", *oauthClient.ThumbnailURL, "Unexpected Thumbnail URL")
}
func TestAccountOauthClient_Delete(t *testing.T) {
client := createMockClient(t)
clientID := "2737bf16b39ab5d7b4a1"
httpmock.RegisterRegexpResponder("DELETE", mockRequestURL(t, fmt.Sprintf("account/oauth-clients/%s", clientID)),
httpmock.NewStringResponder(200, "{}"))
if err := client.DeleteOAuthClient(context.Background(), clientID); err != nil {
t.Fatal(err)
}
}
func TestAccountOauthClient_Reset(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_oauth_client_reset")
assert.NoError(t, err)
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)
clientID := "2737bf16b39ab5d7b4a1"
base.MockPost(fmt.Sprintf("account/oauth-clients/%s/reset-secret", clientID), fixtureData)
oauthClient, err := base.Client.ResetOAuthClientSecret(context.Background(), clientID)
assert.NoError(t, err)
assert.Equal(t, "2737bf16b39ab5d7b4a1", oauthClient.ID, "Unexpected ID")
assert.Equal(t, "Test_Client_1", oauthClient.Label, "Unexpected Label")
assert.False(t, oauthClient.Public, "Unexpected Public value")
assert.Equal(t, "https://example.org/oauth/callback", oauthClient.RedirectURI, "Unexpected Redirect URI")
assert.Equal(t, linodego.OAuthClientStatus("active"), oauthClient.Status, "Unexpected Status")
assert.Equal(t, "https://api.linode.com/v4/account/clients/2737bf16b39ab5d7b4a1/thumbnail", *oauthClient.ThumbnailURL, "Unexpected Thumbnail URL")
assert.Equal(t, "<REDACTED>", oauthClient.Secret, "Secret should have been reset")
assert.NotEmpty(t, oauthClient.Secret, "Secret should not be empty after reset")
}
|