github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/app/authentication_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "net/http" 8 "net/http/httptest" 9 "strconv" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 14 "github.com/mattermost/mattermost-server/v5/model" 15 ) 16 17 func TestParseAuthTokenFromRequest(t *testing.T) { 18 cases := []struct { 19 header string 20 cookie string 21 query string 22 expectedToken string 23 expectedLocation TokenLocation 24 }{ 25 {"", "", "", "", TokenLocationNotFound}, 26 {"token mytoken", "", "", "mytoken", TokenLocationHeader}, 27 {"BEARER mytoken", "", "", "mytoken", TokenLocationHeader}, 28 {"", "mytoken", "", "mytoken", TokenLocationCookie}, 29 {"", "", "mytoken", "mytoken", TokenLocationQueryString}, 30 {"mytoken", "", "", "mytoken", TokenLocationCloudHeader}, 31 } 32 33 for testnum, tc := range cases { 34 pathname := "/test/here" 35 if tc.query != "" { 36 pathname += "?access_token=" + tc.query 37 } 38 req := httptest.NewRequest("GET", pathname, nil) 39 switch tc.expectedLocation { 40 case TokenLocationHeader: 41 req.Header.Add(model.HEADER_AUTH, tc.header) 42 case TokenLocationCloudHeader: 43 req.Header.Add(model.HEADER_CLOUD_TOKEN, tc.header) 44 case TokenLocationCookie: 45 req.AddCookie(&http.Cookie{ 46 Name: model.SESSION_COOKIE_TOKEN, 47 Value: tc.cookie, 48 }) 49 } 50 51 token, location := ParseAuthTokenFromRequest(req) 52 53 require.Equal(t, tc.expectedToken, token, "Wrong token on test "+strconv.Itoa(testnum)) 54 require.Equal(t, tc.expectedLocation, location, "Wrong location on test "+strconv.Itoa(testnum)) 55 } 56 }