Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

94 buyer should be able to pay via mtn mobile money #118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]
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/
48 changes: 17 additions & 31 deletions package-lock.json

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

6 changes: 3 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 All @@ -89,6 +88,7 @@
"@types/jsonwebtoken": "^9.0.6",
"@types/mailgun-js": "^0.22.18",
"@types/morgan": "^1.9.9",
"@types/node-fetch": "^2.6.11",
"@types/nodemailer": "^6.4.15",
"@types/passport": "^1.0.16",
"@types/passport-facebook": "^3.0.3",
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);
});
});
});
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
Loading