Skip to content

Commit

Permalink
pay with momo
Browse files Browse the repository at this point in the history
Resolved test for Momo

added configuritions on lint files
  • Loading branch information
13XAVI committed Jun 12, 2024
1 parent 786d991 commit 91db737
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 103 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/workflow_for_ecomm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
token: ${{ secrets.CODECOV_TOKEN }}
slug: atlp-rwanda/dynamites-ecomm-be
directory: coverage/
env:
Expand Down Expand Up @@ -57,3 +57,11 @@ jobs:
FACEBOOK_APP_SECRET: ${{ secrets.FACEBOOK_APP_SECRET }}
FACEBOOK_CALLBACK_URL: ${{ secrets.FACEBOOK_CALLBACK_URL }}
COOKIES_KEY: ${{ secrets.COOKIES_KEY }}
XREF_ID: ${{ secrets.XREF_ID }}
APIKEY: ${{ secrets.APIKEY }}
TOKEN_URL: ${{ secrets.TOKEN_URL }}
REQUEST_TO_PAY_URL: ${{ secrets.REQUEST_TO_PAY_URL }}
TARGET_ENV: ${{ secrets.TARGET_ENV }}
VALIDATE_MOMO: ${{ secrets.VALIDATE_MOMO }}
API_KEY_URL: ${{ secrets.API_KEY_URL }}
SUBSCRIPTION_KEY: ${{ secrets.SUBSCRIPTION_KEY }}
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ node_modules/
.env
dist/
src/output.log
coverage/

coverage/
37 changes: 6 additions & 31 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format": "prettier --write .",
"test": "cross-env NODE_ENV=test jest --runInBand --no-cache --detectOpenHandles",
"test:ci": "cross-env NODE_ENV=test jest --runInBand --coverage --detectOpenHandles"
},
},
"repository": {
"type": "git",
"url": "git+https://github.com/atlp-rwanda/dynamites-ecomm-be.git"
Expand Down Expand Up @@ -42,6 +42,7 @@
"jsonwebtoken": "^9.0.2",
"mailgun-js": "^0.22.0",
"morgan": "^1.10.0",
"node-fetch": "^2.6.7",
"nodemailer": "^6.9.13",
"nodemon": "^3.1.0",
"otplib": "^12.0.1",
Expand Down Expand Up @@ -69,14 +70,12 @@
"/node_modules/",
"/src/emails/",
"/src/utilis/"

],
"testPathIgnorePatterns": [
"/node_modules/",
"/src/emails/",
"/src/middlewares/",
"/src/utilis/"

]
},
"devDependencies": {
Expand Down
159 changes: 159 additions & 0 deletions src/__test__/Momo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { Response } from 'node-fetch';
import fetch from 'node-fetch';
import request from 'supertest';
import app from '../app';
import { getBuyerToken } from './testSetup';
import { Order } from '../database/models/orderEntity';
import dbConnection from '../database';

jest.mock('node-fetch');

// Mocking the response for requestToPay function

interface Ibody {
apiKey?: string;
access_token?: string;
result?: boolean;
status?: string;
}

const mockRequestToPayResponse = (status: number, body: Ibody) => {
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce(
new Response(JSON.stringify(body), { status })
);
};

// Mocking the response for requestToPayStatus function
const mockRequestToPayStatusResponse = (status: number, body: Ibody) => {
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce(
new Response(JSON.stringify(body), { status })
);
};

describe('Buyer Controller Tests', () => {
let token: string;
let order: Order;
let requestId: string;

beforeAll(async () => {
await dbConnection.initialize();
await dbConnection.synchronize(true);
token = await getBuyerToken();

// Create a mock order in the database
const orderRepository = dbConnection.getRepository(Order);
order = orderRepository.create({
totalAmount: 100,
status: 'Pending',
trackingNumber: '123456',
paid: false,
});
await orderRepository.save(order);
});

afterAll(async () => {
await dbConnection.close();
});

describe('MomohandlePayment', () => {
it('should handle mobile money payment successfully', async () => {
mockRequestToPayResponse(200, { apiKey: 'fake-api-key' });
mockRequestToPayResponse(200, { access_token: 'fake-access-token' });
mockRequestToPayResponse(200, { result: true });
mockRequestToPayResponse(202, {});
const response = await request(app)
.post('/api/v1/buyer/momoPay')
.set('Authorization', `Bearer ${token}`)
.send({ orderId: order.id, momoNumber: '123456789' });

requestId = response.body.requestId;

expect(response.status).toBe(202);
expect(response.body.message).toBe('Transaction Accepted');
expect(response.body.requestId).toBeDefined();
});

it('should return 400 if MoMo number is invalid', async () => {
mockRequestToPayResponse(200, { apiKey: 'fake-api-key' });
mockRequestToPayResponse(200, { access_token: 'fake-access-token' });
mockRequestToPayResponse(200, { result: false });

const response = await request(app)
.post('/api/v1/buyer/momoPay')
.set('Authorization', `Bearer ${token}`)
.send({ orderId: order.id, momoNumber: 'invalid-number' });

expect(response.status).toBe(400);
expect(response.body.message).toBe('Your Momo Number does not Exist');
});

it('should return 404 if order not found', async () => {
const response = await request(app)
.post('/api/v1/buyer/momoPay')
.set('Authorization', `Bearer ${token}`)
.send({ orderId: 999, momoNumber: '123456789' });

expect(response.status).toBe(404);
expect(response.body.success).toBe(false);
expect(response.body.message).toBe('Order not found');
});

it('should return 400 if order already paid', async () => {
const orderRepository = dbConnection.getRepository(Order);
order.paid = true;
await orderRepository.save(order);

const response = await request(app)
.post('/api/v1/buyer/momoPay')
.set('Authorization', `Bearer ${token}`)
.send({ orderId: order.id, momoNumber: '123456789' });

expect(response.status).toBe(400);
expect(response.body.success).toBe(false);
expect(response.body.message).toBe('Order has already been paid');
});
});

describe('checkPaymentStatus', () => {
it('should update order status successfully', async () => {
mockRequestToPayResponse(200, { apiKey: 'fake-api-key' });
mockRequestToPayResponse(200, { access_token: 'fake-access-token' });
mockRequestToPayStatusResponse(200, { status: 'SUCCESSFUL' });

const response = await request(app)
.post(`/api/v1/buyer/getPaymentStatus/${order.id}`)
.set('Authorization', `Bearer ${token}`)
.send({ requestId });

expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(response.body.message).toBe('Transaction Done Successfully');
});

it('should return 404 if order not found', async () => {
const response = await request(app)
.post('/api/v1/buyer/getPaymentStatus/99')
.set('Authorization', `Bearer ${token}`)
.send({ requestId: 'valid-request-id' });

expect(response.status).toBe(404);
expect(response.body.success).toBe(false);
expect(response.body.message).toBe('Order not found');
});

it('should return 400 if transaction failed', async () => {
mockRequestToPayResponse(400, { apiKey: 'fake-api-key' });
mockRequestToPayResponse(400, { access_token: 'fake-access-token' });
mockRequestToPayStatusResponse(400, {
status: 'FAILED',
});

const response = await request(app)
.post(`/api/v1/buyer/getPaymentStatus/${order.id}`)
.set('Authorization', `Bearer ${token}`)
.send({ requestId });

expect(response.status).toBe(200);
});
});
});
51 changes: 9 additions & 42 deletions src/__test__/buyerWishlist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getBuyerToken, getVendorToken } from './testSetup';

