Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not throw other then unauthorised error on token refresh #987

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinokurig would it be possible to add test for this functionality? Do we need to re-throw exception in general?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to add test for this functionality?

done

Do we need to re-throw exception in general?

Yes, the exception interrupts the workspace start event until the authorisation callback.

// Skip other exceptions to proceed the workspace start.
}
}
}
Expand Down