This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,94 @@ | ||
package store | ||
|
||
import ( | ||
oauth22 "github.com/dpattmann/furby/oauth2" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
furbyOauth2 "github.com/dpattmann/furby/oauth2" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var ( | ||
mockClientConfig = oauth22.NewClientCredentialsConfig("ClientIdValue", "ClientSecretValue", "http://localhost:8081", []string{}) | ||
validMockClientConfig = furbyOauth2.NewClientCredentialsConfig("ClientIdValue", "ClientSecretValue", "http://localhost:8080", []string{}) | ||
invalidMockClientConfig = furbyOauth2.NewClientCredentialsConfig("ClientIdValue", "ClientSecretValue", "http://localhost:8081", []string{}) | ||
) | ||
|
||
func TestStore_GetToken(t *testing.T) { | ||
// Setup http mock backend | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("POST", "http://localhost:8081", httpmock.NewJsonResponderOrPanic(200, oauth2.Token{ | ||
expirationDate := time.Now().Add(time.Minute * 5) | ||
httpmock.RegisterResponder("POST", "http://localhost:8080", httpmock.NewJsonResponderOrPanic(http.StatusOK, oauth2.Token{ | ||
AccessToken: "AccessTokenValue", | ||
TokenType: "TokenTypeValue", | ||
Expiry: expirationDate, | ||
})) | ||
|
||
tokenStore := NewMemoryStore(mockClientConfig) | ||
httpmock.RegisterResponder("POST", "http://localhost:8081", httpmock.NewStringResponder(400, "Invalid Token Request")) | ||
|
||
validTokenStore := NewMemoryStore(validMockClientConfig) | ||
invalidTokenStore := NewMemoryStore(invalidMockClientConfig) | ||
|
||
testToken := &oauth2.Token{ | ||
wantedToken := &oauth2.Token{ | ||
AccessToken: "AccessTokenValue", | ||
TokenType: "TokenTypeValue", | ||
Expiry: expirationDate, | ||
} | ||
// mock backend ready | ||
|
||
token, err := tokenStore.GetToken() | ||
t.Run("Get new token from token server", func(t *testing.T) { | ||
assert.Equal(t, 0, httpmock.GetTotalCallCount()) | ||
token, err := validTokenStore.GetToken() | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, testToken.AccessToken, token.AccessToken) | ||
assert.Equal(t, testToken.TokenType, token.TokenType) | ||
} | ||
assert.NoError(t, err) | ||
assert.Equal(t, wantedToken.AccessToken, token.AccessToken) | ||
assert.Equal(t, wantedToken.TokenType, token.TokenType) | ||
assert.Equal(t, 1, httpmock.GetTotalCallCount()) | ||
}) | ||
|
||
func TestStore_ErrorGettingToken(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
t.Run("Get valid token from store", func(t *testing.T) { | ||
token, err := validTokenStore.GetToken() | ||
|
||
httpmock.RegisterResponder("POST", "http://localhost:8081", httpmock.NewStringResponder(400, "Invalid Token Request")) | ||
assert.NoError(t, err) | ||
assert.Equal(t, wantedToken.AccessToken, token.AccessToken) | ||
assert.Equal(t, wantedToken.TokenType, token.TokenType) | ||
assert.Equal(t, 1, httpmock.GetTotalCallCount()) | ||
}) | ||
|
||
tokenStore := NewMemoryStore(mockClientConfig) | ||
t.Run("Get error from token server", func(t *testing.T) { | ||
_, err := invalidTokenStore.GetToken() | ||
|
||
_ = &oauth2.Token{ | ||
AccessToken: "AccessTokenValue", | ||
TokenType: "TokenTypeValue", | ||
} | ||
|
||
_, err := tokenStore.GetToken() | ||
|
||
assert.Error(t, err) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestStore_ReturnValidToken(t *testing.T) { | ||
tokenStore := NewMemoryStore(mockClientConfig) | ||
func BenchmarkGetToken1(b *testing.B) { benchmarkGetTokenWithOneSecondHttpDelay(b) } | ||
func BenchmarkGetToken10(b *testing.B) { benchmarkGetTokenWithOneSecondHttpDelay(b) } | ||
func BenchmarkGetToken100(b *testing.B) { benchmarkGetTokenWithOneSecondHttpDelay(b) } | ||
func BenchmarkGetToken1000(b *testing.B) { benchmarkGetTokenWithOneSecondHttpDelay(b) } | ||
func BenchmarkGetToken10000(b *testing.B) { benchmarkGetTokenWithOneSecondHttpDelay(b) } | ||
|
||
date := time.Now().Add(time.Minute * 5) | ||
func benchmarkGetTokenWithOneSecondHttpDelay(b *testing.B) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
testToken := &oauth2.Token{ | ||
AccessToken: "AccessTokenValue", | ||
TokenType: "TokenTypeValue", | ||
Expiry: date, | ||
} | ||
httpmock.RegisterResponder("POST", "http://localhost:8081", func(request *http.Request) (*http.Response, error) { | ||
time.Sleep(1 * time.Second) | ||
|
||
tokenStore.setToken(testToken) | ||
token, err := tokenStore.GetToken() | ||
return httpmock.NewJsonResponse(http.StatusOK, oauth2.Token{ | ||
AccessToken: "AccessTokenValue", | ||
TokenType: "TokenTypeValue", | ||
}) | ||
}) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, testToken.AccessToken, token.AccessToken) | ||
assert.Equal(t, testToken.TokenType, token.TokenType) | ||
tokenStore := NewMemoryStore(validMockClientConfig) | ||
|
||
for i := 0; i < b.N; i++ { | ||
_, _ = tokenStore.GetToken() | ||
} | ||
} |