Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 5m timeout to page.WaitForRequest #1153

Merged
merged 2 commits into from
Nov 1, 2023
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
18 changes: 15 additions & 3 deletions pkg/provider/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ import (

var logger = logrus.WithField("provider", "browser")

const DEFAULT_TIMEOUT float64 = 300000

// Client client for browser based Identity Provider
type Client struct {
Headless bool
// Setup alternative directory to download playwright browsers to
BrowserDriverDir string
Timeout int
}

// New create new browser based client
func New(idpAccount *cfg.IDPAccount) (*Client, error) {
return &Client{
Headless: idpAccount.Headless,
BrowserDriverDir: idpAccount.BrowserDriverDir,
Timeout: idpAccount.Timeout,
}, nil
}

Expand Down Expand Up @@ -75,10 +79,10 @@ func (cl *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error)
}
}()

return getSAMLResponse(page, loginDetails)
return getSAMLResponse(page, loginDetails, cl)
}

var getSAMLResponse = func(page playwright.Page, loginDetails *creds.LoginDetails) (string, error) {
var getSAMLResponse = func(page playwright.Page, loginDetails *creds.LoginDetails, client *Client) (string, error) {
logger.WithField("URL", loginDetails.URL).Info("opening browser")

if _, err := page.Goto(loginDetails.URL); err != nil {
Expand All @@ -94,7 +98,7 @@ var getSAMLResponse = func(page playwright.Page, loginDetails *creds.LoginDetail
}

logger.Info("waiting ...")
r, _ := page.WaitForRequest(signin_re)
r, _ := page.WaitForRequest(signin_re, client.waitForRequestTimeout())
data, err := r.PostData()
if err != nil {
return "", err
Expand Down Expand Up @@ -123,3 +127,11 @@ func (cl *Client) Validate(loginDetails *creds.LoginDetails) error {

return nil
}

func (cl *Client) waitForRequestTimeout() playwright.PageWaitForRequestOptions {
timeout := float64(cl.Timeout)
if timeout < 30000 {
timeout = DEFAULT_TIMEOUT
}
return playwright.PageWaitForRequestOptions{Timeout: &timeout}
}
48 changes: 45 additions & 3 deletions pkg/provider/browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestNoBrowserDriverFail(t *testing.T) {
assert.ErrorContains(t, err, "could not start driver")
}

func fakeSAMLResponse(page playwright.Page, loginDetails *creds.LoginDetails) (string, error) {
func fakeSAMLResponse(page playwright.Page, loginDetails *creds.LoginDetails, client *Client) (string, error) {
return response, nil
}

Expand Down Expand Up @@ -96,6 +96,14 @@ func TestGetSAMLResponse(t *testing.T) {
</saml:EncryptedAssertion>
</samlp:Response>
`

idpAccount := cfg.IDPAccount{
Headless: true,
Timeout: 100000,
}

client, err := New(&idpAccount)
assert.Nil(t, err)
params := url.Values{}
params.Add("foo1", "bar1")
params.Add("SAMLResponse", samlp)
Expand All @@ -107,12 +115,46 @@ func TestGetSAMLResponse(t *testing.T) {
regex, err := signinRegex()
assert.Nil(t, err)
page.Mock.On("Goto", url).Return(resp, nil)
page.Mock.On("WaitForRequest", regex).Return(req)
page.Mock.On("WaitForRequest", regex, client.waitForRequestTimeout()).Return(req)
req.Mock.On("PostData").Return(params.Encode(), nil)
// loginDetails := &creds.LoginDetails{
// URL: url,
//}
// samlResp, err := getSAMLResponse(page, loginDetails)
// samlResp, err := getSAMLResponse(page, loginDetails, client)
// assert.Nil(t, err)
// assert.Equal(t, samlp, samlResp)
}

func TestWaitForRequestOptions(t *testing.T) {
timeout := float64(100000)
idpAccount := cfg.IDPAccount{
Headless: true,
Timeout: int(timeout),
}

client, err := New(&idpAccount)
assert.Nil(t, err)

options := client.waitForRequestTimeout()
if *options.Timeout != timeout {
t.Errorf("Unexpected value for timeout [%.0f]: expected [%.0f]", *options.Timeout, timeout)
}
}

func TestWaitForRequestOptionsDefaultTimeout(t *testing.T) {
idpAccount := cfg.IDPAccount{
Headless: true,
Timeout: 1000,
}

client, err := New(&idpAccount)

if err != nil {
t.Errorf("Unable to create browser")
}

options := client.waitForRequestTimeout()
if *options.Timeout != DEFAULT_TIMEOUT {
t.Errorf("Unexpected value for timeout [%.0f]: expected [%.0f]", *options.Timeout, DEFAULT_TIMEOUT)
}
}
Loading