2022-01-25 09:57:53 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2022-01-25 09:57:53 +00:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2/constants"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRedirectToStart_Proxy(t *testing.T) {
|
|
|
|
a := newTestApplication()
|
2022-05-26 13:15:30 +00:00
|
|
|
a.proxyConfig.Mode = *api.NewNullableProxyMode(api.PROXYMODE_PROXY.Ptr())
|
2022-01-25 09:57:53 +00:00
|
|
|
a.proxyConfig.ExternalHost = "https://test.goauthentik.io"
|
|
|
|
req, _ := http.NewRequest("GET", "/foo/bar/baz", nil)
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
a.redirectToStart(rr, req)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
|
|
|
loc, _ := rr.Result().Location()
|
2022-02-18 09:32:22 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io/outpost.goauthentik.io/start?rd=https%3A%2F%2Ftest.goauthentik.io%2Ffoo%2Fbar%2Fbaz", loc.String())
|
2022-01-25 09:57:53 +00:00
|
|
|
|
2022-05-20 08:10:26 +00:00
|
|
|
s, _ := a.sessions.Get(req, constants.SessionName)
|
2022-01-25 09:57:53 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io/foo/bar/baz", s.Values[constants.SessionRedirect])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedirectToStart_Forward(t *testing.T) {
|
|
|
|
a := newTestApplication()
|
2022-05-26 13:15:30 +00:00
|
|
|
a.proxyConfig.Mode = *api.NewNullableProxyMode(api.PROXYMODE_FORWARD_SINGLE.Ptr())
|
2022-01-25 09:57:53 +00:00
|
|
|
a.proxyConfig.ExternalHost = "https://test.goauthentik.io"
|
|
|
|
req, _ := http.NewRequest("GET", "/foo/bar/baz", nil)
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
a.redirectToStart(rr, req)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
|
|
|
loc, _ := rr.Result().Location()
|
2022-02-18 09:32:22 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io/outpost.goauthentik.io/start?rd=https%3A%2F%2Ftest.goauthentik.io%2Ffoo%2Fbar%2Fbaz", loc.String())
|
2022-01-25 09:57:53 +00:00
|
|
|
|
2022-05-20 08:10:26 +00:00
|
|
|
s, _ := a.sessions.Get(req, constants.SessionName)
|
2022-01-25 09:57:53 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io/foo/bar/baz", s.Values[constants.SessionRedirect])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedirectToStart_Forward_Domain_Invalid(t *testing.T) {
|
|
|
|
a := newTestApplication()
|
|
|
|
a.proxyConfig.CookieDomain = api.PtrString("foo")
|
2022-05-26 13:15:30 +00:00
|
|
|
a.proxyConfig.Mode = *api.NewNullableProxyMode(api.PROXYMODE_FORWARD_DOMAIN.Ptr())
|
2022-01-25 09:57:53 +00:00
|
|
|
a.proxyConfig.ExternalHost = "https://test.goauthentik.io"
|
|
|
|
req, _ := http.NewRequest("GET", "/foo/bar/baz", nil)
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
a.redirectToStart(rr, req)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
|
|
|
loc, _ := rr.Result().Location()
|
2022-02-18 09:32:22 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io/outpost.goauthentik.io/start?rd=https%3A%2F%2Ftest.goauthentik.io", loc.String())
|
2022-01-25 09:57:53 +00:00
|
|
|
|
2022-05-20 08:10:26 +00:00
|
|
|
s, _ := a.sessions.Get(req, constants.SessionName)
|
2022-01-25 09:57:53 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io", s.Values[constants.SessionRedirect])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedirectToStart_Forward_Domain(t *testing.T) {
|
|
|
|
a := newTestApplication()
|
|
|
|
a.proxyConfig.CookieDomain = api.PtrString("goauthentik.io")
|
2022-05-26 13:15:30 +00:00
|
|
|
a.proxyConfig.Mode = *api.NewNullableProxyMode(api.PROXYMODE_FORWARD_DOMAIN.Ptr())
|
2022-01-25 09:57:53 +00:00
|
|
|
a.proxyConfig.ExternalHost = "https://test.goauthentik.io"
|
|
|
|
req, _ := http.NewRequest("GET", "/foo/bar/baz", nil)
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
a.redirectToStart(rr, req)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
|
|
|
loc, _ := rr.Result().Location()
|
2022-02-18 09:32:22 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io/outpost.goauthentik.io/start?rd=https%3A%2F%2Ftest.goauthentik.io", loc.String())
|
2022-01-25 09:57:53 +00:00
|
|
|
|
2022-05-20 08:10:26 +00:00
|
|
|
s, _ := a.sessions.Get(req, constants.SessionName)
|
2022-01-25 09:57:53 +00:00
|
|
|
assert.Equal(t, "https://test.goauthentik.io", s.Values[constants.SessionRedirect])
|
|
|
|
}
|