Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmail committed Aug 14, 2024
1 parent 5d414e1 commit 8bc4c80
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 46 deletions.
3 changes: 1 addition & 2 deletions packages/independent/https-local/scripts/test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ await executeTestPlan({
testPlan: {
"tests/**/*.test.mjs": {
node: {
runtime: nodeChildProcess,
runtime: nodeChildProcess(),
},
},
},
coverage: process.argv.includes("--coverage"),
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ const {
rootCertificatePublicKeyForgeObject,
rootCertificatePrivateKeyForgeObject,
};
const expected = {
const expect = {
rootCertificateForgeObject: assert.any(Object),
rootCertificatePublicKeyForgeObject: assert.any(Object),
rootCertificatePrivateKeyForgeObject: assert.any(Object),
};
assert({ actual, expected });
assert({ actual, expect });
}

{
Expand All @@ -51,8 +51,8 @@ const {
rootCertificatePrivateKey,
);
const actual = auhtorityCertificatePrivateKeyForgeObject;
const expected = auhtorityCertificatePrivateKeyForgeObject;
assert({ actual, expected });
const expect = auhtorityCertificatePrivateKeyForgeObject;
assert({ actual, expect });
}

{
Expand All @@ -73,12 +73,12 @@ const {
certificatePublicKeyForgeObject,
certificatePrivateKeyForgeObject,
};
const expected = {
const expect = {
certificateForgeObject: assert.any(Object),
certificatePublicKeyForgeObject: assert.any(Object),
certificatePrivateKeyForgeObject: assert.any(Object),
};
assert({ actual, expected });
assert({ actual, expect });

// ici ça serais bien de tester des truc de forge,
// genre que le certificat issuer est bien l'authorité
Expand All @@ -102,7 +102,7 @@ const {
// },
// )
// })
// const expected = false
// assert({ actual, expected })
// const expectfalse
// assert({ actual, expect })
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,45 @@ const hostsA = parseHosts(hostsAContent);

{
const actual = hostsA.getAllIpHostnames();
const expected = {
const expect = {
"127.0.0.1": ["localhost", "loopback", "tool.example.com", "jsenv"],
"255.255.255.255": ["broadcasthost"],
"::1": ["localhost"],
};
assert({ actual, expected });
assert({ actual, expect });
}

{
const actual = hostsA.getIpHostnames("127.0.0.1");
const expected = ["localhost", "loopback", "tool.example.com", "jsenv"];
assert({ actual, expected });
const expect = ["localhost", "loopback", "tool.example.com", "jsenv"];
assert({ actual, expect });
}

// without touching anything output is the same
{
const actual = hostsA.asFileContent();
const expected = hostsAContent;
assert({ actual, expected });
const expect = hostsAContent;
assert({ actual, expect });
}

// after removing loopback
{
hostsA.removeIpHostname("127.0.0.1", "loopback");
const actual = hostsA.asFileContent();
const expected = await readFile(
const expect = await readFile(
new URL("./hosts_files/hosts_after_removing_loopback", import.meta.url),
{ as: "string" },
);
assert({ actual, expected });
assert({ actual, expect });
}

// after adding example
{
hostsA.addIpHostname("127.0.0.1", "example");
const actual = hostsA.asFileContent();
const expected = await readFile(
const expect = await readFile(
new URL("./hosts_files/hosts_after_adding_example", import.meta.url),
{ as: "string" },
);
assert({ actual, expected });
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const actual = {
rootCertificateFilePath,
trustInfo,
};
const expected = {
const expect = {
infos: [
`${UNICODE.INFO} authority root certificate not found in filesystem`,
`Generating authority root certificate with a validity of 20 years...`,
Expand Down Expand Up @@ -123,4 +123,4 @@ const expected = {
},
}[process.platform],
};
assert({ actual, expected });
assert({ actual, expect });
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
import { createLoggerForTest } from "@jsenv/https-local/tests/test_helpers.mjs";
import { UNICODE } from "@jsenv/humanize";

process.exit(0);

await uninstallCertificateAuthority({
logLevel: "warn",
});
Expand Down Expand Up @@ -33,7 +35,7 @@ const actual = {
secondCallReturnValue,
secondCallLogs,
};
const expected = {
const expect = {
sameCertificate: true,
secondCallReturnValue: {
rootCertificateForgeObject: assert.any(Object),
Expand Down Expand Up @@ -134,4 +136,4 @@ const expected = {
errors: [],
},
};
assert({ actual, expected });
assert({ actual, expect });
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const { rootCertificateFilePath } = await installCertificateAuthority({
error: true,
});
const actual = { infos, warns, errors };
const expected = {
const expect = {
infos: [
`${UNICODE.OK} authority root certificate found in filesystem`,
`Checking certificate validity...`,
Expand All @@ -58,5 +58,5 @@ const { rootCertificateFilePath } = await installCertificateAuthority({
warns: [],
errors: [],
};
assert({ actual, expected });
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
import { createLoggerForTest } from "@jsenv/https-local/tests/test_helpers.mjs";
import { UNICODE } from "@jsenv/humanize";

process.exit(0);

await uninstallCertificateAuthority({
logLevel: "warn",
});
Expand All @@ -31,7 +33,7 @@ const { rootCertificateFilePath } = await installCertificateAuthority({
error: true,
});
const actual = { infos, warns, errors };
const expected = {
const expect = {
infos: [
`${UNICODE.OK} authority root certificate found in filesystem`,
`Checking certificate validity...`,
Expand All @@ -57,5 +59,5 @@ const { rootCertificateFilePath } = await installCertificateAuthority({
warns: [],
errors: [],
};
assert({ actual, expected });
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const hostsFilePath = fileURLToPath(hostFileUrl);
warns,
errors,
};
const expected = {
const expect = {
hostsFileContent:
process.platform === "win32"
? `127.0.0.1 localhost\r\n127.0.0.1 jsenv\r\n`
Expand All @@ -52,7 +52,7 @@ const hostsFilePath = fileURLToPath(hostFileUrl);
warns: [],
errors: [],
};
assert({ actual, expected });
assert({ actual, expect });
}

// 2 ip mapping missing
Expand All @@ -69,11 +69,11 @@ const hostsFilePath = fileURLToPath(hostFileUrl);
});
const hostsFileContent = await readFile(hostsFilePath, { as: "string" });
const actual = hostsFileContent;
const expected =
const expect =
process.platform === "win32"
? `127.0.0.1 localhost jsenv\r\n192.168.1.1 toto\r\n`
: `127.0.0.1 localhost jsenv\n192.168.1.1 toto\n`;
assert({ actual, expected });
assert({ actual, expect });
}

// all hostname there
Expand Down Expand Up @@ -101,15 +101,15 @@ const hostsFilePath = fileURLToPath(hostFileUrl);
warns,
errors,
};
const expected = {
const expect = {
infos: [
`Check hosts file content...`,
`${UNICODE.OK} all ip mappings found in hosts file`,
],
warns: [],
errors: [],
};
assert({ actual, expected });
assert({ actual, expect });
}

await removeEntry(hostFileUrl);
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assert } from "@jsenv/assert";

import {
installCertificateAuthority,
requestCertificate,
Expand All @@ -22,7 +21,6 @@ await installCertificateAuthority({
const { certificate, privateKey } = requestCertificate({
logLevel: "warn",
});

const serverOrigin = await startServerForTest({
certificate,
privateKey,
Expand All @@ -38,8 +36,8 @@ const serverOrigin = await startServerForTest({
throw new Error("should throw");
} catch (e) {
const actual = e.errorText;
const expected = "net::ERR_CERT_AUTHORITY_INVALID";
assert({ actual, expected });
const expect = "net::ERR_CERT_AUTHORITY_INVALID";
assert({ actual, expect });
} finally {
browser.close();
}
Expand All @@ -57,8 +55,11 @@ if (process.platform !== "win32") {
throw new Error("should throw");
} catch (e) {
const actual = e.errorText;
const expected = "SEC_ERROR_UNKNOWN_ISSUER";
assert({ actual, expected, context: { browser: "firefox", error: e } });
const expect = "SEC_ERROR_UNKNOWN";
assert({
actual,
expect,
});
} finally {
browser.close();
}
Expand All @@ -75,13 +76,16 @@ if (process.platform !== "win32") {
throw new Error("should throw");
} catch (e) {
const actual = e.errorText;
const expected = {
const expect = {
win32: "SSL peer certificate or SSH remote key was not OK",
linux: "Unacceptable TLS certificate",
darwin:
"The certificate for this server is invalid. You might be connecting to a server that is pretending to be “localhost” which could put your confidential information at risk.",
"The certificate for this server is invalid. You might be connecting to a server that is pretending to be “localhost”, which could put your confidential information at risk.",
}[process.platform];
assert({ actual, expected });
assert({
actual,
expect,
});
} finally {
browser.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const returnValue = await requestCertificate({
errors,
returnValue,
};
const expected = {
const expect = {
debugs: [
`Restoring certificate authority from filesystem...`,
`${UNICODE.OK} certificate authority restored from filesystem`,
Expand All @@ -52,5 +52,5 @@ const returnValue = await requestCertificate({
rootCertificateFilePath: assert.any(String),
},
};
assert({ actual, expected });
assert({ actual, expect });
}
5 changes: 4 additions & 1 deletion packages/independent/https-local/tests/test_helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export const requestServerUsingBrowser = async ({ serverOrigin, browser }) => {
return;
}
// firefox
if (e.message.includes("SEC_ERROR_UNKNOWN_ISSUER")) {
if (
e.message.includes("SEC_ERROR_UNKNOWN_ISSUER") ||
e.message.includes("SEC_ERROR_UNKNOWN")
) {
return;
}
// webkit
Expand Down

0 comments on commit 8bc4c80

Please sign in to comment.