Skip to content

Commit

Permalink
Another try with the cors error
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedElgaidi committed Nov 30, 2023
1 parent 9bcfca6 commit 40d4e90
Show file tree
Hide file tree
Showing 51 changed files with 229 additions and 126 deletions.
13 changes: 7 additions & 6 deletions .eslintrc.js → .eslintrc.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module.exports = {
import { Linter } from 'eslint'

const config: Linter.Config = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
root: true,
env: {
node: true,
Expand All @@ -24,4 +23,6 @@ module.exports = {
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-var-requires': 'off',
},
};
}

export default config
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"moduleNameMapper": {
"^@src/(.*)$": "<rootDir>/$1"
}
}
}
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AppService, WelcomeResponse } from './app.service'
import { Controller, Get } from '@nestjs/common'
import { ResultMessage } from './shared/types'
import { ResultMessage } from '@src/shared/types'

@Controller()
export class AppController {
Expand Down
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { PostModule } from './modules/post/post.module'
import { UserModule } from './modules/user/user.module'
import { AuthModule } from './modules/auth/auth.module'
import { TagModule } from './modules/tag/tag.module'
import { ExceptionsFilter } from './shared/filters'
import { ExceptionsFilter } from '@src/shared/filters'
import { MongooseModule } from '@nestjs/mongoose'
import { AppController } from './app.controller'
import { ConfigModule } from '@nestjs/config'
import { appConfig } from './shared/config'
import { appConfig } from '@src/shared/config'
import { AppService } from './app.service'
import * as compression from 'compression'
import helmet from 'helmet'
Expand Down
2 changes: 1 addition & 1 deletion src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'
import { ResultMessage } from './shared/types'
import { ResultMessage } from '@src/shared/types'

interface ApiLinks {
linkedIn: string
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { configureCors } from './shared/helpers'
import { appConfig } from './shared/config'
import { configureCors } from '@src/shared/helpers'
import { appConfig } from '@src/shared/config'
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Controller, Get, Post, Req, UseInterceptors } from '@nestjs/common'
import { ProtectResourceInterceptor } from 'src/shared/interceptors'
import { ProtectResourceInterceptor } from '@src/shared/interceptors'
import { UserService } from '../../../user/services/user.service'
import { User } from '../../../user/schemas/user.schema'
import { CurrentUser } from '../../../user/decorators'
import { PublicUserDto } from 'src/modules/user/dtos'
import { Serialize } from 'src/shared/decorators'
import { ResultMessage } from 'src/shared/types'
import { PublicUserDto } from '@src/modules/user/dtos'
import { Serialize } from '@src/shared/decorators'
import { ResultMessage } from '@src/shared/types'
import { AuthService } from '../../services'
import { CustomRequest } from 'express'

Expand Down
4 changes: 2 additions & 2 deletions src/modules/auth/controllers/public/public-auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DeviceInfo, IpAddress, Serialize } from 'src/shared/decorators'
import { PublicSessionDto } from 'src/modules/session/dtos'
import { DeviceInfo, IpAddress, Serialize } from '@src/shared/decorators'
import { PublicSessionDto } from '@src/modules/session/dtos'
import { AuthService } from '../../services/auth.service'
import { Body, Controller, Post } from '@nestjs/common'
import { LoginResponse } from '../../types'
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auth/schemas/login-attempt.schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { ExpiryDuration } from 'src/shared/enums'
import { BaseSchema } from 'src/shared/schemas'
import { ExpiryDuration } from '@src/shared/enums'
import { BaseSchema } from '@src/shared/schemas'
import { HydratedDocument } from 'mongoose'

