Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add unit test coverage for account related functions; Add function and test coverage for reset oauth client secret endpoint #636

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions account_oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,14 @@ func (c *Client) DeleteOAuthClient(ctx context.Context, clientID string) error {
err := doDELETERequest(ctx, c, e)
return err
}

// ResetOAuthClientSecret resets the OAuth Client secret for a client with a specified id
func (c *Client) ResetOAuthClientSecret(ctx context.Context, clientID string) (*OAuthClient, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e := formatAPIPath("account/oauth-clients/%s/reset-secret", clientID)
response, err := doPOSTRequest[OAuthClient, any](ctx, c, e)
if err != nil {
return nil, err
}

return response, nil
}
20 changes: 20 additions & 0 deletions test/integration/account_oauth_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"context"
"github.com/stretchr/testify/assert"
"testing"

"github.com/linode/linodego"
Expand Down Expand Up @@ -68,6 +69,25 @@ func TestOAuthClients_List(t *testing.T) {
}
}

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)
Expand Down
190 changes: 190 additions & 0 deletions test/integration/fixtures/TestOAuthClients_Reset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
---
version: 1
interactions:
- request:
body: '{"redirect_uri":"https://example.com","label":"go-client-test","public":true}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- linodego/dev https://github.com/linode/linodego
url: https://api.linode.com/v4beta/account/oauth-clients
method: POST
response:
body: '{"id": "757a5af92718d7369687", "redirect_uri": "https://example.com", "label":
"go-client-test", "status": "active", "secret": "1950e1df7217e8f8f1543ba8b1d12c03e6088eb1429e10549e18c650b677015f",
"thumbnail_url": null, "public": true}'
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Headers:
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
Access-Control-Allow-Methods:
- HEAD, GET, OPTIONS, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Access-Control-Expose-Headers:
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
Akamai-Internal-Account:
- '*'
Cache-Control:
- max-age=0, no-cache, no-store
Connection:
- keep-alive
Content-Length:
- "233"
Content-Security-Policy:
- default-src 'none'
Content-Type:
- application/json
Expires:
- Tue, 03 Dec 2024 01:14:47 GMT
Pragma:
- no-cache
Strict-Transport-Security:
- max-age=31536000
Vary:
- Authorization, X-Filter
X-Accepted-Oauth-Scopes:
- account:read_write
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
- DENY
X-Oauth-Scopes:
- '*'
X-Ratelimit-Limit:
- "1600"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- linodego/dev https://github.com/linode/linodego
url: https://api.linode.com/v4beta/account/oauth-clients/757a5af92718d7369687/reset-secret
method: POST
response:
body: '{"id": "757a5af92718d7369687", "redirect_uri": "https://example.com", "label":
"go-client-test", "status": "active", "secret": "af3ccc4a7d881a8a858593080d0bffb1e9c6b1917cb88e28bd2b10ad885679cb",
"thumbnail_url": null, "public": true}'
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Headers:
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
Access-Control-Allow-Methods:
- HEAD, GET, OPTIONS, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Access-Control-Expose-Headers:
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
Akamai-Internal-Account:
- '*'
Cache-Control:
- max-age=0, no-cache, no-store
Connection:
- keep-alive
Content-Length:
- "233"
Content-Security-Policy:
- default-src 'none'
Content-Type:
- application/json
Expires:
- Tue, 03 Dec 2024 01:14:47 GMT
Pragma:
- no-cache
Strict-Transport-Security:
- max-age=31536000
Vary:
- Authorization, X-Filter
X-Accepted-Oauth-Scopes:
- account:read_write
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
- DENY
X-Oauth-Scopes:
- '*'
X-Ratelimit-Limit:
- "1600"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- linodego/dev https://github.com/linode/linodego
url: https://api.linode.com/v4beta/account/oauth-clients/757a5af92718d7369687
method: DELETE
response:
body: '{}'
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Headers:
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
Access-Control-Allow-Methods:
- HEAD, GET, OPTIONS, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Access-Control-Expose-Headers:
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
Akamai-Internal-Account:
- '*'
Cache-Control:
- max-age=0, no-cache, no-store
Connection:
- keep-alive
Content-Length:
- "2"
Content-Security-Policy:
- default-src 'none'
Content-Type:
- application/json
Expires:
- Tue, 03 Dec 2024 01:14:48 GMT
Pragma:
- no-cache
Strict-Transport-Security:
- max-age=31536000
Vary:
- Authorization, X-Filter
X-Accepted-Oauth-Scopes:
- account:read_write
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
- DENY
X-Oauth-Scopes:
- '*'
X-Ratelimit-Limit:
- "1600"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
90 changes: 90 additions & 0 deletions test/unit/account_availability_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package unit

import (
"context"
"fmt"
"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"testing"
)

func TestAccountAvailabilities_List(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_availability_list")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("account/availability", fixtureData)

availabilities, err := base.Client.ListAccountAvailabilities(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err)

// Check specific region "us-central"
var usCentralAvailability *linodego.AccountAvailability
for _, availability := range availabilities {
if availability.Region == "us-central" {
usCentralAvailability = &availability
break
}
}
if usCentralAvailability == nil {
t.Errorf("Expected region 'us-central' to be in the response, but it was not found")
} else {
expectedAvailable := []string{"Linodes", "NodeBalancers", "Block Storage", "Kubernetes"}
if !equalSlices(usCentralAvailability.Available, expectedAvailable) {
t.Errorf("Expected available resources for 'us-central' to be %v, but got %v", expectedAvailable, usCentralAvailability.Available)
}

if len(usCentralAvailability.Unavailable) != 0 {
t.Errorf("Expected no unavailable resources for 'us-central', but got %v", usCentralAvailability.Unavailable)
}
}

expectedRegionsCount := 40
if len(availabilities) != expectedRegionsCount {
t.Errorf("Expected %d regions, but got %d", expectedRegionsCount, len(availabilities))
}
}

func TestAccountAvailability_Get(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_availability_get")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

regionID := "us-east"

base.MockGet(fmt.Sprintf("account/availability/%s", regionID), fixtureData)

availability, err := base.Client.GetAccountAvailability(context.Background(), regionID)
assert.NoError(t, err)

assert.Equal(t, "us-east", availability.Region, "Expected region to be 'us-east'")

expectedAvailable := []string{"Linodes", "NodeBalancers"}
assert.ElementsMatch(t, expectedAvailable, availability.Available, "Available resources do not match the expected list")

expectedUnavailable := []string{"Kubernetes", "Block Storage"}
assert.ElementsMatch(t, expectedUnavailable, availability.Unavailable, "Unavailable resources do not match the expected list")
}

// Helper function to compare slices in assertion
func equalSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
aMap := make(map[string]bool)
for _, v := range a {
aMap[v] = true
}
for _, v := range b {
if !aMap[v] {
return false
}
}
return true
}
Loading
Loading