Skip to content

Commit

Permalink
feat: Force refresh personal access token (#1137)
Browse files Browse the repository at this point in the history
* feat: Force refresh personal access token

Signed-off-by: Anatolii Bazko <[email protected]>
Signed-off-by: Oleksii Orel <[email protected]>
Co-authored-by: Oleksii Orel <[email protected]>
  • Loading branch information
tolusha and olexii4 authored Jun 27, 2024
1 parent 03adeb1 commit 3991d41
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ export class WorkspaceActionsBulkDeleteButton extends React.Component<Props> {
const { isDisabled, onAction, workspaces } = this.props;

const handleAction = () =>
workspaces.map(workspace =>
onAction?.(WorkspaceAction.DELETE_WORKSPACE, workspace.uid, true),
);
workspaces.map(async workspace => {
if (onAction) {
await onAction(WorkspaceAction.DELETE_WORKSPACE, workspace.uid, true);
}
});

return (
<div data-testid="workspace-actions-bulk-delete">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ describe('OAuth service', () => {
it('should not refresh token if no status section in devworkspace', async () => {
const devWorkspace = new DevWorkspaceBuilder().build();

await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);

expect(refreshFactoryOauthTokenSpy).not.toHaveBeenCalled();
});
it('should not refresh token if no mainUrl in status', async () => {
const status = {};
const devWorkspace = new DevWorkspaceBuilder().withStatus(status).build();

await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);

expect(refreshFactoryOauthTokenSpy).not.toHaveBeenCalled();
});
it('should not refresh token if no projects section in devworkspace', async () => {
const status = { mainUrl: 'https://mainUrl' };
const devWorkspace = new DevWorkspaceBuilder().withStatus(status).build();

await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);

expect(refreshFactoryOauthTokenSpy).not.toHaveBeenCalled();
});
Expand All @@ -58,7 +58,7 @@ describe('OAuth service', () => {
.withProjects(projects)
.build();

await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);

expect(refreshFactoryOauthTokenSpy).not.toHaveBeenCalled();
});
Expand All @@ -78,7 +78,7 @@ describe('OAuth service', () => {
.withProjects(projects)
.build();

await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);

expect(refreshFactoryOauthTokenSpy).not.toHaveBeenCalled();
});
Expand All @@ -102,7 +102,7 @@ describe('OAuth service', () => {

refreshFactoryOauthTokenSpy.mockResolvedValueOnce();

await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);

expect(refreshFactoryOauthTokenSpy).toHaveBeenCalledWith('origin:project');
});
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('OAuth service', () => {

jest.spyOn(common.helpers.errors, 'includesAxiosResponse').mockImplementation(() => false);

await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);

expect(refreshFactoryOauthTokenSpy).toHaveBeenCalledWith('origin:project');
expect(mockOpenOAuthPage).not.toHaveBeenCalled();
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('OAuth service', () => {
jest.spyOn(common.helpers.errors, 'includesAxiosResponse').mockImplementation(() => true);

try {
await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);
} catch (e: any) {
fail('it should not reach here');
}
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('OAuth service', () => {
jest.spyOn(common.helpers.errors, 'includesAxiosResponse').mockImplementation(() => true);

try {
await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);
} catch (e: any) {
fail('it should not reach here');
}
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('OAuth service', () => {
jest.spyOn(common.helpers.errors, 'includesAxiosResponse').mockImplementation(() => true);

try {
await OAuthService.refreshTokenIfNeeded(devWorkspace);
await OAuthService.refreshTokenIfProjectExists(devWorkspace);
} catch (e: any) {
expect(e.response.status).toBe(401);
}
Expand Down
22 changes: 8 additions & 14 deletions packages/dashboard-frontend/src/services/oauth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ export class OAuthService {
}
}

static async refreshTokenIfNeeded(workspace: devfileApi.DevWorkspace): Promise<void> {
// if workspace is not created yet, do not refresh token
if (!workspace.status || !workspace.status.mainUrl) {
return;
}
if (!workspace.spec.template.projects) {
return;
}

const project = workspace.spec.template.projects[0];
if (!project || !project.git) {
return;
}
static async refreshTokenIfProjectExists(workspace: devfileApi.DevWorkspace): Promise<void> {
// Find first git project.
let project = workspace.spec.template.projects?.find(project => !!project.git);
project = project || workspace.spec.template.starterProjects?.find(project => !!project.git);
project = project || workspace.spec.template.dependentProjects?.find(project => !!project.git);

try {
await refreshFactoryOauthToken(project.git.remotes.origin);
if (project) {
await refreshFactoryOauthToken(project.git!.remotes.origin);
}
} catch (e) {
if (!common.helpers.errors.includesAxiosResponse(e)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export const actionCreators: ActionCreators = {
return;
}
try {
await OAuthService.refreshTokenIfNeeded(workspace);
await OAuthService.refreshTokenIfProjectExists(workspace);
await dispatch({ type: Type.REQUEST_DEVWORKSPACE, check: AUTHORIZED });
if (!(await selectAsyncIsAuthorized(getState()))) {
const error = selectSanityCheckError(getState());
Expand Down

0 comments on commit 3991d41

Please sign in to comment.