|
| 1 | +/** |
| 2 | + * Copyright (c) 2020 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { suite, test } from "mocha-typescript"; |
| 8 | +import * as chai from "chai"; |
| 9 | +import { SSHPublicKeyValue } from "."; |
| 10 | + |
| 11 | +const expect = chai.expect; |
| 12 | + |
| 13 | +@suite.only |
| 14 | +class TestSSHPublicKeyValue { |
| 15 | + private key = |
| 16 | + "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDCnrN9UdK1bNGPmZfenTWXLuYYDjlYvZE8S+WOfP08WpR1GETzX5ZvgYOEZGwEE8KUPHC9cge4Hvo/ydIS9aqbZ5MiVGJ8cAIq1Ic89SjlDWU6fl8TwIqOPCi2imAASlEDP4q8vMLK1N6UOW1EVbxyL3uybGd10ysC1t1FxFPveIGNsYE/MOQeuEWS16AplpXYXIfVRSlgAskeBft2w8Ud3B4gNe8ECLA/FXu96UpvZkdtOarA3JZ9Z27GveNJg9Mtmmw0+US0KXiO9x9NyH7G8+mqVDwDY+nNvaFA5gtQxkkl/uY2oz9k/B4Rjlj3jOiUXe5uQs3XUm5m8g9a9fh62DabLpA2fEvtfg+a/VqNe52dNa5YjupwvBd6Inb5uMW/TYjNl6bNHPlXFKw/nwLOVzukpkjxMZUKS6+4BGkpoasj6y2rTU/wkpbdD8J7yjI1p6J9aKkC6KksIWgN7xGmHkv2PCGDqMHTNbnQyowtNKMgA/667vAYJ0qW7HAHBFXJRs6uRi/DI3+c1QV2s4wPCpEHDIYApovQ0fbON4WDPoGMyHd7kPh9xB/bX7Dj0uMXImu1pdTd62fQ/1XXX64+vjAAXS/P9RSCD0RCRt/K3LPKl2m7GPI3y1niaE52XhxZw+ms9ays6NasNVMw/ZC+f02Ti+L5FBEVf8230RVVRQ== [email protected]"; |
| 17 | + |
| 18 | + @test public testValidate() { |
| 19 | + const key = this.key; |
| 20 | + const [t, k, e] = key.split(" "); |
| 21 | + expect( |
| 22 | + SSHPublicKeyValue.validate({ |
| 23 | + key, |
| 24 | + name: "NiceName", |
| 25 | + }), |
| 26 | + ).to.deep.equal({ type: t, key: k, email: e }); |
| 27 | + } |
| 28 | + |
| 29 | + @test public testValidateWithDiffType() { |
| 30 | + const key = this.key; |
| 31 | + const [_, k, e] = key.split(" "); |
| 32 | + expect( |
| 33 | + SSHPublicKeyValue.validate({ |
| 34 | + key: key.replace("ssh-rsa", "[email protected]"), |
| 35 | + name: "NiceName", |
| 36 | + }), |
| 37 | + ).to.deep.equal({ type: "[email protected]", key: k, email: e }); |
| 38 | + } |
| 39 | + |
| 40 | + @test public testValidateWithoutEmail() { |
| 41 | + const key = this.key; |
| 42 | + const [t, k, _] = key.split(" "); |
| 43 | + expect( |
| 44 | + SSHPublicKeyValue.validate({ |
| 45 | + key: key.replace(" [email protected]", ""), |
| 46 | + name: "NiceName", |
| 47 | + }), |
| 48 | + ).to.deep.equal({ type: t, key: k, email: undefined }); |
| 49 | + } |
| 50 | + |
| 51 | + @test public testValidateWithoutEmailButEndsWithSpaces() { |
| 52 | + const key = this.key; |
| 53 | + const [t, k, _] = key.split(" "); |
| 54 | + expect( |
| 55 | + SSHPublicKeyValue.validate({ |
| 56 | + key: key.replace("[email protected]", " "), |
| 57 | + name: "NiceName", |
| 58 | + }), |
| 59 | + ).to.deep.equal({ type: t, key: k, email: undefined }); |
| 60 | + } |
| 61 | + |
| 62 | + @test public testValidateWithError() { |
| 63 | + expect(() => |
| 64 | + SSHPublicKeyValue.validate({ |
| 65 | + key: "Hello World", |
| 66 | + name: "NiceName", |
| 67 | + }), |
| 68 | + ).throw("SSH public key is not available"); |
| 69 | + |
| 70 | + expect(() => |
| 71 | + SSHPublicKeyValue.validate({ |
| 72 | + key: "", |
| 73 | + name: "NiceName", |
| 74 | + }), |
| 75 | + ).throw("SSH public key is not available"); |
| 76 | + } |
| 77 | + |
| 78 | + @test public testGetFingerprint() { |
| 79 | + const key = this.key; |
| 80 | + expect( |
| 81 | + SSHPublicKeyValue.getFingerprint({ |
| 82 | + key, |
| 83 | + name: "NiceName", |
| 84 | + }), |
| 85 | + ).to.equal("eb5445d81477c9ab62dfb5f8f3ffe04e"); |
| 86 | + } |
| 87 | + |
| 88 | + @test public testGetFingerprintWithIncorrectPublicKey() { |
| 89 | + expect(() => |
| 90 | + SSHPublicKeyValue.getFingerprint({ |
| 91 | + key: "Hello World", |
| 92 | + name: "NiceName", |
| 93 | + }), |
| 94 | + ).to.throw("SSH public key is not available"); |
| 95 | + } |
| 96 | +} |
| 97 | +module.exports = new TestSSHPublicKeyValue(); // Only to circumvent no usage warning :-/ |
0 commit comments