Skip to content

Commit

Permalink
test(internalauth): add unittests (#258)
Browse files Browse the repository at this point in the history
* test(internalauth): add unittests

Co-authored-by: Thorsten Essig <tessig@users.noreply.github.com>
  • Loading branch information
bastianccm and tessig committed May 18, 2022
1 parent 3e91126 commit 46341b4
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions core/internalauth/application/oauthService_test.go
@@ -0,0 +1,56 @@
package application_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"flamingo.me/flamingo/v3/core/internalauth/application"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2/clientcredentials"
)

func TestOauthService(t *testing.T) {
service := new(application.OauthService)
service.Inject(&struct {
BaseURL string `inject:"config:core.internalauth.baseurl"`
}{
BaseURL: "http://example.com/",
})

t.Run("GetConfig", func(t *testing.T) {
cfg := service.GetConfig("/path", "client-id", "client-secret", "grant-type")
assert.Equal(t, "client-id", cfg.ClientID)
assert.Equal(t, "client-secret", cfg.ClientSecret)
assert.Equal(t, url.Values{}, cfg.EndpointParams)
assert.Equal(t, "http://example.com/path", cfg.TokenURL)
})

t.Run("GetOauthToken", func(t *testing.T) {
_, err := service.GetOauthToken(context.Background(), &clientcredentials.Config{})
assert.Error(t, err)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
fmt.Fprintf(w, `{"access_token": "test"}`)
}))
defer server.Close()
token, err := service.GetOauthToken(context.Background(), &clientcredentials.Config{
TokenURL: server.URL,
})
assert.NoError(t, err)
assert.Equal(t, "test", token.AccessToken)
})

t.Run("GetClaimsFromToken", func(t *testing.T) {
claims := service.GetClaimsFromToken("token")
assert.Empty(t, claims)

claims = service.GetClaimsFromToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXN0IjoidmFsdWUifQ.signature")
assert.NotEmpty(t, claims)
assert.Equal(t, "value", claims["test"])
})
}

0 comments on commit 46341b4

Please sign in to comment.