Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: airbnb eslint rules #53

Merged
merged 11 commits into from
Nov 29, 2021
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
node_modules
package-lock.json
yarn.lock
.DS_Store

.env
.DS_Store
.env.local
.env.development
.env.production
17 changes: 13 additions & 4 deletions backend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ module.exports = {
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'airbnb',
'airbnb-typescript',
'plugin:prettier/recommended',
'prettier',
],
parserOptions: {
project: './tsconfig.json',
"ecmaVersion": 12,
},
root: true,
env: {
node: true,
jest: true,
es2021: true,
},
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',
'no-underscore-dangle': 'off',
'class-methods-use-this': 'off',
'no-param-reassign': 'off',
'import/prefer-default-export': 'off'

},
};
5 changes: 3 additions & 2 deletions backend/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all"
"trailingComma": "all",
"tabWidth": 2,
"singleQuote": true
}
3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"bcrypt": "^5.0.1",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"eslint-config-airbnb": "^19.0.0",
"eslint-config-airbnb-typescript": "^16.0.0",
"express": "^4.17.1",
"joi": "^17.4.2",
"lint-staged": "^11.1.2",
"mongodb": "^3.7.3",
Expand Down
4 changes: 2 additions & 2 deletions backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import AppController from './app.controller';
import AppService from './app.service';