export type LoginAttemptDocument = HydratedDocument<LoginAttempt>
Expand Down
6 changes: 3 additions & 3 deletions src/modules/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { UserService } from '../../user/services/user.service'
import { LoginAttemptService } from './login-attempt.service'
import { SessionService } from '../../session/services'
import { HashUtil, TokenUtil } from 'src/shared/utils'
import { ResultMessage } from 'src/shared/types'
import { UserService } from '@src/modules/user/services'
import { HashUtil, TokenUtil } from '@src/shared/utils'
import { ResultMessage } from '@src/shared/types'
import { LoginResponse } from '../types'
import { MESSAGES } from '../constants'
import { LoginDto } from '../dtos'
Expand Down
17 changes: 12 additions & 5 deletions src/modules/auth/tests/controllers/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing'
import { AuthController } from '../../controllers/public/public-auth.controller'
import { PublicAuthController } from '../../controllers'
import { AuthService } from '../../services'

describe('AuthController', () => {
let controller: AuthController
describe('PublicAuthController', () => {
let controller: PublicAuthController

beforeEach(async () => {
const fakeAuthService: Partial<AuthService> = {
// login = () => {},
// logOut = () => {},
}

const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
controllers: [PublicAuthController],
providers: [{ provide: AuthService, useValue: fakeAuthService }],
}).compile()

controller = module.get<AuthController>(AuthController)
controller = module.get<PublicAuthController>(PublicAuthController)
})

it('should be defined', () => {
Expand Down
111 changes: 97 additions & 14 deletions src/modules/auth/tests/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,101 @@
import { Test, TestingModule } from '@nestjs/testing'
import { AuthService } from '../../services/auth.service'
// import { UserService } from '@src/modules/user/services'
// import { AuthService, LoginAttemptService } from '../../services'
// import { SessionService } from '@src/modules/session/services'
// import { HashUtil, TokenUtil } from '@src/shared/utils'
// import { Test, TestingModule } from '@nestjs/testing'
// import { LoginDto } from '../../dtos'
// import { CreateUserDto } from '@src/modules/user/dtos'
// import { User } from '@src/modules/user/schemas'

describe('AuthService', () => {
let service: AuthService
// // Import the actual TokenUtil for mocking
// import { TokenUtil as RealTokenUtil } from '@src/shared/utils/token.util'

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile()
// // Mock the TokenUtil methods
// jest.mock('@src/shared/utils/token.util')

service = module.get<AuthService>(AuthService)
})
// describe('🏠Auth Service | Login', () => {
// let authService: AuthService
// let fakeUserService: Partial<UserService>
// let fakeLoginAttemptService: Partial<LoginAttemptService>
// let fakeSessionService: Partial<SessionService>
// let fakeHashUtil: Partial<HashUtil>

it('should be defined', () => {
expect(service).toBeDefined()
})
})
// beforeEach(async () => {
// fakeUserService = {
// createUser: jest.fn(),
// findUserByEmail: jest.fn(),
// }

// fakeLoginAttemptService = {
// isFailedLoginAttemptsExceeded: jest.fn(),
// incrementFailedLoginAttemptsCount: jest.fn(),
// }

// fakeSessionService = {
// createSession: jest.fn(),
// getSession: jest.fn(),
// deleteSession: jest.fn(),
// }

// fakeHashUtil = {
// verifyHash: jest.fn(async () => await Promise.resolve(true)), // Provide a mock implementation
// }

// // Reset the mock implementation for each test
// ;(RealTokenUtil.generateAccessToken as jest.Mock).mockReset()
// ;(RealTokenUtil.generateRefreshToken as jest.Mock).mockReset()
// ;(RealTokenUtil.verifyAccessToken as jest.Mock).mockReset()
// ;(RealTokenUtil.verifyRefreshToken as jest.Mock).mockReset()

// const module: TestingModule = await Test.createTestingModule({
// providers: [
// AuthService,
// { provide: UserService, useValue: fakeUserService },
// { provide: LoginAttemptService, useValue: fakeLoginAttemptService },
// { provide: SessionService, useValue: fakeSessionService },
// { provide: HashUtil, useValue: fakeHashUtil },
// { provide: TokenUtil, useClass: RealTokenUtil }, // Use the actual TokenUtil class
// ],
// }).compile()

// authService = module.get<AuthService>(AuthService)
// })

// it('should login successfully and return tokens', async () => {
// const mockData: LoginDto = {
// email: 'test@example.com',
// password: 'password123',
// }

// const mockUser = { _id: 'mockUserId', password: 'hashedPassword' }
// const mockIpAddress = '127.0.0.1'
// const mockDevice = { browser: 'Chrome' }
// const mockAccessToken = 'mockAccessToken'
// const mockRefreshToken = 'mockRefreshToken'

// fakeUserService.findUserByEmail.mockResolvedValueOnce(mockUser)

// // Mock the TokenUtil methods
// ;(RealTokenUtil.generateAccessToken as jest.Mock).mockResolvedValueOnce(mockAccessToken)
// ;(RealTokenUtil.generateRefreshToken as jest.Mock).mockResolvedValueOnce(mockRefreshToken)

// const result = await authService.login(mockData, mockIpAddress, mockDevice)

// expect(result).toEqual({
// accessToken: mockAccessToken,
// refreshToken: mockRefreshToken,
// })

// expect(fakeLoginAttemptService.isFailedLoginAttemptsExceeded).toHaveBeenCalled()
// expect(fakeUserService.findUserByEmail).toHaveBeenCalledWith(mockData.email)
// expect(fakeHashUtil.verifyHash).toHaveBeenCalledWith(mockData.password, mockUser.password)
// expect(RealTokenUtil.generateAccessToken).toHaveBeenCalledWith({ _id: mockUser._id })
// expect(RealTokenUtil.generateRefreshToken).toHaveBeenCalledWith({ _id: mockUser._id })
// expect(fakeSessionService.createSession).toHaveBeenCalledWith({
// accessToken: mockAccessToken,
// refreshToken: mockRefreshToken,
// ipAddress: mockIpAddress,
// device: mockDevice,
// })
// })
// })
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, UseInterceptors } from '@nestjs/common'
import { ProtectResourceInterceptor } from 'src/shared/interceptors'
import { ResultMessage } from 'src/shared/types'
import { ProtectResourceInterceptor } from '@src/shared/interceptors'
import { ResultMessage } from '@src/shared/types'
import { KeywordService } from '../../services'
import { CreateKeywordDto } from '../../dtos'
import { Keyword } from '../../schemas'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/keyword/repositories/keyword.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'
import { ResultMessage } from 'src/shared/types'
import { ResultMessage } from '@src/shared/types'
import { InjectModel } from '@nestjs/mongoose'
import { CreateKeywordDto } from '../dtos'
import { MESSAGES } from '../constants'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/keyword/schemas/keyword.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { BaseSchema } from 'src/shared/schemas'
import { BaseSchema } from '@src/shared/schemas'
import { HydratedDocument } from 'mongoose'

export type KeywordDocument = HydratedDocument<Keyword>
Expand Down
4 changes: 2 additions & 2 deletions src/modules/keyword/services/keyword.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BadRequestException, Inject, Injectable, NotFoundException, forwardRef } from '@nestjs/common'
import { PostService } from 'src/modules/post/services'
import { PostService } from '@src/modules/post/services'
import { KeywordRepository } from '../repositories'
import { ResultMessage } from 'src/shared/types'
import { ResultMessage } from '@src/shared/types'
import { CreateKeywordDto } from '../dtos'
import { MESSAGES } from '../constants'
import { Keyword } from '../schemas'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get, Param, UseInterceptors } from '@nestjs/common'
import { ProtectResourceInterceptor } from 'src/shared/interceptors'
import { ResultMessage } from 'src/shared/types'
import { ProtectResourceInterceptor } from '@src/shared/interceptors'
import { ResultMessage } from '@src/shared/types'
import { PostService } from '../../services'

