From 2f8cdcf10d846a6c3e5539d1e22473a8d59b833c Mon Sep 17 00:00:00 2001 From: FerreiroAlberto Date: Tue, 30 Apr 2024 09:26:54 +0200 Subject: [PATCH 1/6] Fix Sonar --- .github/workflows/audit.yml | 41 +++++++++++++++++++++++++++++++++++++ .github/workflows/build.yml | 27 ++++++++++++++++++++++++ package.json | 4 ++-- sonar-project.properties | 17 +++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/audit.yml create mode 100644 .github/workflows/build.yml create mode 100644 sonar-project.properties diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml new file mode 100644 index 0000000..b75012c --- /dev/null +++ b/.github/workflows/audit.yml @@ -0,0 +1,41 @@ +name: Audit + +on: push + +jobs: + audit: + runs-on: ubuntu-latest + name: Audit + + steps: + - name: Git checkout + uses: actions/checkout@v4 + + - name: Check if .editorconfig exists + uses: andstor/file-existence-action@v2 + with: + files: '.editorconfig' + allow_failure: true + + - name: EditorConfig validation + uses: snow-actions/eclint@v1.0.1 + + - name: Ensure node_modules is ignored by Git + uses: dkershner6/gitignore-parser@v1 + with: + must_deny: 'node_modules/' + + - name: Install modules + run: npm ci + + - name: ESLint validation + run: npx eslint --ignore-path .gitignore + + - name: Check commit message length + uses: gsactions/commit-message-checker@v1 + with: + pattern: '^[^#].{10,72}' + error: 'The commit message length must be between 10 and 72' + excludeDescription: 'true' # optional: this excludes the description body of a pull request + excludeTitle: 'true' # optional: this excludes the title of a pull request + checkAllCommitMessages: 'true' # optional: this checks all commits associated with a pull request diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..feac451 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,27 @@ +name: Build +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened] + workflow_dispatch: +permissions: + pull-requests: read # allows SonarCloud to decorate PRs with analysis results +jobs: + sonarcloud: + name: SonarCloud + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + - name: Install modules + run: npm ci + - name: Testing coverage + run: npm run test:prod #Change for a valid npm script + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/package.json b/package.json index 71a1cc8..d650cad 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,11 @@ "start": "cross-env NODE_ENV=dev PORT=3600 nest start", "start:dev": "cross-env NODE_ENV=dev PORT=3600 nest start --watch", "start:debug": "nest start --debug --watch", - "start:prod": "node dist", + "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest", + "test:prod": "jest --coverage", "test:watch": "jest --watch", - "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json", "prepare": "husky" diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..48e6c28 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,17 @@ +sonar.projectKey=isdi-coders-2023_Alberto-Ferreiro-Final-Project-202402-Mad-Back +sonar.organization=isdi-coders-2023 + +# This is the name and version displayed in the SonarCloud UI. +#sonar.projectName=Alberto-Ferreiro-Final-Project-202402-Mad-Back +#sonar.projectVersion=1.0 + + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +#sonar.sources=. + +# Encoding of the source code. Default is default system encoding +#sonar.sourceEncoding=UTF-8 +sonar.sources=./src +sonar.test.inclusions=./src///.test., ./src///.spec.* +sonar.javascript.lcov.reportPaths=coverage/lcov.info +sonar.coverage.exclusions=**/src/**/*.spec.*, **/src/main.ts From cd5d4d01e425bcec6ed9425aa84759bb6fc27823 Mon Sep 17 00:00:00 2001 From: FerreiroAlberto Date: Tue, 30 Apr 2024 09:28:49 +0200 Subject: [PATCH 2/6] Fix editorconfig --- editorconfig.txt => .editorconfig | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename editorconfig.txt => .editorconfig (100%) diff --git a/editorconfig.txt b/.editorconfig similarity index 100% rename from editorconfig.txt rename to .editorconfig From 36d6e99b6cebf38d43ef9e42f84017383b085c83 Mon Sep 17 00:00:00 2001 From: FerreiroAlberto Date: Tue, 30 Apr 2024 09:33:46 +0200 Subject: [PATCH 3/6] Fix indentation problems --- .github/workflows/build.yml | 54 +++++++++---------- .husky/commit-msg | 28 +++++----- .husky/pre-push | 30 +++++------ .prettierrc | 8 +-- nest-cli.json | 16 +++--- prisma/migrations/migration_lock.toml | 2 +- sonar-project.properties | 34 ++++++------ src/policies/policies.controller.spec.ts | 40 +++++++------- src/policies/policies.controller.ts | 68 ++++++++++++------------ src/policies/policies.module.ts | 18 +++---- src/policies/policies.service.spec.ts | 36 ++++++------- src/policies/policies.service.ts | 52 +++++++++--------- src/prisma/prisma.module.ts | 16 +++--- src/prisma/prisma.service.spec.ts | 36 ++++++------- test/app.e2e-spec.ts | 48 ++++++++--------- test/jest-e2e.json | 18 +++---- tsconfig.build.json | 8 +-- tsconfig.json | 42 +++++++-------- 18 files changed, 277 insertions(+), 277 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index feac451..968e6d8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,27 +1,27 @@ -name: Build -on: - push: - branches: - - main - pull_request: - types: [opened, synchronize, reopened] - workflow_dispatch: -permissions: - pull-requests: read # allows SonarCloud to decorate PRs with analysis results -jobs: - sonarcloud: - name: SonarCloud - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - - name: Install modules - run: npm ci - - name: Testing coverage - run: npm run test:prod #Change for a valid npm script - - name: SonarCloud Scan - uses: SonarSource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} +name: Build +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened] + workflow_dispatch: +permissions: + pull-requests: read # allows SonarCloud to decorate PRs with analysis results +jobs: + sonarcloud: + name: SonarCloud + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + - name: Install modules + run: npm ci + - name: Testing coverage + run: npm run test:prod #Change for a valid npm script + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.husky/commit-msg b/.husky/commit-msg index 645d498..5f4861b 100644 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,15 +1,15 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -while read line; do - # Skip comments - if [ "${line:0:1}" == "#" ]; then - continue - fi - if [ ${#line} -ge 72 ] || [ ${#line} -le 10 ]; then - echo -e "\033[0;31mThe length of the message has to be between 10 and 72 characters." - exit 1 - fi -done < "${1}" - +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +while read line; do + # Skip comments + if [ "${line:0:1}" == "#" ]; then + continue + fi + if [ ${#line} -ge 72 ] || [ ${#line} -le 10 ]; then + echo -e "\033[0;31mThe length of the message has to be between 10 and 72 characters." + exit 1 + fi +done < "${1}" + exit 0 diff --git a/.husky/pre-push b/.husky/pre-push index 31ca792..2372e72 100644 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -1,15 +1,15 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -local_branch_name="$(git rev-parse --abbrev-ref HEAD)" - -valid_branch_regex='^((hotfix|bugfix|feature)\/[a-zA-Z0-9\-]+)$' - -message="Please check your branch name." - -if [[ ! $local_branch_name =~ $valid_branch_regex ]]; then - echo -e "\033[0;31m$message" - exit 1 -fi - -exit 0 +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +local_branch_name="$(git rev-parse --abbrev-ref HEAD)" + +valid_branch_regex='^((hotfix|bugfix|feature)\/[a-zA-Z0-9\-]+)$' + +message="Please check your branch name." + +if [[ ! $local_branch_name =~ $valid_branch_regex ]]; then + echo -e "\033[0;31m$message" + exit 1 +fi + +exit 0 diff --git a/.prettierrc b/.prettierrc index 3cd0ff0..a20502b 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,4 @@ -{ - "singleQuote": true, - "trailingComma": "all" -} \ No newline at end of file +{ + "singleQuote": true, + "trailingComma": "all" +} diff --git a/nest-cli.json b/nest-cli.json index a8170d1..f9aa683 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -1,8 +1,8 @@ -{ - "$schema": "https://json.schemastore.org/nest-cli", - "collection": "@nestjs/schematics", - "sourceRoot": "src", - "compilerOptions": { - "deleteOutDir": true - } -} +{ + "$schema": "https://json.schemastore.org/nest-cli", + "collection": "@nestjs/schematics", + "sourceRoot": "src", + "compilerOptions": { + "deleteOutDir": true + } +} diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index fbffa92..99e4f20 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "postgresql" \ No newline at end of file +provider = "postgresql" diff --git a/sonar-project.properties b/sonar-project.properties index 48e6c28..2ab8ce4 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,17 +1,17 @@ -sonar.projectKey=isdi-coders-2023_Alberto-Ferreiro-Final-Project-202402-Mad-Back -sonar.organization=isdi-coders-2023 - -# This is the name and version displayed in the SonarCloud UI. -#sonar.projectName=Alberto-Ferreiro-Final-Project-202402-Mad-Back -#sonar.projectVersion=1.0 - - -# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. -#sonar.sources=. - -# Encoding of the source code. Default is default system encoding -#sonar.sourceEncoding=UTF-8 -sonar.sources=./src -sonar.test.inclusions=./src///.test., ./src///.spec.* -sonar.javascript.lcov.reportPaths=coverage/lcov.info -sonar.coverage.exclusions=**/src/**/*.spec.*, **/src/main.ts +sonar.projectKey=isdi-coders-2023_Alberto-Ferreiro-Final-Project-202402-Mad-Back +sonar.organization=isdi-coders-2023 + +# This is the name and version displayed in the SonarCloud UI. +#sonar.projectName=Alberto-Ferreiro-Final-Project-202402-Mad-Back +#sonar.projectVersion=1.0 + + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +#sonar.sources=. + +# Encoding of the source code. Default is default system encoding +#sonar.sourceEncoding=UTF-8 +sonar.sources=./src +sonar.test.inclusions=./src///.test., ./src///.spec.* +sonar.javascript.lcov.reportPaths=coverage/lcov.info +sonar.coverage.exclusions=**/src/**/*.spec.*, **/src/main.ts diff --git a/src/policies/policies.controller.spec.ts b/src/policies/policies.controller.spec.ts index 2c872bf..a53988b 100644 --- a/src/policies/policies.controller.spec.ts +++ b/src/policies/policies.controller.spec.ts @@ -1,20 +1,20 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { PoliciesController } from './policies.controller'; -import { PoliciesService } from './policies.service'; - -describe('PoliciesController', () => { - let controller: PoliciesController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [PoliciesController], - providers: [PoliciesService], - }).compile(); - - controller = module.get(PoliciesController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); +import { Test, TestingModule } from '@nestjs/testing'; +import { PoliciesController } from './policies.controller'; +import { PoliciesService } from './policies.service'; + +describe('PoliciesController', () => { + let controller: PoliciesController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [PoliciesController], + providers: [PoliciesService], + }).compile(); + + controller = module.get(PoliciesController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/policies/policies.controller.ts b/src/policies/policies.controller.ts index 84dec67..c595d9a 100644 --- a/src/policies/policies.controller.ts +++ b/src/policies/policies.controller.ts @@ -1,34 +1,34 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; -import { PoliciesService } from './policies.service'; -import { CreatePolicyDto } from './dto/create-policy.dto'; -import { UpdatePolicyDto } from './dto/update-policy.dto'; - -@Controller('policies') -export class PoliciesController { - constructor(private readonly policiesService: PoliciesService) {} - - @Post() - create(@Body() createPolicyDto: CreatePolicyDto) { - return this.policiesService.create(createPolicyDto); - } - - @Get() - findAll() { - return this.policiesService.findAll(); - } - - @Get(':id') - findOne(@Param('id') id: string) { - return this.policiesService.findOne(+id); - } - - @Patch(':id') - update(@Param('id') id: string, @Body() updatePolicyDto: UpdatePolicyDto) { - return this.policiesService.update(+id, updatePolicyDto); - } - - @Delete(':id') - remove(@Param('id') id: string) { - return this.policiesService.remove(+id); - } -} +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { PoliciesService } from './policies.service'; +import { CreatePolicyDto } from './dto/create-policy.dto'; +import { UpdatePolicyDto } from './dto/update-policy.dto'; + +@Controller('policies') +export class PoliciesController { + constructor(private readonly policiesService: PoliciesService) {} + + @Post() + create(@Body() createPolicyDto: CreatePolicyDto) { + return this.policiesService.create(createPolicyDto); + } + + @Get() + findAll() { + return this.policiesService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.policiesService.findOne(+id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updatePolicyDto: UpdatePolicyDto) { + return this.policiesService.update(+id, updatePolicyDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.policiesService.remove(+id); + } +} diff --git a/src/policies/policies.module.ts b/src/policies/policies.module.ts index 08c9609..86d4821 100644 --- a/src/policies/policies.module.ts +++ b/src/policies/policies.module.ts @@ -1,9 +1,9 @@ -import { Module } from '@nestjs/common'; -import { PoliciesService } from './policies.service'; -import { PoliciesController } from './policies.controller'; - -@Module({ - controllers: [PoliciesController], - providers: [PoliciesService], -}) -export class PoliciesModule {} +import { Module } from '@nestjs/common'; +import { PoliciesService } from './policies.service'; +import { PoliciesController } from './policies.controller'; + +@Module({ + controllers: [PoliciesController], + providers: [PoliciesService], +}) +export class PoliciesModule {} diff --git a/src/policies/policies.service.spec.ts b/src/policies/policies.service.spec.ts index 4a73d42..04cae92 100644 --- a/src/policies/policies.service.spec.ts +++ b/src/policies/policies.service.spec.ts @@ -1,18 +1,18 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { PoliciesService } from './policies.service'; - -describe('PoliciesService', () => { - let service: PoliciesService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [PoliciesService], - }).compile(); - - service = module.get(PoliciesService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); +import { Test, TestingModule } from '@nestjs/testing'; +import { PoliciesService } from './policies.service'; + +describe('PoliciesService', () => { + let service: PoliciesService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PoliciesService], + }).compile(); + + service = module.get(PoliciesService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/policies/policies.service.ts b/src/policies/policies.service.ts index 1872144..f26274a 100644 --- a/src/policies/policies.service.ts +++ b/src/policies/policies.service.ts @@ -1,26 +1,26 @@ -import { Injectable } from '@nestjs/common'; -import { CreatePolicyDto } from './dto/create-policy.dto'; -import { UpdatePolicyDto } from './dto/update-policy.dto'; - -@Injectable() -export class PoliciesService { - create(createPolicyDto: CreatePolicyDto) { - return 'This action adds a new policy'; - } - - findAll() { - return `This action returns all policies`; - } - - findOne(id: number) { - return `This action returns a #${id} policy`; - } - - update(id: number, updatePolicyDto: UpdatePolicyDto) { - return `This action updates a #${id} policy`; - } - - remove(id: number) { - return `This action removes a #${id} policy`; - } -} +import { Injectable } from '@nestjs/common'; +import { CreatePolicyDto } from './dto/create-policy.dto'; +import { UpdatePolicyDto } from './dto/update-policy.dto'; + +@Injectable() +export class PoliciesService { + create(createPolicyDto: CreatePolicyDto) { + return 'This action adds a new policy'; + } + + findAll() { + return `This action returns all policies`; + } + + findOne(id: number) { + return `This action returns a #${id} policy`; + } + + update(id: number, updatePolicyDto: UpdatePolicyDto) { + return `This action updates a #${id} policy`; + } + + remove(id: number) { + return `This action removes a #${id} policy`; + } +} diff --git a/src/prisma/prisma.module.ts b/src/prisma/prisma.module.ts index a5e59be..ec0ce32 100644 --- a/src/prisma/prisma.module.ts +++ b/src/prisma/prisma.module.ts @@ -1,8 +1,8 @@ -import { Module } from '@nestjs/common'; -import { PrismaService } from './prisma.service'; - -@Module({ - providers: [PrismaService], - exports: [PrismaService], -}) -export class PrismaModule {} +import { Module } from '@nestjs/common'; +import { PrismaService } from './prisma.service'; + +@Module({ + providers: [PrismaService], + exports: [PrismaService], +}) +export class PrismaModule {} diff --git a/src/prisma/prisma.service.spec.ts b/src/prisma/prisma.service.spec.ts index aee108e..a68cb9e 100644 --- a/src/prisma/prisma.service.spec.ts +++ b/src/prisma/prisma.service.spec.ts @@ -1,18 +1,18 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { PrismaService } from './prisma.service'; - -describe('PrismaService', () => { - let service: PrismaService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [PrismaService], - }).compile(); - - service = module.get(PrismaService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); +import { Test, TestingModule } from '@nestjs/testing'; +import { PrismaService } from './prisma.service'; + +describe('PrismaService', () => { + let service: PrismaService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PrismaService], + }).compile(); + + service = module.get(PrismaService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index 8c6434f..50cda62 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -1,24 +1,24 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { INestApplication } from '@nestjs/common'; -import * as request from 'supertest'; -import { AppModule } from './../src/app.module'; - -describe('AppController (e2e)', () => { - let app: INestApplication; - - beforeEach(async () => { - const moduleFixture: TestingModule = await Test.createTestingModule({ - imports: [AppModule], - }).compile(); - - app = moduleFixture.createNestApplication(); - await app.init(); - }); - - it('/ (GET)', () => { - return request(app.getHttpServer()) - .get('/') - .expect(200) - .expect('Hello World!'); - }); -}); +import { Test, TestingModule } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import * as request from 'supertest'; +import { AppModule } from './../src/app.module'; + +describe('AppController (e2e)', () => { + let app: INestApplication; + + beforeEach(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + }); + + it('/ (GET)', () => { + return request(app.getHttpServer()) + .get('/') + .expect(200) + .expect('Hello World!'); + }); +}); diff --git a/test/jest-e2e.json b/test/jest-e2e.json index bb66802..e9d912f 100644 --- a/test/jest-e2e.json +++ b/test/jest-e2e.json @@ -1,9 +1,9 @@ -{ - "moduleFileExtensions": ["js", "json", "ts"], - "rootDir": ".", - "testEnvironment": "node", - "testRegex": ".e2e-spec.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - } -} +{ + "moduleFileExtensions": ["js", "json", "ts"], + "rootDir": ".", + "testEnvironment": "node", + "testRegex": ".e2e-spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +} diff --git a/tsconfig.build.json b/tsconfig.build.json index 1d7acd8..64f86c6 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,4 +1,4 @@ -{ - "extends": "./tsconfig.json", - "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] -} +{ + "extends": "./tsconfig.json", + "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] +} diff --git a/tsconfig.json b/tsconfig.json index e0802de..a1c778d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,21 +1,21 @@ -{ - "compilerOptions": { - "module": "commonjs", - "declaration": true, - "removeComments": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "allowSyntheticDefaultImports": true, - "target": "ES2021", - "sourceMap": true, - "outDir": "./dist", - "baseUrl": "./", - "incremental": true, - "skipLibCheck": true, - "strictNullChecks": true, - "noImplicitAny": true, - "strictBindCallApply": true, - "forceConsistentCasingInFileNames": true, - "noFallthroughCasesInSwitch": true - } -} +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "ES2021", + "sourceMap": true, + "outDir": "./dist", + "baseUrl": "./", + "incremental": true, + "skipLibCheck": true, + "strictNullChecks": true, + "noImplicitAny": true, + "strictBindCallApply": true, + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true + } +} From 49aeb104ff1fc4fcd7087d1f4ab2fa0b20d7d474 Mon Sep 17 00:00:00 2001 From: FerreiroAlberto Date: Tue, 30 Apr 2024 13:23:33 +0200 Subject: [PATCH 4/6] Add testing for modules --- package.json | 4 +- .../migration.sql | 16 ++++ prisma/migrations/migration_lock.toml | 2 +- prisma/schema.prisma | 5 +- src/policies/dto/create-policy.dto.ts | 3 +- src/policies/dto/update-policy.dto.ts | 2 +- src/policies/entities/policy.entity.ts | 4 +- src/policies/policies.controller.spec.ts | 70 +++++++++++++++- src/policies/policies.controller.ts | 22 ++++-- src/policies/policies.service.spec.ts | 79 ++++++++++++++++++- src/policies/policies.service.ts | 66 +++++++++++++--- src/users/users.controller.spec.ts | 62 ++++++++++++++- src/users/users.controller.ts | 17 +--- src/users/users.service.spec.ts | 76 +++++++++++++++++- src/users/users.service.ts | 13 +-- 15 files changed, 385 insertions(+), 56 deletions(-) create mode 100644 prisma/migrations/20240430100624_adjust_policies/migration.sql diff --git a/package.json b/package.json index d650cad..d3c4145 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,9 @@ "coveragePathIgnorePatterns": [ "module", "dto", - "entity" + "entity", + "main", + "prisma" ], "coverageDirectory": "../coverage", "testEnvironment": "node" diff --git a/prisma/migrations/20240430100624_adjust_policies/migration.sql b/prisma/migrations/20240430100624_adjust_policies/migration.sql new file mode 100644 index 0000000..3616676 --- /dev/null +++ b/prisma/migrations/20240430100624_adjust_policies/migration.sql @@ -0,0 +1,16 @@ +/* + Warnings: + + - A unique constraint covering the columns `[policyNumber]` on the table `Policy` will be added. If there are existing duplicate values, this will fail. + - Added the required column `carAge` to the `Policy` table without a default value. This is not possible if the table is not empty. + - Added the required column `plateNumber` to the `Policy` table without a default value. This is not possible if the table is not empty. + - Added the required column `policyType` to the `Policy` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "Policy" ADD COLUMN "carAge" INTEGER NOT NULL, +ADD COLUMN "plateNumber" TEXT NOT NULL, +ADD COLUMN "policyType" TEXT NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "Policy_policyNumber_key" ON "Policy"("policyNumber"); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index 99e4f20..fbffa92 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "postgresql" +provider = "postgresql" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0f4182d..292f63d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -29,7 +29,10 @@ id String @id @default(uuid()) userId String carMake String carModel String -policyNumber Int @default(autoincrement()) +carAge Int +plateNumber String +policyType String +policyNumber Int @default(autoincrement()) @unique claims Claim[] user User @relation(fields: [userId], references: [id]) } diff --git a/src/policies/dto/create-policy.dto.ts b/src/policies/dto/create-policy.dto.ts index 84deee2..dedb30f 100644 --- a/src/policies/dto/create-policy.dto.ts +++ b/src/policies/dto/create-policy.dto.ts @@ -1,7 +1,8 @@ export class CreatePolicyDto { carMake: string; carModel: string; - carAge: string; + carAge: number; plateNumber: string; policyType: string; + userId: string; } diff --git a/src/policies/dto/update-policy.dto.ts b/src/policies/dto/update-policy.dto.ts index 5ed9541..57a63b1 100644 --- a/src/policies/dto/update-policy.dto.ts +++ b/src/policies/dto/update-policy.dto.ts @@ -4,7 +4,7 @@ import { CreatePolicyDto } from './create-policy.dto'; export class UpdatePolicyDto extends PartialType(CreatePolicyDto) { carMake?: string; carModel?: string; - carAge?: string; + carAge?: number; plateNumber?: string; policyType: string; } diff --git a/src/policies/entities/policy.entity.ts b/src/policies/entities/policy.entity.ts index f2967bc..603c42d 100644 --- a/src/policies/entities/policy.entity.ts +++ b/src/policies/entities/policy.entity.ts @@ -3,8 +3,8 @@ export class Policy { userId: number; carMake: string; carModel: string; - carAge: string; - plateNumber: string; + carAge: number; + plateNumber: number; policyNumber: string; policyType: string; claims: Claim[]; diff --git a/src/policies/policies.controller.spec.ts b/src/policies/policies.controller.spec.ts index a53988b..d299220 100644 --- a/src/policies/policies.controller.spec.ts +++ b/src/policies/policies.controller.spec.ts @@ -1,6 +1,25 @@ import { Test, TestingModule } from '@nestjs/testing'; import { PoliciesController } from './policies.controller'; import { PoliciesService } from './policies.service'; +import { PrismaService } from '../prisma/prisma.service'; +import { CreatePolicyDto } from './dto/create-policy.dto'; + +const mockPoliciesService = { + findAll: jest.fn().mockResolvedValue([]), + findOne: jest.fn().mockResolvedValue({}), + create: jest.fn().mockResolvedValue({}), + update: jest.fn().mockResolvedValue({}), + delete: jest.fn().mockResolvedValue({}), +}; + +const mockPrismaService = { + policy: { + findMany: jest.fn().mockResolvedValue([]), + findUnique: jest.fn().mockReturnValue({}), + create: jest.fn().mockReturnValue({}), + update: jest.fn().mockReturnValue({}), + }, +}; describe('PoliciesController', () => { let controller: PoliciesController; @@ -8,7 +27,17 @@ describe('PoliciesController', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [PoliciesController], - providers: [PoliciesService], + providers: [ + PoliciesService, + { + provide: PoliciesService, + useValue: mockPoliciesService, + }, + { + provide: PrismaService, + useValue: mockPrismaService, + }, + ], }).compile(); controller = module.get(PoliciesController); @@ -17,4 +46,43 @@ describe('PoliciesController', () => { it('should be defined', () => { expect(controller).toBeDefined(); }); + describe('When we use the method findAll', () => { + it('should return all users', async () => { + const result = await controller.findAll(); + expect(mockPoliciesService.findAll).toHaveBeenCalled(); + expect(result).toEqual([]); + }); + }); + describe('When we use the method findOne', () => { + it('should return the user with the id', async () => { + const result = await controller.findOne('1'); + expect(mockPoliciesService.findOne).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + }); + describe('When we use the method create', () => { + it('should create a new policy', async () => { + const mockPolicyDto = {} as CreatePolicyDto; + const result = await controller.create('1', mockPolicyDto); + expect(mockPoliciesService.create).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + }); + describe('When we use the method update', () => { + it('should update a policy', async () => { + const mockPolicyDto = { + policyType: '12345', + } as CreatePolicyDto; + const result = await controller.update('1', mockPolicyDto); + expect(mockPoliciesService.update).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + }); + describe('When we use the method delete', () => { + it('should delete and return a policy', async () => { + const result = await controller.delete('1'); + expect(mockPoliciesService.delete).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + }); }); diff --git a/src/policies/policies.controller.ts b/src/policies/policies.controller.ts index c595d9a..6d914f8 100644 --- a/src/policies/policies.controller.ts +++ b/src/policies/policies.controller.ts @@ -1,4 +1,12 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { + Controller, + Get, + Post, + Body, + Patch, + Param, + Delete, +} from '@nestjs/common'; import { PoliciesService } from './policies.service'; import { CreatePolicyDto } from './dto/create-policy.dto'; import { UpdatePolicyDto } from './dto/update-policy.dto'; @@ -8,8 +16,8 @@ export class PoliciesController { constructor(private readonly policiesService: PoliciesService) {} @Post() - create(@Body() createPolicyDto: CreatePolicyDto) { - return this.policiesService.create(createPolicyDto); + create(@Param('id') id: string, @Body() createPolicyDto: CreatePolicyDto) { + return this.policiesService.create(id, createPolicyDto); } @Get() @@ -19,16 +27,16 @@ export class PoliciesController { @Get(':id') findOne(@Param('id') id: string) { - return this.policiesService.findOne(+id); + return this.policiesService.findOne(id); } @Patch(':id') update(@Param('id') id: string, @Body() updatePolicyDto: UpdatePolicyDto) { - return this.policiesService.update(+id, updatePolicyDto); + return this.policiesService.update(id, updatePolicyDto); } @Delete(':id') - remove(@Param('id') id: string) { - return this.policiesService.remove(+id); + delete(@Param('id') id: string) { + return this.policiesService.delete(id); } } diff --git a/src/policies/policies.service.spec.ts b/src/policies/policies.service.spec.ts index 04cae92..8a08bea 100644 --- a/src/policies/policies.service.spec.ts +++ b/src/policies/policies.service.spec.ts @@ -1,12 +1,28 @@ import { Test, TestingModule } from '@nestjs/testing'; import { PoliciesService } from './policies.service'; +import { PrismaService } from '../prisma/prisma.service'; +import { CreatePolicyDto } from './dto/create-policy.dto'; +import { UpdatePolicyDto } from './dto/update-policy.dto'; + +const mockPrisma = { + policy: { + findMany: jest.fn().mockResolvedValue([]), + findUnique: jest.fn().mockReturnValue({}), + create: jest.fn().mockReturnValue({}), + update: jest.fn().mockReturnValue({}), + delete: jest.fn().mockReturnValue({}), + }, +}; describe('PoliciesService', () => { let service: PoliciesService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [PoliciesService], + providers: [ + { provide: PrismaService, useValue: mockPrisma }, + PoliciesService, + ], }).compile(); service = module.get(PoliciesService); @@ -15,4 +31,65 @@ describe('PoliciesService', () => { it('should be defined', () => { expect(service).toBeDefined(); }); + describe('When we use the method findOne', () => { + it('Then it should return the user with the right id', async () => { + const result = await service.findOne('1'); + expect(mockPrisma.policy.findUnique).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + + it('Then it should throw an error if the user is not found', async () => { + mockPrisma.policy.findUnique.mockReturnValueOnce(null); + expect(service.findOne('1')).rejects.toThrow('Policy 1 not found'); + }); + }); + describe('When we use the method findAll', () => { + it('Then it should return all the users', async () => { + const result = await service.findAll(); + expect(mockPrisma.policy.findMany).toHaveBeenCalled(); + expect(result).toEqual([]); + }); + }); + describe('When we use the method create', () => { + it('Then it should return the new user', async () => { + const result = await service.create('1', {} as CreatePolicyDto); + expect(mockPrisma.policy.create).toHaveBeenCalled(); + + expect(result).toEqual({}); + }); + }); + describe('When we use the method update', () => { + it('Then it should return the updated user', async () => { + const result = await service.update('1', {} as UpdatePolicyDto); + expect(mockPrisma.policy.update).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + + it('Then it should throw an error if the user is not found', async () => { + mockPrisma.policy.findUnique.mockReturnValueOnce(null); + mockPrisma.policy.update.mockRejectedValueOnce( + new Error('Policy not found'), + ); + expect(service.update('1', {} as UpdatePolicyDto)).rejects.toThrow( + 'Policy 1 not found', + ); + }); + }); + describe('When we use the method delete', () => { + it('Then it should return the deleted user', async () => { + mockPrisma.policy.findUnique.mockReturnValueOnce({}); + mockPrisma.policy.delete.mockReturnValueOnce({}); + const result = await service.delete('1'); + expect(mockPrisma.policy.delete).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + + it('Then it should throw an error if the user is not found', async () => { + mockPrisma.policy.findUnique.mockReturnValueOnce(null); + mockPrisma.policy.delete.mockRejectedValueOnce( + new Error('Policy not found'), + ); + expect(service.delete('1')).rejects.toThrow('Policy 1 not found'); + }); + }); }); diff --git a/src/policies/policies.service.ts b/src/policies/policies.service.ts index f26274a..65acd17 100644 --- a/src/policies/policies.service.ts +++ b/src/policies/policies.service.ts @@ -1,26 +1,70 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { CreatePolicyDto } from './dto/create-policy.dto'; import { UpdatePolicyDto } from './dto/update-policy.dto'; +import { PrismaService } from '../prisma/prisma.service'; + +const select = { + id: true, + carMake: true, + carModel: true, + carAge: true, + plateNumber: true, + policyNumber: true, + claims: { + select: { + status: true, + type: true, + phoneNumber: true, + address: true, + }, + }, +}; @Injectable() export class PoliciesService { - create(createPolicyDto: CreatePolicyDto) { - return 'This action adds a new policy'; + constructor(private prisma: PrismaService) {} + async create(id: string, data: CreatePolicyDto) { + return this.prisma.policy.create({ + data, + select, + }); } - findAll() { - return `This action returns all policies`; + async findAll() { + return this.prisma.policy.findMany({ select }); } - findOne(id: number) { - return `This action returns a #${id} policy`; + async findOne(id: string) { + const policy = await this.prisma.policy.findUnique({ + where: { id }, + select, + }); + if (!policy) { + throw new NotFoundException(`Policy ${id} not found`); + } + return policy; } - update(id: number, updatePolicyDto: UpdatePolicyDto) { - return `This action updates a #${id} policy`; + async update(id: string, data: UpdatePolicyDto) { + try { + return await this.prisma.policy.update({ + where: { id }, + data, + select, + }); + } catch (error) { + throw new NotFoundException(`Policy ${id} not found`); + } } - remove(id: number) { - return `This action removes a #${id} policy`; + async delete(id: string) { + try { + return await this.prisma.policy.delete({ + where: { id }, + select, + }); + } catch (error) { + throw new NotFoundException(`Policy ${id} not found`); + } } } diff --git a/src/users/users.controller.spec.ts b/src/users/users.controller.spec.ts index a76d310..fa2808b 100644 --- a/src/users/users.controller.spec.ts +++ b/src/users/users.controller.spec.ts @@ -1,6 +1,25 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; +import { CreateUserDto } from './dto/create-user.dto'; +import { PrismaService } from '../prisma/prisma.service'; + +const mockUsersService = { + findAll: jest.fn().mockResolvedValue([]), + findOne: jest.fn().mockResolvedValue({}), + findForLogin: jest.fn().mockResolvedValue({}), + create: jest.fn().mockResolvedValue({}), + update: jest.fn().mockResolvedValue({}), +}; + +const mockPrismaService = { + user: { + findMany: jest.fn().mockResolvedValue([]), + findUnique: jest.fn().mockReturnValue({}), + create: jest.fn().mockReturnValue({}), + update: jest.fn().mockReturnValue({}), + }, +}; describe('UsersController', () => { let controller: UsersController; @@ -8,7 +27,16 @@ describe('UsersController', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [UsersController], - providers: [UsersService], + providers: [ + { + provide: UsersService, + useValue: mockUsersService, + }, + { + provide: PrismaService, + useValue: mockPrismaService, + }, + ], }).compile(); controller = module.get(UsersController); @@ -17,4 +45,36 @@ describe('UsersController', () => { it('should be defined', () => { expect(controller).toBeDefined(); }); + describe('When we use the method findAll', () => { + it('should return all users', async () => { + const result = await controller.findAll(); + expect(mockUsersService.findAll).toHaveBeenCalled(); + expect(result).toEqual([]); + }); + }); + describe('When we use the method findOne', () => { + it('should return the user with the id', async () => { + const result = await controller.findOne('1'); + expect(mockUsersService.findOne).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + }); + describe('When we use the method register', () => { + it('should create a new user', async () => { + const mockUserDto = {} as CreateUserDto; + const result = await controller.register(mockUserDto); + expect(mockUsersService.create).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + }); + describe('When we use the method update', () => { + it('should update a user', async () => { + const mockUserDto = { + password: '12345', + } as CreateUserDto; + const result = await controller.update('1', mockUserDto); + expect(mockUsersService.update).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + }); }); diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 2f7d8f5..2736da1 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -1,12 +1,4 @@ -import { - Controller, - Get, - Post, - Body, - Patch, - Param, - Delete, -} from '@nestjs/common'; +import { Controller, Get, Post, Body, Patch, Param } from '@nestjs/common'; import { UsersService } from './users.service'; import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; @@ -16,7 +8,7 @@ export class UsersController { constructor(private readonly usersService: UsersService) {} @Post() - create(@Body() createUserDto: CreateUserDto) { + register(@Body() createUserDto: CreateUserDto) { return this.usersService.create(createUserDto); } @@ -34,9 +26,4 @@ export class UsersController { update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) { return this.usersService.update(id, updateUserDto); } - - @Delete(':id') - remove(@Param('id') id: string) { - return this.usersService.delete(id); - } } diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts index 62815ba..763ccf6 100644 --- a/src/users/users.service.spec.ts +++ b/src/users/users.service.spec.ts @@ -1,12 +1,24 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UsersService } from './users.service'; +import { PrismaService } from '../prisma/prisma.service'; +const mockPrisma = { + user: { + findMany: jest.fn().mockResolvedValue([]), + findUnique: jest.fn().mockReturnValue({}), + create: jest.fn().mockReturnValue({}), + update: jest.fn().mockReturnValue({}), + }, +}; describe('UsersService', () => { let service: UsersService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [UsersService], + providers: [ + { provide: PrismaService, useValue: mockPrisma }, + UsersService, + ], }).compile(); service = module.get(UsersService); @@ -15,4 +27,66 @@ describe('UsersService', () => { it('should be defined', () => { expect(service).toBeDefined(); }); + describe('When we use the method findOne', () => { + it('Then it should return the user with the right id', async () => { + const result = await service.findOne('1'); + expect(mockPrisma.user.findUnique).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + + it('Then it should throw an error if the user is not found', async () => { + mockPrisma.user.findUnique.mockReturnValueOnce(null); + expect(service.findOne('1')).rejects.toThrow('User 1 not found'); + }); + }); + describe('When we use the method findAll', () => { + it('Then it should return all the users', async () => { + const result = await service.findAll(); + expect(mockPrisma.user.findMany).toHaveBeenCalled(); + expect(result).toEqual([]); + }); + }); + describe('When we use the method findForLogin', () => { + it('Then it should return the user with the matching email', async () => { + const result = await service.findForLogin('cara@papa.com'); + expect(mockPrisma.user.findUnique).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + + it('Then it should throw an error if the user is not found', async () => { + mockPrisma.user.findUnique.mockReturnValueOnce(null); + expect(service.findForLogin('cara@papa.com')).rejects.toThrow( + 'Invalid input', + ); + }); + }); + describe('When we use the method create', () => { + it('Then it should return the new user', async () => { + const result = await service.create({ + name: 'pepe', + email: 'email', + age: 35, + licenseYear: 18, + password: '12345', + }); + expect(mockPrisma.user.create).toHaveBeenCalled(); + + expect(result).toEqual({}); + }); + }); + describe('When we use the method update', () => { + it('Then it should return the updated user', async () => { + const result = await service.update('1', {}); + expect(mockPrisma.user.update).toHaveBeenCalled(); + expect(result).toEqual({}); + }); + + it('Then it should throw an error if the user is not found', async () => { + mockPrisma.user.findUnique.mockReturnValueOnce(null); + mockPrisma.user.update.mockRejectedValueOnce(new Error('User not found')); + expect(service.update('1', { name: 'pepito' })).rejects.toThrow( + 'User 1 not found', + ); + }); + }); }); diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 8f87b88..ca41c78 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -5,7 +5,7 @@ import { } from '@nestjs/common'; import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; -import { PrismaService } from 'src/prisma/prisma.service'; +import { PrismaService } from '../prisma/prisma.service'; const select = { id: true, @@ -75,15 +75,4 @@ export class UsersService { throw new NotFoundException(`User ${id} not found`); } } - - async delete(id: string) { - try { - return await this.prisma.user.delete({ - where: { id }, - select, - }); - } catch (error) { - throw new NotFoundException(`User ${id} not found`); - } - } } From 26ab8db051ef89e382992811cf362c9467e04d60 Mon Sep 17 00:00:00 2001 From: FerreiroAlberto Date: Tue, 30 Apr 2024 13:30:43 +0200 Subject: [PATCH 5/6] Fix Security Hotspots for Sonar --- prisma/migrations/migration_lock.toml | 2 +- src/policies/policies.service.spec.ts | 2 +- src/users/users.controller.spec.ts | 5 +++-- src/users/users.service.spec.ts | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index fbffa92..99e4f20 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "postgresql" \ No newline at end of file +provider = "postgresql" diff --git a/src/policies/policies.service.spec.ts b/src/policies/policies.service.spec.ts index 8a08bea..3cb2ecd 100644 --- a/src/policies/policies.service.spec.ts +++ b/src/policies/policies.service.spec.ts @@ -89,7 +89,7 @@ describe('PoliciesService', () => { mockPrisma.policy.delete.mockRejectedValueOnce( new Error('Policy not found'), ); - expect(service.delete('1')).rejects.toThrow('Policy 1 not found'); + expect(service.delete('2')).rejects.toThrow('Policy 2 not found'); }); }); }); diff --git a/src/users/users.controller.spec.ts b/src/users/users.controller.spec.ts index fa2808b..92cad70 100644 --- a/src/users/users.controller.spec.ts +++ b/src/users/users.controller.spec.ts @@ -3,6 +3,7 @@ import { UsersController } from './users.controller'; import { UsersService } from './users.service'; import { CreateUserDto } from './dto/create-user.dto'; import { PrismaService } from '../prisma/prisma.service'; +import { UpdateUserDto } from './dto/update-user.dto'; const mockUsersService = { findAll: jest.fn().mockResolvedValue([]), @@ -70,8 +71,8 @@ describe('UsersController', () => { describe('When we use the method update', () => { it('should update a user', async () => { const mockUserDto = { - password: '12345', - } as CreateUserDto; + passemail: 'cara@papa.com', + } as UpdateUserDto; const result = await controller.update('1', mockUserDto); expect(mockUsersService.update).toHaveBeenCalled(); expect(result).toEqual({}); diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts index 763ccf6..871c22d 100644 --- a/src/users/users.service.spec.ts +++ b/src/users/users.service.spec.ts @@ -67,7 +67,7 @@ describe('UsersService', () => { email: 'email', age: 35, licenseYear: 18, - password: '12345', + password: 'GHgndbdfas567*¨?32**', }); expect(mockPrisma.user.create).toHaveBeenCalled(); From c8e1eb5897f40eec71bc9d703e0623a3fa8ca8c8 Mon Sep 17 00:00:00 2001 From: FerreiroAlberto Date: Tue, 30 Apr 2024 13:37:44 +0200 Subject: [PATCH 6/6] Fix SecHotspot for Sonar- --- src/users/users.service.spec.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts index 871c22d..29f8a44 100644 --- a/src/users/users.service.spec.ts +++ b/src/users/users.service.spec.ts @@ -1,6 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UsersService } from './users.service'; import { PrismaService } from '../prisma/prisma.service'; +import { CreateUserDto } from './dto/create-user.dto'; const mockPrisma = { user: { @@ -62,13 +63,7 @@ describe('UsersService', () => { }); describe('When we use the method create', () => { it('Then it should return the new user', async () => { - const result = await service.create({ - name: 'pepe', - email: 'email', - age: 35, - licenseYear: 18, - password: 'GHgndbdfas567*¨?32**', - }); + const result = await service.create({} as CreateUserDto); expect(mockPrisma.user.create).toHaveBeenCalled(); expect(result).toEqual({});