Skip to content

Commit

Permalink
Add more tests and improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sans-arch committed Nov 26, 2023
1 parent a147d8e commit d5096fd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const config: Config.InitialOptions = {
testMatch: [`${baseTestDir}/**/*.ts`],
testPathIgnorePatterns: [
'/node_modules/',
'/docker-app_data-pc-host/',
`${baseDir}/@types`,
`${baseDir}/repositories/prisma.ts`,
`${baseDir}/repositories/`,
Expand Down
16 changes: 12 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"license": "MIT",
"dependencies": {
"@prisma/client": "^5.5.2",
"@types/node": "^20.10.0",
"axios": "^1.5.0",
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
Expand Down
7 changes: 1 addition & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import listsRoute from './app/routes/listsRoute';
dotenv.config();

const app = express();
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
optionsSuccessStatus: 200
}));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

Expand Down
11 changes: 8 additions & 3 deletions src/tests/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UserRepository } from '../../app/repositories/UserRepository/types';
import { UserService } from '../../app/services/userService'
import { User } from '../../app/models/UserModel'
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';

describe('UserService test suite', () => {
let userService: UserService;
Expand Down Expand Up @@ -112,13 +113,17 @@ describe('UserService test suite', () => {
});

it('should be possible to validate an valid token', async () => {
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiZW1haWwiOiJqb2huQGRvZS5jb20iLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3MDA5NDIyMzIsImV4cCI6MTcwMDk0NTgzMn0.TcmOfashr8yntuRUf5mNvErjd9nB8JuRwHrZSaLfdWo';
const jwtSecret = String(process.env.JWT_SECRET);

await expect(userService.validateToken(token)).resolves.toHaveProperty('id');
const token = jwt.sign({ id: 1, email: 'carlos@test.com', name: 'Carlos' }, jwtSecret);

await expect(userService.validateToken(token)).resolves.toBeTruthy();
});

it('should be possible to validate an invalid token', async () => {
const token = 'eyJhbGciOiJIUzI1NiIsIdJKASJKJKAJSdWo';
const jwtSecret = 'WrongSecret';

const token = jwt.sign('test', jwtSecret);

await expect(userService.validateToken(token)).rejects.toThrow();
});
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"typeRoots": ["./node_modules/@types", "./@types/"],
"noImplicitAny": false
},
"include": ["src/server.ts", "src/app/**/*.ts"],
"include": ["src/server.ts", "src/app/**/*.ts", "src/tests/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit d5096fd

Please sign in to comment.