From cffb00f3864553e31eb31f4cbbd62fa0772b086f Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Mon, 23 Nov 2020 18:20:47 +0900 Subject: [PATCH] Refactor: extract tests into authentication_test.go (#431) --- pkg/cmd/authentication_test.go | 141 ++++++++++++++++++++++ pkg/cmd/cmd_test.go | 210 ++------------------------------- 2 files changed, 148 insertions(+), 203 deletions(-) create mode 100644 pkg/cmd/authentication_test.go diff --git a/pkg/cmd/authentication_test.go b/pkg/cmd/authentication_test.go new file mode 100644 index 00000000..1f9f8479 --- /dev/null +++ b/pkg/cmd/authentication_test.go @@ -0,0 +1,141 @@ +package cmd + +import ( + "testing" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/int128/kubelogin/pkg/usecases/authentication" + "github.com/int128/kubelogin/pkg/usecases/authentication/authcode" + "github.com/int128/kubelogin/pkg/usecases/authentication/ropc" + "github.com/spf13/pflag" +) + +func Test_authenticationOptions_grantOptionSet(t *testing.T) { + tests := map[string]struct { + args []string + want authentication.GrantOptionSet + }{ + "NoFlag": { + want: authentication.GrantOptionSet{ + AuthCodeBrowserOption: &authcode.BrowserOption{ + BindAddress: defaultListenAddress, + AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, + RedirectURLHostname: "localhost", + }, + }, + }, + "FullOptions": { + args: []string{ + "--grant-type", "authcode", + "--listen-address", "127.0.0.1:10080", + "--listen-address", "127.0.0.1:20080", + "--skip-open-browser", + "--authentication-timeout-sec", "10", + "--local-server-cert", "/path/to/local-server-cert", + "--local-server-key", "/path/to/local-server-key", + "--open-url-after-authentication", "https://example.com/success.html", + "--oidc-redirect-url-hostname", "example", + "--oidc-auth-request-extra-params", "ttl=86400", + "--oidc-auth-request-extra-params", "reauth=true", + "--username", "USER", + "--password", "PASS", + }, + want: authentication.GrantOptionSet{ + AuthCodeBrowserOption: &authcode.BrowserOption{ + BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, + SkipOpenBrowser: true, + AuthenticationTimeout: 10 * time.Second, + LocalServerCertFile: "/path/to/local-server-cert", + LocalServerKeyFile: "/path/to/local-server-key", + OpenURLAfterAuthentication: "https://example.com/success.html", + RedirectURLHostname: "example", + AuthRequestExtraParams: map[string]string{"ttl": "86400", "reauth": "true"}, + }, + }, + }, + "when --listen-port is set, it should convert the port to address": { + args: []string{ + "--listen-port", "10080", + "--listen-port", "20080", + }, + want: authentication.GrantOptionSet{ + AuthCodeBrowserOption: &authcode.BrowserOption{ + BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, + AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, + RedirectURLHostname: "localhost", + }, + }, + }, + "when --listen-port is set, it should ignore --listen-address flags": { + args: []string{ + "--listen-port", "10080", + "--listen-port", "20080", + "--listen-address", "127.0.0.1:30080", + "--listen-address", "127.0.0.1:40080", + }, + want: authentication.GrantOptionSet{ + AuthCodeBrowserOption: &authcode.BrowserOption{ + BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, + AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, + RedirectURLHostname: "localhost", + }, + }, + }, + "GrantType=authcode-keyboard": { + args: []string{ + "--grant-type", "authcode-keyboard", + }, + want: authentication.GrantOptionSet{ + AuthCodeKeyboardOption: &authcode.KeyboardOption{}, + }, + }, + "GrantType=password": { + args: []string{ + "--grant-type", "password", + "--listen-address", "127.0.0.1:10080", + "--listen-address", "127.0.0.1:20080", + "--username", "USER", + "--password", "PASS", + }, + want: authentication.GrantOptionSet{ + ROPCOption: &ropc.Option{ + Username: "USER", + Password: "PASS", + }, + }, + }, + "GrantType=auto": { + args: []string{ + "--listen-address", "127.0.0.1:10080", + "--listen-address", "127.0.0.1:20080", + "--username", "USER", + "--password", "PASS", + }, + want: authentication.GrantOptionSet{ + ROPCOption: &ropc.Option{ + Username: "USER", + Password: "PASS", + }, + }, + }, + } + + for name, c := range tests { + t.Run(name, func(t *testing.T) { + var o authenticationOptions + f := pflag.NewFlagSet("", pflag.ContinueOnError) + o.addFlags(f) + if err := f.Parse(c.args); err != nil { + t.Fatalf("Parse error: %s", err) + } + got, err := o.grantOptionSet() + if err != nil { + t.Fatalf("grantOptionSet error: %s", err) + } + if diff := cmp.Diff(c.want, got); diff != "" { + t.Errorf("mismatch (-want +got):\n%s", diff) + } + }) + } +} diff --git a/pkg/cmd/cmd_test.go b/pkg/cmd/cmd_test.go index 6f26ed16..3049cd8e 100644 --- a/pkg/cmd/cmd_test.go +++ b/pkg/cmd/cmd_test.go @@ -8,10 +8,8 @@ import ( "github.com/golang/mock/gomock" "github.com/int128/kubelogin/pkg/oidc" "github.com/int128/kubelogin/pkg/testing/logger" - "github.com/int128/kubelogin/pkg/tlsclientconfig" "github.com/int128/kubelogin/pkg/usecases/authentication" "github.com/int128/kubelogin/pkg/usecases/authentication/authcode" - "github.com/int128/kubelogin/pkg/usecases/authentication/ropc" "github.com/int128/kubelogin/pkg/usecases/credentialplugin" "github.com/int128/kubelogin/pkg/usecases/credentialplugin/mock_credentialplugin" "github.com/int128/kubelogin/pkg/usecases/standalone" @@ -39,59 +37,12 @@ func TestCmd_Run(t *testing.T) { }, }, }, - "when --listen-port is set, it should convert the port to address": { - args: []string{ - executable, - "--listen-port", "10080", - "--listen-port", "20080", - }, - in: standalone.Input{ - GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeBrowserOption: &authcode.BrowserOption{ - BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, - AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, - RedirectURLHostname: "localhost", - }, - }, - }, - }, - "when --listen-port is set, it should ignore --listen-address flags": { - args: []string{ - executable, - "--listen-port", "10080", - "--listen-port", "20080", - "--listen-address", "127.0.0.1:30080", - "--listen-address", "127.0.0.1:40080", - }, - in: standalone.Input{ - GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeBrowserOption: &authcode.BrowserOption{ - BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, - AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, - RedirectURLHostname: "localhost", - }, - }, - }, - }, "FullOptions": { args: []string{executable, "--kubeconfig", "/path/to/kubeconfig", "--context", "hello.k8s.local", "--user", "google", - "--certificate-authority", "/path/to/cacert", - "--certificate-authority-data", "BASE64ENCODED", - "--insecure-skip-tls-verify", "-v1", - "--grant-type", "authcode", - "--listen-address", "127.0.0.1:10080", - "--listen-address", "127.0.0.1:20080", - "--skip-open-browser", - "--authentication-timeout-sec", "10", - "--local-server-cert", "/path/to/local-server-cert", - "--local-server-key", "/path/to/local-server-key", - "--open-url-after-authentication", "https://example.com/success.html", - "--username", "USER", - "--password", "PASS", }, in: standalone.Input{ KubeconfigFilename: "/path/to/kubeconfig", @@ -99,61 +50,9 @@ func TestCmd_Run(t *testing.T) { KubeconfigUser: "google", GrantOptionSet: authentication.GrantOptionSet{ AuthCodeBrowserOption: &authcode.BrowserOption{ - BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, - SkipOpenBrowser: true, - AuthenticationTimeout: 10 * time.Second, - LocalServerCertFile: "/path/to/local-server-cert", - LocalServerKeyFile: "/path/to/local-server-key", - OpenURLAfterAuthentication: "https://example.com/success.html", - RedirectURLHostname: "localhost", - }, - }, - TLSClientConfig: tlsclientconfig.Config{ - CACertFilename: []string{"/path/to/cacert"}, - CACertData: []string{"BASE64ENCODED"}, - SkipTLSVerify: true, - }, - }, - }, - "GrantType=authcode-keyboard": { - args: []string{executable, - "--grant-type", "authcode-keyboard", - }, - in: standalone.Input{ - GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeKeyboardOption: &authcode.KeyboardOption{}, - }, - }, - }, - "GrantType=password": { - args: []string{executable, - "--grant-type", "password", - "--listen-address", "127.0.0.1:10080", - "--listen-address", "127.0.0.1:20080", - "--username", "USER", - "--password", "PASS", - }, - in: standalone.Input{ - GrantOptionSet: authentication.GrantOptionSet{ - ROPCOption: &ropc.Option{ - Username: "USER", - Password: "PASS", - }, - }, - }, - }, - "GrantType=auto": { - args: []string{executable, - "--listen-address", "127.0.0.1:10080", - "--listen-address", "127.0.0.1:20080", - "--username", "USER", - "--password", "PASS", - }, - in: standalone.Input{ - GrantOptionSet: authentication.GrantOptionSet{ - ROPCOption: &ropc.Option{ - Username: "USER", - Password: "PASS", + BindAddress: defaultListenAddress, + AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, + RedirectURLHostname: "localhost", }, }, }, @@ -217,7 +116,7 @@ func TestCmd_Run(t *testing.T) { }, GrantOptionSet: authentication.GrantOptionSet{ AuthCodeBrowserOption: &authcode.BrowserOption{ - BindAddress: []string{"127.0.0.1:8000", "127.0.0.1:18000"}, + BindAddress: defaultListenAddress, AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, RedirectURLHostname: "localhost", }, @@ -232,22 +131,7 @@ func TestCmd_Run(t *testing.T) { "--oidc-client-secret", "YOUR_CLIENT_SECRET", "--oidc-extra-scope", "email", "--oidc-extra-scope", "profile", - "--certificate-authority", "/path/to/cacert", - "--certificate-authority-data", "BASE64ENCODED", - "--insecure-skip-tls-verify", "-v1", - "--grant-type", "authcode", - "--listen-address", "127.0.0.1:10080", - "--listen-address", "127.0.0.1:20080", - "--skip-open-browser", - "--authentication-timeout-sec", "10", - "--local-server-cert", "/path/to/local-server-cert", - "--local-server-key", "/path/to/local-server-key", - "--open-url-after-authentication", "https://example.com/success.html", - "--oidc-auth-request-extra-params", "ttl=86400", - "--oidc-auth-request-extra-params", "reauth=true", - "--username", "USER", - "--password", "PASS", }, in: credentialplugin.Input{ TokenCacheDir: defaultTokenCacheDir, @@ -259,89 +143,9 @@ func TestCmd_Run(t *testing.T) { }, GrantOptionSet: authentication.GrantOptionSet{ AuthCodeBrowserOption: &authcode.BrowserOption{ - BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, - SkipOpenBrowser: true, - AuthenticationTimeout: 10 * time.Second, - LocalServerCertFile: "/path/to/local-server-cert", - LocalServerKeyFile: "/path/to/local-server-key", - OpenURLAfterAuthentication: "https://example.com/success.html", - RedirectURLHostname: "localhost", - AuthRequestExtraParams: map[string]string{"ttl": "86400", "reauth": "true"}, - }, - }, - TLSClientConfig: tlsclientconfig.Config{ - CACertFilename: []string{"/path/to/cacert"}, - CACertData: []string{"BASE64ENCODED"}, - SkipTLSVerify: true, - }, - }, - }, - "GrantType=authcode-keyboard": { - args: []string{executable, - "get-token", - "--oidc-issuer-url", "https://issuer.example.com", - "--oidc-client-id", "YOUR_CLIENT_ID", - "--grant-type", "authcode-keyboard", - "--oidc-auth-request-extra-params", "ttl=86400", - }, - in: credentialplugin.Input{ - TokenCacheDir: defaultTokenCacheDir, - Provider: oidc.Provider{ - IssuerURL: "https://issuer.example.com", - ClientID: "YOUR_CLIENT_ID", - }, - GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeKeyboardOption: &authcode.KeyboardOption{ - AuthRequestExtraParams: map[string]string{"ttl": "86400"}, - }, - }, - }, - }, - "GrantType=password": { - args: []string{executable, - "get-token", - "--oidc-issuer-url", "https://issuer.example.com", - "--oidc-client-id", "YOUR_CLIENT_ID", - "--grant-type", "password", - "--listen-address", "127.0.0.1:10080", - "--listen-address", "127.0.0.1:20080", - "--username", "USER", - "--password", "PASS", - }, - in: credentialplugin.Input{ - TokenCacheDir: defaultTokenCacheDir, - Provider: oidc.Provider{ - IssuerURL: "https://issuer.example.com", - ClientID: "YOUR_CLIENT_ID", - }, - GrantOptionSet: authentication.GrantOptionSet{ - ROPCOption: &ropc.Option{ - Username: "USER", - Password: "PASS", - }, - }, - }, - }, - "GrantType=auto": { - args: []string{executable, - "get-token", - "--oidc-issuer-url", "https://issuer.example.com", - "--oidc-client-id", "YOUR_CLIENT_ID", - "--listen-address", "127.0.0.1:10080", - "--listen-address", "127.0.0.1:20080", - "--username", "USER", - "--password", "PASS", - }, - in: credentialplugin.Input{ - TokenCacheDir: defaultTokenCacheDir, - Provider: oidc.Provider{ - IssuerURL: "https://issuer.example.com", - ClientID: "YOUR_CLIENT_ID", - }, - GrantOptionSet: authentication.GrantOptionSet{ - ROPCOption: &ropc.Option{ - Username: "USER", - Password: "PASS", + BindAddress: defaultListenAddress, + AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, + RedirectURLHostname: "localhost", }, }, },