Skip to content

Commit

Permalink
update test to work with simplesamlphp 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jackHay22 committed Nov 15, 2023
1 parent 6120aed commit 59fde56
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions tests/integration/saml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package integration

import (
"fmt"
"html"
"io"
"net/http"
"net/http/cookiejar"
Expand Down Expand Up @@ -77,39 +76,37 @@ func TestSAMLRegistration(t *testing.T) {
req, err = http.NewRequest("GET", test.RedirectURL(resp), nil)
assert.NoError(t, err)

var formRedirectURL *url.URL
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
// capture the redirected destination to use in POST request
formRedirectURL = req.URL
return nil
}

res, err := client.Do(req)
client.CheckRedirect = nil
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)

// find the auth state hidden input
authStateMatcher := regexp.MustCompile(`<input.*?name="AuthState".*?value="([^"]+)".*?>`)
body, err := io.ReadAll(res.Body)
assert.NoError(t, err)

matches := authStateMatcher.FindStringSubmatch(string(body))
assert.Len(t, matches, 2)
assert.NoError(t, res.Body.Close())
assert.NotNil(t, formRedirectURL)

form := url.Values{
"username": {"user1"},
"password": {"user1pass"},
"AuthState": {html.UnescapeString(matches[1])},
"username": {"user1"},
"password": {"user1pass"},
}

req, err = http.NewRequest("POST", fmt.Sprintf("http://%s/simplesaml/module.php/core/loginuserpass.php", samlURL), strings.NewReader(form.Encode()))
req, err = http.NewRequest("POST", formRedirectURL.String(), strings.NewReader(form.Encode()))
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

res, err = client.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)

samlResMatcher := regexp.MustCompile(`<input.*?value="([^"]+)".*?>`)

body, err = io.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
assert.NoError(t, err)

matches = samlResMatcher.FindStringSubmatch(string(body))
samlResMatcher := regexp.MustCompile(`<input.*?name="SAMLResponse".*?value="([^"]+)".*?>`)
matches := samlResMatcher.FindStringSubmatch(string(body))
assert.Len(t, matches, 2)
assert.NoError(t, res.Body.Close())

Expand Down

0 comments on commit 59fde56

Please sign in to comment.