diff options
Diffstat (limited to 'api/src/test')
-rw-r--r-- | api/src/test/jwt.ts | 37 | ||||
-rw-r--r-- | api/src/test/jwt2.ts | 13 | ||||
-rw-r--r-- | api/src/test/mongo_test.ts | 40 | ||||
-rw-r--r-- | api/src/test/password_test.ts | 12 | ||||
-rw-r--r-- | api/src/test/server_benchmark.ts | 39 | ||||
-rw-r--r-- | api/src/test/test.ts | 3 |
6 files changed, 144 insertions, 0 deletions
diff --git a/api/src/test/jwt.ts b/api/src/test/jwt.ts new file mode 100644 index 00000000..bdad513b --- /dev/null +++ b/api/src/test/jwt.ts @@ -0,0 +1,37 @@ +const jwa = require("jwa"); + +var STR64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".split(""); + +function base64url(string: string, encoding: string) { + // @ts-ignore + return Buffer.from(string, encoding).toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); +} + +function to64String(input: number, current = ""): string { + if (input < 0 && current.length == 0) { + input = input * -1; + } + var modify = input % 64; + var remain = Math.floor(input / 64); + var result = STR64[modify] + current; + return remain <= 0 ? result : to64String(remain, result); +} + +function to64Parse(input: string) { + var result = 0; + var toProc = input.split(""); + var e; + for (e in toProc) { + result = result * 64 + STR64.indexOf(toProc[e]); + } + return result; +} + +// @ts-ignore +const start = `${base64url("311129357362135041")}.${to64String(Date.now())}`; +const signature = jwa("HS256").sign(start, `test`); +const token = `${start}.${signature}`; +console.log(token); + +// MzExMTI5MzU3MzYyMTM1MDQx.XdQb_rA.907VgF60kocnOTl32MSUWGSSzbAytQ0jbt36KjLaxuY +// MzExMTI5MzU3MzYyMTM1MDQx.XdQbaPy.4vGx4L7IuFJGsRe6IL3BeybLIvbx4Vauvx12pwNsy2U diff --git a/api/src/test/jwt2.ts b/api/src/test/jwt2.ts new file mode 100644 index 00000000..e231233d --- /dev/null +++ b/api/src/test/jwt2.ts @@ -0,0 +1,13 @@ +import jwt from "jsonwebtoken"; + +const algorithm = "HS256"; +const iat = Math.floor(Date.now() / 1000); + +// @ts-ignore +const token = jwt.sign({ id: "311129357362135041" }, "secret", { + algorithm, +}); +console.log(token); + +const decoded = jwt.verify(token, "secret", { algorithms: [algorithm] }); +console.log(decoded); diff --git a/api/src/test/mongo_test.ts b/api/src/test/mongo_test.ts new file mode 100644 index 00000000..44b04c5b --- /dev/null +++ b/api/src/test/mongo_test.ts @@ -0,0 +1,40 @@ +import mongoose, { Schema, Types } from "mongoose"; +require("mongoose-long")(mongoose); + +const userSchema = new Schema({ + id: String, +}); + +const messageSchema = new Schema({ + id: String, + content: String, +}); +const message = mongoose.model("message", messageSchema, "messages"); +const user = mongoose.model("user", userSchema, "users"); + +messageSchema.virtual("u", { + ref: user, + localField: "id", + foreignField: "id", + justOne: true, +}); + +messageSchema.set("toObject", { virtuals: true }); +messageSchema.set("toJSON", { virtuals: true }); + +async function main() { + const conn = await mongoose.connect("mongodb://localhost:27017/lambert?readPreference=secondaryPreferred", { + useNewUrlParser: true, + useUnifiedTopology: false, + }); + console.log("connected"); + + // const u = await new user({ name: "test" }).save(); + // await new message({ user: u._id, content: "test" }).save(); + + const test = await message.findOne({}).populate("u").exec(); + // @ts-ignore + console.log(test?.toJSON()); +} + +main(); diff --git a/api/src/test/password_test.ts b/api/src/test/password_test.ts new file mode 100644 index 00000000..59d36621 --- /dev/null +++ b/api/src/test/password_test.ts @@ -0,0 +1,12 @@ +import { check } from "./../util/passwordStrength"; + +console.log(check("123456789012345")); +// -> 0.25 +console.log(check("ABCDEFGHIJKLMOPQ")); +// -> 0.25 +console.log(check("ABC123___...123")); +// -> +console.log(check("")); +// -> +// console.log(check("")); +// // -> diff --git a/api/src/test/server_benchmark.ts b/api/src/test/server_benchmark.ts new file mode 100644 index 00000000..c582ee89 --- /dev/null +++ b/api/src/test/server_benchmark.ts @@ -0,0 +1,39 @@ +// @ts-nocheck +import "missing-native-js-functions"; +import { config } from "dotenv"; +config(); +import { DiscordServer } from "../Server"; +import fetch from "node-fetch"; +import { promises } from "fs"; +const count = 100; + +async function main() { + const server = new DiscordServer({ port: 3000 }); + await server.start(); + + const tasks = []; + for (let i = 0; i < count; i++) { + tasks.push(test()); + } + + await Promise.all(tasks); + + console.log("logging in 5secs"); + setTimeout(async () => { + await test(); + + process.exit(); + }, 5000); +} +main(); + +async function test() { + const res = await fetch("http://localhost:3000/api/v8/guilds/813524615463698433/members/813524464300982272", { + headers: { + authorization: + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjgxMzUyNDQ2NDMwMDk4MjI3MiIsImlhdCI6MTYxNDAyOTc0Nn0.6WQiU4D5HHRi3sliHOQe1hsW-hZTEttvdtZuNIdviNI", + }, + }); + + return await res.text(); +} diff --git a/api/src/test/test.ts b/api/src/test/test.ts new file mode 100644 index 00000000..478edcc4 --- /dev/null +++ b/api/src/test/test.ts @@ -0,0 +1,3 @@ +import { Snowflake } from "@fosscord/server-util"; + +console.log(Snowflake.deconstruct("0")); |