File: account_oauth_client_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (110 lines) | stat: -rw-r--r-- 3,259 bytes parent folder | download
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
package integration

import (
	"context"
	"testing"

	"github.com/linode/linodego"
	. "github.com/linode/linodego"
	"github.com/stretchr/testify/assert"
)

func TestOAuthClient_GetMissing(t *testing.T) {
	client, teardown := createTestClient(t, "fixtures/TestOAuthClient_GetMissing")
	defer teardown()

	i, err := client.GetOAuthClient(context.Background(), "does-not-exist")
	if err == nil {
		t.Errorf("should have received an error requesting a missing oauthClient, got %v", i)
	}
	e, ok := err.(*Error)
	if !ok {
		t.Errorf("should have received an Error requesting a missing oauthClient, got %v", e)
	}

	if e.Code != 404 {
		t.Errorf("should have received a 404 Code requesting a missing oauthClient, got %v", e.Code)
	}
}

func TestOAuthClient_GetFound(t *testing.T) {
	createOpts := linodego.OAuthClientCreateOptions{
		Public:      true,
		RedirectURI: "https://example.com",
		Label:       "go-client-test",
	}

	client, oauthClient, teardown, err := setupOAuthClient(t, createOpts, "fixtures/TestOAuthClient_GetFound")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	i, err := client.GetOAuthClient(context.Background(), oauthClient.ID)
	if err != nil {
		t.Errorf("Error getting oauthClient, expected struct, got %v and error %v", i, err)
	}
	if i.Label != createOpts.Label {
		t.Errorf("Expected a specific oauthClient, but got a different one %v", i)
	}
}

func TestOAuthClients_List(t *testing.T) {
	createOpts := linodego.OAuthClientCreateOptions{
		Public:      true,
		RedirectURI: "https://example.com",
		Label:       "go-client-test",
	}
	client, _, teardown, err := setupOAuthClient(t, createOpts, "fixtures/TestOAuthClients_List")
	defer teardown()
	if err != nil {
		t.Error(err)
	}
	i, err := client.ListOAuthClients(context.Background(), nil)
	if err != nil {
		t.Errorf("Error listing oauthClients, expected struct, got error %v", err)
	}
	if len(i) == 0 {
		t.Errorf("Expected a list of oauthClients, but got none %v", i)
	}
}

func TestOAuthClients_Reset(t *testing.T) {
	createOpts := linodego.OAuthClientCreateOptions{
		Public:      true,
		RedirectURI: "https://example.com",
		Label:       "go-client-test",
	}
	client, oauthClient, teardown, err := setupOAuthClient(t, createOpts, "fixtures/TestOAuthClients_Reset")
	defer teardown()
	if err != nil {
		t.Error(err)
	}
	oauthClientAfterReset, err := client.ResetOAuthClientSecret(context.Background(), oauthClient.ID)
	if err != nil {
		t.Errorf("Error resetting oauthClient secret, expected struct, got error %v", err)
	}

	assert.NotEqual(t, oauthClient.Secret, oauthClientAfterReset.Secret, "Secret should have been reset")
}

func setupOAuthClient(
	t *testing.T,
	createOpts linodego.OAuthClientCreateOptions,
	fixturesYaml string,
) (*linodego.Client, *linodego.OAuthClient, func(), error) {
	t.Helper()
	client, fixtureTeardown := createTestClient(t, fixturesYaml)
	oauthClient, err := client.CreateOAuthClient(context.Background(), createOpts)
	if err != nil {
		t.Errorf("Error creating test OAuthClient: %s", err)
	}

	teardown := func() {
		if err := client.DeleteOAuthClient(context.Background(), oauthClient.ID); err != nil {
			t.Errorf("Error deleting test OAuthClient: %s", err)
		}
		fixtureTeardown()
	}
	return client, oauthClient, teardown, err
}