Skip to content

Commit

Permalink
chore(config): Update prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjaysah101 committed Sep 22, 2024
1 parent ab396af commit c64af28
Show file tree
Hide file tree
Showing 134 changed files with 1,395 additions and 1,245 deletions.
7 changes: 5 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"singleQuote": true,
"singleQuote": false,
"arrowParens": "always",
"printWidth": 120
"printWidth": 120,
"semi": true,
"tabWidth": 2,
"trailingComma": "es5"
}
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.8'
version: "3.8"

services:
question-service:
Expand Down
34 changes: 17 additions & 17 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import typescriptEslint from '@typescript-eslint/eslint-plugin';
import globals from 'globals';
import tsParser from '@typescript-eslint/parser';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import js from '@eslint/js';
import { FlatCompat } from '@eslint/eslintrc';
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import globals from "globals";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand All @@ -16,30 +16,30 @@ const compat = new FlatCompat({

export default [
{
ignores: ['**/.eslintrc.js', '**/*.config.js', '**/*.md', '**/build', 'eslint.config.mjs', 'coverage**/*'],
ignores: ["**/.eslintrc.js", "**/*.config.js", "**/*.md", "**/build", "eslint.config.mjs", "coverage**/*"],
},
...compat.extends('eslint:recommended', 'plugin:@typescript-eslint/recommended'),
...compat.extends("eslint:recommended", "plugin:@typescript-eslint/recommended"),
{
plugins: {
'@typescript-eslint': typescriptEslint,
"@typescript-eslint": typescriptEslint,
},
languageOptions: {
globals: {
...globals.browser,
},
parser: tsParser,
ecmaVersion: 'latest',
sourceType: 'module',
ecmaVersion: "latest",
sourceType: "module",
parserOptions: {
project: './tsconfig.eslint.json',
project: "./tsconfig.eslint.json",
},
},

rules: {
indent: ['error', 2],
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/strict-boolean-expressions': 'error',
'no-console': 'error',
indent: ["error", 2],
"@typescript-eslint/prefer-nullish-coalescing": "error",
"@typescript-eslint/strict-boolean-expressions": "error",
"no-console": "error",
},
},
];
18 changes: 9 additions & 9 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Config } from 'jest';
import type { Config } from "jest";

const config: Config = {
verbose: true,
preset: 'ts-jest',
moduleFileExtensions: ['js', 'ts', 'json', 'node'],
modulePathIgnorePatterns: ['<rootDir>/build'],
transformIgnorePatterns: ['node_modules/(?!(jest-)?ts-jest)'],
roots: ['<rootDir>/src'],
setupFilesAfterEnv: ['<rootDir>/src/jest.setup.ts'],
testEnvironment: 'node',
preset: "ts-jest",
moduleFileExtensions: ["js", "ts", "json", "node"],
modulePathIgnorePatterns: ["<rootDir>/build"],
transformIgnorePatterns: ["node_modules/(?!(jest-)?ts-jest)"],
roots: ["<rootDir>/src"],
setupFilesAfterEnv: ["<rootDir>/src/jest.setup.ts"],
testEnvironment: "node",
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
"^@/(.*)$": "<rootDir>/src/$1",
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/__test__/env.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('Environment Variables', () => {
it('should have an application port', () => {
describe("Environment Variables", () => {
it("should have an application port", () => {
expect(process.env.APP_PORT).toBeDefined();
});
});
104 changes: 61 additions & 43 deletions src/api/v1/auth/__test__/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { sign } from 'jsonwebtoken';
import { envConstants, errorMap, ErrorTypeEnum, STATUS_CODES } from '@/constants';
import { sign } from "jsonwebtoken";
import { envConstants, errorMap, ErrorTypeEnum, STATUS_CODES } from "@/constants";
import {
expectSignUpSuccess,
expectUnauthorizedResponseForInvalidToken,
login,
renewToken,
signUp,
} from '@/utils/test';
import { Login, loginSchema } from '@/api/v1/auth/auth.validation';
import { ZodError, ZodIssue } from 'zod';
} from "@/utils/test";
import { Login, loginSchema } from "@/api/v1/auth/auth.validation";
import { ZodError, ZodIssue } from "zod";

const VALID_CREDENTIALS = {
username: 'validUser',
email: 'validemail1@example.com',
password: 'ValidPassword123@',
confirmPassword: 'ValidPassword123@',
username: "validUser",
email: "validemail1@example.com",
password: "ValidPassword123@",
confirmPassword: "ValidPassword123@",
};

type ValidationResult = { success: true; data: Login } | { success: false; error: ZodIssue[] };
Expand All @@ -31,117 +31,135 @@ const validateLoginSchema = (body: unknown): ValidationResult => {
}
};

describe('Auth Test', () => {
it('should respond with 404 when user not found', async () => {
describe("Auth Test", () => {
it("should respond with 404 when user not found", async () => {
const errorObject = errorMap[ErrorTypeEnum.enum.USER_NOT_FOUND];
const response = await login({
email: 'notRegistered@test.com',
password: 'notRegistered123@',
email: "notRegistered@test.com",
password: "notRegistered123@",
});

expect(response.statusCode).toBe(STATUS_CODES.NOT_FOUND);

expect(response.body).toMatchObject({
message: errorObject.body.message,
code: errorObject.body.code,
status: 'failed',
status: "failed",
});
});

it('should respond with 401 for invalid credentials', async () => {
it("should respond with 401 for invalid credentials", async () => {
const errorObject = errorMap[ErrorTypeEnum.enum.INVALID_CREDENTIALS];

const res = await signUp(VALID_CREDENTIALS);
expectSignUpSuccess(res);

const response = await login({
...VALID_CREDENTIALS,
password: 'notRegistered123@',
password: "notRegistered123@",
});
expect(response.statusCode).toBe(STATUS_CODES.UNAUTHORIZED);
expect(response.body).toMatchObject({
message: errorObject.body.message,
code: errorObject.body.code,
status: 'failed',
status: "failed",
});
});

it('should respond with 401 for invalid token', async () => {
const response = await renewToken('Bearer invalidToken');
it("should respond with 401 for invalid token", async () => {
const response = await renewToken("Bearer invalidToken");
expectUnauthorizedResponseForInvalidToken(response);
});

it('should throw an error if the token is expired', async () => {
it("should throw an error if the token is expired", async () => {
const errorObject = errorMap[ErrorTypeEnum.enum.TOKEN_EXPIRED];

const loginResponse = await login(VALID_CREDENTIALS);

// Mock verifyToken to throw a TokenExpiredError
const refreshToken = sign({ id: loginResponse.body.userId }, envConstants.JWT_REFRESH_SECRET, { expiresIn: '0s' });
const refreshToken = sign({ id: loginResponse.body.userId }, envConstants.JWT_REFRESH_SECRET, { expiresIn: "0s" });

const response = await renewToken(`Bearer ${refreshToken}`);

expect(response.statusCode).toBe(STATUS_CODES.UNAUTHORIZED);
expect(response.body).toMatchObject({
message: errorObject.body.message,
code: errorObject.body.code,
status: 'failed',
status: "failed",
});
});

test('should accept valid email login', () => {
const result = validateLoginSchema({ email: 'user@example.com', password: 'Password123@' });
test("should accept valid email login", () => {
const result = validateLoginSchema({
email: "user@example.com",
password: "Password123@",
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data).toEqual({ email: 'user@example.com', password: 'Password123@' });
expect(result.data).toEqual({
email: "user@example.com",
password: "Password123@",
});
}
});

test('should accept valid username login', () => {
const result = validateLoginSchema({ username: 'user123', password: 'Password123@' });
test("should accept valid username login", () => {
const result = validateLoginSchema({
username: "user123",
password: "Password123@",
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data).toEqual({ username: 'user123', password: 'Password123@' });
expect(result.data).toEqual({
username: "user123",
password: "Password123@",
});
}
});

test('should reject invalid email format', () => {
const result = validateLoginSchema({ email: 'invalid-email', password: 'Password123@' });
test("should reject invalid email format", () => {
const result = validateLoginSchema({
email: "invalid-email",
password: "Password123@",
});
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error[0].message).toBe('Invalid email');
expect(result.error[0].message).toBe("Invalid email");
}
});

test('should reject when both email and username are missing', () => {
const result = validateLoginSchema({ password: 'Password123@' });
test("should reject when both email and username are missing", () => {
const result = validateLoginSchema({ password: "Password123@" });
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error[0].message).toBe('Please provide either an email or a username');
expect(result.error[0].message).toBe("Please provide either an email or a username");
}
});

test('should reject when password is missing', () => {
const result = validateLoginSchema({ email: 'user@example.com' });
test("should reject when password is missing", () => {
const result = validateLoginSchema({ email: "user@example.com" });
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error[0].message).toBe('Required');
expect(result.error[0].message).toBe("Required");
}
});

test('should reject when email is empty string', () => {
const result = validateLoginSchema({ email: '', password: 'Password123@' });
test("should reject when email is empty string", () => {
const result = validateLoginSchema({ email: "", password: "Password123@" });
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error[0].message).toBe('Invalid email');
expect(result.error[0].message).toBe("Invalid email");
}
});

test('should reject when username is empty string', () => {
const result = validateLoginSchema({ username: '', password: 'Password123@' });
test("should reject when username is empty string", () => {
const result = validateLoginSchema({
username: "",
password: "Password123@",
});
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error[0].message).toBe('username must be at least 3 characters');
expect(result.error[0].message).toBe("username must be at least 3 characters");
}
});
});
Loading

0 comments on commit c64af28

Please sign in to comment.