|
| 1 | +// This test needs to run first |
| 2 | +const chai = require('chai'); |
| 3 | +const db = require('../src/db'); |
| 4 | + |
| 5 | +const { expect } = chai; |
| 6 | + |
| 7 | +const TEST_REPO = { |
| 8 | + project: 'finos', |
| 9 | + name: 'db-test-repo', |
| 10 | + url: 'https://github.com/finos/db-test-repo.git', |
| 11 | +}; |
| 12 | + |
| 13 | +const TEST_USER = { |
| 14 | + username: 'db-u1', |
| 15 | + password: 'abc', |
| 16 | + gitAccount: 'db-test-user', |
| 17 | + |
| 18 | + admin: true, |
| 19 | +}; |
| 20 | + |
| 21 | +const TEST_PUSH = { |
| 22 | + steps: [], |
| 23 | + error: false, |
| 24 | + blocked: true, |
| 25 | + allowPush: false, |
| 26 | + authorised: false, |
| 27 | + canceled: true, |
| 28 | + rejected: false, |
| 29 | + autoApproved: false, |
| 30 | + autoRejected: false, |
| 31 | + commitData: [], |
| 32 | + id: '0000000000000000000000000000000000000000__1744380874110', |
| 33 | + type: 'push', |
| 34 | + method: 'get', |
| 35 | + timestamp: 1744380903338, |
| 36 | + project: 'finos', |
| 37 | + repoName: 'db-test-repo.git', |
| 38 | + url: 'https://github.com/finos/db-test-repo.git', |
| 39 | + repo: 'finos/db-test-repo.git', |
| 40 | + user: 'db-test-user', |
| 41 | + |
| 42 | + lastStep: null, |
| 43 | + blockedMessage: |
| 44 | + '\n\n\nGitProxy has received your push:\n\nhttp://localhost:8080/requests/0000000000000000000000000000000000000000__1744380874110\n\n\n', |
| 45 | + _id: 'GIMEz8tU2KScZiTz', |
| 46 | + attestation: null, |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * Clean up response data from the DB by removing an extraneous properties, |
| 51 | + * allowing comparison with expect. |
| 52 | + * @param {object} example Example element from which columns to retain are extracted |
| 53 | + * @param {array} responses Array of responses to clean. |
| 54 | + * @return {array} Array of cleaned up responses. |
| 55 | + */ |
| 56 | +const cleanResponseData = (example, responses) => { |
| 57 | + const columns = Object.keys(example); |
| 58 | + return responses.map((response) => { |
| 59 | + const cleanResponse = {}; |
| 60 | + columns.forEach((col) => { |
| 61 | + cleanResponse[col] = response[col]; |
| 62 | + }); |
| 63 | + return cleanResponse; |
| 64 | + }); |
| 65 | +}; |
| 66 | + |
| 67 | +// Use this test as a template |
| 68 | +describe('Database client', async () => { |
| 69 | + before(async function () {}); |
| 70 | + |
| 71 | + it('should be able to create a repo', async function () { |
| 72 | + await db.createRepo(TEST_REPO); |
| 73 | + const repos = await db.getRepos(); |
| 74 | + const cleanRepos = cleanResponseData(TEST_REPO, repos); |
| 75 | + expect(cleanRepos).to.deep.include(TEST_REPO); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should be able to delete a repo', async function () { |
| 79 | + await db.deleteRepo(TEST_REPO.name); |
| 80 | + const repos = await db.getRepos(); |
| 81 | + const cleanRepos = cleanResponseData(TEST_REPO, repos); |
| 82 | + expect(cleanRepos).to.not.deep.include(TEST_REPO); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should be able to create a user', async function () { |
| 86 | + await db.createUser( |
| 87 | + TEST_USER.username, |
| 88 | + TEST_USER.password, |
| 89 | + TEST_USER.email, |
| 90 | + TEST_USER.gitAccount, |
| 91 | + TEST_USER.admin, |
| 92 | + ); |
| 93 | + const users = await db.getUsers(); |
| 94 | + console.log('TEST USER:', JSON.stringify(TEST_USER, null, 2)); |
| 95 | + console.log('USERS:', JSON.stringify(users, null, 2)); |
| 96 | + // remove password as it will have been hashed |
| 97 | + // eslint-disable-next-line no-unused-vars |
| 98 | + const { password: _, ...TEST_USER_CLEAN } = TEST_USER; |
| 99 | + const cleanUsers = cleanResponseData(TEST_USER_CLEAN, users); |
| 100 | + console.log('CLEAN USERS:', JSON.stringify(cleanUsers, null, 2)); |
| 101 | + expect(cleanUsers).to.deep.include(TEST_USER_CLEAN); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should be able to delete a user', async function () { |
| 105 | + await db.deleteUser(TEST_USER.username); |
| 106 | + const users = await db.getUsers(); |
| 107 | + const cleanUsers = cleanResponseData(TEST_USER, users); |
| 108 | + expect(cleanUsers).to.not.deep.include(TEST_USER); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should be able to create a push', async function () { |
| 112 | + await db.writeAudit(TEST_PUSH); |
| 113 | + const pushes = await db.getPushes(); |
| 114 | + const cleanPushes = cleanResponseData(TEST_PUSH, pushes); |
| 115 | + expect(cleanPushes).to.deep.include(TEST_PUSH); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should be able to delete a push', async function () { |
| 119 | + await db.deletePush(TEST_PUSH.id); |
| 120 | + const pushes = await db.getPushes(); |
| 121 | + const cleanPushes = cleanResponseData(TEST_PUSH, pushes); |
| 122 | + expect(cleanPushes).to.not.deep.include(TEST_PUSH); |
| 123 | + }); |
| 124 | + |
| 125 | + after(async function () {}); |
| 126 | +}); |
0 commit comments