Skip to content

Commit

Permalink
Refactor test (#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Dec 24, 2022
1 parent cda2ecc commit 345465a
Showing 1 changed file with 130 additions and 77 deletions.
207 changes: 130 additions & 77 deletions pkg/usecases/authentication/devicecode/devicecode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 345465a

Please sign in to comment.