OpenAuth_server/src/testdata.ts

64 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-11-06 19:48:50 +00:00
import User, { Gender } from "./models/user";
import Client from "./models/client";
import { Logging } from "@hibas123/nodelogging";
import RegCode from "./models/regcodes";
import * as moment from "moment";
import Permission from "./models/permissions";
import { ObjectID } from "bson";
import DB from "./database";
export default async function TestData() {
await DB.db.dropDatabase();
let u = await User.findOne({ username: "test" });
if (!u) {
Logging.log("Adding test user");
u = User.new({
username: "test",
birthday: new Date(),
gender: Gender.male,
name: "Test Test",
password: "125d6d03b32c84d492747f79cf0bf6e179d287f341384eb5d6d3197525ad6be8e6df0116032935698f99a09e265073d1d6c32c274591bf1d0a20ad67cba921bc",
salt: "test",
admin: true
})
await User.save(u);
}
let c = await Client.findOne({ client_id: "test001" });
if (!c) {
Logging.log("Adding test client")
c = Client.new({
client_id: "test001",
client_secret: "test001",
internal: true,
maintainer: u._id,
name: "Test Client",
website: "http://example.com",
redirect_url: "http://example.com"
})
await Client.save(c);
}
2018-11-06 20:27:07 +00:00
let perm = await Permission.findOne({ id: 0 });
2018-11-06 19:48:50 +00:00
if (!perm) {
Logging.log("Adding test permission")
perm = Permission.new({
_id: new ObjectID("507f1f77bcf86cd799439011"),
name: "TestPerm",
description: "Permission just for testing purposes",
client: c._id
})
Permission.save(perm);
}
2018-11-06 20:27:07 +00:00
let r = await RegCode.findOne({ token: "test" });
2018-11-06 19:48:50 +00:00
if (!r) {
Logging.log("Adding test reg_code")
r = RegCode.new({
token: "test",
valid: true,
validTill: moment().add("1", "year").toDate()
})
await RegCode.save(r);
}
}