2023-09-06 22:20:09 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-09-15 13:06:31 +01:00
|
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
2023-09-06 22:20:09 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-09-15 13:06:31 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-09-06 22:20:09 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-09-15 13:06:31 +01:00
|
|
|
func TestUserAuth_NextFlowUrl(t *testing.T) {
|
2024-02-09 15:24:40 +00:00
|
|
|
u := UserAuth{NeedOtp: true}
|
2023-09-15 13:06:31 +01:00
|
|
|
assert.Equal(t, url.URL{Path: "/login/otp"}, *u.NextFlowUrl(&url.URL{}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/login/otp", RawQuery: url.Values{"redirect": {"/hello"}}.Encode()}, *u.NextFlowUrl(&url.URL{Path: "/hello"}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/login/otp", RawQuery: url.Values{"redirect": {"/hello?a=A"}}.Encode()}, *u.NextFlowUrl(&url.URL{Path: "/hello", RawQuery: url.Values{"a": {"A"}}.Encode()}))
|
2024-02-09 15:24:40 +00:00
|
|
|
u.NeedOtp = false
|
2023-09-15 13:06:31 +01:00
|
|
|
assert.Nil(t, u.NextFlowUrl(&url.URL{}))
|
|
|
|
}
|
|
|
|
|
2023-09-06 22:20:09 +01:00
|
|
|
func TestUserAuth_IsGuest(t *testing.T) {
|
|
|
|
var u UserAuth
|
|
|
|
assert.True(t, u.IsGuest())
|
2024-05-31 14:57:54 +01:00
|
|
|
u.Subject = uuid.NewString()
|
2023-09-15 13:06:31 +01:00
|
|
|
assert.False(t, u.IsGuest())
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeSessionStore struct {
|
|
|
|
m map[string]any
|
|
|
|
saveFunc func(map[string]any) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeSessionStore) Context() context.Context { return context.Background() }
|
|
|
|
func (f *fakeSessionStore) SessionID() string { return "fakeSessionStore" }
|
|
|
|
func (f *fakeSessionStore) Set(key string, value interface{}) { f.m[key] = value }
|
|
|
|
|
|
|
|
func (f *fakeSessionStore) Get(key string) (a interface{}, ok bool) {
|
|
|
|
if a, ok = f.m[key]; false {
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequireAuthentication(t *testing.T) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOptionalAuthentication(t *testing.T) {
|
2024-02-09 15:24:40 +00:00
|
|
|
h := &HttpServer{}
|
2023-09-15 13:06:31 +01:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "https://example.com/hello", nil)
|
|
|
|
assert.NoError(t, err)
|
2024-05-31 14:57:54 +01:00
|
|
|
auth, err := h.internalAuthenticationHandler(nil, req)
|
2023-09-15 13:06:31 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, auth.IsGuest())
|
2024-05-31 14:57:54 +01:00
|
|
|
auth.Subject = "567"
|
2023-09-15 13:06:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareRedirectUrl(t *testing.T) {
|
|
|
|
assert.Equal(t, url.URL{Path: "/hello"}, *PrepareRedirectUrl("/hello", &url.URL{}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/world"}, *PrepareRedirectUrl("/world", &url.URL{}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/a", RawQuery: url.Values{"redirect": {"/hello"}}.Encode()}, *PrepareRedirectUrl("/a", &url.URL{Path: "/hello"}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/a", RawQuery: url.Values{"redirect": {"/hello?a=A"}}.Encode()}, *PrepareRedirectUrl("/a", &url.URL{Path: "/hello", RawQuery: url.Values{"a": {"A"}}.Encode()}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/a", RawQuery: url.Values{"redirect": {"/hello?a=A&b=B"}}.Encode()}, *PrepareRedirectUrl("/a", &url.URL{Path: "/hello", RawQuery: url.Values{"a": {"A"}, "b": {"B"}}.Encode()}))
|
2023-09-24 18:24:16 +01:00
|
|
|
|
|
|
|
assert.Equal(t, url.URL{Path: "/hello", RawQuery: "z=y"}, *PrepareRedirectUrl("/hello?z=y", &url.URL{}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/world", RawQuery: "z=y"}, *PrepareRedirectUrl("/world?z=y", &url.URL{}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/a", RawQuery: url.Values{"z": {"y"}, "redirect": {"/hello"}}.Encode()}, *PrepareRedirectUrl("/a?z=y", &url.URL{Path: "/hello"}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/a", RawQuery: url.Values{"z": {"y"}, "redirect": {"/hello?a=A"}}.Encode()}, *PrepareRedirectUrl("/a?z=y", &url.URL{Path: "/hello", RawQuery: url.Values{"a": {"A"}}.Encode()}))
|
|
|
|
assert.Equal(t, url.URL{Path: "/a", RawQuery: url.Values{"z": {"y"}, "redirect": {"/hello?a=A&b=B"}}.Encode()}, *PrepareRedirectUrl("/a?z=y", &url.URL{Path: "/hello", RawQuery: url.Values{"a": {"A"}, "b": {"B"}}.Encode()}))
|
2023-09-06 22:20:09 +01:00
|
|
|
}
|