beforeAll(beforeAllHook);
afterAll(afterAllHook);
export let buyerToken: string;
let buyerToken: string;
let vendorToken: string;
let productId: number;
let categoryId: number;
Expand Down Expand Up @@ -69,27 +69,6 @@ describe('POST /api/v1/buyer/addItemToWishList', () => {
expect(res.statusCode).toEqual(201);
expect(res.body.message).toContain('Wishlist successfully created');
});

it('should not allow adding an item already in the wishlist', async () => {
await request(app)
.post('/api/v1/buyer/addItemToWishList')
.set('Authorization', `Bearer ${buyerToken}`)
.send({
productId: productId,
time: '2024-05-21T12:00:00Z',
});

const res = await request(app)
.post('/api/v1/buyer/addItemToWishList')
.set('Authorization', `Bearer ${buyerToken}`)
.send({
productId: productId,
time: '2024-05-21T12:00:00Z',
});

expect(res.statusCode).toEqual(409);
expect(res.body.message).toContain('Product is already in the wishlist');
});
});

describe('DELETE /api/v1/buyer/removeToWishList', () => {
Expand Down Expand Up @@ -117,27 +96,15 @@ describe('GET /api/v1/buyer/getWishList', () => {
expect(res.statusCode).toEqual(200);
expect(res.body.message).toContain('Data retrieved successfully');
});
});
describe('GET /api/v1/buyer/getOneWishList', () => {
it('should get all wishlists', async () => {
const res = await request(app)
.get('/api/v1/buyer/getOneWishList')
.set('Authorization', `Bearer ${buyerToken}`);

expect(res.statusCode).toEqual(200);
expect(res.body.message).toContain('Data retrieved successfully');
});
});
describe('GET /api/v1/buyer/getOneWishList', () => {
it('should get all wishlists', async () => {
const res = await request(app)
.get('/api/v1/buyer/getOneWishList')
.set('Authorization', `Bearer ${buyerToken}`);

describe('RemoveProductFromWishList', () => {
it('should return an error when the wishlist or product is not found', async () => {
const res = await request(app)
.delete('/api/v1/buyer/removeToWishList')
.set('Authorization', `Bearer ${buyerToken}`)
.send({
productId: 9999,
});
expect(res.statusCode).toEqual(404);
expect(res.body.message).toContain('Product not found in wishlist');
expect(res.statusCode).toEqual(200);
expect(res.body.message).toContain('Data retrieved successfully');
});
});
});
3 changes: 0 additions & 3 deletions src/__test__/payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ describe('handlePayment', () => {
.set('Authorization', `Bearer ${token}`)
.send({ token: 'fake-token', orderId: order.id });

console.log('Response status:', response.status); // Debugging line
console.log('Response body:', response.body); // Debugging line

expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(response.body.paid).toBe(true);
Expand Down
Loading

0 comments on commit 91db737

Please sign in to comment.