Skip to content

Commit fe64a91

Browse files
committed
test: more DB test coverage
1 parent 79a30ba commit fe64a91

File tree

3 files changed

+211
-5
lines changed

3 files changed

+211
-5
lines changed

src/db/file/pushes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const cancel = async (id: string) => {
130130
return { message: `cancel ${id}` };
131131
};
132132

133-
export const canUserCancelPush = async (id: string, user: any) => {
133+
export const canUserCancelPush = async (id: string, user: string) => {
134134
return new Promise<boolean>(async (resolve) => {
135135
const pushDetail = await getPush(id);
136136
if (!pushDetail) {
@@ -149,14 +149,14 @@ export const canUserCancelPush = async (id: string, user: any) => {
149149
});
150150
};
151151

152-
export const canUserApproveRejectPush = async (id: string, user: any) => {
152+
export const canUserApproveRejectPush = async (id: string, user: string) => {
153153
return new Promise<boolean>(async (resolve) => {
154154
const action = await getPush(id);
155155
if (!action) {
156156
resolve(false);
157157
return;
158158
}
159-
const repoName = action?.repoName.replace('.git', '');
159+
const repoName = action.repoName.replace('.git', '');
160160
const isAllowed = await repo.canUserApproveRejectPushRepo(repoName, user);
161161

162162
resolve(isAllowed);

src/db/file/repo.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ export const getRepo = async (name: string) => {
5050
};
5151

5252
export const createRepo = async (repo: Repo) => {
53-
repo.name = repo.name.toLowerCase();
54-
5553
if (isBlank(repo.project)) {
5654
throw new Error('Project name cannot be empty');
5755
}
5856
if (isBlank(repo.name)) {
5957
throw new Error('Repository name cannot be empty');
58+
} else {
59+
repo.name = repo.name.toLowerCase();
6060
}
6161
if (isBlank(repo.url)) {
6262
throw new Error('URL cannot be empty');
@@ -153,6 +153,8 @@ export const removeUserCanAuthorise = async (name: string, user: string) => {
153153

154154
const options = { multi: false, upsert: false };
155155
db.update({ name: name }, repo, options, (err) => {
156+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
157+
/* istanbul ignore if */
156158
if (err) {
157159
reject(err);
158160
} else {
@@ -176,6 +178,8 @@ export const removeUserCanPush = async (name: string, user: string) => {
176178

177179
const options = { multi: false, upsert: false };
178180
db.update({ name: name }, repo, options, (err) => {
181+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
182+
/* istanbul ignore if */
179183
if (err) {
180184
reject(err);
181185
} else {

test/testDb.test.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ describe('Database clients', async () => {
9595
const repos2 = await db.getRepos({ url: TEST_REPO.url });
9696
const cleanRepos2 = cleanResponseData(TEST_REPO, repos2);
9797
expect(cleanRepos2[0]).to.eql(TEST_REPO);
98+
99+
// passing an empty query should produce same results as no query
100+
const repos3 = await db.getRepos();
101+
const repos4 = await db.getRepos({});
102+
expect(repos3).to.have.same.deep.members(repos4);
98103
});
99104

100105
it('should be able to retrieve a repo by name', async function () {
@@ -270,6 +275,17 @@ describe('Database clients', async () => {
270275
// leave user in place for next test(s)
271276
});
272277

278+
it('should throw an error when authorising a user to push on non-existent repo', async function () {
279+
let threwError = false;
280+
try {
281+
// uppercase the filter value to confirm db client is lowercasing inputs
282+
await db.addUserCanPush('non-existent-repo', TEST_USER.username);
283+
} catch (e) {
284+
threwError = true;
285+
}
286+
expect(threwError).to.be.true;
287+
});
288+
273289
it('should be able to authorise a user to push and confirm that they can', async function () {
274290
let threwError = false;
275291
try {
@@ -301,6 +317,16 @@ describe('Database clients', async () => {
301317
expect(threwError).to.be.false;
302318
});
303319

320+
it('should throw an error when de-authorising a user to push on non-existent repo', async function () {
321+
let threwError = false;
322+
try {
323+
await db.removeUserCanPush('non-existent-repo', TEST_USER.username);
324+
} catch (e) {
325+
threwError = true;
326+
}
327+
expect(threwError).to.be.true;
328+
});
329+
304330
it("should be able to de-authorise a user to push and confirm that they can't", async function () {
305331
let threwError = false;
306332
try {
@@ -331,6 +357,16 @@ describe('Database clients', async () => {
331357
expect(threwError).to.be.false;
332358
});
333359

360+
it('should throw an error when authorising a user to authorise on non-existent repo', async function () {
361+
let threwError = false;
362+
try {
363+
await db.addUserCanAuthorise('non-existent-repo', TEST_USER.username);
364+
} catch (e) {
365+
threwError = true;
366+
}
367+
expect(threwError).to.be.true;
368+
});
369+
334370
it('should be able to authorise a user to authorise and confirm that they can', async function () {
335371
let threwError = false;
336372
try {
@@ -361,6 +397,17 @@ describe('Database clients', async () => {
361397
expect(threwError).to.be.false;
362398
});
363399

400+
it('should throw an error when de-authorising a user to push on non-existent repo', async function () {
401+
let threwError = false;
402+
try {
403+
// uppercase the filter value to confirm db client is lowercasing inputs
404+
await db.removeUserCanAuthorise('non-existent-repo', TEST_USER.username);
405+
} catch (e) {
406+
threwError = true;
407+
}
408+
expect(threwError).to.be.true;
409+
});
410+
364411
it("should be able to de-authorise a user to authorise and confirm that they can't", async function () {
365412
let threwError = false;
366413
try {
@@ -397,6 +444,33 @@ describe('Database clients', async () => {
397444
expect(threwError).to.be.false;
398445
});
399446

447+
it('should NOT throw an error when checking whether a user can push on non-existent repo', async function () {
448+
let threwError = false;
449+
try {
450+
// uppercase the filter value to confirm db client is lowercasing inputs
451+
const allowed = await db.isUserPushAllowed('non-existent-repo', TEST_USER.username);
452+
expect(allowed).to.be.false;
453+
} catch (e) {
454+
threwError = true;
455+
}
456+
expect(threwError).to.be.false;
457+
});
458+
459+
it('should NOT throw an error when checking whether a user can authorise on non-existent repo', async function () {
460+
let threwError = false;
461+
try {
462+
// uppercase the filter value to confirm db client is lowercasing inputs
463+
const allowed = await db.canUserApproveRejectPushRepo(
464+
'non-existent-repo',
465+
TEST_USER.username,
466+
);
467+
expect(allowed).to.be.false;
468+
} catch (e) {
469+
threwError = true;
470+
}
471+
expect(threwError).to.be.false;
472+
});
473+
400474
it('should be able to create a push', async function () {
401475
await db.writeAudit(TEST_PUSH);
402476
const pushes = await db.getPushes();
@@ -411,6 +485,134 @@ describe('Database clients', async () => {
411485
expect(cleanPushes).to.not.deep.include(TEST_PUSH);
412486
});
413487

488+
it('should be able to authorise a push', async function () {
489+
// first create the push
490+
await db.writeAudit(TEST_PUSH);
491+
let threwError = false;
492+
try {
493+
const msg = await db.authorise(TEST_PUSH.id);
494+
expect(msg).to.have.property('message');
495+
} catch (e) {
496+
console.error('Error: ', e);
497+
threwError = true;
498+
}
499+
expect(threwError).to.be.false;
500+
// clean up
501+
await db.deletePush(TEST_PUSH.id);
502+
});
503+
504+
it('should throw an error when authorising a non-existent a push', async function () {
505+
let threwError = false;
506+
try {
507+
await db.authorise(TEST_PUSH.id);
508+
} catch (e) {
509+
threwError = true;
510+
}
511+
expect(threwError).to.be.true;
512+
});
513+
514+
it('should be able to reject a push', async function () {
515+
// first create the push
516+
await db.writeAudit(TEST_PUSH);
517+
let threwError = false;
518+
try {
519+
const msg = await db.reject(TEST_PUSH.id);
520+
expect(msg).to.have.property('message');
521+
} catch (e) {
522+
threwError = true;
523+
}
524+
expect(threwError).to.be.false;
525+
// clean up
526+
await db.deletePush(TEST_PUSH.id);
527+
});
528+
529+
it('should throw an error when rejecting a non-existent a push', async function () {
530+
let threwError = false;
531+
try {
532+
await db.reject(TEST_PUSH.id);
533+
} catch (e) {
534+
threwError = true;
535+
}
536+
expect(threwError).to.be.true;
537+
});
538+
539+
it('should be able to cancel a push', async function () {
540+
// first create the push
541+
await db.writeAudit(TEST_PUSH);
542+
let threwError = false;
543+
try {
544+
const msg = await db.cancel(TEST_PUSH.id);
545+
expect(msg).to.have.property('message');
546+
} catch (e) {
547+
threwError = true;
548+
}
549+
expect(threwError).to.be.false;
550+
// clean up
551+
await db.deletePush(TEST_PUSH.id);
552+
});
553+
554+
it('should throw an error when cancelling a non-existent a push', async function () {
555+
let threwError = false;
556+
try {
557+
await db.cancel(TEST_PUSH.id);
558+
} catch (e) {
559+
threwError = true;
560+
}
561+
expect(threwError).to.be.true;
562+
});
563+
564+
it('should be able to check if a user can cancel push', async function () {
565+
let threwError = false;
566+
const repoName = TEST_PUSH.repoName.replace('.git', '');
567+
try {
568+
// push does not exist yet, should return false
569+
let allowed = await db.canUserCancelPush(TEST_PUSH.id, TEST_USER.username);
570+
expect(allowed).to.be.false;
571+
572+
// create the push - user should already exist and not authorised to push
573+
await db.writeAudit(TEST_PUSH);
574+
allowed = await db.canUserCancelPush(TEST_PUSH.id, TEST_USER.username);
575+
expect(allowed).to.be.false;
576+
577+
// authorise user and recheck
578+
await db.addUserCanPush(repoName, TEST_USER.username);
579+
allowed = await db.canUserCancelPush(TEST_PUSH.id, TEST_USER.username);
580+
expect(allowed).to.be.true;
581+
} catch (e) {
582+
threwError = true;
583+
}
584+
expect(threwError).to.be.false;
585+
// clean up
586+
await db.deletePush(TEST_PUSH.id);
587+
await db.removeUserCanPush(repoName, TEST_USER.username);
588+
});
589+
590+
it('should be able to check if a user can approve/reject push', async function () {
591+
let threwError = false;
592+
const repoName = TEST_PUSH.repoName.replace('.git', '');
593+
try {
594+
// push does not exist yet, should return false
595+
let allowed = await db.canUserApproveRejectPush(TEST_PUSH.id, TEST_USER.username);
596+
expect(allowed).to.be.false;
597+
598+
// create the push - user should already exist and not authorised to push
599+
await db.writeAudit(TEST_PUSH);
600+
allowed = await db.canUserApproveRejectPush(TEST_PUSH.id, TEST_USER.username);
601+
expect(allowed).to.be.false;
602+
603+
// authorise user and recheck
604+
await db.addUserCanAuthorise(repoName, TEST_USER.username);
605+
allowed = await db.canUserApproveRejectPush(TEST_PUSH.id, TEST_USER.username);
606+
expect(allowed).to.be.true;
607+
} catch (e) {
608+
threwError = true;
609+
}
610+
expect(threwError).to.be.false;
611+
// clean up
612+
await db.deletePush(TEST_PUSH.id);
613+
await db.removeUserCanAuthorise(repoName, TEST_USER.username);
614+
});
615+
414616
after(async function () {
415617
await db.deleteRepo(TEST_REPO.name);
416618
await db.deleteUser(TEST_USER.username);

0 commit comments

Comments
 (0)