Skip to content

Commit

Permalink
update README with code coverage stats, refactor ESLint config to Com…
Browse files Browse the repository at this point in the history
…monJS, and add Token and Prisma tests
  • Loading branch information
deveshsangwan committed Feb 23, 2025
1 parent 189f062 commit 17a3378
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Welcome to the Cricket Score API! This project is designed to provide real-time
Our aim is to maintain high code coverage to ensure the quality of the project. Here are our current stats:

[![codecov](https://codecov.io/gh/deveshsangwan/cricketScoreApi/graph/badge.svg?token=A3JMLLNTG4)](https://codecov.io/gh/deveshsangwan/cricketScoreApi)
![Functions](https://img.shields.io/badge/functions-92.98%25-brightgreen.svg?style=flat)
![Lines](https://img.shields.io/badge/lines-85.35%25-yellow.svg?style=flat)
![Functions](https://img.shields.io/badge/functions-94.73%25-brightgreen.svg?style=flat)
![Lines](https://img.shields.io/badge/lines-86.74%25-yellow.svg?style=flat)

## 🚀 Getting Started

Expand Down
6 changes: 3 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typeScriptParser from '@typescript-eslint/parser';
import globals from 'globals';
const typeScriptParser = require('@typescript-eslint/parser');
const globals = require('globals');

export default [
module.exports = [
{
languageOptions: {
parser: typeScriptParser,
Expand Down
105 changes: 105 additions & 0 deletions test/Token.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { assert } from 'chai';
import sinon from 'sinon';
import jwt, { SignOptions } from 'jsonwebtoken';
import { Token } from '../app/dist/src/Token';
import * as Logger from '../app/dist/core/Logger';
import config from '../app/dist/core/configuration';

describe('Token', () => {
let sandbox: sinon.SinonSandbox;
const validSecret = 'test-secret';
const validClientId = 'test-client-id';
const validClientSecret = 'test-client-secret';
const validTokenExpiry = '1h';

beforeEach(() => {
sandbox = sinon.createSandbox();

// Mock environment variables
process.env.SECRET_KEY = validSecret;
process.env.CLIENT_ID = validClientId;
process.env.CLIENT_SECRET = validClientSecret;

// Mock config
sandbox.stub(config, 'get').returns(validTokenExpiry);

// Mock logger
sandbox.stub(Logger, 'writeLogError');
});

afterEach(() => {
sandbox.restore();
delete process.env.SECRET_KEY;
delete process.env.CLIENT_ID;
delete process.env.CLIENT_SECRET;
});

describe('constructor', () => {
it('should initialize successfully with valid environment variables', () => {
assert.doesNotThrow(() => new Token());
});

it('should throw error when environment variables are missing', () => {
delete process.env.SECRET_KEY;
assert.throws(() => new Token(), 'Missing required environment variables');
});
});

describe('generateToken', () => {
let token: Token;

beforeEach(() => {
token = new Token();
});

it('should generate valid token with correct credentials', () => {
const credentials = {
clientId: validClientId,
clientSecret: validClientSecret
};

const jwtSignStub = sandbox.stub(jwt, 'sign').callsFake(() => 'valid-token');

const result = token.generateToken(credentials);

assert.equal(result, 'valid-token');
assert.isTrue(jwtSignStub.calledOnce);
assert.deepEqual(jwtSignStub.firstCall.args[0], { clientId: validClientId });
assert.equal(jwtSignStub.firstCall.args[1], validSecret);
assert.deepEqual(jwtSignStub.firstCall.args[2], {
algorithm: 'HS256' as jwt.Algorithm,
expiresIn: validTokenExpiry
} as SignOptions);
});

it('should throw error with invalid clientId', () => {
const credentials = {
clientId: 'invalid-client-id',
clientSecret: validClientSecret
};

assert.throws(() => token.generateToken(credentials), 'Invalid credentials');
});

it('should throw error with invalid clientSecret', () => {
const credentials = {
clientId: validClientId,
clientSecret: 'invalid-client-secret'
};

assert.throws(() => token.generateToken(credentials), 'Invalid credentials');
});

it('should handle jwt.sign errors', () => {
const credentials = {
clientId: validClientId,
clientSecret: validClientSecret
};

const error = new Error('JWT sign error');
sandbox.stub(jwt, 'sign').throws(error);

assert.throws(() => token.generateToken(credentials), error.message);
});
});
});
31 changes: 17 additions & 14 deletions test/Prisma.test.ts → test/core/Prisma.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai';
import { assert } from 'chai';
import sinon from 'sinon';

describe('Prisma Client', () => {
Expand All @@ -10,7 +10,7 @@ describe('Prisma Client', () => {
// Backup original env
originalEnv = { ...process.env };
// Clear module cache to test initialization
delete require.cache[require.resolve('../app/dist/core/prisma')];
delete require.cache[require.resolve('../../app/dist/core/prisma')];
// Clear global prisma instance
delete global.prisma;

Expand Down Expand Up @@ -44,22 +44,25 @@ describe('Prisma Client', () => {
// Clear module cache
delete require.cache[require.resolve('@prisma/client')];
delete require.cache[require.resolve('@prisma/extension-optimize')];
delete require.cache[require.resolve('../app/dist/core/prisma')];
delete require.cache[require.resolve('../../app/dist/core/prisma')];
});

it('should throw error when DATABASE_URL is missing', () => {
delete process.env.DATABASE_URL;
expect(() => require('../app/dist/core/prisma')).to.throw('DATABASE_URL environment variable is required');
assert.throws(
() => require('../../app/dist/core/prisma'),
'DATABASE_URL environment variable is required'
);
});

it('should create basic client when only DATABASE_URL is present', () => {
process.env.DATABASE_URL = 'test-database-url';
delete process.env.OPTIMIZE_API_KEY;

require('../app/dist/core/prisma');
require('../../app/dist/core/prisma');

expect(prismaStub.calledOnce).to.be.true;
expect(prismaStub.firstCall.args[0]).to.deep.equal({
assert.isTrue(prismaStub.calledOnce);
assert.deepEqual(prismaStub.firstCall.args[0], {
datasources: {
db: {
url: 'test-database-url'
Expand All @@ -72,10 +75,10 @@ describe('Prisma Client', () => {
process.env.DATABASE_URL = 'test-database-url';
process.env.OPTIMIZE_API_KEY = 'test-optimize-key';

require('../app/dist/core/prisma');
require('../../app/dist/core/prisma');

expect(optimizeStub.calledOnce).to.be.true;
expect(optimizeStub.firstCall.args[0]).to.deep.equal({
assert.isTrue(optimizeStub.calledOnce);
assert.deepEqual(optimizeStub.firstCall.args[0], {
apiKey: 'test-optimize-key'
});
});
Expand All @@ -84,17 +87,17 @@ describe('Prisma Client', () => {
process.env.DATABASE_URL = 'test-database-url';
process.env.NODE_ENV = 'development';

require('../app/dist/core/prisma');
require('../../app/dist/core/prisma');

expect(global.prisma).to.not.be.undefined;
assert.isDefined(global.prisma);
});

it('should not save prisma reference to global in production', () => {
process.env.DATABASE_URL = 'test-database-url';
process.env.NODE_ENV = 'production';

require('../app/dist/core/prisma');
require('../../app/dist/core/prisma');

expect(global.prisma).to.be.undefined;
assert.isUndefined(global.prisma);
});
});

0 comments on commit 17a3378

Please sign in to comment.