Skip to content

Commit

Permalink
Do not throw other then unauthorised error on token refresh (#987)
Browse files Browse the repository at this point in the history
Do not throw exceptions other then unauthorised exception on token refresh request when starting a workspace.
  • Loading branch information
vinokurig committed Nov 15, 2023
1 parent 7008057 commit a2269d8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ describe('OAuth service', () => {
try {
await OAuthService.refreshTokenIfNeeded(devWorkspace);
} catch (e: any) {
expect(e.response.data.responseData.attributes.oauth_authentication_url).toBe(
'https://git-lub/oauth/url',
);
fail('it should not reach here');
}

expect(refreshFactoryOauthTokenSpy).toHaveBeenCalledWith('origin:project');
Expand Down Expand Up @@ -225,10 +223,53 @@ describe('OAuth service', () => {
try {
await OAuthService.refreshTokenIfNeeded(devWorkspace);
} catch (e: any) {
expect(e.response.status).toBe(401);
fail('it should not reach here');
}

expect(refreshFactoryOauthTokenSpy).toHaveBeenCalledWith('origin:project');
expect(mockOpenOAuthPage).not.toHaveBeenCalled();
});

it('should redirect to oauth window if error has OAuth response', async () => {
const status = { mainUrl: 'https://mainUrl' };
const projects = [
{
name: 'project',
git: {
remotes: {
origin: 'origin:project',
},
},
},
];
const devWorkspace = new DevWorkspaceBuilder()
.withStatus(status)
.withProjects(projects)
.build();

refreshFactoryOauthTokenSpy.mockRejectedValueOnce({
isAxiosError: false,
code: '401',
response: {
status: 401,
data: {
attributes: {
oauth_provider: 'git-lab',
oauth_authentication_url: 'https://git-lub/oauth/url',
},
},
},
} as AxiosError);

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

try {
await OAuthService.refreshTokenIfNeeded(devWorkspace);
} catch (e: any) {
expect(e.response.status).toBe(401);
}

expect(refreshFactoryOauthTokenSpy).toHaveBeenCalledWith('origin:project');
expect(mockOpenOAuthPage).toHaveBeenCalled();
});
});
4 changes: 3 additions & 1 deletion packages/dashboard-frontend/src/services/oauth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ export default class OAuthService {
response.data.attributes.oauth_authentication_url,
redirectUrl.toString(),
);
// Interrupt the workspace start. The workspace should start again after the authentication.
throw e;
}
throw e;
// Skip other exceptions to proceed the workspace start.
}
}
}
Expand Down

0 comments on commit a2269d8

Please sign in to comment.