-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
152 lines (139 loc) · 3.7 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"use strict";
const fastify = require("fastify")();
const tap = require("tap");
const fastifyMongoose = require("./index");
class PostClass {
get fullTitle() {
return `Title: ${this.title}`;
}
}
tap.test("fastify.mongoose should exist", async test => {
test.plan(12);
fastify.register(fastifyMongoose.plugin, {
uri: "mongodb://localhost:27017/test",
settings: {
useNewUrlParser: true,
config: {
autoIndex: true
}
},
models: [
{
name: "posts",
alias: "Post",
schema: {
title: {
type: String,
required: true
},
content: {
type: String,
required: true
},
author: {
type: "ObjectId",
ref: "Account",
validateExistance: true
}
},
class: PostClass
},
{
name: "accounts",
alias: "Account",
schema: {
username: {
type: String
},
password: {
type: String,
select: false,
required: true
},
email: {
type: String,
unique: true,
required: true,
validate: {
validator: v => {
// Super simple email regex: https://stackoverflow.com/a/4964763/7028187
return /^.+@.{2,}\..{2,}$/.test(v);
},
message: props => `${props.value} is not a valid email!`
}
},
posts: [
{
type: "ObjectId",
ref: "Post",
validateExistance: true
}
],
createdAtUTC: {
type: Date,
required: true
}
}
}
],
useNameAndAlias: true
});
fastify.post("/", async ({ body }, reply) => {
const { username, password, email } = body;
const createdAtUTC = new Date();
const account = new fastify.mongoose.Account({
username,
password,
email,
createdAtUTC
});
await account.save();
return await fastify.mongoose.Account.findOne({ email });
});
fastify.patch("/", async ({ body }, reply) => {
const { title, content, author } = body;
const post = new fastify.mongoose.Post({
title,
content,
author
});
await post.save();
const foundPost = await fastify.mongoose.Post.findById(post._id);
return foundPost.toObject({ virtuals: true });
});
try {
await fastify.ready();
test.ok(fastify.mongoose.instance);
test.ok(fastify.mongoose.Account);
const testEmail = `${(+new Date()).toString(36).slice(-5)}@example.com`;
let { statusCode, payload } = await fastify.inject({
method: "POST",
url: "/",
payload: {
username: "test",
password: "pass",
email: testEmail
}
});
const { username, password, email, _id } = JSON.parse(payload);
test.strictEqual(statusCode, 200);
test.strictEqual(username, "test");
test.strictEqual(password, undefined);
test.strictEqual(email, testEmail);
({ statusCode, payload } = await fastify.inject({
method: "PATCH",
url: "/",
payload: { author: _id, title: "Hello World", content: "foo bar" }
}));
const { title, fullTitle, content, author } = JSON.parse(payload);
test.strictEqual(fullTitle, "Title: Hello World");
test.strictEqual(title, "Hello World");
test.strictEqual(content, "foo bar");
test.strictEqual(author, _id);
test.ok(fastifyMongoose.decorator().instance);
test.ok(fastifyMongoose.decorator().Account);
} catch (e) {
test.fail("Fastify threw", e);
}
fastify.close();
});