|
1 | 1 | package linkedin_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
4 | 8 | "testing" |
| 9 | + "time" |
5 | 10 |
|
6 | 11 | "github.com/markbates/goth" |
7 | 12 | "github.com/markbates/goth/providers/linkedin" |
8 | 13 | "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/mock" |
| 15 | + "github.com/stretchr/testify/require" |
9 | 16 | ) |
10 | 17 |
|
| 18 | +type MockParams struct { |
| 19 | + params map[string]string |
| 20 | +} |
| 21 | + |
| 22 | +func (m *MockParams) Get(key string) string { |
| 23 | + return m.params[key] |
| 24 | +} |
| 25 | + |
| 26 | +type MockedHTTPClient struct { |
| 27 | + mock.Mock |
| 28 | +} |
| 29 | + |
| 30 | +func (m *MockedHTTPClient) RoundTrip(req *http.Request) (*http.Response, error) { |
| 31 | + args := m.Mock.Called(req) |
| 32 | + return args.Get(0).(*http.Response), args.Error(1) |
| 33 | +} |
| 34 | + |
11 | 35 | func Test_Implements_Session(t *testing.T) { |
12 | 36 | t.Parallel() |
13 | 37 | a := assert.New(t) |
@@ -46,3 +70,73 @@ func Test_String(t *testing.T) { |
46 | 70 |
|
47 | 71 | a.Equal(s.String(), s.Marshal()) |
48 | 72 | } |
| 73 | + |
| 74 | +func Test_Authorize(t *testing.T) { |
| 75 | + session := &linkedin.Session{} |
| 76 | + params := &MockParams{ |
| 77 | + params: map[string]string{ |
| 78 | + "code": "authorization_code", |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + t.Run("happy path", func(t *testing.T) { |
| 83 | + mockClient := new(MockedHTTPClient) |
| 84 | + p := linkedinProvider() |
| 85 | + p.HTTPClient = &http.Client{Transport: mockClient} |
| 86 | + mockClient.On("RoundTrip", mock.Anything).Return(&http.Response{ |
| 87 | + StatusCode: http.StatusOK, |
| 88 | + Body: io.NopCloser(strings.NewReader(`{"access_token":"test_token","expires_in":3600, "refresh_token":"refresh_token"}`)), |
| 89 | + }, nil) |
| 90 | + token, err := session.Authorize(p, params) |
| 91 | + require.NoError(t, err) |
| 92 | + assert.Equal(t, "test_token", token) |
| 93 | + assert.Equal(t, session.AccessToken, "test_token") |
| 94 | + assert.WithinDuration(t, session.ExpiresAt, time.Now().Add(3600*time.Second), 1*time.Second) |
| 95 | + assert.Equal(t, session.RefreshToken, "refresh_token") |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("error on request", func(t *testing.T) { |
| 99 | + mockClient := new(MockedHTTPClient) |
| 100 | + p := linkedinProvider() |
| 101 | + p.HTTPClient = &http.Client{Transport: mockClient} |
| 102 | + mockClient.On("RoundTrip", mock.Anything).Return(&http.Response{}, errors.New("request error")) |
| 103 | + _, err := session.Authorize(p, params) |
| 104 | + require.Error(t, err) |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("non-200 status code", func(t *testing.T) { |
| 108 | + mockClient := new(MockedHTTPClient) |
| 109 | + p := linkedinProvider() |
| 110 | + p.HTTPClient = &http.Client{Transport: mockClient} |
| 111 | + mockClient.On("RoundTrip", mock.Anything).Return(&http.Response{ |
| 112 | + StatusCode: http.StatusForbidden, |
| 113 | + Body: io.NopCloser(strings.NewReader(``)), |
| 114 | + }, nil) |
| 115 | + _, err := session.Authorize(p, params) |
| 116 | + require.Error(t, err) |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run("error on response decode", func(t *testing.T) { |
| 120 | + mockClient := new(MockedHTTPClient) |
| 121 | + p := linkedinProvider() |
| 122 | + p.HTTPClient = &http.Client{Transport: mockClient} |
| 123 | + mockClient.On("RoundTrip", mock.Anything).Return(&http.Response{ |
| 124 | + StatusCode: http.StatusOK, |
| 125 | + Body: io.NopCloser(strings.NewReader(`not a json`)), |
| 126 | + }, nil) |
| 127 | + _, err := session.Authorize(p, params) |
| 128 | + require.Error(t, err) |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("error code in response", func(t *testing.T) { |
| 132 | + mockClient := new(MockedHTTPClient) |
| 133 | + p := linkedinProvider() |
| 134 | + p.HTTPClient = &http.Client{Transport: mockClient} |
| 135 | + mockClient.On("RoundTrip", mock.Anything).Return(&http.Response{ |
| 136 | + StatusCode: http.StatusOK, |
| 137 | + Body: io.NopCloser(strings.NewReader(`{"code":1,"msg":"error message"}`)), |
| 138 | + }, nil) |
| 139 | + _, err := session.Authorize(p, params) |
| 140 | + require.Error(t, err) |
| 141 | + }) |
| 142 | +} |
0 commit comments