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

Desobekify BrowserContext.grantPermissions options parsing #1511

Merged
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
13 changes: 7 additions & 6 deletions browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
return bc.Cookies(urls...) //nolint:wrapcheck
})
},
"grantPermissions": func(permissions []string, opts sobek.Value) *sobek.Promise {
"grantPermissions": func(permissions []string, opts sobek.Value) (*sobek.Promise, error) {
popts, err := exportTo[common.GrantPermissionsOptions](vu.Runtime(), opts)
if err != nil {
return nil, fmt.Errorf("parsing grant permission options: %w", err)
}
return k6ext.Promise(vu.Context(), func() (any, error) {
popts := common.NewGrantPermissionsOptions()
popts.Parse(vu.Context(), opts)

return nil, bc.GrantPermissions(permissions, popts) //nolint:wrapcheck
})
return nil, bc.GrantPermissions(permissions, popts)
}), nil
},
"setDefaultNavigationTimeout": bc.SetDefaultNavigationTimeout,
"setDefaultTimeout": bc.SetDefaultTimeout,
Expand Down
12 changes: 12 additions & 0 deletions browser/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ func panicIfFatalError(ctx context.Context, err error) {
}
}

// exportTo exports the Sobek value to a Go value.
// It returns the zero value of T if obj does not exist in the Sobek runtime.
// It's caller's responsibility to check for nilness.
func exportTo[T any](rt *sobek.Runtime, obj sobek.Value) (T, error) {
var t T
if !sobekValueExists(obj) {
return t, nil
}
err := rt.ExportTo(obj, &t)
return t, err //nolint:wrapcheck
}

// exportArg exports the value and returns it.
// It returns nil if the value is undefined or null.
func exportArg(gv sobek.Value) any {
Expand Down
9 changes: 5 additions & 4 deletions browser/sync_browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ func syncMapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //n
"close": bc.Close,
"cookies": bc.Cookies,
"grantPermissions": func(permissions []string, opts sobek.Value) error {
pOpts := common.NewGrantPermissionsOptions()
pOpts.Parse(vu.Context(), opts)

return bc.GrantPermissions(permissions, pOpts) //nolint:wrapcheck
popts, err := exportTo[common.GrantPermissionsOptions](vu.Runtime(), opts)
if err != nil {
return fmt.Errorf("parsing grant permission options: %w", err)
}
return bc.GrantPermissions(permissions, popts)
},
"setDefaultNavigationTimeout": bc.SetDefaultNavigationTimeout,
"setDefaultTimeout": bc.SetDefaultTimeout,
Expand Down
4 changes: 2 additions & 2 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func NewBrowserContext(
}

if len(opts.Permissions) > 0 {
err := b.GrantPermissions(opts.Permissions, NewGrantPermissionsOptions())
err := b.GrantPermissions(opts.Permissions, GrantPermissionsOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (b *BrowserContext) Close() error {
}

// GrantPermissions enables the specified permissions, all others will be disabled.
func (b *BrowserContext) GrantPermissions(permissions []string, opts *GrantPermissionsOptions) error {
func (b *BrowserContext) GrantPermissions(permissions []string, opts GrantPermissionsOptions) error {
b.logger.Debugf("BrowserContext:GrantPermissions", "bctxid:%v", b.id)

permsToProtocol := map[string]cdpbrowser.PermissionType{
Expand Down
20 changes: 0 additions & 20 deletions common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,3 @@ func (w *WaitForEventOptions) Parse(ctx context.Context, optsOrPredicate sobek.V
type GrantPermissionsOptions struct {
Origin string
}

// NewGrantPermissionsOptions returns a new GrantPermissionsOptions.
func NewGrantPermissionsOptions() *GrantPermissionsOptions {
return &GrantPermissionsOptions{}
}

// Parse parses the options from opts if opts exists in the sobek runtime.
func (g *GrantPermissionsOptions) Parse(ctx context.Context, opts sobek.Value) {
rt := k6ext.Runtime(ctx)

if sobekValueExists(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
if k == "origin" {
g.Origin = opts.Get(k).String()
break
}
}
}
}
10 changes: 8 additions & 2 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,10 @@ func TestBrowserContextGrantPermissions(t *testing.T) {
bCtx, err := tb.NewContext(nil)
require.NoError(t, err)

err = bCtx.GrantPermissions([]string{tc.permission}, common.NewGrantPermissionsOptions())
err = bCtx.GrantPermissions(
[]string{tc.permission},
common.GrantPermissionsOptions{},
)

if tc.wantErr == "" {
assert.NoError(t, err)
Expand Down Expand Up @@ -968,7 +971,10 @@ func TestBrowserContextClearPermissions(t *testing.T) {

require.False(t, hasPermission(tb, p, "geolocation"))

err = bCtx.GrantPermissions([]string{"geolocation"}, common.NewGrantPermissionsOptions())
err = bCtx.GrantPermissions(
[]string{"geolocation"},
common.GrantPermissionsOptions{},
)
require.NoError(t, err)
require.True(t, hasPermission(tb, p, "geolocation"))

Expand Down
Loading