@Controller('/admin/posts/analytics')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseInterceptors } from '@nestjs/common'
import { ProtectResourceInterceptor } from 'src/shared/interceptors'
import { ProtectResourceInterceptor } from '@src/shared/interceptors'
import { CreatePostDto, DeletePostDto } from '../../dtos'
import { ResultMessage } from 'src/shared/types'
import { ResultMessage } from '@src/shared/types'
import { Post as BlogPost } from '../../schemas'
import { PostsFilter } from '../../interfaces'
import { Pagination } from 'src/shared/dtos'
import { Pagination } from '@src/shared/dtos'
import { PostService } from '../../services'

@Controller('/admin/posts')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get, Param, Query } from '@nestjs/common'
import { PostsFilter } from '../../interfaces'
import { Pagination } from 'src/shared/dtos'
import { Pagination } from '@src/shared/dtos'
import { PostService } from '../../services'
import { Post } from '../../schemas'

Expand Down
2 changes: 1 addition & 1 deletion src/modules/post/interfaces/all-posts.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostsFilter } from './posts-filter.interface'
import { Pagination } from 'src/shared/dtos'
import { Pagination } from '@src/shared/dtos'

export interface GetAllPostsDto {
filter?: PostsFilter
Expand Down
6 changes: 3 additions & 3 deletions src/modules/post/repositories/post.repository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CreatePostDto, DeletePostDto } from '../dtos'
import { Injectable, NotFoundException, InternalServerErrorException } from '@nestjs/common'
import { CountDocumentsDto, CountDocumentsQuery } from 'src/shared/dtos'
import { ResultMessage } from 'src/shared/types'
import { CountDocumentsDto, CountDocumentsQuery } from '@src/shared/dtos'
import { ResultMessage } from '@src/shared/types'
import { InjectModel } from '@nestjs/mongoose'
import { Entities } from 'src/shared/enums'
import { Entities } from '@src/shared/enums'
import { MESSAGES } from '../constants'
import { Post } from '../schemas'
import { Model } from 'mongoose'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/post/schemas/post.schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { HydratedDocument, Types } from 'mongoose'
import { BaseSchema } from 'src/shared/schemas'
import { BaseSchema } from '@src/shared/schemas'
import { Keyword } from '../../keyword/schemas'
import { Series } from '../../series/schemas'
import { Tag } from '../../tag/schemas'
Expand Down
4 changes: 2 additions & 2 deletions src/modules/post/services/post.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable, BadRequestException, NotFoundException, Inject, forwardRef } from '@nestjs/common'
import { SortFieldOptions, SortValueOptions } from 'src/shared/enums'
import { SortFieldOptions, SortValueOptions } from '@src/shared/enums'
import { KeywordService } from '../../keyword/services'
import { CreatePostDto, DeletePostDto } from '../dtos'
import { SeriesService } from '../../series/services'
import { ResultMessage } from 'src/shared/types'
import { ResultMessage } from '@src/shared/types'
import { PostRepository } from '../repositories'
import { TagService } from '../../tag/services'
import { GetAllPostsDto } from '../interfaces'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseInterceptors } from '@nestjs/common'
import { ProtectResourceInterceptor } from 'src/shared/interceptors'
import { ResultMessage } from 'src/shared/types'
import { ProtectResourceInterceptor } from '@src/shared/interceptors'
import { ResultMessage } from '@src/shared/types'
import { QuoteService } from '../../services'
import { Pagination } from 'src/shared/dtos'
import { Pagination } from '@src/shared/dtos'
import { CreateQuoteDto } from '../../dtos'
import { Quote } from '../../schemas'

Expand Down
4 changes: 2 additions & 2 deletions src/modules/quote/repositories/quote.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'
import { ResultMessage } from 'src/shared/types'
import { ResultMessage } from '@src/shared/types'
import { InjectModel } from '@nestjs/mongoose'
import { Pagination } from 'src/shared/dtos'
import { Pagination } from '@src/shared/dtos'
import { CreateQuoteDto } from '../dtos'
import { Model, Types } from 'mongoose'
import { MESSAGES } from '../constants'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/quote/schemas/quote.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { BaseSchema } from 'src/shared/schemas'
import { BaseSchema } from '@src/shared/schemas'
import { HydratedDocument } from 'mongoose'

export type QuoteDocument = HydratedDocument<Quote>
Expand Down
Loading

0 comments on commit 40d4e90

Please sign in to comment.