Skip to content

Commit

Permalink
fix: firefox permissions were not being set properly
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranayub committed Jul 29, 2020
1 parent 59b55c0 commit f814837
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('helpers', () => {

it('should set Firefox notifications preferences path', () => {
expect(getBrowserLaunchOptionsPermissionsPath('firefox', 'notifications')).toBe(
'preferences.permissions.default.desktop-notification',
'preferences.["permissions.default.desktop-notification"]',
)
})
})
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get, forOwn, set } from 'lodash'
import { onBeforeBrowserLaunch, modifyAndTransformPluginEnv } from '../plugin'
import { PermissionState } from '../types'
import { getBrowserLaunchOptionsPermissionsPath, getBrowserLaunchOptionsPermissionsContainerPath } from '../helpers'
import { getBrowserLaunchOptionsPermissionsPath } from '../helpers'
import { PREFERENCES_ROOT_PATH_BY_FAMILY } from '../constants'

const EMPTY_LAUNCH_OPTIONS = {} as Cypress.BrowserLaunchOptions
Expand Down Expand Up @@ -160,14 +160,12 @@ describe('plugin', () => {

const initialLaunchOptions = {} as Cypress.BrowserLaunchOptions
const geolocationPath = getBrowserLaunchOptionsPermissionsPath(browserFamily, 'geolocation')
const containerPath = getBrowserLaunchOptionsPermissionsContainerPath(browserFamily)

set(initialLaunchOptions, geolocationPath, PermissionState.allow)

const launchOptions = handle({ family: browserFamily }, initialLaunchOptions)

expect(get(launchOptions, geolocationPath)).toBeUndefined()
expect(get(launchOptions, containerPath)).toBeUndefined()
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { BrowserPermissions } from './types'
export const PLUGIN_ENV_VAR = 'browserPermissions'

export const PREFERENCES_ROOT_PATH_BY_FAMILY: Record<Cypress.BrowserFamily, string> = {
chromium: 'preferences.default.',
firefox: 'preferences.',
chromium: 'preferences.default',
firefox: 'preferences',
}

export const PERMISSIONS_PREF_CONTAINER_BY_FAMILY: Record<Cypress.BrowserFamily, string> = {
Expand Down
16 changes: 5 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,11 @@ export function getBrowserLaunchOptionsPermissionsPath(browserFamily: Cypress.Br
const valuePath = PERMISSIONS_PREF_NAME_BY_FAMILY[browserFamily][permission]

if (rootPath && containerPath && valuePath) {
return `${rootPath}${containerPath}.${valuePath}`
}
return undefined
}

export function getBrowserLaunchOptionsPermissionsContainerPath(browserFamily: Cypress.BrowserFamily) {
const rootPath = PREFERENCES_ROOT_PATH_BY_FAMILY[browserFamily]
const containerPath = PERMISSIONS_PREF_CONTAINER_BY_FAMILY[browserFamily]

if (rootPath && containerPath) {
return `${rootPath}${containerPath}`
if (browserFamily === 'firefox') {
return `${rootPath}.["${containerPath}.${valuePath}"]`
} else {
return `${rootPath}.${containerPath}.${valuePath}`
}
}
return undefined
}
32 changes: 22 additions & 10 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { forOwn, set, unset, keys, pickBy, camelCase, mapKeys } from 'lodash'
import { PermissionState } from './types'
import {
getBrowserPermissionsFromEnv,
getBrowserLaunchOptionsPermissionsPath,
getBrowserLaunchOptionsPermissionsContainerPath,
} from './helpers'
import { PLUGIN_ENV_VAR } from './constants'
import { getBrowserPermissionsFromEnv, getBrowserLaunchOptionsPermissionsPath } from './helpers'
import { PLUGIN_ENV_VAR, PREFERENCES_ROOT_PATH_BY_FAMILY, PERMISSIONS_PREF_CONTAINER_BY_FAMILY } from './constants'

function resetBrowserPermissions(
browser: Pick<Cypress.Browser, 'family'>,
launchOptions: Cypress.BrowserLaunchOptions,
) {
const path = getBrowserLaunchOptionsPermissionsContainerPath(browser.family)
const rootPath = PREFERENCES_ROOT_PATH_BY_FAMILY[browser.family]

if (path) {
unset(launchOptions, path)
switch (browser.family) {
case 'chromium': {
const containerPath = PERMISSIONS_PREF_CONTAINER_BY_FAMILY.chromium

unset(launchOptions, `${rootPath}.${containerPath}`)
break
}
case 'firefox': {
const containerPath = PERMISSIONS_PREF_CONTAINER_BY_FAMILY.firefox

Object.keys(launchOptions[rootPath])
.filter((pref) => pref.startsWith(containerPath))
.forEach((pref) => {
delete launchOptions[rootPath][pref]
})
break
}
}
}

Expand All @@ -26,12 +37,13 @@ export function onBeforeBrowserLaunch(config: Pick<Cypress.PluginConfigOptions,
// By default, unset preferences Cypress doesn't set automatically
resetBrowserPermissions(browser, launchOptions)

// Set Chrome launchOptions preferences
// Set launchOptions preferences
forOwn(requestedPermissions, (value: PermissionState, permission: string) => {
const path = getBrowserLaunchOptionsPermissionsPath(browser.family, permission)

if (path) {
set(launchOptions, path, value)

console.info(`[browserPermissions] permission '${permission}' => '${PermissionState[value]}'`)
}
})
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export enum PermissionState {
block = 2,
}

export type BrowserPermissionName = keyof BrowserPermissions

export interface BrowserPermissions {
camera: PermissionState
microphone: PermissionState
Expand Down

0 comments on commit f814837

Please sign in to comment.