Guest User

Untitled

a guest
Mar 5th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@nestjs/common';
  2. import { PrismaService } from '../prisma/prisma.service';
  3. import { EditUserDto } from './dto';
  4.  
  5. @Injectable()
  6. export class UserService {
  7.   constructor(private prisma: PrismaService) {}
  8.   async editUser(userId: number, dto: EditUserDto) {
  9.     const user = await this.prisma.user.update({
  10.       where: {
  11.         id: userId,
  12.       },
  13.       data: {
  14.         ...dto,
  15.       },
  16.     });
  17.  
  18.     delete user.hash;
  19.     return user;
  20.   }
  21. }
  22.  
  23. ============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
  24.  
  25.  
  26. //HERE IS test file im running:
  27.  
  28. import { Test } from '@nestjs/testing';
  29. import { INestApplication, ValidationPipe } from '@nestjs/common';
  30. import { AppModule } from '../src/app.module';
  31. import { PrismaService } from '../src/prisma/prisma.service';
  32. import * as pactum from 'pactum';
  33. import { AuthDto } from 'src/auth/dto';
  34. import { EditUserDto } from 'src/user/dto';
  35.  
  36. describe('App e2e', () => {
  37.   let app: INestApplication;
  38.   let prisma: PrismaService;
  39.  
  40.   beforeAll(async () => {
  41.     const moduleRef = await Test.createTestingModule({
  42.       imports: [AppModule],
  43.     }).compile();
  44.     app = moduleRef.createNestApplication();
  45.     app.useGlobalPipes(
  46.       new ValidationPipe({
  47.         whitelist: true,
  48.       }),
  49.     );
  50.     await app.init();
  51.     await app.listen(3333);
  52.  
  53.     prisma = app.get(PrismaService);
  54.     await prisma.cleanDb();
  55.     pactum.request.setBaseUrl('http://localhost:3333');
  56.   });
  57.  
  58.   afterAll(() => {
  59.     app.close();
  60.   });
  61.  
  62.   describe('Auth', () => {
  63.     const dto: AuthDto = {
  64.       email: 'vlad@gmail.com',
  65.       password: '123',
  66.     };
  67.  
  68.     //Signup test process
  69.     describe('Signup', () => {
  70.       it('should throw if email empty', () => {
  71.         return pactum
  72.           .spec()
  73.           .post('/auth/signup')
  74.           .withBody({
  75.             password: dto.password,
  76.           })
  77.           .expectStatus(400);
  78.       });
  79.  
  80.       it('should throw if password empty', () => {
  81.         return pactum
  82.           .spec()
  83.           .post('/auth/signup')
  84.           .withBody({
  85.             email: dto.email,
  86.           })
  87.           .expectStatus(400);
  88.       });
  89.  
  90.       it('should throw if no body (no email, nor password)', () => {
  91.         return pactum.spec().post('/auth/signup').expectStatus(400);
  92.       });
  93.  
  94.       it('should signup', () => {
  95.         return pactum
  96.           .spec()
  97.           .post('/auth/signup')
  98.           .withBody(dto)
  99.           .expectStatus(201);
  100.       });
  101.     });
  102.  
  103.     //Signin test process
  104.     describe('Signin', () => {
  105.       it('should throw if email empty', () => {
  106.         return pactum
  107.           .spec()
  108.           .post('/auth/signin')
  109.           .withBody({
  110.             password: dto.password,
  111.           })
  112.           .expectStatus(400);
  113.       });
  114.  
  115.       it('should throw if password empty', () => {
  116.         return pactum
  117.           .spec()
  118.           .post('/auth/signin')
  119.           .withBody({
  120.             email: dto.email,
  121.           })
  122.           .expectStatus(400);
  123.       });
  124.  
  125.       it('should throw if no body (no email, nor password)', () => {
  126.         return pactum.spec().post('/auth/signin').expectStatus(400);
  127.       });
  128.  
  129.       it('should signin', () => {
  130.         return pactum
  131.           .spec()
  132.           .post('/auth/signin')
  133.           .withBody(dto)
  134.           .expectStatus(200)
  135.           .stores('userAt', 'access_token');
  136.       });
  137.     });
  138.   });
  139.  
  140.   describe('User', () => {
  141.     describe('Get me', () => {
  142.       it('should get current user', () => {
  143.         return pactum
  144.           .spec()
  145.           .get('/users/me')
  146.           .withHeaders({
  147.             Authorization: 'Bearer $S{userAt}',
  148.           })
  149.           .expectStatus(200);
  150.       });
  151.     });
  152.     describe('Edit user', () => {
  153.       it('should edit user', () => {
  154.         const dto: EditUserDto = {
  155.           firstName: 'Daniel',
  156.           email: 'ddanielfenoll@gmail.com',
  157.         };
  158.         return pactum
  159.           .spec()
  160.           .patch('/users')
  161.           .withBody(dto)
  162.           .withHeaders({
  163.             Authorization: 'Bearer $S{userAt}',
  164.           })
  165.           .expectStatus(200)
  166.           .inspect();
  167.       });
  168.     });
  169.   });
  170.  
  171.   describe('Bookmarks', () => {
  172.     describe('Create bookmark', () => {});
  173.     describe('Get bookmarks', () => {});
  174.     describe('Get bookmark by id', () => {});
  175.     describe('Edit bookmark by id', () => {});
  176.     describe('Delete bookmark by id', () => {});
  177.   });
  178. });
  179.  
Advertisement
Add Comment
Please, Sign In to add comment