Skip to content

Commit

Permalink
fix: improved e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
VASHvic committed Oct 9, 2022
1 parent a705cd9 commit 1a415be
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 21 deletions.
22 changes: 11 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
module.exports = {
parser: '@typescript-eslint/parser',
parser: "@typescript-eslint/parser",
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
project: "tsconfig.json",
sourceType: "module",
},
plugins: ['@typescript-eslint/eslint-plugin'],
plugins: ["@typescript-eslint/eslint-plugin"],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
ignorePatterns: [".eslintrc.js"],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
},
};
4 changes: 0 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

## ez

- fer delete user
- acabar de mirar retornos
- posarli fastify ( problemes de versió, crec que per passport, deixar per a més avant)

## hard

- no puc pasarli variables env a la github action de 2e2
- usar un pas de la action pa crear una bd???
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test": "jest --runInBand --detectOpenHandles --config ./jest.config.json",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"e2e": "jest --runInBand --detectOpenHandles --forceExit --config ./test/jest-e2e.json"
"e2e": "jest --runInBand --forceExit --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^9.1.2",
Expand Down
4 changes: 4 additions & 0 deletions src/dto/deleteUser.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class DeleteUserDto {
email: string;
password: string;
}
11 changes: 10 additions & 1 deletion src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
UseGuards,
} from "@nestjs/common";
import { DeleteUserDto } from "src/dto/deleteUser.dto";
import { Public } from "../auth/decorators/public.decorator";
import { ApiKeyGuard } from "../auth/guards/api-key.guard";
import { LocalAuthGuard } from "../auth/guards/local-auth.guard";
Expand Down Expand Up @@ -58,12 +59,20 @@ export class UserController {
return this.userService.update(dto);
}

@UseGuards(LocalAuthGuard)
@Delete("delete")
public async deleteUser(@Body() dto: DeleteUserDto): Promise<boolean> {
return this.userService.deleteUser(dto);
}

@Post("signUp")
@Public()
public async signUp(@Body() dto: createUserDto): Promise<SafeUserType> {
try {
return await this.userService.signUp(dto);
} catch (e) {
console.log(e);

const error = await this.userErrorService.saveError(e);
throw error;
}
Expand Down
8 changes: 8 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UpdateUserDto } from "src/dto/updateUserDto";
import { User, UserDocument } from "./schemas/user.schema";
import * as bycript from "bcrypt";
import { SafeUserType, UserType } from "src/auth/types/types";
import { DeleteUserDto } from "src/dto/deleteUser.dto";

@Injectable()
export class UserService {
Expand Down Expand Up @@ -46,4 +47,11 @@ export class UserService {
const { password, __v, ...rta } = updatedUser.toJSON();
return rta as SafeUserType;
}

public async deleteUser(dto: DeleteUserDto): Promise<boolean> {
const { deletedCount } = await this.userModel
.deleteOne({ email: dto.email })
.exec();
return deletedCount > 0;
}
}
43 changes: 39 additions & 4 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AppModule } from "./../src/app.module";

describe("Starting App", () => {
let app: INestApplication;
let randomUser = (Math.random() + 1).toString(36).substring(7);
const randomUser = (Math.random() + 1).toString(36).substring(7);
let httpServer: any;
//mirar si pug afafar la conexió com michael guay

Expand All @@ -29,11 +29,9 @@ describe("Starting App", () => {
const response = await request(httpServer)
.get("/user/get")
.set("Auth", "abc");

expect(response.status).toBe(200);
});
});

describe("USER create a user /user/signup (POST)", () => {
it("should create a user", async () => {
const response = await request(httpServer)
Expand All @@ -46,6 +44,10 @@ describe("Starting App", () => {
//arreplegar id y pasarliu a getById

expect(response.status).toBe(201);
expect(response.body).toMatchObject({
name: randomUser,
email: `${randomUser}@mail.com`,
});
});
});

Expand All @@ -59,6 +61,14 @@ describe("Starting App", () => {
});

expect(response.status).toBe(201);
expect(response.body).toMatchObject({
access_token: expect.any(String),
user: {
_id: expect.any(String),
name: randomUser,
email: `${randomUser}@mail.com`,
},
});
});
});

Expand All @@ -76,7 +86,7 @@ describe("Starting App", () => {
//TODO: tornar resposta sencera
});
describe("USER update the user just created /user/update (PATCH)", () => {
it("should delete the user", async () => {
it("should uypdate the user", async () => {
const response = await request(httpServer)
.patch("/user/update")
.send({
Expand All @@ -91,5 +101,30 @@ describe("Starting App", () => {
});
});

describe("USER delete the user just updated /user/delete (DELETE)", () => {
it("should fail to delete the user when no invalid password", async () => {
const response = await request(httpServer)
.delete("/user/delete")
.send({
name: randomUser + "1",
email: `${randomUser}@gmail.com`,
password: `BAD${randomUser}1`,
});
expect(response.status).toBe(401);
});

it("should delete the user", async () => {
const response = await request(httpServer)
.delete("/user/delete")
.send({
name: randomUser + "1",
email: `${randomUser}@gmail.com`,
password: `${randomUser}1`,
});
expect(response.status).toBe(200);
expect(response.text).toBe("true");
});
});

//afegir delete user
});

0 comments on commit 1a415be

Please sign in to comment.