-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from hebertsanto/feature-auth
Feature auth
- Loading branch information
Showing
25 changed files
with
217 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CommentsController } from './controller/comments/comments.controller'; | ||
import { CommentsService } from './services/comments/comments.service'; | ||
import { ProjectsService } from 'src/projects/services/projects/projects.service'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { SharedServicesModule } from 'src/shared/shared-services.module'; | ||
|
||
@Module({ | ||
imports: [SharedServicesModule], | ||
controllers: [CommentsController], | ||
providers: [CommentsService, ProjectsService, UserService, HashService], | ||
providers: [CommentsService], | ||
}) | ||
export class CommentsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,87 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CommentsService } from './comments.service'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { PrismaService } from 'src/database/prisma.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { ProjectsService } from 'src/projects/services/projects/projects.service'; | ||
import { LoggerService } from 'src/logger/logger.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { PrismaService } from 'src/database/prisma.service'; | ||
import { Comment } from 'src/comments/types/comments'; | ||
import { PrismaModule } from 'src/database/prisma.module'; | ||
|
||
describe('CommentsService', () => { | ||
let service: CommentsService; | ||
let userService: UserService; | ||
let projectService: ProjectsService; | ||
let prismaService: PrismaService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CommentsService, | ||
UserService, | ||
PrismaService, | ||
HashService, | ||
ProjectsService, | ||
HashService, | ||
LoggerService, | ||
], | ||
imports: [PrismaModule], | ||
}).compile(); | ||
|
||
service = module.get<CommentsService>(CommentsService); | ||
userService = module.get<UserService>(UserService); | ||
projectService = module.get<ProjectsService>(ProjectsService); | ||
prismaService = module.get<PrismaService>(PrismaService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should create a comment', async () => { | ||
const commentData: Comment = { | ||
userId: 'user_id', | ||
projectId: 'project_id', | ||
comment: 'com', | ||
questionId: 'fda', | ||
// add other required fields | ||
}; | ||
|
||
const createCommentSpy = jest | ||
.spyOn(prismaService.comments, 'create') | ||
.mockResolvedValueOnce(commentData as any); | ||
|
||
const checkUserExistenceSpy = jest | ||
.spyOn(userService, 'checkUserExistence') | ||
.mockResolvedValueOnce({ | ||
id: 'fad', | ||
name: 'Hebert', | ||
email: '[email protected]', | ||
emailStatus: 'PENDING', | ||
password: '203040', | ||
createAt: new Date(), | ||
updateAt: new Date(), | ||
teamMemberShipId: '303002', | ||
}); | ||
|
||
const checkProjectExistenceSpy = jest | ||
.spyOn(projectService, 'checkProjectExistence') | ||
.mockResolvedValueOnce({ | ||
id: 'uuid', | ||
name: 'Awesome Project', | ||
description: 'This is a sample project', | ||
priority: 'MIN', | ||
userId: 'fmadm', | ||
url: 'https://example.com/project', | ||
visibility: true, | ||
createAt: new Date(), | ||
updateAt: new Date(), | ||
}); | ||
|
||
const createdComment = await service.createComment(commentData); | ||
|
||
expect(checkUserExistenceSpy).toHaveBeenCalledWith('user_id'); | ||
expect(checkProjectExistenceSpy).toHaveBeenCalledWith('project_id'); | ||
expect(createCommentSpy).toHaveBeenCalledWith({ data: commentData }); | ||
expect(createdComment).toEqual(commentData as any); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DecisionsController } from './controller/decisions/decisions.controller'; | ||
import { DecisionsService } from './services/decisions/decisions.service'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { ProjectsService } from 'src/projects/services/projects/projects.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { SharedServicesModule } from 'src/shared/shared-services.module'; | ||
|
||
@Module({ | ||
imports: [SharedServicesModule], | ||
controllers: [DecisionsController], | ||
providers: [DecisionsService, UserService, ProjectsService, HashService], | ||
providers: [DecisionsService], | ||
}) | ||
export class DecisionsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { FollowProjectService } from './service/follow-project/follow-project.service'; | ||
import { FollowProjectController } from './controller/follow-project/follow-project.controller'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { ProjectsService } from 'src/projects/services/projects/projects.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { SharedServicesModule } from 'src/shared/shared-services.module'; | ||
|
||
@Module({ | ||
providers: [FollowProjectService, UserService, ProjectsService, HashService], | ||
imports: [SharedServicesModule], | ||
providers: [FollowProjectService], | ||
controllers: [FollowProjectController], | ||
}) | ||
export class FollowProjectModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GoalsController } from './controller/goals/goals.controller'; | ||
import { GoalsService } from './services/goals/goals.service'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { ProjectsService } from 'src/projects/services/projects/projects.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { SharedServicesModule } from 'src/shared/shared-services.module'; | ||
|
||
@Module({ | ||
imports: [SharedServicesModule], | ||
controllers: [GoalsController], | ||
providers: [GoalsService, UserService, ProjectsService, HashService], | ||
providers: [GoalsService], | ||
}) | ||
export class GoalsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor(private jwtService: JwtService) {} | ||
|
||
async generateToken(payload: any) { | ||
return this.jwtService.sign(payload); | ||
} | ||
|
||
async verifyToken(token: string) { | ||
return this.jwtService.verify(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Injectable, NestMiddleware, NotFoundException } from '@nestjs/common'; | ||
import { JsonWebTokenError, JwtService } from '@nestjs/jwt'; | ||
import { i18n } from 'src/i18n'; | ||
|
||
@Injectable() | ||
export class JwtMiddleware implements NestMiddleware { | ||
constructor(private jwtService: JwtService) {} | ||
|
||
use(req: any, _res: any, next: () => void) { | ||
console.log(req); | ||
const token = req.headers.authorization?.split(' ')[1]; | ||
|
||
if (!token) { | ||
throw new NotFoundException(i18n()['exception.tokenNotProvided']); | ||
} | ||
|
||
const decoded = this.jwtService.verify(token); | ||
|
||
if (!decoded) { | ||
throw new JsonWebTokenError(i18n()['exception.tokenMalformed']); | ||
} | ||
|
||
req.user = decoded; | ||
|
||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ProfileController } from './controller/profile/profile.controller'; | ||
import { ProfileService } from './services/profile/profile.service'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { SharedServicesModule } from 'src/shared/shared-services.module'; | ||
|
||
@Module({ | ||
imports: [SharedServicesModule], | ||
controllers: [ProfileController], | ||
providers: [ProfileService, UserService, HashService], | ||
providers: [ProfileService], | ||
}) | ||
export class ProfileModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ProjectIdeaController } from './controllers/project-idea.controller'; | ||
import { ProjectIdeaService } from './services/project-idea.service'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { SharedServicesModule } from 'src/shared/shared-services.module'; | ||
|
||
@Module({ | ||
imports: [SharedServicesModule], | ||
controllers: [ProjectIdeaController], | ||
providers: [ProjectIdeaService, UserService, HashService], | ||
providers: [ProjectIdeaService], | ||
}) | ||
export class ProjectIdea {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ProjectsController } from './controller/projects/projects.controller'; | ||
import { ProjectsService } from './services/projects/projects.service'; | ||
import { UserService } from 'src/user/services/user/user.service'; | ||
import { HashService } from 'src/hash/service/hash/hash.service'; | ||
import { SharedServicesModule } from 'src/shared/shared-services.module'; | ||
|
||
@Module({ | ||
imports: [SharedServicesModule], | ||
controllers: [ProjectsController], | ||
providers: [ProjectsService, UserService, HashService], | ||
providers: [ProjectsService], | ||
}) | ||
export class ProjectsModule {} |
Oops, something went wrong.