feat: add oauth token option for fetch action

Signed-off-by: ElaineDeMattosSilvaB <elaine.de-mattos-silva-bezerra@deutschebahn.com>
This commit is contained in:
ElaineDeMattosSilvaB
2024-06-27 21:44:08 +02:00
parent 30f11804b9
commit 64e7e9409f
3 changed files with 57 additions and 18 deletions
+31 -1
View File
@@ -17,7 +17,7 @@
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { GitLabIntegrationConfig } from './config';
import { getGitLabFileFetchUrl } from './core';
import { getGitLabFileFetchUrl, getGitLabRequestOptions } from './core';
const worker = setupServer();
@@ -186,4 +186,34 @@ describe('gitlab core', () => {
});
});
});
describe('getGitLabRequestOptions', () => {
it('should return Authorization header when oauthToken is provided', () => {
const oauthToken = 'mock-oauth-token';
const result = getGitLabRequestOptions(
configSelfHosteWithRelativePath,
oauthToken,
);
expect(result).toEqual({
headers: {
Authorization: `Bearer ${oauthToken}`,
},
});
});
it('should return private-token header when oauthToken is undefined', () => {
const oauthToken = undefined;
const result = getGitLabRequestOptions(
configSelfHosteWithRelativePath,
oauthToken,
);
expect(result).toEqual({
headers: {
'PRIVATE-TOKEN': configSelfHosteWithRelativePath.token,
},
});
});
});
});
+13 -4
View File
@@ -14,11 +14,11 @@
* limitations under the License.
*/
import fetch from 'cross-fetch';
import {
getGitLabIntegrationRelativePath,
GitLabIntegrationConfig,
} from './config';
import fetch from 'cross-fetch';
/**
* Given a URL pointing to a file on a provider, returns a URL that is suitable
@@ -51,9 +51,18 @@ export async function getGitLabFileFetchUrl(
* @param config - The relevant provider config
* @public
*/
export function getGitLabRequestOptions(config: GitLabIntegrationConfig): {
headers: Record<string, string>;
} {
export function getGitLabRequestOptions(
config: GitLabIntegrationConfig,
oauthToken?: string,
): { headers: Record<string, string> } {
if (oauthToken) {
return {
headers: {
Authorization: `Bearer ${oauthToken}`,
},
};
}
const { token = '' } = config;
return {
headers: {