2020-10-28 15:35:41 +02:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseAuthTokenFromRequest(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
header string
|
|
|
|
cookie string
|
|
|
|
query string
|
|
|
|
expectedToken string
|
|
|
|
expectedLocation TokenLocation
|
|
|
|
}{
|
|
|
|
{"", "", "", "", TokenLocationNotFound},
|
|
|
|
{"token mytoken", "", "", "mytoken", TokenLocationHeader},
|
|
|
|
{"BEARER mytoken", "", "", "mytoken", TokenLocationHeader},
|
|
|
|
{"", "mytoken", "", "mytoken", TokenLocationCookie},
|
|
|
|
{"", "", "mytoken", "mytoken", TokenLocationQueryString},
|
|
|
|
}
|
|
|
|
|
|
|
|
for testnum, tc := range cases {
|
|
|
|
pathname := "/test/here"
|
|
|
|
if tc.query != "" {
|
|
|
|
pathname += "?access_token=" + tc.query
|
|
|
|
}
|
|
|
|
req := httptest.NewRequest("GET", pathname, nil)
|
|
|
|
if tc.header != "" {
|
2021-06-21 11:21:42 +02:00
|
|
|
req.Header.Add(HeaderAuth, tc.header)
|
2020-10-28 15:35:41 +02:00
|
|
|
}
|
|
|
|
if tc.cookie != "" {
|
|
|
|
req.AddCookie(&http.Cookie{
|
2021-05-24 19:06:11 +02:00
|
|
|
Name: "FOCALBOARDAUTHTOKEN",
|
2020-10-28 15:35:41 +02:00
|
|
|
Value: tc.cookie,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
token, location := ParseAuthTokenFromRequest(req)
|
|
|
|
|
|
|
|
require.Equal(t, tc.expectedToken, token, "Wrong token on test "+strconv.Itoa(testnum))
|
|
|
|
require.Equal(t, tc.expectedLocation, location, "Wrong location on test "+strconv.Itoa(testnum))
|
|
|
|
}
|
|
|
|
}
|