|
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright 2023 The Vouch Proxy Authors. |
| 4 | +Use of this source code is governed by The MIT License (MIT) that |
| 5 | +can be found in the LICENSE file. Software distributed under The |
| 6 | +MIT License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 7 | +OR CONDITIONS OF ANY KIND, either express or implied. |
| 8 | +
|
| 9 | +*/ |
| 10 | + |
| 11 | +package openid |
| 12 | + |
| 13 | +import ( |
| 14 | + "net/http" |
| 15 | + "testing" |
| 16 | + |
| 17 | + mockhttp "github.com/karupanerura/go-mock-http-response" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/vouch/vouch-proxy/pkg/cfg" |
| 20 | + "github.com/vouch/vouch-proxy/pkg/domains" |
| 21 | + "github.com/vouch/vouch-proxy/pkg/structs" |
| 22 | + "golang.org/x/oauth2" |
| 23 | +) |
| 24 | + |
| 25 | +type ReqMatcher func(*http.Request) bool |
| 26 | + |
| 27 | +type FunResponsePair struct { |
| 28 | + matcher ReqMatcher |
| 29 | + response *mockhttp.ResponseMock |
| 30 | +} |
| 31 | + |
| 32 | +type Transport struct { |
| 33 | + MockError error |
| 34 | +} |
| 35 | + |
| 36 | +func (c *Transport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 37 | + if c.MockError != nil { |
| 38 | + return nil, c.MockError |
| 39 | + } |
| 40 | + for _, p := range mockedResponses { |
| 41 | + if p.matcher(req) { |
| 42 | + requests = append(requests, req.URL.String()) |
| 43 | + return p.response.MakeResponse(req), nil |
| 44 | + } |
| 45 | + } |
| 46 | + return nil, nil |
| 47 | +} |
| 48 | + |
| 49 | +func mockResponse(fun ReqMatcher, statusCode int, headers map[string]string, body []byte) { |
| 50 | + mockedResponses = append(mockedResponses, FunResponsePair{matcher: fun, response: mockhttp.NewResponseMock(statusCode, headers, body)}) |
| 51 | +} |
| 52 | + |
| 53 | +func urlEquals(value string) ReqMatcher { |
| 54 | + return func(r *http.Request) bool { |
| 55 | + return r.URL.String() == value |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +var ( |
| 60 | + user *structs.User |
| 61 | + token = &oauth2.Token{AccessToken: "123"} |
| 62 | + mockedResponses = []FunResponsePair{} |
| 63 | + requests []string |
| 64 | + client = &http.Client{Transport: &Transport{}} |
| 65 | +) |
| 66 | + |
| 67 | +func setUp(t *testing.T) { |
| 68 | + log = cfg.Logging.Logger |
| 69 | + cfg.InitForTestPurposesWithProvider("openid") |
| 70 | + |
| 71 | + cfg.Cfg.AllowAllUsers = false |
| 72 | + cfg.Cfg.WhiteList = make([]string, 0) |
| 73 | + cfg.Cfg.TeamWhiteList = make([]string, 0) |
| 74 | + cfg.Cfg.Domains = []string{"domain1"} |
| 75 | + |
| 76 | + domains.Configure() |
| 77 | + |
| 78 | + mockedResponses = []FunResponsePair{} |
| 79 | + requests = make([]string, 0) |
| 80 | + |
| 81 | + user = &structs.User{Username: "testuser", Email: "test@example.com"} |
| 82 | + |
| 83 | + origPrepareTokensAndClient := prepareTokensAndClient |
| 84 | + t.Cleanup(func() { prepareTokensAndClient = origPrepareTokensAndClient }) |
| 85 | + prepareTokensAndClient = func(_ *http.Request, _ *structs.PTokens, _ bool, opts ...oauth2.AuthCodeOption) (*http.Client, *oauth2.Token, error) { |
| 86 | + return client, token, nil |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestGetUserInfo(t *testing.T) { |
| 91 | + setUp(t) |
| 92 | + |
| 93 | + cfg.GenOAuth.UserInfoURL = "https://some/api/for/info" |
| 94 | + userInfoContent := []byte(`{"id": "1234", "username": "myusername", "email": "my@email.com"}`) |
| 95 | + mockResponse(urlEquals(cfg.GenOAuth.UserInfoURL), http.StatusOK, map[string]string{}, userInfoContent) |
| 96 | + |
| 97 | + cfg.GenOAuth.UserTeamURL = "https://some/api/for/teams" |
| 98 | + userTeamContent := []byte(`[{"id": "1234567890", "name": "some room name"}, {"id": "xxx-not-relevant", "name": "some other room"}]`) |
| 99 | + mockResponse(urlEquals(cfg.GenOAuth.UserTeamURL), http.StatusOK, map[string]string{}, userTeamContent) |
| 100 | + |
| 101 | + cfg.Cfg.TeamWhiteList = append(cfg.Cfg.TeamWhiteList, "1234567890", "some-other-team") |
| 102 | + |
| 103 | + provider := Provider{} |
| 104 | + err := provider.GetUserInfo(nil, user, &structs.CustomClaims{}, &structs.PTokens{}) |
| 105 | + |
| 106 | + assert.Nil(t, err) |
| 107 | + assert.Equal(t, "myusername", user.Username) |
| 108 | + assert.Equal(t, []string{"1234567890"}, user.TeamMemberships) |
| 109 | +} |
0 commit comments