From 345465a5d385431821ab34fe3a689729e04350d0 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sat, 24 Dec 2022 17:00:59 +0900 Subject: [PATCH] Refactor test (#847) --- .../devicecode/devicecode_test.go | 207 +++++++++++------- 1 file changed, 130 insertions(+), 77 deletions(-) diff --git a/pkg/usecases/authentication/devicecode/devicecode_test.go b/pkg/usecases/authentication/devicecode/devicecode_test.go index bc621743..949d8821 100644 --- a/pkg/usecases/authentication/devicecode/devicecode_test.go +++ b/pkg/usecases/authentication/devicecode/devicecode_test.go @@ -14,92 +14,145 @@ import ( ) func TestDeviceCode(t *testing.T) { - mockBrowser := browser.NewMockInterface(t) - logger := logger.New(t) - mockClient := client.NewMockInterface(t) + ctx := context.TODO() - dc := &DeviceCode{ - Browser: mockBrowser, - Logger: logger, - } + t.Run("Authorization error", func(t *testing.T) { + mockClient := client.NewMockInterface(t) + dc := &DeviceCode{ + Browser: browser.NewMockInterface(t), + Logger: logger.New(t), + } + errTest := errors.New("test error") + mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(nil, errTest).Once() + _, err := dc.Do(ctx, &Option{}, mockClient) + if !errors.Is(err, errTest) { + t.Errorf("returned error is not the test error: %v", err) + } + }) - ctx := context.Background() - errTest := errors.New("test error") + t.Run("Server returns verification_uri_complete", func(t *testing.T) { + mockBrowser := browser.NewMockInterface(t) + mockClient := client.NewMockInterface(t) + dc := &DeviceCode{ + Browser: mockBrowser, + Logger: logger.New(t), + } + mockResponse := &oauth2dev.AuthorizationResponse{ + DeviceCode: "device-code-1", + VerificationURIComplete: "https://example.com/verificationComplete?code=code123", + ExpiresIn: 2, + Interval: 1, + } + mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(&oauth2dev.AuthorizationResponse{ + Interval: 1, + ExpiresIn: 2, + VerificationURIComplete: "https://example.com/verificationComplete?code=code123", + DeviceCode: "device-code-1", + }, nil).Once() + mockBrowser.EXPECT().Open("https://example.com/verificationComplete?code=code123").Return(nil).Once() + mockClient.EXPECT().ExchangeDeviceCode(mock.Anything, mockResponse).Return(&oidc.TokenSet{ + IDToken: "test-id-token", + }, nil).Once() + ts, err := dc.Do(ctx, &Option{}, mockClient) + if err != nil { + t.Errorf("returned unexpected error: %v", err) + } + if ts.IDToken != "test-id-token" { + t.Errorf("wrong returned tokenset: %v", err) + } + }) - mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(nil, errTest).Once() - _, err := dc.Do(ctx, &Option{}, mockClient) - if !errors.Is(err, errTest) { - t.Errorf("returned error is not the test error: %v", err) - } + t.Run("Server returns verification_uri", func(t *testing.T) { + mockBrowser := browser.NewMockInterface(t) + mockClient := client.NewMockInterface(t) + dc := &DeviceCode{ + Browser: mockBrowser, + Logger: logger.New(t), + } + mockResponseWithoutComplete := &oauth2dev.AuthorizationResponse{ + DeviceCode: "device-code-1", + VerificationURI: "https://example.com/verificationComplete", + ExpiresIn: 2, + Interval: 1, + } + mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(&oauth2dev.AuthorizationResponse{ + Interval: 1, + ExpiresIn: 2, + VerificationURI: "https://example.com/verificationComplete", + DeviceCode: "device-code-1", + }, nil).Once() + mockBrowser.EXPECT().Open("https://example.com/verificationComplete").Return(nil).Once() + mockClient.EXPECT().ExchangeDeviceCode(mock.Anything, mockResponseWithoutComplete).Return(&oidc.TokenSet{ + IDToken: "test-id-token", + }, nil).Once() + ts, err := dc.Do(ctx, &Option{}, mockClient) + if err != nil { + t.Errorf("returned unexpected error: %v", err) + } + if ts.IDToken != "test-id-token" { + t.Errorf("wrong returned tokenset: %v", err) + } + }) - mockResponse := &oauth2dev.AuthorizationResponse{DeviceCode: "device-code-1", UserCode: "", VerificationURI: "", VerificationURIComplete: "https://example.com/verificationComplete?code=code123", VerificationURL: "", ExpiresIn: 2, Interval: 1} - mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(&oauth2dev.AuthorizationResponse{ - Interval: 1, - ExpiresIn: 2, - VerificationURIComplete: "https://example.com/verificationComplete?code=code123", - DeviceCode: "device-code-1", - }, nil).Once() - mockBrowser.EXPECT().Open("https://example.com/verificationComplete?code=code123").Return(nil).Once() - mockClient.EXPECT().ExchangeDeviceCode(mock.Anything, mockResponse).Return(&oidc.TokenSet{ - IDToken: "test-id-token", - }, nil).Once() - ts, err := dc.Do(ctx, &Option{}, mockClient) - if err != nil { - t.Errorf("returned unexpected error: %v", err) - } - if ts.IDToken != "test-id-token" { - t.Errorf("wrong returned tokenset: %v", err) - } - - mockResponseWithoutComplete := &oauth2dev.AuthorizationResponse{DeviceCode: "device-code-1", UserCode: "", VerificationURI: "https://example.com/verificationComplete", VerificationURIComplete: "", VerificationURL: "", ExpiresIn: 2, Interval: 1} - mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(&oauth2dev.AuthorizationResponse{ - Interval: 1, - ExpiresIn: 2, - VerificationURI: "https://example.com/verificationComplete", - DeviceCode: "device-code-1", - }, nil).Once() - mockBrowser.EXPECT().Open("https://example.com/verificationComplete").Return(nil).Once() - mockClient.EXPECT().ExchangeDeviceCode(mock.Anything, mockResponseWithoutComplete).Return(&oidc.TokenSet{ - IDToken: "test-id-token", - }, nil).Once() - ts, err = dc.Do(ctx, &Option{}, mockClient) - if err != nil { - t.Errorf("returned unexpected error: %v", err) - } - if ts.IDToken != "test-id-token" { - t.Errorf("wrong returned tokenset: %v", err) - } - - mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(&oauth2dev.AuthorizationResponse{ - Interval: 1, - ExpiresIn: 2, - VerificationURIComplete: "https://example.com/verificationComplete?code=code123", - DeviceCode: "device-code-1", - }, nil).Once() - mockBrowser.EXPECT().Open("https://example.com/verificationComplete?code=code123").Return(nil).Once() - mockClient.EXPECT().ExchangeDeviceCode(mock.Anything, mockResponse).Return(nil, errTest).Once() - _, err = dc.Do(ctx, &Option{}, mockClient) - if err == nil { - t.Errorf("did not return error: %v", err) - } + t.Run("Error when exchanging the device code", func(t *testing.T) { + mockBrowser := browser.NewMockInterface(t) + mockClient := client.NewMockInterface(t) + dc := &DeviceCode{ + Browser: mockBrowser, + Logger: logger.New(t), + } + mockResponse := &oauth2dev.AuthorizationResponse{ + DeviceCode: "device-code-1", + VerificationURIComplete: "https://example.com/verificationComplete?code=code123", + ExpiresIn: 2, + Interval: 1, + } + mockClient.EXPECT().GetDeviceAuthorization(ctx).Return(&oauth2dev.AuthorizationResponse{ + Interval: 1, + ExpiresIn: 2, + VerificationURIComplete: "https://example.com/verificationComplete?code=code123", + DeviceCode: "device-code-1", + }, nil).Once() + mockBrowser.EXPECT().Open("https://example.com/verificationComplete?code=code123").Return(nil).Once() + mockClient.EXPECT().ExchangeDeviceCode(mock.Anything, mockResponse).Return(nil, errors.New("test error")).Once() + _, err := dc.Do(ctx, &Option{}, mockClient) + if err == nil { + t.Errorf("did not return error: %v", err) + } + }) } -func TestOpenUrl(t *testing.T) { - ctx := context.Background() - browserMock := browser.NewMockInterface(t) - deviceCode := &DeviceCode{ - Browser: browserMock, - Logger: logger.New(t), - } - +func TestDeviceCode_openURL(t *testing.T) { + ctx := context.TODO() const url = "https://example.com" var testError = errors.New("test error") - browserMock.EXPECT().Open(url).Return(testError).Once() - deviceCode.openURL(ctx, nil, url) + t.Run("Continue if error opening the browser", func(t *testing.T) { + browserMock := browser.NewMockInterface(t) + deviceCode := &DeviceCode{ + Browser: browserMock, + Logger: logger.New(t), + } + browserMock.EXPECT().Open(url).Return(testError).Once() + deviceCode.openURL(ctx, nil, url) + }) - deviceCode.openURL(ctx, &Option{SkipOpenBrowser: true}, url) + t.Run("SkipOpenBrowser is set", func(t *testing.T) { + browserMock := browser.NewMockInterface(t) + deviceCode := &DeviceCode{ + Browser: browserMock, + Logger: logger.New(t), + } + deviceCode.openURL(ctx, &Option{SkipOpenBrowser: true}, url) + }) - browserMock.EXPECT().OpenCommand(ctx, url, "test-command").Return(testError).Once() - deviceCode.openURL(ctx, &Option{BrowserCommand: "test-command"}, url) + t.Run("BrowserCommand is set", func(t *testing.T) { + browserMock := browser.NewMockInterface(t) + deviceCode := &DeviceCode{ + Browser: browserMock, + Logger: logger.New(t), + } + browserMock.EXPECT().OpenCommand(ctx, url, "test-command").Return(testError).Once() + deviceCode.openURL(ctx, &Option{BrowserCommand: "test-command"}, url) + }) }