describe('AppController', () => {
let appController: AppController;
Expand Down
4 changes: 2 additions & 2 deletions backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import AppService from './app.service';

@Controller()
export class AppController {
export default class AppController {
constructor(private readonly appService: AppService) {}

@Get()
Expand Down
10 changes: 5 additions & 5 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { DatabaseModule } from './database/database.module';
import { UsersModule } from './users/users.module';
import { AuthModule } from './auth/auth.module';
import { BoardsModule } from './boards/boards.module';
import * as Joi from 'joi';
import DatabaseModule from './database/database.module';
import UsersModule from './users/users.module';
import AuthModule from './auth/auth.module';
import BoardsModule from './boards/boards.module';

@Module({
imports: [
Expand All @@ -30,4 +30,4 @@ import * as Joi from 'joi';
controllers: [],
providers: [],
})
export class AppModule {}
export default class AppModule {}
2 changes: 1 addition & 1 deletion backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
export default class AppService {
getHello(): string {
return 'Hello World!';
}
Expand Down
26 changes: 11 additions & 15 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import {
Res,
} from '@nestjs/common';
import { Response } from 'express';
import JwtAuthenticationGuard from '../guards/jwtAuth.guard';
import { AuthService } from './auth.service';
import AuthService from './auth.service';
import RegisterDto from '../users/dto/register.dto';
import { LocalAuthGuard } from '../guards/localAuth.guard';
import LocalAuthGuard from '../guards/localAuth.guard';
import RequestWithUser from '../interfaces/requestWithUser.interface';
import { UsersService } from '../users/users.service';
import UsersService from '../users/users.service';
import JwtRefreshGuard from '../guards/jwtRefreshAuth.guard';
import { LoginUserDto } from '../users/dto/login.dto';
import LoginUserDto from '../users/dto/login.dto';

@Controller('auth')
export class AuthController {
export default class AuthController {
constructor(
private readonly authService: AuthService,
private readonly usersService: UsersService,
Expand Down Expand Up @@ -47,18 +46,15 @@ export class AuthController {

const { name, email } = user;

const userWToken: LoginUserDto = { name, email, accessToken, refreshToken };
const userWToken: LoginUserDto = {
name,
email,
accessToken,
refreshToken,
};
return response.send(userWToken);
}

@UseGuards(JwtAuthenticationGuard)
@Get()
authenticate(@Req() request: RequestWithUser) {
const user = request.user;
user.password = undefined;
return user;
}

@UseGuards(JwtRefreshGuard)
@Get('refresh')
refresh(@Req() request: RequestWithUser) {
Expand Down
14 changes: 7 additions & 7 deletions backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UsersModule } from '../users/users.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { LocalStrategy } from './strategy/local.strategy';
import { JwtStrategy } from './strategy/jwt.strategy';
import { JwtRefreshTokenStrategy } from './strategy/refresh.strategy';
import AuthService from './auth.service';
import AuthController from './auth.controller';
import UsersModule from '../users/users.module';
import LocalStrategy from './strategy/local.strategy';
import JwtStrategy from './strategy/jwt.strategy';
import JwtRefreshTokenStrategy from './strategy/refresh.strategy';
import {
describeJWT,
JWT_ACCESS_TOKEN_SECRET,
Expand Down Expand Up @@ -35,4 +35,4 @@ import {
providers: [AuthService, LocalStrategy, JwtStrategy, JwtRefreshTokenStrategy],
controllers: [AuthController],
})
export class AuthModule {}
export default class AuthModule {}
12 changes: 6 additions & 6 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import errors from '../database/types/errors';
import RegisterDto from '../users/dto/register.dto';
import { compare, encrypt } from '../utils/bcrypt';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import UsersService from '../users/users.service';
import Errors from '../database/types/errors';
import RegisterDto from '../users/dto/register.dto';
import { compare, encrypt } from '../utils/bcrypt';
import TokenPayload from '../interfaces/tokenPayload.interface';
import {
JWT_ACCESS_TOKEN_EXPIRATION_TIME,
Expand All @@ -20,7 +20,7 @@ import {
} from '../constants/httpExceptions';

@Injectable()
export class AuthService {
export default class AuthService {
constructor(
private readonly usersService: UsersService,
private readonly jwtService: JwtService,
Expand Down Expand Up @@ -64,7 +64,7 @@ export class AuthService {
createdUser.password = undefined;
return createdUser;
} catch (error) {
if (error?.code === errors.UniqueViolation) {
if (error?.code === Errors.UniqueViolation) {
throw new HttpException(
describeExceptions(EMAIL_EXISTS),
HttpStatus.BAD_REQUEST,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/auth/strategy/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { UsersService } from '../../users/users.service';
import UsersService from '../../users/users.service';
import TokenPayload from '../../interfaces/tokenPayload.interface';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
export default class JwtStrategy extends PassportStrategy(Strategy) {
constructor(
private readonly configService: ConfigService,
private readonly userService: UsersService,
Expand Down
5 changes: 3 additions & 2 deletions backend/src/auth/strategy/local.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { AuthService } from '../auth.service';
import AuthService from '../auth.service';
import UserEntity from '../../users/entity/user.entity';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
export default class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authenticationService: AuthService) {
super({
usernameField: 'email',
});
}

async validate(email: string, password: string): Promise<UserEntity> {
return this.authenticationService.getAuthenticatedUser(email, password);
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/auth/strategy/refresh.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Request } from 'express';
import { UsersService } from '../../users/users.service';
import UsersService from '../../users/users.service';
import TokenPayload from '../../interfaces/tokenPayload.interface';

@Injectable()
export class JwtRefreshTokenStrategy extends PassportStrategy(
export default class JwtRefreshTokenStrategy extends PassportStrategy(
Strategy,
'jwt-refresh-token',
) {
Expand Down
43 changes: 18 additions & 25 deletions backend/src/auth/tests/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { getRepositoryToken } from '@nestjs/typeorm';
import { UsersService } from '../../users/users.service';
import { AuthController } from '../auth.controller';
import UsersService from '../../users/users.service';
import AuthController from '../auth.controller';
import UserEntity from '../../users/entity/user.entity';
import { mockedUser } from '../../mocks/user.mock';
import { AuthService } from '../auth.service';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import mockedUser from '../../mocks/user.mock';
import AuthService from '../auth.service';
import jwtService from '../../mocks/jwtService.mock';
import configService from '../../mocks/configService.mock';
import * as request from 'supertest';

describe('AuthController', () => {
let app: INestApplication;
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('AuthController', () => {
...userData,
};
delete expectedData.password;
return await request(app.getHttpServer())
return request(app.getHttpServer())
.post('/auth/register')
.send({
email: mockedUser.email,
Expand All @@ -66,33 +66,26 @@ describe('AuthController', () => {
});
});
describe('and using invalid data', () => {
it('should throw an error because full data wasnt submitted', async () => {
return await request(app.getHttpServer())
it('should throw an error because full data wasnt submitted', () =>
request(app.getHttpServer())
.post('/auth/register')
.send({
name: '',
password: '',
email: '',
})
.expect(400);
});
it('should throw an error because full data wasnt submitted', async () => {
return await request(app.getHttpServer())
.send({ name: '', password: '', email: '' })
.expect(400));
it('should throw an error because full data wasnt submitted', async () =>
request(app.getHttpServer())
.post('/auth/register')
.send({
name: mockedUser.name,
})
.expect(400);
});
it('should throw an error because full data wasnt submitted', async () => {
return await request(app.getHttpServer())
.expect(400));
it('should throw an error because full data wasnt submitted', async () =>
request(app.getHttpServer())
.post('/auth/register')
.send({
name: mockedUser.name,
password: mockedUser.password,
})
.expect(400);
});
.expect(400));
it('should throw an error because password is short', async () => {
const res = await request(app.getHttpServer())
.post('/auth/register')
Expand Down
8 changes: 4 additions & 4 deletions backend/src/auth/tests/auth.service.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AuthService } from '../auth.service';
import { Test } from '@nestjs/testing';
import * as bcrypt from 'bcrypt';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { getRepositoryToken } from '@nestjs/typeorm';
import AuthService from '../auth.service';
import UserEntity from '../../users/entity/user.entity';
import { UsersService } from '../../users/users.service';
import UsersService from '../../users/users.service';
import jwtService from '../../mocks/jwtService.mock';
import configService from '../../mocks/configService.mock';
import * as bcrypt from 'bcrypt';
import { mockedUser } from '../../mocks/user.mock';
import mockedUser from '../../mocks/user.mock';

jest.mock('bcrypt');

Expand Down
6 changes: 3 additions & 3 deletions backend/src/auth/tests/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from '../auth.service';
import { UsersService } from '../../users/users.service';
import UserEntity from '../../users/entity/user.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import AuthService from '../auth.service';
import UsersService from '../../users/users.service';
import UserEntity from '../../users/entity/user.entity';
import jwtService from '../../mocks/jwtService.mock';
import configService from '../../mocks/configService.mock';

Expand Down
Loading