smartssh/test/test.ts

85 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-07-27 13:52:01 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-10-11 11:05:29 +00:00
import * as smartssh from '../ts/index.js';
import * as path from 'path';
2017-06-15 15:52:22 +00:00
2018-09-17 20:32:31 +00:00
let testSshInstance: smartssh.SshInstance;
let testSshKey: smartssh.SshKey;
2017-06-15 15:52:22 +00:00
tap.test('should create a valid SshKey object', async () => {
testSshKey = new smartssh.SshKey({
host: 'example.com',
private: 'someExamplePrivateKey',
2022-10-11 11:05:29 +00:00
public: 'someExamplePublicKey',
2018-09-17 20:32:31 +00:00
});
2022-10-11 11:05:29 +00:00
expect(testSshKey).toBeInstanceOf(smartssh.SshKey);
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('.type should be a valid type', async () => {
2022-10-11 11:05:29 +00:00
expect(testSshKey.type).toEqual('duplex');
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('.publicKey should be public key', async () => {
2022-10-11 11:05:29 +00:00
expect(testSshKey.pubKey).toEqual('someExamplePublicKey');
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('.privateKey should be private key', async () => {
2022-10-11 11:05:29 +00:00
expect(testSshKey.privKey).toEqual('someExamplePrivateKey');
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('.publicKeyBase64 should be public key base 64 encoded', async () => {
// tslint:disable-next-line:no-unused-expression
2018-09-17 20:32:31 +00:00
testSshKey.pubKeyBase64;
});
2017-06-15 15:52:22 +00:00
tap.test('.store() should store the file to disk', async () => {
2018-09-17 20:32:31 +00:00
testSshKey.store(path.join(process.cwd(), 'test/temp'));
});
2017-06-15 15:52:22 +00:00
// SSH INstance
tap.test("'new' keyword should create a new SshInstance object from class", async () => {
testSshInstance = new smartssh.SshInstance({
2022-10-11 11:05:29 +00:00
sshDirPath: path.join(process.cwd(), 'test/temp/'),
2018-09-17 20:32:31 +00:00
});
2022-10-11 11:05:29 +00:00
expect(testSshInstance).toBeInstanceOf(smartssh.SshInstance);
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('.addKey() should accept a new SshKey object', async () => {
2018-09-17 20:32:31 +00:00
testSshInstance.addKey(
new smartssh.SshKey({
public: 'somePublicKey',
private: 'somePrivateKey',
2022-10-11 11:05:29 +00:00
host: 'gitlab.com',
2018-09-17 20:32:31 +00:00
})
);
testSshInstance.addKey(
new smartssh.SshKey({
public: 'somePublicKey',
private: 'somePrivateKey',
2022-10-11 11:05:29 +00:00
host: 'bitbucket.org',
2018-09-17 20:32:31 +00:00
})
);
testSshInstance.addKey(
new smartssh.SshKey({
public: 'someGitHubPublicKey',
private: 'someGitHubPrivateKey',
2022-10-11 11:05:29 +00:00
host: 'github.com',
2018-09-17 20:32:31 +00:00
})
);
});
2017-06-15 15:52:22 +00:00
tap.test('.sshKeys should point to an array of sshKeys', async () => {
2018-09-17 20:32:31 +00:00
let sshKeyArray = testSshInstance.sshKeys;
2022-10-11 11:05:29 +00:00
expect(sshKeyArray).toBeInstanceOf(Array);
expect(sshKeyArray[0].host).toEqual('gitlab.com');
expect(sshKeyArray[1].host).toEqual('bitbucket.org');
expect(sshKeyArray[2].host).toEqual('github.com');
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('.getKey() should get a specific key selected by host', async () => {
2022-10-11 11:05:29 +00:00
expect(testSshInstance.getKey('github.com').pubKey).toEqual('someGitHubPublicKey');
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('.removeKey() should remove a key', async () => {
2018-09-17 20:32:31 +00:00
testSshInstance.removeKey(testSshInstance.getKey('bitbucket.org'));
2022-10-11 11:05:29 +00:00
expect(testSshInstance.sshKeys[1].host).toEqual('github.com');
2018-09-17 20:32:31 +00:00
});
2017-06-15 15:52:22 +00:00
tap.test('it should store to disk', async () => {
2018-09-17 20:32:31 +00:00
testSshInstance.writeToDisk();
});
2017-06-15 15:52:22 +00:00
2018-09-17 20:32:31 +00:00
tap.start();