mirror of
https://github.com/int128/kubelogin.git
synced 2026-03-02 17:00:20 +00:00
116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
package authcode
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/int128/kubelogin/pkg/adaptors/browser/mock_browser"
|
|
"github.com/int128/kubelogin/pkg/adaptors/oidcclient"
|
|
"github.com/int128/kubelogin/pkg/adaptors/oidcclient/mock_oidcclient"
|
|
"github.com/int128/kubelogin/pkg/domain/jwt"
|
|
"github.com/int128/kubelogin/pkg/domain/oidc"
|
|
"github.com/int128/kubelogin/pkg/testing/logger"
|
|
)
|
|
|
|
func TestBrowser_Do(t *testing.T) {
|
|
dummyTokenClaims := jwt.Claims{
|
|
Subject: "YOUR_SUBJECT",
|
|
Expiry: time.Date(2019, 1, 2, 3, 4, 5, 0, time.UTC),
|
|
Pretty: "PRETTY_JSON",
|
|
}
|
|
timeout := 5 * time.Second
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
|
|
defer cancel()
|
|
o := &BrowserOption{
|
|
BindAddress: []string{"127.0.0.1:8000"},
|
|
SkipOpenBrowser: true,
|
|
RedirectURLHostname: "localhost",
|
|
AuthRequestExtraParams: map[string]string{"ttl": "86400", "reauth": "true"},
|
|
}
|
|
mockOIDCClient := mock_oidcclient.NewMockInterface(ctrl)
|
|
mockOIDCClient.EXPECT().SupportedPKCEMethods()
|
|
mockOIDCClient.EXPECT().
|
|
GetTokenByAuthCode(gomock.Any(), gomock.Any(), gomock.Any()).
|
|
Do(func(_ context.Context, in oidcclient.GetTokenByAuthCodeInput, readyChan chan<- string) {
|
|
if diff := cmp.Diff(o.BindAddress, in.BindAddress); diff != "" {
|
|
t.Errorf("BindAddress mismatch (-want +got):\n%s", diff)
|
|
}
|
|
if diff := cmp.Diff(o.RedirectURLHostname, in.RedirectURLHostname); diff != "" {
|
|
t.Errorf("RedirectURLHostname mismatch (-want +got):\n%s", diff)
|
|
}
|
|
if diff := cmp.Diff(o.AuthRequestExtraParams, in.AuthRequestExtraParams); diff != "" {
|
|
t.Errorf("AuthRequestExtraParams mismatch (-want +got):\n%s", diff)
|
|
}
|
|
readyChan <- "LOCAL_SERVER_URL"
|
|
}).
|
|
Return(&oidc.TokenSet{
|
|
IDToken: "YOUR_ID_TOKEN",
|
|
RefreshToken: "YOUR_REFRESH_TOKEN",
|
|
IDTokenClaims: dummyTokenClaims,
|
|
}, nil)
|
|
u := Browser{
|
|
Logger: logger.New(t),
|
|
}
|
|
got, err := u.Do(ctx, o, mockOIDCClient)
|
|
if err != nil {
|
|
t.Errorf("Do returned error: %+v", err)
|
|
}
|
|
want := &oidc.TokenSet{
|
|
IDToken: "YOUR_ID_TOKEN",
|
|
RefreshToken: "YOUR_REFRESH_TOKEN",
|
|
IDTokenClaims: dummyTokenClaims,
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("mismatch (-want +got):\n%s", diff)
|
|
}
|
|
})
|
|
|
|
t.Run("OpenBrowser", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
|
|
defer cancel()
|
|
o := &BrowserOption{
|
|
BindAddress: []string{"127.0.0.1:8000"},
|
|
}
|
|
mockOIDCClient := mock_oidcclient.NewMockInterface(ctrl)
|
|
mockOIDCClient.EXPECT().SupportedPKCEMethods()
|
|
mockOIDCClient.EXPECT().
|
|
GetTokenByAuthCode(gomock.Any(), gomock.Any(), gomock.Any()).
|
|
Do(func(_ context.Context, _ oidcclient.GetTokenByAuthCodeInput, readyChan chan<- string) {
|
|
readyChan <- "LOCAL_SERVER_URL"
|
|
}).
|
|
Return(&oidc.TokenSet{
|
|
IDToken: "YOUR_ID_TOKEN",
|
|
RefreshToken: "YOUR_REFRESH_TOKEN",
|
|
IDTokenClaims: dummyTokenClaims,
|
|
}, nil)
|
|
mockBrowser := mock_browser.NewMockInterface(ctrl)
|
|
mockBrowser.EXPECT().
|
|
Open("LOCAL_SERVER_URL")
|
|
u := Browser{
|
|
Logger: logger.New(t),
|
|
Browser: mockBrowser,
|
|
}
|
|
got, err := u.Do(ctx, o, mockOIDCClient)
|
|
if err != nil {
|
|
t.Errorf("Do returned error: %+v", err)
|
|
}
|
|
want := &oidc.TokenSet{
|
|
IDToken: "YOUR_ID_TOKEN",
|
|
RefreshToken: "YOUR_REFRESH_TOKEN",
|
|
IDTokenClaims: dummyTokenClaims,
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("mismatch (-want +got):\n%s", diff)
|
|
}
|
|
})
|
|
}
|