Skip to content

Commit

Permalink
Fix bug where after create/cancel, specifiable fields were retained
Browse files Browse the repository at this point in the history
  • Loading branch information
kimlisa committed Oct 31, 2024
1 parent 0932aae commit 130be68
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ export function RequestCheckout<T extends PendingListItem>({
{({ validator }) => (
<>
{!isRequestKubeResourceError &&
createAttempt.status !== 'failed' &&
fetchResourceRequestRolesAttempt.status === 'failed' && (
<Alert
kind="danger"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ export function useSpecifiableFields() {
);
}

function reset() {
setDryRunResponse(null);
setResourceRequestRoles([]);
setSelectedResourceRequestRoles([]);
setSelectedReviewers([]);
setStartTime(null);
setMaxDuration(null);
setPendingRequestTtl(null);
}

function preselectPendingRequestTtlOption(
newMaxDuration: number,
dryRequestCreated: Date
Expand Down Expand Up @@ -190,5 +200,6 @@ export function useSpecifiableFields() {
startTime,
onStartTimeChange,
onDryRunChange,
reset,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,102 @@ test(`fetching requestable roles for a kube cluster's namespaces only creates re
})
);
});

test('after creating an access request, pending requests and specifiable fields are reset', async () => {
const kube = makeKube();
const cluster = makeRootCluster();
const appContext = new MockAppContext();
appContext.clustersService.setState(draftState => {
draftState.clusters.set(rootClusterUri, cluster);
});
await appContext.workspacesService.setActiveWorkspace(rootClusterUri);
await appContext.workspacesService
.getWorkspaceAccessRequestsService(rootClusterUri)
.addOrRemoveResource({
kind: 'kube',
resource: kube,
});

let mockedCreateAccessRequestFn = jest.spyOn(
appContext.tshd,
'createAccessRequest'
);

const wrapper = ({ children }) => (
<MockAppContextProvider appContext={appContext}>
{children}
</MockAppContextProvider>
);

let { result } = renderHook(useAccessRequestCheckout, {
wrapper,
});

await waitFor(() => {
result.current.setSelectedReviewers([{ value: 'bob', label: 'bob' }]);
expect(result.current.selectedReviewers).toEqual([
{ value: 'bob', label: 'bob' },
]);
});

await waitFor(() => {
result.current.setSelectedResourceRequestRoles(['apple', 'banana']);
expect(result.current.selectedResourceRequestRoles).toEqual([
'apple',
'banana',
]);
});

await waitFor(async () => {
await result.current.createRequest({
suggestedReviewers: result.current.selectedReviewers.map(r => r.value),
});
expect(mockedCreateAccessRequestFn).toHaveBeenCalledWith({
rootClusterUri: '/clusters/teleport-local',
resourceIds: [
{
clusterName: 'teleport-local',
kind: 'kube_cluster',
name: kube.name,
subResourceName: '',
},
],
roles: ['apple', 'banana'],
suggestedReviewers: ['bob'],
});
});

// Call create again, should've cleared reviewers and previous roles.
mockedCreateAccessRequestFn.mockClear();
await waitFor(async () => {
// A successful create would've cleared all selected resources,
// so we add it back here to allow creating again.
expect(result.current.pendingAccessRequests).toHaveLength(0);
await appContext.workspacesService
.getWorkspaceAccessRequestsService(rootClusterUri)
.addOrRemoveResource({
kind: 'kube',
resource: kube,
});
});

await waitFor(async () => {
await result.current.createRequest({
suggestedReviewers: result.current.selectedReviewers.map(r => r.value),
});
expect(mockedCreateAccessRequestFn).toHaveBeenCalledWith({
rootClusterUri: '/clusters/teleport-local',
resourceIds: [
{
clusterName: 'teleport-local',
kind: 'kube_cluster',
name: kube.name,
subResourceName: '',
},
],
// These fields gotten cleared after the first create.
roles: [],
suggestedReviewers: [],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default function useAccessRequestCheckout() {
onDryRunChange,
startTime,
onStartTimeChange,
reset: resetSpecifiableFields,
} = useSpecifiableFields();

const [showCheckout, setShowCheckout] = useState(false);
Expand Down Expand Up @@ -371,6 +372,7 @@ export default function useAccessRequestCheckout() {
}

function reset() {
resetSpecifiableFields();
if (workspaceAccessRequest) {
return workspaceAccessRequest.clearPendingAccessRequest();
}
Expand Down

0 comments on commit 130be68

Please sign in to comment.