Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Keycloak
^^^^^^^^^^^^

.. note::
This guide has been written for version 24.0.3
This guide has been written for version 26.5.5

.. warning::
In a previous version of this guide, the client mapping was for the predefined mapper "Group memberships", which in some cases always returned the value "admin". Please make sure that you are using a custom mapper, as described in :ref:`oidcconfig_keycloak_opt`
Expand Down Expand Up @@ -233,7 +233,7 @@ Gokapi Configuration
+---------------------------+-----------------------------------------------------------------------+--------------------------------------------+
| Client Secret | Client secret provided | AhXeV7_EXAMPLE_KEY |
+---------------------------+-----------------------------------------------------------------------+--------------------------------------------+
| Recheck identity | If open ``Consent required`` is disabled, use a low interval | 12 hours |
| Recheck identity | If ``Consent required`` is disabled, use a low interval | 12 hours |
+---------------------------+-----------------------------------------------------------------------+--------------------------------------------+
| Admin email address | The email address for the super-admin | gokapi@example.com |
+---------------------------+-----------------------------------------------------------------------+--------------------------------------------+
Expand Down
70 changes: 56 additions & 14 deletions internal/test/TestHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func ResponseBodyContains(t MockT, got *httptest.ResponseRecorder, want string)
}
}

// ResponseIsRedirect fails test if not correct redirect
func ResponseIsRedirect(t MockT, got *httptest.ResponseRecorder, wantUrl string, ignoreParam bool) {
t.Helper()
IsEqualInt(t, got.Code, http.StatusTemporaryRedirect)
location := got.Header().Get("Location")
if ignoreParam {
location = strings.Split(location, "?")[0]
}
if !strings.HasSuffix(location, wantUrl) {
t.Errorf("Redirect Location mismatch: got %s, want to end with %s", location, wantUrl)
}
}

// ResponseBodyIs fails test if http response is not the exact string
func ResponseBodyIs(t MockT, got *httptest.ResponseRecorder, want string) {
t.Helper()
Expand Down Expand Up @@ -256,6 +269,12 @@ func HttpPageResult(t MockT, config HttpTestConfig) []*http.Cookie {
config.init(t)
client := &http.Client{}

if config.RedirectUrl != "" {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}

data := url.Values{}
for _, value := range config.PostValues {
data.Add(value.Key, value.Value)
Expand Down Expand Up @@ -309,7 +328,10 @@ func HttpPageResultJson(t MockT, config HttpTestConfig) []*http.Cookie {

func checkResponse(t MockT, response *http.Response, config HttpTestConfig) {
t.Helper()
IsEqualBool(t, response != nil, true)
if response == nil {
t.Errorf("No response received")
return
}
if response.StatusCode != config.ResultCode {
t.Errorf("Status Code - Got: %d Want: %d", response.StatusCode, config.ResultCode)
}
Expand All @@ -319,6 +341,15 @@ func checkResponse(t MockT, response *http.Response, config HttpTestConfig) {
if config.IsHtml && !bytes.Contains(content, []byte("</html>")) {
t.Errorf(config.Url + ": Incorrect response, no HTML tag")
}
if config.RedirectUrl != "" {
location := response.Header.Get("Location")
if config.IgnoreRedirectParm {
location = strings.Split(location, "?")[0]
}
if !strings.HasSuffix(location, config.RedirectUrl) {
t.Errorf("Redirect Location mismatch: got %s, want to end with %s", location, config.RedirectUrl)
}
}
for _, requiredString := range config.RequiredContent {
if !bytes.Contains(content, []byte(requiredString)) {
t.Errorf(config.Url + ": Incorrect response. Got:\n" + string(content))
Expand All @@ -333,18 +364,20 @@ func checkResponse(t MockT, response *http.Response, config HttpTestConfig) {

// HttpTestConfig is a struct for http test init
type HttpTestConfig struct {
Url string
RequiredContent []string
ExcludedContent []string
IsHtml bool
Method string
PostValues []PostBody
Cookies []Cookie
Headers []Header
UploadFileName string
UploadFieldName string
ResultCode int
Body io.Reader
Url string
RequiredContent []string
ExcludedContent []string
IsHtml bool
IgnoreRedirectParm bool
Method string
PostValues []PostBody
Cookies []Cookie
Headers []Header
UploadFileName string
UploadFieldName string
RedirectUrl string
ResultCode int
Body io.Reader
}

func (c *HttpTestConfig) init(t MockT) {
Expand All @@ -356,7 +389,11 @@ func (c *HttpTestConfig) init(t MockT) {
c.Method = "GET"
}
if c.ResultCode == 0 {
c.ResultCode = 200
if c.RedirectUrl == "" {
c.ResultCode = 200
} else {
c.ResultCode = 307
}
}
}

Expand Down Expand Up @@ -444,6 +481,11 @@ func HttpPostRequest(t MockT, config HttpTestConfig) []*http.Cookie {
r.Header.Set(header.Name, header.Value)
}
client := &http.Client{}
if config.RedirectUrl != "" {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
response, err := client.Do(r)
IsNil(t, err)
defer response.Body.Close()
Expand Down
Loading
Loading