-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test:added gitops/util and modified some existing tests
Signed-off-by: Abinand P <[email protected]>
- Loading branch information
1 parent
34e4e7e
commit f7560d6
Showing
4 changed files
with
216 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '); | ||
}); | ||
}); |