@@ -2,6 +2,7 @@ package auth
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "io"
78 "net/http"
@@ -11,8 +12,11 @@ import (
1112 "testing"
1213
1314 "github.com/coreos/go-oidc"
15+ "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service"
16+ stdConfig "github.com/flyteorg/flytestdlib/config"
1417 "github.com/stretchr/testify/assert"
1518 "github.com/stretchr/testify/mock"
19+ "github.com/stretchr/testify/require"
1620 "golang.org/x/oauth2"
1721 "google.golang.org/protobuf/types/known/structpb"
1822
@@ -21,8 +25,6 @@ import (
2125 "github.com/flyteorg/flyteadmin/auth/interfaces/mocks"
2226 "github.com/flyteorg/flyteadmin/pkg/common"
2327 "github.com/flyteorg/flyteadmin/plugins"
24- "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service"
25- stdConfig "github.com/flyteorg/flytestdlib/config"
2628)
2729
2830const (
@@ -50,8 +52,8 @@ func setupMockedAuthContextAtEndpoint(endpoint string) *mocks.AuthenticationCont
5052 Timeout : IdpConnectionTimeout ,
5153 }
5254 mockAuthCtx .OnCookieManagerMatch ().Return (mockCookieHandler )
53- mockCookieHandler .OnSetTokenCookiesMatch (mock .Anything , mock .Anything , mock .Anything , mock . Anything ).Return (nil )
54- mockCookieHandler .OnSetUserInfoCookieMatch (mock .Anything , mock .Anything , mock .Anything , mock . Anything ).Return (nil )
55+ mockCookieHandler .OnSetTokenCookiesMatch (mock .Anything , mock .Anything , mock .Anything ).Return (nil ). Once ( )
56+ mockCookieHandler .OnSetUserInfoCookieMatch (mock .Anything , mock .Anything , mock .Anything ).Return (nil ). Once ( )
5557 mockAuthCtx .OnOAuth2ClientConfigMatch (mock .Anything ).Return (& dummyOAuth2Config )
5658 mockAuthCtx .OnGetHTTPClient ().Return (dummyHTTPClient )
5759 return mockAuthCtx
@@ -255,6 +257,97 @@ func TestGetLoginHandler(t *testing.T) {
255257 assert .True (t , strings .Contains (w .Header ().Get ("Set-Cookie" ), "flyte_csrf_state=" ))
256258}
257259
260+ func TestGetLogoutHandler (t * testing.T ) {
261+ ctx := context .Background ()
262+
263+ t .Run ("no_hook_no_redirect" , func (t * testing.T ) {
264+ cookieHandler := & CookieManager {}
265+ authCtx := mocks.AuthenticationContext {}
266+ authCtx .OnCookieManager ().Return (cookieHandler ).Once ()
267+ w := httptest .NewRecorder ()
268+ r := plugins .NewRegistry ()
269+ req , err := http .NewRequest (http .MethodGet , "/logout" , nil )
270+ require .NoError (t , err )
271+
272+ GetLogoutEndpointHandler (ctx , & authCtx , r )(w , req )
273+
274+ assert .Equal (t , http .StatusOK , w .Code )
275+ require .Len (t , w .Result ().Cookies (), 3 )
276+ authCtx .AssertExpectations (t )
277+ })
278+
279+ t .Run ("no_hook_with_redirect" , func (t * testing.T ) {
280+ ctx := context .Background ()
281+ cookieHandler := & CookieManager {}
282+ authCtx := mocks.AuthenticationContext {}
283+ authCtx .OnCookieManager ().Return (cookieHandler ).Once ()
284+ w := httptest .NewRecorder ()
285+ r := plugins .NewRegistry ()
286+ req , err := http .NewRequest (http .MethodGet , "/logout?redirect_url=/foo" , nil )
287+ require .NoError (t , err )
288+
289+ GetLogoutEndpointHandler (ctx , & authCtx , r )(w , req )
290+
291+ assert .Equal (t , http .StatusTemporaryRedirect , w .Code )
292+ authCtx .AssertExpectations (t )
293+ require .Len (t , w .Result ().Cookies (), 3 )
294+ })
295+
296+ t .Run ("with_hook_with_redirect" , func (t * testing.T ) {
297+ ctx := context .Background ()
298+ cookieHandler := & CookieManager {}
299+ authCtx := mocks.AuthenticationContext {}
300+ authCtx .OnCookieManager ().Return (cookieHandler ).Once ()
301+ w := httptest .NewRecorder ()
302+ r := plugins .NewRegistry ()
303+ hook := new (mock.Mock )
304+ err := r .Register (plugins .PluginIDLogoutHook , LogoutHookFunc (func (
305+ ctx context.Context ,
306+ authCtx interfaces.AuthenticationContext ,
307+ request * http.Request ,
308+ w http.ResponseWriter ) error {
309+ return hook .MethodCalled ("hook" ).Error (0 )
310+ }))
311+ hook .On ("hook" ).Return (nil ).Once ()
312+ require .NoError (t , err )
313+ req , err := http .NewRequest (http .MethodGet , "/logout?redirect_url=/foo" , nil )
314+ require .NoError (t , err )
315+
316+ GetLogoutEndpointHandler (ctx , & authCtx , r )(w , req )
317+
318+ assert .Equal (t , http .StatusTemporaryRedirect , w .Code )
319+ require .Len (t , w .Result ().Cookies (), 3 )
320+ authCtx .AssertExpectations (t )
321+ hook .AssertExpectations (t )
322+ })
323+
324+ t .Run ("hook_error" , func (t * testing.T ) {
325+ ctx := context .Background ()
326+ authCtx := mocks.AuthenticationContext {}
327+ w := httptest .NewRecorder ()
328+ r := plugins .NewRegistry ()
329+ hook := new (mock.Mock )
330+ err := r .Register (plugins .PluginIDLogoutHook , LogoutHookFunc (func (
331+ ctx context.Context ,
332+ authCtx interfaces.AuthenticationContext ,
333+ request * http.Request ,
334+ w http.ResponseWriter ) error {
335+ return hook .MethodCalled ("hook" ).Error (0 )
336+ }))
337+ hook .On ("hook" ).Return (errors .New ("fail" )).Once ()
338+ require .NoError (t , err )
339+ req , err := http .NewRequest (http .MethodGet , "/logout?redirect_url=/foo" , nil )
340+ require .NoError (t , err )
341+
342+ GetLogoutEndpointHandler (ctx , & authCtx , r )(w , req )
343+
344+ assert .Equal (t , http .StatusInternalServerError , w .Code )
345+ assert .Empty (t , w .Result ().Cookies ())
346+ authCtx .AssertExpectations (t )
347+ hook .AssertExpectations (t )
348+ })
349+ }
350+
258351func TestGetHTTPRequestCookieToMetadataHandler (t * testing.T ) {
259352 ctx := context .Background ()
260353 // These were generated for unit testing only.
0 commit comments