Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ describe('fetchRemoteScorecardAndPlugins', () => {
const mockFetch = vi.fn();
const validProjectUrl = 'https://app.valid-url.com/org/test-org/project/test-project';
const testToken = 'test-token';
const mockProjectId = 'prj_123';
const mockOrgId = 'org_123';

const mockProject = {
id: mockProjectId,
organizationId: mockOrgId,
slug: 'test-project',
};

beforeEach(() => {
global.fetch = mockFetch;
Expand Down Expand Up @@ -80,15 +88,84 @@ describe('fetchRemoteScorecardAndPlugins', () => {
);
});

it('should throw error when project config fetch is not found (404)', async () => {
mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 404,
});

await expect(
fetchRemoteScorecardAndPlugins({ projectUrl: validProjectUrl, auth: testToken })
).rejects.toThrow();

expect(errorUtils.exitWithError).toHaveBeenCalledWith(
expect.stringContaining('Failed to fetch project config')
);
});

it('should throw error when project config fetch is unauthorized (401)', async () => {
mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 401,
});

await expect(
fetchRemoteScorecardAndPlugins({ projectUrl: validProjectUrl, auth: testToken })
).rejects.toThrow();

expect(errorUtils.exitWithError).toHaveBeenCalledWith(
expect.stringContaining('Unauthorized access to project config')
);
});

it('should throw error when project has no scorecard config', async () => {
mockFetch.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: 'project-123',
slug: 'test-project',
config: {},
}),
});
mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: {},
}),
});

await expect(
fetchRemoteScorecardAndPlugins({ projectUrl: validProjectUrl, auth: testToken })
).rejects.toThrow();

expect(errorUtils.exitWithError).toHaveBeenCalledWith(
expect.stringContaining('No scorecard configuration found')
);
});

it('should throw error when project config is null', async () => {
mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: null,
}),
});

await expect(
fetchRemoteScorecardAndPlugins({ projectUrl: validProjectUrl, auth: testToken })
Expand All @@ -104,16 +181,22 @@ describe('fetchRemoteScorecardAndPlugins', () => {
levels: [{ name: 'Gold', rules: {} }],
};

mockFetch.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: 'project-123',
slug: 'test-project',
config: {
scorecard: mockScorecard,
},
}),
});
mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: {
scorecard: mockScorecard,
},
}),
});

const result = await fetchRemoteScorecardAndPlugins({
projectUrl: validProjectUrl,
Expand All @@ -134,11 +217,16 @@ describe('fetchRemoteScorecardAndPlugins', () => {
const mockPluginsCode = 'export default [() => ({ id: "test-plugin" })]';

mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: 'project-123',
slug: 'test-project',
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: {
scorecard: mockScorecard,
pluginsUrl: 'https://example.com/plugins.js',
Expand All @@ -159,7 +247,7 @@ describe('fetchRemoteScorecardAndPlugins', () => {
scorecard: mockScorecard,
plugins: mockPluginsCode,
});
expect(mockFetch).toHaveBeenCalledTimes(2);
expect(mockFetch).toHaveBeenCalledTimes(3);
});

it('should return scorecard without plugins when plugin fetch fails', async () => {
Expand All @@ -168,11 +256,16 @@ describe('fetchRemoteScorecardAndPlugins', () => {
};

mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: 'project-123',
slug: 'test-project',
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: {
scorecard: mockScorecard,
pluginsUrl: 'https://example.com/plugins.js',
Expand All @@ -195,13 +288,20 @@ describe('fetchRemoteScorecardAndPlugins', () => {
});

it('should use correct auth headers with access token', async () => {
mockFetch.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: 'project-123',
config: { scorecard: { levels: [] } },
}),
});
mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: { scorecard: { levels: [] } },
}),
});

await fetchRemoteScorecardAndPlugins({
projectUrl: validProjectUrl,
Expand All @@ -211,7 +311,7 @@ describe('fetchRemoteScorecardAndPlugins', () => {
expect(mockFetch).toHaveBeenCalledWith(
expect.any(URL),
expect.objectContaining({
headers: { Cookie: `accessToken=${testToken}` },
headers: { Cookie: `accessToken=${testToken}`, version: '2' },
})
);
});
Expand All @@ -220,13 +320,20 @@ describe('fetchRemoteScorecardAndPlugins', () => {
const apiKey = 'test-api-key';
process.env.REDOCLY_AUTHORIZATION = apiKey;

mockFetch.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: 'project-123',
config: { scorecard: { levels: [] } },
}),
});
mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: { scorecard: { levels: [] } },
}),
});

await fetchRemoteScorecardAndPlugins({
projectUrl: validProjectUrl,
Expand All @@ -237,7 +344,7 @@ describe('fetchRemoteScorecardAndPlugins', () => {
expect(mockFetch).toHaveBeenCalledWith(
expect.any(URL),
expect.objectContaining({
headers: { Authorization: `Bearer ${apiKey}` },
headers: { Authorization: `Bearer ${apiKey}`, version: '2' },
})
);
});
Expand All @@ -249,11 +356,16 @@ describe('fetchRemoteScorecardAndPlugins', () => {
const mockPluginsCode = 'export default [() => ({ id: "test-plugin" })]';

mockFetch
.mockResolvedValueOnce({
status: 200,
json: async () => mockProject,
})
.mockResolvedValueOnce({
status: 200,
json: async () => ({
id: 'project-123',
slug: 'test-project',
id: mockProjectId,
projectId: mockProjectId,
organizationId: mockOrgId,
config: {
scorecard: mockScorecard,
pluginsUrl: 'https://example.com/plugins.js',
Expand All @@ -276,6 +388,6 @@ describe('fetchRemoteScorecardAndPlugins', () => {
scorecard: mockScorecard,
plugins: mockPluginsCode,
});
expect(mockFetch).toHaveBeenCalledTimes(2);
expect(mockFetch).toHaveBeenCalledTimes(3);
});
});
Loading
Loading