|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package token |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "io" |
| 23 | + "net/http" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 28 | + "github.com/apache/incubator-devlake/impls/logruslog" |
| 29 | + "github.com/apache/incubator-devlake/plugins/github/models" |
| 30 | + "github.com/sirupsen/logrus" |
| 31 | + "github.com/stretchr/testify/assert" |
| 32 | + "github.com/stretchr/testify/mock" |
| 33 | +) |
| 34 | + |
| 35 | +func TestRoundTripper401Refresh(t *testing.T) { |
| 36 | + mockRT := new(MockRoundTripper) |
| 37 | + client := &http.Client{Transport: mockRT} |
| 38 | + |
| 39 | + conn := &models.GithubConnection{ |
| 40 | + GithubConn: models.GithubConn{ |
| 41 | + RefreshToken: "refresh_token", |
| 42 | + GithubAccessToken: models.GithubAccessToken{ |
| 43 | + AccessToken: api.AccessToken{ |
| 44 | + Token: "old_token", |
| 45 | + }, |
| 46 | + }, |
| 47 | + TokenExpiresAt: time.Now().Add(10 * time.Minute), // Not expired |
| 48 | + GithubAppKey: models.GithubAppKey{ |
| 49 | + AppKey: api.AppKey{ |
| 50 | + AppId: "123", |
| 51 | + SecretKey: "secret", |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + logger, _ := logruslog.NewDefaultLogger(logrus.New()) |
| 58 | + tp := NewTokenProvider(conn, nil, client, logger) |
| 59 | + rt := NewRefreshRoundTripper(mockRT, tp) |
| 60 | + |
| 61 | + // Request |
| 62 | + req, _ := http.NewRequest("GET", "https://api.github.com/user", nil) |
| 63 | + |
| 64 | + // 1. First call returns 401 |
| 65 | + resp401 := &http.Response{ |
| 66 | + StatusCode: 401, |
| 67 | + Body: io.NopCloser(bytes.NewBufferString("Unauthorized")), |
| 68 | + } |
| 69 | + mockRT.On("RoundTrip", mock.MatchedBy(func(r *http.Request) bool { |
| 70 | + return r.Header.Get("Authorization") == "Bearer old_token" && r.URL.String() == "https://api.github.com/user" |
| 71 | + })).Return(resp401, nil).Once() |
| 72 | + |
| 73 | + // 2. Refresh call (triggered by 401) |
| 74 | + respRefresh := &http.Response{ |
| 75 | + StatusCode: 200, |
| 76 | + Body: io.NopCloser(bytes.NewBufferString(`{"access_token":"new_token","refresh_token":"new_refresh_token","expires_in":3600,"refresh_token_expires_in":3600}`)), |
| 77 | + } |
| 78 | + // The refresh call uses the same client, so it goes through mockRT too! |
| 79 | + mockRT.On("RoundTrip", mock.MatchedBy(func(r *http.Request) bool { |
| 80 | + return r.URL.String() == "https://github.com/login/oauth/access_token" |
| 81 | + })).Return(respRefresh, nil).Once() |
| 82 | + |
| 83 | + // 3. Retry call with new token |
| 84 | + resp200 := &http.Response{ |
| 85 | + StatusCode: 200, |
| 86 | + Body: io.NopCloser(bytes.NewBufferString("Success")), |
| 87 | + } |
| 88 | + mockRT.On("RoundTrip", mock.MatchedBy(func(r *http.Request) bool { |
| 89 | + return r.Header.Get("Authorization") == "Bearer new_token" && r.URL.String() == "https://api.github.com/user" |
| 90 | + })).Return(resp200, nil).Once() |
| 91 | + |
| 92 | + // Execute |
| 93 | + resp, err := rt.RoundTrip(req) |
| 94 | + assert.NoError(t, err) |
| 95 | + assert.Equal(t, 200, resp.StatusCode) |
| 96 | + |
| 97 | + body, _ := io.ReadAll(resp.Body) |
| 98 | + assert.Equal(t, "Success", string(body)) |
| 99 | + |
| 100 | + mockRT.AssertExpectations(t) |
| 101 | +} |
0 commit comments