Skip to content

Commit

Permalink
test: add server_test.ts and fix server.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kawarimidoll committed Aug 14, 2021
1 parent 4032d9f commit 3574f03
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
19 changes: 11 additions & 8 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,30 @@ import { renderHtml } from "./render_html.ts";
import { ConfigObject } from "./types.ts";
import { deleteItem, getItem, putItem } from "./dynamodb.ts";

function applyHash(token: string) {
export function applyHash(token: string) {
return createHash("sha256").update(`${token}`).toString();
}

const NAME_REGEX = /[a-z][a-z0-9_-]{2,64}/;
const TOKEN_REGEX = /[!-~]{8,128}/;
function validateName(name: string) {
const NAME_REGEX = /^[a-z][a-z0-9_-]{2,64}$/;
const TOKEN_REGEX = /^[!-~]{8,128}$/;

export function validateName(name: string) {
return NAME_REGEX.test(name);
}
function validateToken(token: string) {

export function validateToken(token: string) {
return TOKEN_REGEX.test(token);
}
function validateConfig(config: string) {

export function validateConfig(config: string) {
try {
const { list } = JSON.parse(config);
return !!(list);
} catch (_) {
return false;
}
}
function encodeConfig(rawConfig: string) {
export function encodeConfig(rawConfig: string) {
// use parse and stringify to minify json
return encode(
gzip(
Expand All @@ -42,7 +45,7 @@ function encodeConfig(rawConfig: string) {
),
);
}
function decodeConfig(compressedConfig: string) {
export function decodeConfig(compressedConfig: string) {
return JSON.parse(
new TextDecoder().decode(gunzip(decode(compressedConfig))),
) as ConfigObject;
Expand Down
52 changes: 52 additions & 0 deletions server_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { assert, assertEquals } from "./deps.ts";
Deno.env.set("AWS_ACCESS_KEY_ID", "dummy-id");
Deno.env.set("AWS_SECRET_ACCESS_KEY", "dummy-key");
import {
decodeConfig,
encodeConfig,
validateConfig,
validateName,
validateToken,
} from "./server.ts";

Deno.test("encode and decode", () => {
const config = {
description: "<description>",
twitter: "twitter",
list: {
id1: {
icon: "feather/github",
items: [{ icon: "feather/github" }],
},
},
};
const configStr = JSON.stringify(config);
assertEquals(decodeConfig(encodeConfig(configStr)), config);
});

Deno.test("validateConfig", () => {
assert(
validateConfig(
`{"list":{"id1":{"icon":"devicons/github","items":[{"text":"github","link":"https://github.com/"}]}}}`,
),
);
assert(!validateConfig("{}"));
assert(!validateConfig(""));
});

Deno.test("validateName", () => {
assert(validateName("this-is-valid_123"));
assert(!validateName("invalid name"));
assert(!validateName(""));
assert(!validateName("o"));
assert(!validateName("_no"));
assert(!validateName("名前"));
});

Deno.test("validateToken", () => {
assert(validateToken("this-is-valid_123"));
assert(!validateToken("invalid token"));
assert(!validateToken(""));
assert(!validateToken("short"));
assert(!validateToken("秘密"));
});
2 changes: 2 additions & 0 deletions velociraptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ scripts:
test:
desc: Runs the tests
cmd: deno test --reload
allow:
- env
gitHook: pre-push

cov:
Expand Down

0 comments on commit 3574f03

Please sign in to comment.