Files
opencloud/services/proxy/pkg/middleware/oidc_auth_test.go
Jörn Friedrich Dreyer fad94d2038 bump mockery, add test stub for oidc_auth.go, align mock generation (#8321)
* bump mockery, add test stub for oidc_auth.go

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* use .mockery.yaml for all mocks

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* drop legacy go:generate mockery

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* align mock placement

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

---------

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
2024-02-01 10:07:44 +01:00

68 lines
1.7 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"time"
"github.com/golang-jwt/jwt/v4"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
oidcmocks "github.com/owncloud/ocis/v2/ocis-pkg/oidc/mocks"
"github.com/stretchr/testify/mock"
"go-micro.dev/v4/store"
)
var _ = Describe("Authenticating requests", Label("OIDCAuthenticator"), func() {
var authenticator Authenticator
oc := oidcmocks.OIDCClient{}
oc.On("VerifyAccessToken", mock.Anything, mock.Anything).Return(
oidc.RegClaimsWithSID{
SessionID: "a-session-id",
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Unix(1147483647, 0)),
},
}, jwt.MapClaims{
"sid": "a-session-id",
"exp": 1147483647,
},
nil,
)
/*
// to test with skipUserInfo: true, we need to also use an interface so we can mock the UserInfo.Claim call
oc.On("UserInfo", mock.Anything, mock.Anything).Return(
&oidc.UserInfo{
Subject: "my-sub",
EmailVerified: true,
Email: "test@example.org",
},
nil,
)
*/
BeforeEach(func() {
authenticator = &OIDCAuthenticator{
OIDCIss: "http://idp.example.com",
Logger: log.NewLogger(),
oidcClient: &oc,
userInfoCache: store.NewMemoryStore(),
skipUserInfo: true,
}
})
When("the request contains correct data", func() {
It("should successfully authenticate", func() {
req := httptest.NewRequest(http.MethodGet, "http://example.com/example/path", http.NoBody)
req.Header.Set(_headerAuthorization, "Bearer jwt.token.sig")
req2, valid := authenticator.Authenticate(req)
Expect(valid).To(Equal(true))
Expect(req2).ToNot(BeNil())
})
})
})