mirror of https://github.com/MaxLeiter/Drift
Add some (WIP) tests
parent
b64281b1ac
commit
86e323fbca
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,27 @@
|
||||
import byteToMB from "@lib/byte-to-mb"
|
||||
|
||||
describe("byteToMB", () => {
|
||||
it("converts 0 bytes to 0 MB", () => {
|
||||
expect(byteToMB(0)).toEqual(0)
|
||||
})
|
||||
|
||||
it("converts 1024 bytes to 0.001 MB", () => {
|
||||
expect(byteToMB(1024)).toBeCloseTo(0.001)
|
||||
})
|
||||
|
||||
it("converts 1048576 bytes to 1 MB", () => {
|
||||
expect(byteToMB(1048576)).toEqual(1)
|
||||
})
|
||||
|
||||
it("converts 3145728 bytes to 3 MB", () => {
|
||||
expect(byteToMB(3145728)).toEqual(3)
|
||||
})
|
||||
|
||||
it("returns NaN when given a negative number", () => {
|
||||
expect(byteToMB(-1)).toBeNaN()
|
||||
})
|
||||
|
||||
it("returns NaN when given a non-numeric value", () => {
|
||||
expect(byteToMB("test" as any)).toBeNaN()
|
||||
})
|
||||
})
|
@ -1,4 +1,9 @@
|
||||
const byteToMB = (bytes: number) =>
|
||||
Math.round((bytes / 1024 / 1024) * 100) / 100
|
||||
const byteToMB = (bytes: number) => {
|
||||
if (bytes < 0) {
|
||||
return NaN
|
||||
}
|
||||
|
||||
return Math.round((bytes / 1024 / 1024) * 100) / 100
|
||||
}
|
||||
|
||||
export default byteToMB
|
||||
|
@ -0,0 +1,54 @@
|
||||
import { verifyApiUser } from "../verify-api-user"
|
||||
import { prismaMock } from "src/test/prisma.mock"
|
||||
import "src/test/react.mock"
|
||||
import { User } from "@prisma/client"
|
||||
|
||||
describe("verifyApiUser", () => {
|
||||
const mockReq = {} as any
|
||||
const mockRes = {} as any
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("returns null if there is no userId param or auth token", async () => {
|
||||
mockReq.query = {}
|
||||
mockReq.headers = {}
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("returns the user id if there is a userId param and it matches the authenticated user id", async () => {
|
||||
mockReq.query = { userId: "123" }
|
||||
const mockUser = { id: "123" }
|
||||
const mockGetCurrentUser = prismaMock.user.findUnique.mockResolvedValue(
|
||||
mockUser as User
|
||||
)
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(mockGetCurrentUser).toHaveBeenCalled()
|
||||
expect(result).toEqual("123")
|
||||
})
|
||||
|
||||
it("returns null if there is a userId param but it doesn't match the authenticated user id", async () => {
|
||||
mockReq.query = { userId: "123" }
|
||||
const mockUser = { id: "456" }
|
||||
const mockGetCurrentUser = jest.fn().mockResolvedValue(mockUser)
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(mockGetCurrentUser).toHaveBeenCalled()
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("returns the user id if there is an auth token and it is valid", async () => {
|
||||
mockReq.query = {}
|
||||
mockReq.headers.authorization = "Bearer mytoken"
|
||||
const mockUser = { userId: "123", expiresAt: new Date(Date.now() + 10000) }
|
||||
const mockFindUnique = jest.fn().mockResolvedValue(mockUser)
|
||||
const mockPrisma = { apiToken: { findUnique: mockFindUnique } } as any
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(mockFindUnique).toHaveBeenCalledWith({
|
||||
where: { token: "mytoken" },
|
||||
select: { userId: true, expiresAt: true }
|
||||
})
|
||||
expect(result).toEqual("123")
|
||||
})
|
||||
})
|
@ -0,0 +1,29 @@
|
||||
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
|
||||
|
||||
# Optional if you use Vercel (defaults to VERCEL_URL).
|
||||
# Necessary in development unless you use the vercel CLI (`vc dev`)
|
||||
DRIFT_URL=http://localhost:3000
|
||||
|
||||
# Optional: The first user becomes an admin. Defaults to false
|
||||
ENABLE_ADMIN=false
|
||||
|
||||
# Required: Next auth secret is a required valid JWT secret. You can generate one with `openssl rand -hex 32`
|
||||
NEXTAUTH_SECRET=7f8b8b5c5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5f5
|
||||
|
||||
# Required: but unnecessary if you use a supported host like Vercel
|
||||
NEXTAUTH_URL=http://localhost:3000
|
||||
|
||||
# Optional: for locking your instance
|
||||
REGISTRATION_PASSWORD=
|
||||
|
||||
# Optional: for if you want GitHub oauth. Currently incompatible with the registration password
|
||||
GITHUB_CLIENT_ID=
|
||||
GITHUB_CLIENT_SECRET=
|
||||
|
||||
# Optional: if you want to support credential auth (username/password, supports registration password)
|
||||
# Defaults to true
|
||||
CREDENTIAL_AUTH=true
|
||||
|
||||
# Optional:
|
||||
WELCOME_CONTENT=
|
||||
WELCOME_TITLE=
|
@ -0,0 +1,16 @@
|
||||
import { PrismaClient } from "@prisma/client"
|
||||
|
||||
import { mockDeep, mockReset, DeepMockProxy } from "jest-mock-extended"
|
||||
|
||||
import { prisma } from "@lib/server/prisma"
|
||||
|
||||
jest.mock("@lib/server/prisma", () => ({
|
||||
__esModule: true,
|
||||
default: mockDeep<PrismaClient>()
|
||||
}))
|
||||
|
||||
beforeEach(() => {
|
||||
mockReset(prismaMock)
|
||||
})
|
||||
|
||||
export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>
|
@ -0,0 +1,12 @@
|
||||
jest.mock("react", () => {
|
||||
const ActualReact = jest.requireActual("react")
|
||||
|
||||
return {
|
||||
...ActualReact,
|
||||
cache: jest.fn((fn) => {
|
||||
return fn()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export {}
|
@ -0,0 +1,4 @@
|
||||
import * as dotenv from "dotenv"
|
||||
import * as path from "path"
|
||||
|
||||
dotenv.config({ path: path.resolve(__dirname, "./.env.test") })
|
@ -1,44 +1,61 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"plugins": [
|
||||
{
|
||||
"name": "typescript-plugin-css-modules"
|
||||
},
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"target": "es2020",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictBindCallApply": true,
|
||||
"strictPropertyInitialization": true,
|
||||
"noImplicitThis": true,
|
||||
"alwaysStrict": true,
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@components/*": ["src/app/components/*"],
|
||||
"@lib/*": ["src/lib/*"],
|
||||
"@styles/*": ["src/app/styles/*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
"compilerOptions": {
|
||||
"plugins": [
|
||||
{
|
||||
"name": "typescript-plugin-css-modules"
|
||||
},
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"target": "es2020",
|
||||
"lib": [
|
||||
"dom",
|
||||
"dom.iterable",
|
||||
"esnext"
|
||||
],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictBindCallApply": true,
|
||||
"strictPropertyInitialization": true,
|
||||
"noImplicitThis": true,
|
||||
"alwaysStrict": true,
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@components/*": [
|
||||
"src/app/components/*"
|
||||
],
|
||||
"@lib/*": [
|
||||
"src/lib/*"
|
||||
],
|
||||
"@styles/*": [
|
||||
"src/app/styles/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"next-env.d.ts",
|
||||
"**/*.ts",
|
||||
"**/*.tsx",
|
||||
".next/types/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue