Skip to content

Commit

Permalink
conflict resolusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndevu12 committed Jun 4, 2024
2 parents 795238d + 229fcb1 commit 9a1f94c
Show file tree
Hide file tree
Showing 34 changed files with 1,324 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ env:
GOOGLE_CLIENT_ID: ${{secrets.GOOGLE_CLIENT_ID}}
GOOGLE_CLIENT_SECRET: ${{secrets.GOOGLE_CLIENT_SECRET}}

TEST_USER_EMAIL: ${{secrets.TEST_USER_EMAIL}}
TEST_USER_PASS: ${{secrets.TEST_USER_PASS}}
TEST_VENDOR_EMAIL: ${{secrets.TEST_VENDOR_EMAIL}}
TEST_VENDOR1_EMAIL: ${{secrets.TEST_VENDOR1_EMAIL}}
TEST_BUYER_EMAIL: ${{secrets.TEST_BUYER_EMAIL}}
TEST_SAMPLE_BUYER_EMAIL: ${{secrets.TEST_SAMPLE_BUYER_EMAIL}}
TEST_VENDOR2_EMAIL: ${{secrets.TEST_VENDOR2_EMAIL}}

STRIPE_SECRET_KEY: ${{secrets.STRIPE_SECRET_KEYT}}

jobs:
Expand Down
9 changes: 5 additions & 4 deletions src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,9 @@ describe('Cart| Order management for guest/buyer', () => {
let productId: any;
let feedbackId: any;
let feedback2Id: any;

describe('Create order', () => {
it('should return 400 when user ID is not provided', async () => {
it('should return 201 when user is found', async () => {
const response = await request(app)
.post('/product/orders')
.send({
Expand Down Expand Up @@ -433,11 +434,11 @@ describe('Cart| Order management for guest/buyer', () => {
expect(response.body.message).toBe('Transaction history retrieved successfully');
});

it('should return 400 when user ID is not provided', async () => {
it('should return 400 when user is not AUTHORIZED', async () => {
const response = await request(app)
.get('/product/orders/history')
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
.set('Authorization', `Bearer ''`);
expect(response.status).toBe(403);
});
});

Expand Down
44 changes: 44 additions & 0 deletions src/__test__/isValid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { isTokenValide } from '../middlewares/isValid';
import { Request, Response, NextFunction } from 'express';
import { getRepository } from 'typeorm';
import { User } from '../entities/User';

jest.mock('typeorm', () => ({
...jest.requireActual('typeorm'),
getRepository: jest.fn().mockImplementation((entity: any) => {
if (entity === User) {
return {
findOne: jest.fn(),
};
}
return jest.requireActual('typeorm').getRepository(entity);
}),
}));

const mockRequest = (userPayload: any): Request => {
return {
cookies: { token: 'mockToken' },
user: userPayload,
} as unknown as Request;
};

const mockResponse = () => {
const res: any = {};
res.status = jest.fn().mockReturnValue(res);
res.json = jest.fn().mockReturnValue(res);
return res;
};

const mockNext = jest.fn();

describe('isTokenValide middleware', () => {
it('should return 401 if no user payload', async () => {
const req = mockRequest(null);
const res = mockResponse();

await isTokenValide(req as Request, res as Response, mockNext as NextFunction);

expect(res.status).toHaveBeenCalledWith(401);
expect(res.json).toHaveBeenCalledWith({ Message: 'Sorry, You are not authorized' });
});
});
83 changes: 83 additions & 0 deletions src/__test__/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import request from 'supertest';
import { app, server } from '../index';
import { createConnection, getRepository } from 'typeorm';
import { User, UserInterface } from '../entities/User';
import { cleanDatabase } from './test-assets/DatabaseCleanup';
import jwt from 'jsonwebtoken';
import { v4 as uuid } from 'uuid';
import { dbConnection } from '../startups/dbConnection';


const adminId = uuid();
const adminId1 = uuid();

const jwtSecretKey = process.env.JWT_SECRET || '';

const getAccessToken = (id: string, email: string) => {
return jwt.sign(
{
id: id,
email: email,
},
jwtSecretKey
);
};


if (!process.env.TEST_USER_EMAIL || !process.env.TEST_BUYER_EMAIL || !process.env.TEST_VENDOR1_EMAIL || !process.env.TEST_VENDOR_EMAIL || !process.env.TEST_USER_PASS) throw new Error('TEST_USER_PASS or TEST_USER_EMAIL not set in .env');

const sampleAdmin: UserInterface = {
id: adminId,
firstName: 'admin',
lastName: 'user',
email: process.env.TEST_USER_EMAIL,
password: process.env.TEST_USER_PASS,
userType: 'Admin',
gender: 'Male',
phoneNumber: '126380997',
photoUrl: 'https://example.com/photo.jpg',
verified: true,
role: 'ADMIN',
};

const sampleAdmin1: UserInterface = {
id: adminId1,
firstName: 'admin',
lastName: 'user',
email: '[email protected]',
password: process.env.TEST_USER_PASS,
userType: 'Admin',
gender: 'Male',
phoneNumber: '126380997',
photoUrl: 'https://example.com/photo.jpg',
verified: false,
role: 'ADMIN',
};

beforeAll(async () => {
const connection = await dbConnection();

const userRepository = connection?.getRepository(User);
await userRepository?.save([sampleAdmin, sampleAdmin1]);
});

afterAll(async () => {
await cleanDatabase();

server.close();
});

describe('POST /user/login', () => {
it('should not login a user with unverified email', async () => {

const loginUser = {
email: '[email protected]',
password: process.env.TEST_USER_LOGIN_PASS,
};

const loginResponse = await request(app).post('/user/login').send(loginUser);

expect(loginResponse.status).toBe(400);
expect(loginResponse.body).toBeDefined();
});
});
Loading

0 comments on commit 9a1f94c

Please sign in to comment.