Skip to content

Commit

Permalink
Merge pull request #7 from Badhan-BUET-Zone/masum/#6/test-donations-r…
Browse files Browse the repository at this point in the history
…eport

Tests for donation report route
  • Loading branch information
mirmahathir1 authored May 5, 2024
2 parents 869fb59 + 66b4776 commit d74077e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
`badhan-test` contains the automated API testing script written using Node.JS. The API being tested is currently the active backend for Badhan, BUET Zone Android app (https://play.google.com/store/apps/details?id=com.mmmbadhan) and Website (https://badhan-buet.web.app)

### Run the tests
* Create `config` directory and paste `config.js` inside it (Get the `config.js` file from the system administrators)
* Create `config` directory and paste `config.js` inside it (Get the `config.js` file from the system administrators)

If you have Nodejs installed, you can run the test by following command
* `npm install` or `npm i`
* `npm run test` or `npm t`

To run only a single test file, you can use the following command
* `npm test -- <path to the test file>`.
Example: `npm test -- signIn/success` or `npm t -- signIn/success` in short.

### Run Test the tests using Docker

* Install Docker.
* Run `bin/install`
* Run `bin/up` to run all tests at once in series
Expand Down
109 changes: 109 additions & 0 deletions tests/donations/report.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const {badhanAxios} = require('../../api');
const validate = require('jsonschema').validate;
const env = require('../../config');
const {processError} = require('../fixtures/helpers');

const getReportsSchema = {
type: "object",
additionalProperties: false,
properties: {
status: {type: "string"},
statusCode: {const: 200},
message: {type: "string"},
report: {
type: "array",
minItems: 1,
items: {
type: "object",
additionalProperties: false,
properties: {
counts: {
type: "array",
minItems: 1,
items: {
type: "object",
additionalProperties: false,
properties: {
"month": {type: "integer"},
"year": {type: "integer"},
"count": {type: "integer"}
},
required: ["month", "year", "count"]
}
},
bloodGroup: {type: "integer"},
}
}
},
firstDonationCount: {type: "integer"},
},
required: ["status", "statusCode", "message", "report", "firstDonationCount"]
}

const invalidRequestSchema = {
type: "object",
additionalProperties: false,
properties: {
status: {type: "string"},
statusCode: {const: 400},
message: {type: "string"},
},
required: ["status", "statusCode", "message"]
}

// success
test('GET/donations/report success', async () => {
try {
let signInResponse = await badhanAxios.post('/users/signin', {
phone: env.SUPERADMIN_PHONE,
password: env.SUPERADMIN_PASSWORD
});
const authHeader = {headers: {"x-auth": signInResponse.data.token}};

const donorId = (await badhanAxios.get('/users/me', authHeader)).data.donor._id;

const donationDate = new Date().getTime();
await badhanAxios.post("/donations",{donorId: donorId,date: donationDate,}, authHeader);

// start date 3 months before donation date
const startDate = new Date(donationDate - 3 * 30 * 24 * 60 * 60 * 1000).getTime();
let getReportsResponse = await badhanAxios.get(`/donations/report?startDate=${startDate}&endDate=${donationDate}`, authHeader);
expect(validate(getReportsResponse.data, getReportsSchema).errors).toEqual([]);

await badhanAxios.delete(`/donations?donorId=${donorId}&date=${donationDate}`, authHeader);
await badhanAxios.delete('/users/signout', authHeader);

} catch (e) {
throw processError(e);
}
})

// invalid request test
test('GET/donations/report invalid request', async () => {
// no date query params
try {
await badhanAxios.get('/donations/report');
} catch (e) {
let validationResult = validate(e.response.data, invalidRequestSchema);
expect(validationResult.errors).toEqual([]);
}

// invalid start date
try {
await badhanAxios.get('/donations/report?startDate=2&endDate=1717113600000');
} catch (e) {
let validationResult = validate(e.response.data, invalidRequestSchema);
expect(validationResult.errors).toEqual([]);
}

// invalid end date
try {
await badhanAxios.get('/donations/report?startDate=1707237110000&endDate=2');
} catch (e) {
let validationResult = validate(e.response.data, invalidRequestSchema);
expect(validationResult.errors).toEqual([]);
}
})



0 comments on commit d74077e

Please sign in to comment.