|
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs'; |
| 3 | +import Logger, { LogLevel, StreamHandler } from '@matrixai/logger'; |
| 4 | +import PolykeyAgent from 'polykey/dist/PolykeyAgent'; |
| 5 | +import { vaultOps } from 'polykey/dist/vaults'; |
| 6 | +import * as keysUtils from 'polykey/dist/keys/utils'; |
| 7 | +import * as testUtils from '../utils'; |
| 8 | + |
| 9 | +describe('commandTouch', () => { |
| 10 | + const password = 'password'; |
| 11 | + const logger = new Logger('CLI Test', LogLevel.WARN, [new StreamHandler()]); |
| 12 | + let dataDir: string; |
| 13 | + let polykeyAgent: PolykeyAgent; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + dataDir = await fs.promises.mkdtemp( |
| 17 | + path.join(globalThis.tmpDir, 'polykey-test-'), |
| 18 | + ); |
| 19 | + polykeyAgent = await PolykeyAgent.createPolykeyAgent({ |
| 20 | + password: password, |
| 21 | + options: { |
| 22 | + nodePath: dataDir, |
| 23 | + agentServiceHost: '127.0.0.1', |
| 24 | + clientServiceHost: '127.0.0.1', |
| 25 | + keys: { |
| 26 | + passwordOpsLimit: keysUtils.passwordOpsLimits.min, |
| 27 | + passwordMemLimit: keysUtils.passwordMemLimits.min, |
| 28 | + strictMemoryLock: false, |
| 29 | + }, |
| 30 | + }, |
| 31 | + logger: logger, |
| 32 | + }); |
| 33 | + }); |
| 34 | + afterEach(async () => { |
| 35 | + await polykeyAgent.stop(); |
| 36 | + await fs.promises.rm(dataDir, { |
| 37 | + force: true, |
| 38 | + recursive: true, |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + test('should create a secret if it does not exist', async () => { |
| 43 | + const vaultName = 'vault'; |
| 44 | + const vaultId = await polykeyAgent.vaultManager.createVault(vaultName); |
| 45 | + const secretName = 'secret'; |
| 46 | + const command = [ |
| 47 | + 'secrets', |
| 48 | + 'touch', |
| 49 | + '-np', |
| 50 | + dataDir, |
| 51 | + `${vaultName}:${secretName}`, |
| 52 | + ]; |
| 53 | + const result = await testUtils.pkStdio(command, { |
| 54 | + env: { PK_PASSWORD: password }, |
| 55 | + cwd: dataDir, |
| 56 | + }); |
| 57 | + expect(result.exitCode).toBe(0); |
| 58 | + await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => { |
| 59 | + await vault.readF(async (efs) => { |
| 60 | + await expect(efs.exists(secretName)).resolves.toBeTruthy(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + test('should update mtime if secret exists', async () => { |
| 65 | + const vaultName = 'vault'; |
| 66 | + const vaultId = await polykeyAgent.vaultManager.createVault(vaultName); |
| 67 | + const secretName = 'secret'; |
| 68 | + let oldMtime: Date | undefined = undefined; |
| 69 | + await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => { |
| 70 | + await vaultOps.writeSecret(vault, secretName, secretName); |
| 71 | + oldMtime = await vault.readF(async (efs) => { |
| 72 | + return (await efs.stat(secretName)).mtime; |
| 73 | + }); |
| 74 | + }); |
| 75 | + if (oldMtime == null) fail('Mtime cannot be nullish'); |
| 76 | + const command = [ |
| 77 | + 'secrets', |
| 78 | + 'touch', |
| 79 | + '-np', |
| 80 | + dataDir, |
| 81 | + `${vaultName}:${secretName}`, |
| 82 | + ]; |
| 83 | + const startTime = new Date(); |
| 84 | + const result = await testUtils.pkStdio(command, { |
| 85 | + env: { PK_PASSWORD: password }, |
| 86 | + cwd: dataDir, |
| 87 | + }); |
| 88 | + const endTime = new Date(); |
| 89 | + expect(result.exitCode).toBe(0); |
| 90 | + await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => { |
| 91 | + await vault.readF(async (efs) => { |
| 92 | + await expect(efs.exists(secretName)).resolves.toBeTruthy(); |
| 93 | + // File content isn't modified |
| 94 | + const content = await efs.readFile(secretName); |
| 95 | + expect(content.toString()).toEqual(secretName); |
| 96 | + // Timestamp has changed |
| 97 | + const stat = await efs.stat(secretName); |
| 98 | + expect( |
| 99 | + stat.mtime >= startTime && |
| 100 | + stat.mtime <= endTime && |
| 101 | + stat.mtime !== oldMtime, |
| 102 | + ).toBeTruthy(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + test('should update mtime if directory exists', async () => { |
| 107 | + const vaultName = 'vault'; |
| 108 | + const vaultId = await polykeyAgent.vaultManager.createVault(vaultName); |
| 109 | + const dirName = 'dir'; |
| 110 | + let oldMtime: Date | undefined = undefined; |
| 111 | + await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => { |
| 112 | + await vaultOps.mkdir(vault, dirName); |
| 113 | + oldMtime = await vault.readF(async (efs) => { |
| 114 | + return (await efs.stat(dirName)).mtime; |
| 115 | + }); |
| 116 | + }); |
| 117 | + if (oldMtime == null) fail('Mtime cannot be nullish'); |
| 118 | + const command = [ |
| 119 | + 'secrets', |
| 120 | + 'touch', |
| 121 | + '-np', |
| 122 | + dataDir, |
| 123 | + `${vaultName}:${dirName}`, |
| 124 | + ]; |
| 125 | + const startTime = new Date(); |
| 126 | + const result = await testUtils.pkStdio(command, { |
| 127 | + env: { PK_PASSWORD: password }, |
| 128 | + cwd: dataDir, |
| 129 | + }); |
| 130 | + const endTime = new Date(); |
| 131 | + expect(result.exitCode).toBe(0); |
| 132 | + await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => { |
| 133 | + await vault.readF(async (efs) => { |
| 134 | + await expect(efs.exists(dirName)).resolves.toBeTruthy(); |
| 135 | + // Timestamp has changed |
| 136 | + const stat = await efs.stat(dirName); |
| 137 | + expect( |
| 138 | + stat.mtime >= startTime && |
| 139 | + stat.mtime <= endTime && |
| 140 | + stat.mtime !== oldMtime, |
| 141 | + ).toBeTruthy(); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + test('should fail if parent directory does not exist', async () => { |
| 146 | + const vaultName = 'vault'; |
| 147 | + const vaultId = await polykeyAgent.vaultManager.createVault(vaultName); |
| 148 | + const secretName = path.join('dir', 'secret'); |
| 149 | + const command = [ |
| 150 | + 'secrets', |
| 151 | + 'touch', |
| 152 | + '-np', |
| 153 | + dataDir, |
| 154 | + `${vaultName}:${secretName}`, |
| 155 | + ]; |
| 156 | + const result = await testUtils.pkStdio(command, { |
| 157 | + env: { PK_PASSWORD: password }, |
| 158 | + cwd: dataDir, |
| 159 | + }); |
| 160 | + expect(result.exitCode).not.toBe(0); |
| 161 | + expect(result.stderr).toInclude('No such file or directory'); |
| 162 | + await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => { |
| 163 | + await vault.readF(async (efs) => { |
| 164 | + await expect(efs.exists(secretName)).resolves.toBeFalsy(); |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments