Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust http gateway #39

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,109 +1,109 @@
import { Account } from "../src/Account";
import { InMemoryDb } from "../src/InMemoryDb";
import { InternalServer } from "../src/InternalServer";
import { Status } from "../src/Status";
describe('InternalServer', () => {
let server: InternalServer;
let db: InMemoryDb;
import { Account } from "../src/types/Account";
import { FunctionManager } from "../src/functions/FunctionManager";
import { HttpResponse, IRepository, InMemoryDb, RepositoryBase } from "serverless-utils";
describe("InternalServer", () => {
let manager: FunctionManager;
let accountRepo: IRepository<Account>;

beforeEach(() => {
db = new InMemoryDb();
server = new InternalServer(db, 'adminKey');
const db = new InMemoryDb();
accountRepo = new RepositoryBase<Account>(db, "name");
manager = new FunctionManager(accountRepo, "adminKey");
});

describe('verify', () => {
it('should return 404 when wrong adminKey is used', async () => {
const response = await server.verify('test', 'key', 'wrongKey');
expect(response).toEqual(Status.NotFound());
describe("verify", () => {
it("should return 404 when wrong adminKey is used", async () => {
const response = await manager.verify("test", "key", "wrongKey");
expect(response).toEqual(HttpResponse.NotFound());
});

it('should return 404 when account is not found', async () => {
const response = await server.verify('test', 'key', 'adminKey');
expect(response).toEqual(Status.NotFound());
it("should return 404 when account is not found", async () => {
const response = await manager.verify("test", "key", "adminKey");
expect(response).toEqual(HttpResponse.NotFound());
});

it('should return 200 when account is found and key matches', async () => {
const account: Account = { name: 'test', apiKey: 'key', isDeleted: false };
await db.save('user', 'test', account);
it("should return 200 when account is found and key matches", async () => {
const account: Account = { name: "test", apiKey: "key", isDeleted: false };
await accountRepo.save(account);

const response = await server.verify('test', 'key', 'adminKey');
expect(response).toEqual(Status.Ok({ message: 'Verified.' }));
const response = await manager.verify("test", "key", "adminKey");
expect(response).toEqual(HttpResponse.Ok({ message: "Verified." }));
});
});

describe('create', () => {
it('should return 404 when wrong adminKey is used', async () => {
const response = await server.create('test', 'wrongKey');
expect(response).toEqual(Status.NotFound());
describe("create", () => {
it("should return 404 when wrong adminKey is used", async () => {
const response = await manager.create("test", "wrongKey");
expect(response).toEqual(HttpResponse.NotFound());
});

it('should create account and return 200 when correct adminKey is used', async () => {
const response = await server.create('test', 'adminKey');
it("should create account and return 200 when correct adminKey is used", async () => {
const response = await manager.create("test", "adminKey");
expect(response.statusCode).toBe(200);

const body = (response as any).body;
expect(body).toHaveProperty('message', 'Account created.');
expect(body).toHaveProperty('apiKey');
expect(body).toHaveProperty("message", "Account created.");
expect(body).toHaveProperty("apiKey");

const account = await db.read<Account>('user', 'test');
expect(account).toHaveProperty('name', 'test');
expect(account).toHaveProperty('apiKey', body.apiKey);
const account = await accountRepo.read("test");
expect(account).toHaveProperty("name", "test");
expect(account).toHaveProperty("apiKey", body.apiKey);
});
});

describe('delete', () => {
it('should return 404 when wrong adminKey is used', async () => {
const response = await server.delete('test', 'wrongKey');
expect(response).toEqual(Status.NotFound());
describe("delete", () => {
it("should return 404 when wrong adminKey is used", async () => {
const response = await manager.delete("test", "wrongKey");
expect(response).toEqual(HttpResponse.NotFound());
});

it('should mark account as deleted and return 200 when correct adminKey is used', async () => {
const account: Account = { name: 'test', apiKey: 'key', isDeleted: false };
await db.save('user', 'test', account);
it("should mark account as deleted and return 200 when correct adminKey is used", async () => {
const account: Account = { name: "test", apiKey: "key", isDeleted: false };
await accountRepo.save(account);

const response = await server.delete('test', 'adminKey');
expect(response).toEqual(Status.Ok(undefined));
const response = await manager.delete("test", "adminKey");
expect(response).toEqual(HttpResponse.Ok(undefined));

const deletedAccount = await db.read<Account>('user', 'test');
expect(deletedAccount).toHaveProperty('isDeleted', true);
const deletedAccount = await accountRepo.read("test");
expect(deletedAccount).toHaveProperty("isDeleted", true);
});
});

describe('restore', () => {
it('should return 404 when wrong adminKey is used', async () => {
const response = await server.restore('test', 'wrongKey');
expect(response).toEqual(Status.NotFound());
describe("restore", () => {
it("should return 404 when wrong adminKey is used", async () => {
const response = await manager.restore("test", "wrongKey");
expect(response).toEqual(HttpResponse.NotFound());
});

it('should mark account as not deleted and return 200 when correct adminKey is used', async () => {
const account: Account = { name: 'test', apiKey: 'key', isDeleted: true };
await db.save('user', 'test', account);
it("should mark account as not deleted and return 200 when correct adminKey is used", async () => {
const account: Account = { name: "test", apiKey: "key", isDeleted: true };
await accountRepo.save(account);

const response = await server.restore('test', 'adminKey');
expect(response).toEqual(Status.Ok(undefined));
const response = await manager.restore("test", "adminKey");
expect(response).toEqual(HttpResponse.Ok(undefined));

const restoredAccount = await db.read<Account>('user', 'test');
expect(restoredAccount).toHaveProperty('isDeleted', false);
const restoredAccount = await accountRepo.read("test");
expect(restoredAccount).toHaveProperty("isDeleted", false);
});
});

describe('get', () => {
it('should return 404 when wrong adminKey is used', async () => {
const response = await server.get('test', 'wrongKey');
expect(response).toEqual(Status.NotFound());
describe("get", () => {
it("should return 404 when wrong adminKey is used", async () => {
const response = await manager.get("test", "wrongKey");
expect(response).toEqual(HttpResponse.NotFound());
});

it('should return 404 when account is not found', async () => {
const response = await server.get('test', 'adminKey');
expect(response).toEqual(Status.NotFound());
it("should return 404 when account is not found", async () => {
const response = await manager.get("test", "adminKey");
expect(response).toEqual(HttpResponse.NotFound());
});

it('should return account and 200 when correct adminKey is used and account exists', async () => {
const account: Account = { name: 'test', apiKey: 'key', isDeleted: false };
await db.save('user', 'test', account);
it("should return account and 200 when correct adminKey is used and account exists", async () => {
const account: Account = { name: "test", apiKey: "key", isDeleted: false };
await accountRepo.save(account);

const response = await server.get('test', 'adminKey');
expect(response).toEqual(Status.Ok(account));
const response = await manager.get("test", "adminKey");
expect(response).toEqual(HttpResponse.Ok(account));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
"test": "jest --passWithNoTests --runInBand --detectOpenHandles --verbose"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "3.254.0"
"@aws-sdk/client-dynamodb": "3.254.0",
"@polywrap/result": "^0.10.1",
"serverless-utils": "file:../../../../utils/serverless-utils"
},
"devDependencies": {
"@types/node": "16.11.11",
"@types/jest": "26.0.8",
"@types/node": "16.11.11",
"jest": "27.1.1",
"ts-jest": "27.0.5",
"ts-node": "10.3.0",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading