From 654237c4879d6da3e3d93f9336a86ae61b92532e Mon Sep 17 00:00:00 2001 From: Flora Thiebaut Date: Wed, 16 Oct 2024 15:28:40 +0200 Subject: [PATCH 1/3] add tests --- internal/oidc/client_store_test.go | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 internal/oidc/client_store_test.go diff --git a/internal/oidc/client_store_test.go b/internal/oidc/client_store_test.go new file mode 100644 index 00000000..13e6b3d5 --- /dev/null +++ b/internal/oidc/client_store_test.go @@ -0,0 +1,35 @@ +package oidc + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAuthHandler(t *testing.T) { + client := oidcClient{ + client: newMockRelyingParty("https://token.url"), + id: "renku", + } + store := ClientStore{ + "renku": client, + } + + handler, err := store.AuthHandler("renku", "abcde-12345") + require.NoError(t, err) + + e := echo.New() + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + err = echo.WrapHandler(handler)(c) + require.NoError(t, err) + + // handler(c.Response(), req) + assert.Equal(t, http.StatusOK, rec.Code) + // assert.Equal(t, "", handler) +} From b7d8790eac0f6b59bd2be5d2f87c46acf63a1f0c Mon Sep 17 00:00:00 2001 From: Flora Thiebaut Date: Wed, 16 Oct 2024 15:42:19 +0200 Subject: [PATCH 2/3] udpate --- internal/oidc/client_store_test.go | 20 ++++++++++++++++---- internal/oidc/client_test.go | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/internal/oidc/client_store_test.go b/internal/oidc/client_store_test.go index 13e6b3d5..c70e4729 100644 --- a/internal/oidc/client_store_test.go +++ b/internal/oidc/client_store_test.go @@ -12,7 +12,7 @@ import ( func TestAuthHandler(t *testing.T) { client := oidcClient{ - client: newMockRelyingParty("https://token.url"), + client: mockRelyingParty{isPKCE: false, tokenURL: "https://token.url"}, id: "renku", } store := ClientStore{ @@ -29,7 +29,19 @@ func TestAuthHandler(t *testing.T) { err = echo.WrapHandler(handler)(c) require.NoError(t, err) - // handler(c.Response(), req) - assert.Equal(t, http.StatusOK, rec.Code) - // assert.Equal(t, "", handler) + assert.Equal(t, http.StatusFound, rec.Code) + assert.Contains(t, rec.Header().Get("location"), "client_id=mock-client&response_type=code&state=abcde-12345") +} + +func TestAuthHandlerInvalidProvider(t *testing.T) { + client := oidcClient{ + client: mockRelyingParty{isPKCE: false, tokenURL: "https://token.url"}, + id: "renku", + } + store := ClientStore{ + "renku": client, + } + + _, err := store.AuthHandler("another", "abcde-12345") + assert.ErrorContains(t, err, "cannot find the provider with ID another") } diff --git a/internal/oidc/client_test.go b/internal/oidc/client_test.go index 41d86945..e56ead30 100644 --- a/internal/oidc/client_test.go +++ b/internal/oidc/client_test.go @@ -23,12 +23,13 @@ type mockRelyingParty struct { func (m mockRelyingParty) OAuthConfig() *oauth2.Config { return &oauth2.Config{ + ClientID: "mock-client", Endpoint: oauth2.Endpoint{TokenURL: m.tokenURL}, } } func (m mockRelyingParty) Issuer() string { - return "" + return "mock-issuer" } func (m mockRelyingParty) IsPKCE() bool { From eae1d97df6c5d9f04e9df2176518c69ad3ac9d42 Mon Sep 17 00:00:00 2001 From: Flora Thiebaut Date: Wed, 16 Oct 2024 16:06:44 +0200 Subject: [PATCH 3/3] update --- internal/oidc/client_store_test.go | 28 ++++++++++++++++++++++++++++ internal/oidc/client_test.go | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/internal/oidc/client_store_test.go b/internal/oidc/client_store_test.go index c70e4729..ec2b8bfc 100644 --- a/internal/oidc/client_store_test.go +++ b/internal/oidc/client_store_test.go @@ -45,3 +45,31 @@ func TestAuthHandlerInvalidProvider(t *testing.T) { _, err := store.AuthHandler("another", "abcde-12345") assert.ErrorContains(t, err, "cannot find the provider with ID another") } + +func TestUserProfileURL(t *testing.T) { + client := oidcClient{ + client: mockRelyingParty{isPKCE: false, tokenURL: "https://token.url"}, + id: "renku", + } + store := ClientStore{ + "renku": client, + } + + profileURL, err := store.UserProfileURL("renku") + require.NoError(t, err) + + assert.Equal(t, "https://token.url/account?referrer=renku", profileURL.String()) +} + +func TestUserProfileURLInvalidProvider(t *testing.T) { + client := oidcClient{ + client: mockRelyingParty{isPKCE: false, tokenURL: "https://token.url"}, + id: "renku", + } + store := ClientStore{ + "renku": client, + } + + _, err := store.UserProfileURL("another") + assert.ErrorContains(t, err, "cannot find the provider with ID another") +} diff --git a/internal/oidc/client_test.go b/internal/oidc/client_test.go index e56ead30..0c83755c 100644 --- a/internal/oidc/client_test.go +++ b/internal/oidc/client_test.go @@ -29,7 +29,7 @@ func (m mockRelyingParty) OAuthConfig() *oauth2.Config { } func (m mockRelyingParty) Issuer() string { - return "mock-issuer" + return "https://token.url" } func (m mockRelyingParty) IsPKCE() bool {