Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Added improved validation and tests to backend models (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiancross committed Feb 26, 2021
1 parent 9192e50 commit 3237a76
Show file tree
Hide file tree
Showing 17 changed files with 337 additions and 120 deletions.
3 changes: 2 additions & 1 deletion packages/backend-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"ts-node/register"
],
"environmentVariables": {
"UNIFED_INTERNAL_REFERENCE": "this"
"UNIFED_INTERNAL_REFERENCE": "this",
"UNIFED_SITE_HOST": "localhost:8080"
}
}
}
26 changes: 19 additions & 7 deletions packages/backend-core/src/models/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@
*/

import test from "ava";
import { validate } from "class-validator";
import { Base } from "../base";

class MockBase extends Base {}

test("id getter", (t) => {
test("Invalid ID", async (t) => {
const base = new MockBase();
t.is(base.id, undefined);
base.host = "foo.edu";

const result = await validate(base);

t.is(result.length, 1);
});

test("id setter", (t) => {
test("Valid", async (t) => {
const base = new MockBase();
base.id = "someid";
t.is(base.id, "someid");
base.id = "foo";
base.host = "foo.edu";

const result = await validate(base);

t.is(result.length, 0);
t.is(base.id, "foo");
t.is(base.host, "foo.edu");
});

test("toJSON", (t) => {
const base = new MockBase();
base.id = "someid";
t.deepEqual(base.toJSON(), { id: "someid" });
base.id = "foo";

t.deepEqual(base.toJSON(), { id: "foo" });
});
62 changes: 56 additions & 6 deletions packages/backend-core/src/models/__tests__/community.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,67 @@
*/

import test from "ava";
import { validate } from "class-validator";
import { Community } from "../community";

test("Title too short", async (t) => {
const community = new Community();
community.id = "foo";
community.title = "";
community.description = "baz";
community.admins = [];

const result = await validate(community);

t.is(result.length, 1);
});

test("Title too long", async (t) => {
const community = new Community();
community.id = "foo";
community.title = "a".repeat(64 + 1);
community.description = "baz";
community.admins = [];

const result = await validate(community);

t.is(result.length, 1);
});

test("Description too short", async (t) => {
const community = new Community();
community.id = "foo";
community.title = "bar";
community.description = "";
community.admins = [];

const result = await validate(community);

t.is(result.length, 1);
});

test("Description too long", async (t) => {
const community = new Community();
community.id = "foo";
community.title = "bar";
community.description = "a".repeat(10 * 1024 + 1);
community.admins = [];

const result = await validate(community);

t.is(result.length, 1);
});

test("toJSON", (t) => {
const community = new Community();
community.id = "someid";
community.title = "Some title";
community.description = "Some description";
community.id = "foo";
community.title = "bar";
community.description = "baz";

t.deepEqual(community.toJSON(), {
id: "someid",
title: "Some title",
description: "Some description",
id: "foo",
title: "bar",
description: "baz",
admins: [],
});
});
135 changes: 92 additions & 43 deletions packages/backend-core/src/models/__tests__/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,115 @@
*/

import test from "ava";
import { validate } from "class-validator";
import { Post } from "../post";
import { RemoteReference } from "../remote-reference";

test("toJSON - community", (t) => {
const getPost = () => {
const post = new Post();
post.id = "someid";
post.community = "somecomm";
post.title = "A title";
post.id = "foo";
post.community = "bar";
post.parentPost = "baz";
post.title = "bat";
post.contentType = "markdown";
post.body = "This is a post body";
post.body = "bax";

post.author = new RemoteReference();
post.author.id = "someusername";
post.author.host = "somehost:420";
post.author.id = "mat";
post.author.host = "foo.edu";

post.approved = true;
post.createdAt = new Date("2020-11-13T09:25:10+00:00");
post.updatedAt = new Date("2020-11-13T09:26:07+00:00");

t.like(post.toJSON(), {
id: "someid",
parentPost: null,
community: "somecomm",
title: "A title",
contentType: "markdown",
body: "This is a post body",
children: [],
author: {
id: "someusername",
host: "somehost:420",
},
created: 1605259510,
modified: 1605259567,
});
return post;
};

test("Valid", async (t) => {
const post = getPost();

const result = await validate(post);

t.is(result.length, 0);
});

test("toJSON - post", (t) => {
const post = new Post();
post.id = "someid";
post.community = "somecomm";
post.parentPost = "parentpost";
post.title = "A title";
post.contentType = "markdown";
post.body = "This is a post body";
post.author = new RemoteReference();
post.author.id = "someusername";
post.author.host = "somehost:420";
post.approved = true;
post.createdAt = new Date("2020-11-13T09:25:10+00:00");
post.updatedAt = new Date("2020-11-13T09:26:07+00:00");
test("Invalid community", async (t) => {
const post = getPost();
post.community = undefined;

const result = await validate(post);

t.is(result.length, 1);
});

test("Valid parentPost", async (t) => {
const post = getPost();
post.parentPost = undefined;

const result = await validate(post);

t.is(result.length, 0);
});

test("Title too short", async (t) => {
const post = getPost();
post.title = "";

const result = await validate(post);

t.is(result.length, 1);
});

test("Title too long", async (t) => {
const post = getPost();
post.title = "a".repeat(128 + 1);

const result = await validate(post);

t.is(result.length, 1);
});

test("Invalid contentType", async (t) => {
const post = getPost();
post.contentType = "bar";

const result = await validate(post);

t.is(result.length, 1);
});

test("Body too short", async (t) => {
const post = getPost();
post.body = "";

const result = await validate(post);

t.is(result.length, 1);
});

test("Body too long", async (t) => {
const post = getPost();
post.body = "a".repeat(1024 * 1024 * 500 + 1);

const result = await validate(post);

t.is(result.length, 1);
});

test("toJSON", (t) => {
const post = getPost();

t.like(post.toJSON(), {
id: "someid",
parentPost: "parentpost",
community: "somecomm",
title: "A title",
id: "foo",
community: "bar",
parentPost: "baz",
title: "bat",
contentType: "markdown",
body: "This is a post body",
body: "bax",
children: [],
author: {
id: "someusername",
host: "somehost:420",
id: "mat",
host: "foo.edu",
},
created: 1605259510,
modified: 1605259567,
Expand Down
41 changes: 41 additions & 0 deletions packages/backend-core/src/models/__tests__/public-user-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* CS3099 Group A3
*/

import test from "ava";
import { validate } from "class-validator";
import { PublicUser, UserProfile } from "../public-user";

test("Invalid username", async (t) => {
const user = new PublicUser();
user.profile = new UserProfile();
user.profile.name = "foo";

const result = await validate(user);

t.is(result.length, 1);
});

test("Invalid profile", async (t) => {
const user = new PublicUser();
user.username = "foo";

const result = await validate(user);

t.is(result.length, 1);
});

test("toJSON", (t) => {
const user = new PublicUser();
user.username = "foo";

user.profile = new UserProfile();
user.profile.name = "bar";

t.deepEqual(user.toJSON(), {
username: "foo",
profile: {
name: "bar",
},
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
import test from "ava";
import { RemoteReference } from "../remote-reference";

test("toJSON", (t) => {
test("Internal reference", async (t) => {
const reference = new RemoteReference();
reference.id = "someid";
reference.host = "localhost:8080";
reference.id = "foo";
reference.host = "this";

t.deepEqual(reference.toJSON(), {
id: "someid",
id: "foo",
host: "localhost:8080",
});
});

test("toJSON", (t) => {
const reference = new RemoteReference();
reference.id = "foo";
reference.host = "bar";

t.deepEqual(reference.toJSON(), {
id: "foo",
host: "bar",
});
});
2 changes: 1 addition & 1 deletion packages/backend-core/src/models/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import test from "ava";
import { User } from "../user";

test("New user", (t) => {
test("Load", (t) => {
new User();
t.pass();
});
6 changes: 1 addition & 5 deletions packages/backend-core/src/models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ export type EntityID = string;
export abstract class Base extends defaultClasses.TimeStamps {
@IsNotEmpty()
@IsString()
@Property({ default: uuidv4 })
@Property({ default: uuidv4, required: true })
_id!: EntityID;

@IsNotEmpty()
@IsString()
@Field()
host?: string;

@IsNotEmpty()
@IsString()
@Field(() => ID)
get id(): EntityID {
return this._id;
Expand Down
Loading

0 comments on commit 3237a76

Please sign in to comment.