Created new tests, added SWC for faster compiling.

This commit is contained in:
Hackntosh 2023-06-24 17:38:34 -03:00
parent 4b301eb985
commit b21a832c13
20 changed files with 9848 additions and 996 deletions

3
.gitignore vendored
View file

@ -5,4 +5,5 @@ prisma/*.db
.DS_Store
prisma/migrations/dev
client
dist
dist
pnpm-lock.yaml

21
.swcrc Normal file
View file

@ -0,0 +1,21 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true,
"dynamicImport": true
},
"target": "es2020",
"baseUrl": "./"
},
"exclude": [
"tests/",
"@types/",
"interfaces/"
],
"module": {
"type": "commonjs"
},
"minify": true
}

View file

@ -1,4 +1,4 @@
FROM node:16 as builder
FROM node:18 as builder
# Create app dir
WORKDIR /app
@ -6,7 +6,7 @@ WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma/
RUN npm install pm2 -g
RUN npm install -D @swc/cli @swc/core
RUN npm install
COPY . .
@ -14,10 +14,12 @@ COPY . .
RUN npm run build
# Stage 2
FROM node:16
FROM node:18
WORKDIR /app
RUN npm i pm2 -g
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/prisma ./prisma/
COPY --from=builder /app/dist ./dist/

View file

@ -1 +1,19 @@
# Social Media - Unnamed
# Social Media - Unnamed
A simple social media created with React Native and Express.
## To-do - Backend
- Create/update/delete Posts
- Password recuperation
- Two step verification
- Able to choose a profile picture
- Following/unfollowing features
- Like posts
- Probably pinned posts
- Authentication ✅
- Add more verification (like, if the password is too short)
## License
[MIT](https://choosealicense.com/licenses/mit/)

View file

@ -1,5 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
testEnvironment: 'node',
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest'
}
}

10601
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,7 @@
"version": "0.0.1",
"description": "A social media",
"scripts": {
"build": "tsc --build",
"build": "swc src -d dist",
"dev:start": "ts-node-dev --transpile-only --respawn src/server.ts",
"docker": "docker compose up -d",
"docker:build": "docker build -t api . && docker compose up -d",
@ -15,13 +15,9 @@
"prisma:generate": "npx prisma generate",
"prisma:seed": "prisma db seed",
"prisma:studio": "npx prisma studio",
"prod:start": "nodemon dist/src/server.js",
"prod:start": "pm2-runtime start dist/server.js",
"test": "jest"
},
"aliases": {
"@services": "./src/services",
"@db": "./prisma"
},
"ts-standard": {
"project": "tsconfig.json",
"ignore": [
@ -32,6 +28,9 @@
"author": "Cookie",
"license": "MIT",
"devDependencies": {
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.66",
"@swc/jest": "^0.2.26",
"@types/bcrypt": "^5.0.0",
"@types/compression": "^1.7.2",
"@types/dotenv": "^8.2.0",

View file

@ -1,11 +0,0 @@
FROM node:14
RUN openssl version -v
RUN uname -a
WORKDIR /app
RUN npm install -g prisma --unsafe-perm
ADD . ./prisma/
CMD ["prisma", "migrate", "up"]

View file

@ -1,28 +0,0 @@
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main () {
await prisma.user.deleteMany({
where: {
username: 'cookie_'
}
})
console.log('Seeding database...')
await prisma.user.create({
data: {
displayName: 'Cookie_',
username: 'cookie_',
email: 'cookie@cookie.com',
password: '$2a$10$vBx5LkpZNMC3j1bHopUcp.ZcsMt5xfWlUJJGYIb4511aNccnHmMqi' // cookie
}
})
}
main()
.catch((e) => console.error(e))
.finally(async () => {
await prisma.$disconnect()
})

View file

@ -1,7 +1,5 @@
import { PrismaClient } from '@prisma/client'
// Cria uma instância do cliente Prisma
const prisma = new PrismaClient()
// Exporta o cliente Prisma para ser usado em outros módulos
export default prisma

View file

@ -1,5 +1,5 @@
import { verify } from 'jsonwebtoken'
import prisma from '../../prisma/client'
import prisma from '../db'
import { Response, Request, NextFunction } from 'express'
import jwtPayload from '../interfaces/jwt'

View file

@ -1,9 +1,9 @@
import prisma from '../../../prisma/client'
import prisma from '../../db'
async function createPostService (content: string, authorId: string) {
async function createPostService (content: string, authorId: string): Promise<Object | Error> {
const user = await prisma.user.findFirst({ where: { id: authorId } })
if (user == null) {
if (user === null) {
return new Error('This user doesn\'t exists')
}

View file

@ -1,6 +1,6 @@
import * as bcrypt from 'bcrypt'
import jsonwebtoken from 'jsonwebtoken'
import prisma from '../../../prisma/client'
import prisma from '../../db'
async function userAuthService (email: string, password: string): Promise<Object | Error> {
const user = await prisma.user.findFirst({

View file

@ -1,4 +1,4 @@
import prisma from '../../../prisma/client'
import prisma from '../../db'
async function userInfoService (id: string): Promise<Object> {
const user = await prisma.user.findFirst({

View file

@ -1,6 +1,6 @@
import * as bcrypt from 'bcrypt'
import validator from 'validator'
import prisma from '../../../prisma/client'
import prisma from '../../db'
async function userSignupService (username: string, email: string, password: string): Promise<Object | Error> {
if (username === undefined || email === undefined || password === undefined) {

View file

@ -0,0 +1,55 @@
import prisma from '../../db'
import app from '../../app'
import request from 'supertest'
let token = ''
describe('POST /post/create', () => {
beforeAll(async () => {
await request(app).post('/user/create').send({
username: 'dummmyuser7',
email: 'random1@email.com',
password: 'pass'
})
const response = await request(app).post('/user/auth').send({
email: 'random1@email.com',
password: 'pass'
}).expect(200)
token = response.body.token
})
afterAll(async () => {
await prisma.post.deleteMany({
where: {
content: '4764ba063310b6f8bab31e8348b2188a'
}
})
await prisma.user.deleteMany({
where: {
username: 'dummmyuser7'
}
})
await prisma.$disconnect()
})
it('should respond with 200 status code if the user send the token and the content', async () => {
const response = await request(app).post('/post/create').send({
content: '4764ba063310b6f8bab31e8348b2188a'
}).set('Authorization', `Bearer ${token}`).expect(200)
expect(response.body).toHaveProperty('id')
expect(response.body).toHaveProperty('content')
expect(response.body).toHaveProperty('authorId')
expect(response.body).toHaveProperty('createdAt')
expect(response.body).toHaveProperty('updatedAt')
})
it('should respond with 400 status code if the user send no token', async () => {
const response = await request(app).post('/post/create').expect(401)
expect(response.body).toHaveProperty('error')
})
})

View file

@ -1,12 +1,12 @@
import request from 'supertest'
import prisma from '../../../prisma/client'
import prisma from '../../db'
import app from '../../app'
describe('POST /user/auth', () => {
beforeAll(async () => {
await prisma.user.create({
data: {
username: 'test',
username: 'dummmyuser6',
email: 'test@test.com',
password: 'pass'
}
@ -16,7 +16,7 @@ describe('POST /user/auth', () => {
afterAll(async () => {
await prisma.user.deleteMany({
where: {
username: 'test'
username: 'dummmyuser6'
}
})
await prisma.$disconnect()

View file

@ -1,7 +1,46 @@
import prisma from '../../db'
import app from '../../app'
import request from 'supertest'
describe('POST /user/info', () => {
it('TODO', async () => {
let token = ''
describe('POST /user/info', () => {
beforeAll(async () => {
await request(app).post('/user/create').send({
username: 'dummmyuser5',
email: 'random3@email.com',
password: 'pass'
})
const response = await request(app).post('/user/auth').send({
email: 'random3@email.com',
password: 'pass'
}).expect(200)
token = response.body.token
})
afterAll(async () => {
await prisma.user.deleteMany({
where: {
username: 'dummmyuser5'
}
})
await prisma.$disconnect()
})
it('should respond with 200 status code and return the user data', async () => {
const response = await request(app).get('/user/info').set('Authorization', `Bearer ${token}`).expect(200)
expect(response.body).toHaveProperty('id')
expect(response.body).toHaveProperty('displayName')
expect(response.body).toHaveProperty('username')
expect(response.body).toHaveProperty('createdAt')
})
it('should respond with 400 status code if the user send no token', async () => {
const response = await request(app).get('/user/info').expect(401)
expect(response.body).toHaveProperty('error')
})
})

View file

@ -1,9 +1,9 @@
import request from 'supertest'
import prisma from '../../../prisma/client'
import prisma from '../../db'
import app from '../../app'
const mockUser = {
username: 'username11',
username: 'dummmyuser1',
email: 'random@email.com',
password: 'totallysafepass'
}
@ -28,14 +28,14 @@ describe('POST /user/create', () => {
it('should respond with a 400 status code for an existing username', async () => {
await prisma.user.create({
data: {
username: 'username12',
username: 'dummmyuser2',
email: 'user@email.com',
password: 'reallystrongpass'
}
})
const response = await request(app).post('/user/create').send({
username: 'username12',
username: 'dummmyuser2',
email: 'user1@email.com',
password: 'reallystrongpass'
}).expect(400)
@ -46,14 +46,14 @@ describe('POST /user/create', () => {
it('should respond with a 400 status code for an existing email', async () => {
await prisma.user.create({
data: {
username: 'username13',
username: 'dummmyuser3',
email: 'user13@email.com',
password: '1234'
}
})
const response = await request(app).post('/user/create').send({
username: 'heythatscool',
username: 'dummmyuser4',
email: 'user13@email.com',
password: '12345'
}).expect(400)
@ -69,7 +69,7 @@ describe('POST /user/create', () => {
})
afterAll(async () => {
const usersToDelete = ['username11', 'username12', 'username13']
const usersToDelete = ['dummmyuser1', 'dummmyuser2', 'dummmyuser3', 'dummmyuser4']
await prisma.user.deleteMany({
where: {

View file

@ -106,6 +106,6 @@
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
}
}