-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dagit] Disable unloadables if no permissions (#8021)
### Summary & Motivation Disallow sensor/schedule toggling on unloadable sensors/schedules for viewers who don't have permissions. ### How I Tested These Changes `yarn jest Unloadable`
- Loading branch information
Showing
4 changed files
with
381 additions
and
16 deletions.
There are no files selected for viewing
252 changes: 252 additions & 0 deletions
252
js_modules/dagit/packages/core/src/instigation/Unloadable.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
import {gql, useQuery} from '@apollo/client'; | ||
import {act, render, screen} from '@testing-library/react'; | ||
import * as React from 'react'; | ||
|
||
import {PYTHON_ERROR_FRAGMENT} from '../app/PythonErrorInfo'; | ||
import {TestProvider} from '../testing/TestProvider'; | ||
import {InstigationStatus} from '../types/globalTypes'; | ||
|
||
import {INSTIGATION_STATE_FRAGMENT} from './InstigationUtils'; | ||
import {UnloadableSchedules, UnloadableSensors} from './Unloadable'; | ||
import {UnloadableInstigationStatesQuery} from './types/UnloadableInstigationStatesQuery'; | ||
|
||
describe('Unloadables', () => { | ||
const UNLOADABLE_INSTIGATION_STATES_QUERY = gql` | ||
query UnloadableInstigationStatesQuery { | ||
unloadableInstigationStatesOrError { | ||
... on InstigationStates { | ||
results { | ||
id | ||
...InstigationStateFragment | ||
} | ||
} | ||
...PythonErrorFragment | ||
} | ||
} | ||
${PYTHON_ERROR_FRAGMENT} | ||
${INSTIGATION_STATE_FRAGMENT} | ||
`; | ||
|
||
const defaultMocks = { | ||
InstigationStates: () => ({ | ||
results: () => [...new Array(1)], | ||
}), | ||
}; | ||
|
||
describe('Sensors', () => { | ||
const Test = () => { | ||
const {data, loading} = useQuery<UnloadableInstigationStatesQuery>( | ||
UNLOADABLE_INSTIGATION_STATES_QUERY, | ||
); | ||
if (loading) { | ||
return <div>Loading…</div>; | ||
} | ||
const states = data?.unloadableInstigationStatesOrError; | ||
if (states && states.__typename === 'InstigationStates') { | ||
return <UnloadableSensors sensorStates={states.results} />; | ||
} | ||
return <div>Error!</div>; | ||
}; | ||
|
||
it('shows enabled ON switch if running and viewer has permission to edit sensors', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.RUNNING, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{edit_sensor: true}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(true); | ||
expect(switchElem.disabled).toBe(false); | ||
}); | ||
|
||
it('shows disabled OFF switch if stopped, even if viewer has permission to edit sensors', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.STOPPED, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{edit_sensor: true}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(false); | ||
expect(switchElem.disabled).toBe(true); | ||
}); | ||
|
||
it('shows disabled ON switch if running and no permission to edit sensors', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.RUNNING, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{edit_sensor: false}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(true); | ||
expect(switchElem.disabled).toBe(true); | ||
}); | ||
|
||
it('shows disabled OFF switch if stopped and no permission to edit sensors', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.STOPPED, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{edit_sensor: false}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(false); | ||
expect(switchElem.disabled).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Schedules', () => { | ||
const Test = () => { | ||
const {data, loading} = useQuery<UnloadableInstigationStatesQuery>( | ||
UNLOADABLE_INSTIGATION_STATES_QUERY, | ||
); | ||
if (loading) { | ||
return <div>Loading…</div>; | ||
} | ||
const states = data?.unloadableInstigationStatesOrError; | ||
if (states && states.__typename === 'InstigationStates') { | ||
return <UnloadableSchedules scheduleStates={states.results} />; | ||
} | ||
return <div>Error!</div>; | ||
}; | ||
|
||
it('shows enabled ON switch if running and viewer has permission to stop schedules', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.RUNNING, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{stop_running_schedule: true}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(true); | ||
expect(switchElem.disabled).toBe(false); | ||
}); | ||
|
||
it('shows disabled OFF switch if stopped, even if viewer has permission to start schedule', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.STOPPED, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{start_schedule: true}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(false); | ||
expect(switchElem.disabled).toBe(true); | ||
}); | ||
|
||
it('shows disabled ON switch if running and no permission to stop schedules', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.RUNNING, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{stop_running_schedule: false}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(true); | ||
expect(switchElem.disabled).toBe(true); | ||
}); | ||
|
||
it('shows disabled OFF switch if stopped and no permission to start schedule', async () => { | ||
const mocks = { | ||
InstigationState: () => ({ | ||
status: () => InstigationStatus.STOPPED, | ||
}), | ||
}; | ||
|
||
await act(async () => { | ||
render( | ||
<TestProvider | ||
apolloProps={{mocks: [defaultMocks, mocks]}} | ||
permissionOverrides={{start_schedule: false}} | ||
> | ||
<Test /> | ||
</TestProvider>, | ||
); | ||
}); | ||
|
||
const switchElem: HTMLInputElement = screen.getByRole('checkbox'); | ||
expect(switchElem.checked).toBe(false); | ||
expect(switchElem.disabled).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.