Skip to content

Commit

Permalink
#28 Finished tests for env and added tests for action util
Browse files Browse the repository at this point in the history
  • Loading branch information
JabobKrauskopf committed Mar 5, 2023
1 parent 4a2b23b commit 2a7a1b0
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
101 changes: 101 additions & 0 deletions test/utils/action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type { JwtPayload } from "jsonwebtoken";
import {
getUploadAction,
getDownloadAction,
getVerifyAction,
} from "../../server/utils/actions";
import { verifyJWT } from "../../server/utils/jwt";

describe("Actions Utils", () => {
beforeAll(() => {
Date.now = jest.fn(() => 1677970800);
});

test("Generate upload action", async () => {
const gitUser = "user";
const repo = "repo";
const oid = "15";

const uploadAction = getUploadAction(gitUser, repo, oid);

const authorizationHeader = uploadAction.header.Authorization.split(" ");
const token = verifyJWT<JwtPayload>(authorizationHeader[1]!);

expect(uploadAction).toStrictEqual({
href: "http://localhost:8000/user/repo/objects/15",
expires_in: 900,
header: {
Authorization:
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnaXRVc2VyIjoidXNlciIsInJlcG8iOiJyZXBvIiwiYWN0aW9uIjoidXBsb2FkIiwib2lkIjoiMTUiLCJpYXQiOjE2Nzc5NzAsImV4cCI6MTY3ODg3MH0.b1MBJB3lbmwQ9qPuOxReIgEPnDFm6EZUWJEaFKFXhvM",
},
});
expect(authorizationHeader.length).toBe(2);
expect(token.isVerified).toBe(true);
expect(token.payload).toStrictEqual({
gitUser,
repo,
oid,
action: "upload",
iat: 1677970,
exp: 1678870,
});
});

test("Generate download action", async () => {
const gitUser = "user";
const repo = "repo";
const oid = "15";

const downloadAction = getDownloadAction(gitUser, repo, oid);

const authorizationHeader = downloadAction.header.Authorization.split(" ");
const token = verifyJWT<JwtPayload>(authorizationHeader[1]!);

expect(downloadAction).toStrictEqual({
href: "http://localhost:8000/user/repo/objects/15",
expires_in: 900,
header: {
Authorization:
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnaXRVc2VyIjoidXNlciIsInJlcG8iOiJyZXBvIiwiYWN0aW9uIjoiZG93bmxvYWQiLCJvaWQiOiIxNSIsImlhdCI6MTY3Nzk3MCwiZXhwIjoxNjc4ODcwfQ._qPCx-n6d4TUOw7iwFK7jZ6tCvJYTgOzgT5EnFuISPU",
},
});
expect(authorizationHeader.length).toBe(2);
expect(token.isVerified).toBe(true);
expect(token.payload).toStrictEqual({
gitUser,
repo,
oid,
action: "download",
iat: 1677970,
exp: 1678870,
});
});

test("Generate verify action", async () => {
const gitUser = "user";
const repo = "repo";

const verifyAction = getVerifyAction(gitUser, repo);

const authorizationHeader = verifyAction.header.Authorization.split(" ");
const token = verifyJWT<JwtPayload>(authorizationHeader[1]!);

expect(verifyAction).toStrictEqual({
href: "http://localhost:8000/user/repo/verify",
expires_in: 900,
header: {
Authorization:
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnaXRVc2VyIjoidXNlciIsInJlcG8iOiJyZXBvIiwiYWN0aW9uIjoidmVyaWZ5IiwiaWF0IjoxNjc3OTcwLCJleHAiOjE2Nzg4NzB9.8KWdxQZ-JOc-Sl-I7urgoRYKZChqxwbJ2MnwoLG_gyY",
},
});
expect(authorizationHeader.length).toBe(2);
expect(token.isVerified).toBe(true);
expect(token.payload).toStrictEqual({
gitUser,
repo,
action: "verify",
iat: 1677970,
exp: 1678870,
});
});
});
58 changes: 58 additions & 0 deletions test/utils/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,64 @@ describe("Env Utils", () => {
}).toThrow();
});
});

describe("SSH_ENABLED Env Variable", () => {
test("SSH_ENABLED can be enabled and dependents can be correctly set", () => {
process.env.SSH_ENABLED = "true";
process.env.SSH_PRIVATE_KEY_PATH = "path";
process.env.SSH_PORT = "30";

const { env } = require("../../server/utils/env");

expect(env.SSH_ENABLED).toBe("true");
expect(env.SSH_PRIVATE_KEY_PATH).toBe("path");
expect(env.SSH_PORT).toBe(30);
});

test("SSH_ENABLED can be disabled and dependents are not set", () => {
process.env.SSH_ENABLED = "false";
process.env.SSH_PRIVATE_KEY_PATH = "path";
process.env.SSH_PORT = "30";

const { env } = require("../../server/utils/env");

expect(env.SSH_ENABLED).toBe("false");
expect(env.SSH_PRIVATE_KEY_PATH).toBe(undefined);
expect(env.SSH_PORT).toBe(undefined);
});

test("SSH_ENABLED can be unset and dependents are not set", () => {
process.env.SSH_ENABLED = undefined;
process.env.SSH_PRIVATE_KEY_PATH = "path";
process.env.SSH_PORT = "30";

const { env } = require("../../server/utils/env");

expect(env.SSH_ENABLED).toBe(undefined);
expect(env.SSH_PRIVATE_KEY_PATH).toBe(undefined);
expect(env.SSH_PORT).toBe(undefined);
});

test("SSH_PORT has correct default value", () => {
process.env.SSH_ENABLED = "true";
process.env.SSH_PRIVATE_KEY_PATH = "path";

const { env } = require("../../server/utils/env");

expect(env.SSH_ENABLED).toBe("true");
expect(env.SSH_PRIVATE_KEY_PATH).toBe("path");
expect(env.SSH_PORT).toBe(22);
});

test("SSH_ENABLED throws error if SSH_PRIVATE_KEY_PATH is not set", () => {
process.env.SSH_ENABLED = "true";
process.env.SSH_PRIVATE_KEY_PATH = undefined;

expect(() => {
require("../../server/utils/env");
}).toThrow();
});
});
});

export {};

0 comments on commit 2a7a1b0

Please sign in to comment.