feat: add oauth token option for fetch action
Signed-off-by: ElaineDeMattosSilvaB <elaine.de-mattos-silva-bezerra@deutschebahn.com>
This commit is contained in:
@@ -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,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user