Skip to content

Commit

Permalink
Add permission assertions
Browse files Browse the repository at this point in the history
This change adds code to assert on whether the permissions have been
granted before and after certain actions.

Co-authored-by: İnanç Gümüş <inanc.gumus@grafana.com>
  • Loading branch information
ankur22 and inancgumus committed Oct 23, 2023
1 parent befd7fa commit 8c31905
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,15 +845,33 @@ func TestBrowserContextGrantPermissions(t *testing.T) {
func TestBrowserContextClearPermissions(t *testing.T) {
t.Parallel()

hasPermission := func(tb *testBrowser, p *common.Page, perm string) bool {
t.Helper()

js := fmt.Sprintf(`
(perm) => navigator.permissions.query(
{ name: %q }
).then(result => result.state)
`, perm)
v := p.Evaluate(tb.toGojaValue(js))

return tb.asGojaValue(v).String() == "granted"
}

t.Run("no_permissions_set", func(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t)
bCtx, err := tb.NewContext(nil)
require.NoError(t, err)
p, err := bCtx.NewPage()
require.NoError(t, err)

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

err = bCtx.ClearPermissions()
assert.NoError(t, err)
require.False(t, hasPermission(tb, p, "geolocation"))
})

t.Run("permissions_set", func(t *testing.T) {
Expand All @@ -862,11 +880,17 @@ func TestBrowserContextClearPermissions(t *testing.T) {
tb := newTestBrowser(t)
bCtx, err := tb.NewContext(nil)
require.NoError(t, err)
p, err := bCtx.NewPage()
require.NoError(t, err)

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

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

err = bCtx.ClearPermissions()
assert.NoError(t, err)
require.False(t, hasPermission(tb, p, "geolocation"))
})
}

0 comments on commit 8c31905

Please sign in to comment.