Skip to content

Commit

Permalink
test:added gitops/util and modified some existing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Abinand P <[email protected]>
  • Loading branch information
Abiji-2020 committed Nov 29, 2024
1 parent 34e4e7e commit f7560d6
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 25 deletions.
2 changes: 1 addition & 1 deletion source/lib/gitops/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function configurePermit(
status: gitConfigResponse.status,
};
} else {
throw new Error('Invalid Configuration ' + response);
throw new Error('Invalid Configuration ');
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/OPAPolicy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ describe('OPA Policy Command', () => {
const options = {
serverUrl: 'http://localhost:8181',
keyAccount: 'testAccount',
apiKey: "permit_key_".concat("a".repeat(97)),
apiKey: 'permit_key_'.concat('a'.repeat(97)),
};
(fetch as any).mockResolvedValueOnce({
ok:true,
json:async ()=> ({
result:[
{ id: 'policy1', name: 'policyName1' },
{ id: 'policy2', name: 'policyName2' },
]}),
ok: true,
json: async () => ({
result: [
{ id: 'policy1', name: 'policyName1' },
{ id: 'policy2', name: 'policyName2' },
],
}),
status: 200,
});
const { stdin, lastFrame } = render(<Policy options={options} />);
Expand All @@ -31,7 +32,6 @@ describe('OPA Policy Command', () => {
expect(lastFrame()?.toString()).toMatch('policy1');
expect(lastFrame()?.toString()).toMatch('policy2');
stdin.write(enter);

});
it('should render the policy command with error', async () => {
const options = {
Expand Down
38 changes: 22 additions & 16 deletions tests/lib/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { describe, vi, it, expect } from "vitest";
import * as api from "../../source/lib/api";
import { describe, vi, it, expect } from 'vitest';
import * as api from '../../source/lib/api';
global.fetch = vi.fn();
describe("API", () => {
it("should call the apiCall", async()=>{
(fetch as any).mockResolvedValueOnce({
headers: {},
status: 200,
json: async () =>({id: "testId", name: "testName"})
});
const response = await api.apiCall("testEndpoint", "testToken", "testCookie", "GET", "testBody");
expect(response.status).toBe(200);
expect(response.response.id).toBe("testId");
expect(response.response.name).toBe("testName");
expect(response.headers).toEqual({});
})
})
describe('API', () => {
it('should call the apiCall', async () => {
(fetch as any).mockResolvedValueOnce({
headers: {},
status: 200,
json: async () => ({ id: 'testId', name: 'testName' }),
});
const response = await api.apiCall(
'testEndpoint',
'testToken',
'testCookie',
'GET',
'testBody',
);
expect(response.status).toBe(200);
expect(response.response.id).toBe('testId');
expect(response.response.name).toBe('testName');
expect(response.headers).toEqual({});
});
});
185 changes: 185 additions & 0 deletions tests/lib/gitops_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { describe, vi, it, expect } from 'vitest';
import * as utils from '../../source/lib/gitops/utils';
import { apiCall } from '../../source/lib/api';
import ssh from 'micro-key-producer/ssh.js';
import { randomBytes } from 'micro-key-producer/utils.js';
vi.mock('../../source/lib/api', () => ({
apiCall: vi.fn(),
}));
vi.mock('micro-key-producer/ssh.js', () => ({
default: vi.fn(),
}));
vi.mock('micro-key-producer/utils.js', () => ({
randomBytes: vi.fn(),
}));

describe('getProjectList', () => {
it('should return a list of projects', async () => {
(apiCall as any).mockResolvedValueOnce({
status: 200,
response: [
{
key: 'testKey',
urn_namespace: 'testNamespace',
id: 'testId',
organization_id: 'testOrgId',
created_at: 'testCreatedAt',
updated_at: 'testUpdatedAt',
name: 'testName',
},
],
});
const projects = await utils.getProjectList(
'permit_key_'.concat('a'.repeat(96)),
);
expect(projects).toEqual([
{
key: 'testKey',
urn_namespace: 'testNamespace',
id: 'testId',
organization_id: 'testOrgId',
created_at: 'testCreatedAt',
updated_at: 'testUpdatedAt',
name: 'testName',
},
]);
});
it('should throw an error if the status is not 200', async () => {
(apiCall as any).mockResolvedValueOnce({
status: 400,
response: 'testError',
});
await expect(
utils.getProjectList('permit_key_'.concat('a'.repeat(96))),
).rejects.toThrow('Failed to fetch projects: testError');
});
});

describe('getRepoList', () => {
it('should return a list of repos', async () => {
(apiCall as any).mockResolvedValueOnce({
status: 200,
response: [
{ status: 'valid', key: 'testKey' },
{ status: 'invalid', key: 'testKey2' },
],
});
const repos = await utils.getRepoList(
'permit_key_'.concat('a'.repeat(96)),
'testProjectKey',
);
expect(repos).toEqual([
{ status: 'valid', key: 'testKey' },
{ status: 'invalid', key: 'testKey2' },
]);
});
});

describe('generateSSHKey', () => {
it('should generate an SSH key', () => {
(randomBytes as any).mockReturnValueOnce(new Uint8Array(32));
(ssh as any).mockReturnValueOnce({
publicKeyBytes: new Uint8Array(8),
publicKey: 'publicKey',
privateKey: 'privateKey',
fingerprint: 'testFingerprint',
});
const key = utils.generateSSHKey();
expect(key).toStrictEqual({
publicKeyBytes: new Uint8Array(8),
publicKey: 'publicKey',
privateKey: 'privateKey',
fingerprint: 'testFingerprint',
});
});
});

describe('Configure Permit', async () => {
it('should configure permit', async () => {
const gitconfig = {
url: 'testUrl',
mainBranchName: 'testMainBranchName',
credentials: {
authType: 'ssh',
username: 'git',
privateKey: 'privateKey',
},
key: 'testKey',
activateWhenValidated: true,
};
(apiCall as any).mockResolvedValueOnce({
status: 200,
response: {
id: 'testId',
key: 'testKey',
status: 'valid',
},
});
const response = await utils.configurePermit(
'permit_key_'.concat('a'.repeat(96)),
'testProjectKey',
gitconfig,
);
expect(response).toStrictEqual({
id: 'testId',
key: 'testKey',
status: 'valid',
});
});
it('should throw an error if the status is 422', async () => {
const gitconfig = {
url: 'testUrl',
mainBranchName: 'testMainBranchName',
credentials: {
authType: 'ssh',
username: 'git',
privateKey: 'privateKey',
},
key: 'testKey',
activateWhenValidated: true,
};
(apiCall as any).mockResolvedValueOnce({
status: 422,
response: {
id: 'testId',
key: 'testKey',
status: 'valid',
},
});
await expect(
utils.configurePermit(
'permit_key_'.concat('a'.repeat(96)),
'testProjectKey',
gitconfig,
),
).rejects.toThrow('Validation Error in Configuring Permit');
});
it('should throw an error if the status is not 200', async () => {
const gitconfig = {
url: 'testUrl',
mainBranchName: 'testMainBranchName',
credentials: {
authType: 'ssh',
username: 'git',
privateKey: 'privateKey',
},
key: 'testKey',
activateWhenValidated: true,
};
(apiCall as any).mockResolvedValueOnce({
status: 400,
response: {
id: 'testId',
key: 'testKey',
status: 'valid',
},
});
await expect(
utils.configurePermit(
'permit_key_'.concat('a'.repeat(96)),
'testProjectKey',
gitconfig,
),
).rejects.toThrow('Invalid Configuration ');
});
});

0 comments on commit f7560d6

Please sign in to comment.