diff --git a/packages/dashboard-frontend/src/services/oauth/__tests__/index.spec.ts b/packages/dashboard-frontend/src/services/oauth/__tests__/index.spec.ts index 361a0d221..53dae0399 100644 --- a/packages/dashboard-frontend/src/services/oauth/__tests__/index.spec.ts +++ b/packages/dashboard-frontend/src/services/oauth/__tests__/index.spec.ts @@ -185,9 +185,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'); @@ -229,10 +227,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(); + }); }); diff --git a/packages/dashboard-frontend/src/services/oauth/index.ts b/packages/dashboard-frontend/src/services/oauth/index.ts index cab684ed7..005e8e66e 100644 --- a/packages/dashboard-frontend/src/services/oauth/index.ts +++ b/packages/dashboard-frontend/src/services/oauth/index.ts @@ -64,8 +64,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. } } }