Skip to content

Commit

Permalink
Finalized Unit tests and Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itsAdee committed May 11, 2024
1 parent f9d982b commit 12cf38d
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 20 deletions.
2 changes: 1 addition & 1 deletion StorageMgmtServ/models/__test__/Integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('UserStorage and Image Integration', () => {
img,
});

// Update the usedStorage field manually

await UserStorageModel.findOneAndUpdate(
{ userID },
{ $inc: { usedStorage: imageSize } }
Expand Down
3 changes: 0 additions & 3 deletions StorageMgmtServ/models/__test__/User.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,4 @@ describe('UserStorage Model', () => {

expect(updatedUserStorage.usedStorage).toBe(initialUsedStorage + additionalUsedStorage);
});

// Add more test cases as needed

});
1 change: 0 additions & 1 deletion StorageMgmtServ/models/__test__/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ describe('Image Model', () => {
expect(foundImage.updatedAt).toEqual(createdImage.updatedAt);
});

// Add more test cases as needed

});
2 changes: 1 addition & 1 deletion UsageMntrServ/models/__test__/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('UsageRequest Model Test', () => {
const validUsageRequest = new UsageRequest(usageRequestData);
const savedUsageRequest = await validUsageRequest.save();

// Object Id should be defined when successfully saved to MongoDB.

expect(savedUsageRequest._id).toBeDefined();
expect(savedUsageRequest.userID).toBe(usageRequestData.userID);
expect(savedUsageRequest.usedBandwidth).toBe(usageRequestData.usedBandwidth);
Expand Down
109 changes: 104 additions & 5 deletions UsageMntrServ/routes/__test__/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ it("returns 404 when id is not available", async () => {
it("returns 200 when creating a new user", async () => {
const newUserId = '507f1f77bcf86cd799439011';
const response = await request(app)
.post('/api/usageMntr/createUser') // Replace with the correct route
.post('/api/usageMntr/createUser')
.send({ newUserId })
.expect(201);

Expand All @@ -33,7 +33,7 @@ it('returns 201 when updating the usage bandwidth', async () => {
const userID = '507f1f77bcf86cd799439011';
const bandwidth = 1000;

// Create a daily usage instance before running the test

await new DailyUsage({ userID, usedBandwidth: 0, totalBandwidth: 25000000 }).save();

const response = await request(app)
Expand All @@ -48,13 +48,13 @@ it('creates a new user and updates the usage bandwidth', async () => {
const userID = '507f1f77bcf86cd799439011';
const bandwidth = 1000;

// Create a new user

await request(app)
.post('/api/usageMntr/createUser')
.send({ userID })
.expect(201);

// Update the usage bandwidth

const response = await request(app)
.post('/api/usageMntr/updateUsage')
.send({ userID, bandwidth });
Expand All @@ -69,7 +69,7 @@ describe('Check Daily Usage', () => {
const userID = '507f1f77bcf86cd799439011';
const bandwidth = 1000;

// Create a new user

await request(app)
.post('/api/usageMntr/createUser')
.send({ userID });
Expand All @@ -88,4 +88,103 @@ describe('Check Daily Usage', () => {
expect(res.body).toEqual([]);
})

});

describe('Check Monthly Usage', () => {
it ('returns 200 when getting the monthly usage', async () => {
const userID = '507f1f77bcf86cd799439011';
const bandwidth = 1000;


await request(app)
.post('/api/usageMntr/createUser')
.send({ userID });

const response = await request(app)
.get(`/api/usageMntr/usage/user/${userID}/month`);
expect(response.status).toEqual(200);
expect(response.body).toEqual([]);


});

it("should return 500 when the user is not found", async () => {
const nonExistentUserID = '507f1f77bcf86cd799439011';
const res = await request(app).get(`/api/usageMntr/usage/user/${nonExistentUserID}/month`);
expect(res.status).toEqual(200);
expect(res.body).toEqual([]);
})

it("should return the usage object when the user is found", async () => {
const userID = '507f1f77bcf86cd799439011';
const bandwidth = 1000;


await request(app)
.post('/api/usageMntr/createUser')
.send({ userID })
.expect(201);


const response = await request(app)
.post('/api/usageMntr/updateUsage')
.send({ userID, bandwidth });

const res = await request(app).get(`/api/usageMntr/usage/user/${userID}/month`);
expect(res.status).toEqual(200);
res.body.forEach(item => {
expect(item).toHaveProperty('downloadBandwidth');
expect(item).toHaveProperty('uploadBandwidth');
});
});
});


describe('Check Yearly Usage', () => {
it ('returns 200 when getting the yearly usage', async () => {
const userID = '507f1f77bcf86cd799439011';
const bandwidth = 1000;


await request(app)
.post('/api/usageMntr/createUser')
.send({ userID });

const response = await request(app)
.get(`/api/usageMntr/usage/user/${userID}/year`);
expect(response.status).toEqual(200);
expect(response.body).toEqual([]);


});

it("should return 500 when the user is not found", async () => {
const nonExistentUserID = '507f1f77bcf86cd799439011';
const res = await request(app).get(`/api/usageMntr/usage/user/${nonExistentUserID}/year`);
expect(res.status).toEqual(200);
expect(res.body).toEqual([]);
})

it("should return the usage object when the user is found", async () => {
const userID = '507f1f77bcf86cd799439011';
const bandwidth = 1000;


await request(app)
.post('/api/usageMntr/createUser')
.send({ userID })
.expect(201);


const response = await request(app)
.post('/api/usageMntr/updateUsage')
.send({ userID, bandwidth });

const res = await request(app).get(`/api/usageMntr/usage/user/${userID}/year`);
expect(res.status).toEqual(200);
res.body.forEach(item => {
expect(item).toHaveProperty('downloadBandwidth');
expect(item).toHaveProperty('uploadBandwidth');
});
});
});
6 changes: 2 additions & 4 deletions UserAccMgmtServ/models/__test__/models.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const mongoose = require('mongoose');
const User = require('../user'); // Import your User model

const User = require('../user');
describe('User Model Test', () => {


Expand All @@ -9,12 +8,11 @@ describe('User Model Test', () => {
const validUser = new User(userData);
const savedUser = await validUser.save();

// Object Id should be defined when successfully saved to MongoDB.
expect(savedUser._id).toBeDefined();
expect(savedUser.username).toBe(userData.username);
expect(savedUser.password).toBe(userData.password);
expect(savedUser.email).toBe(userData.email);
});

// You should add more tests to validate your schema.

});
9 changes: 4 additions & 5 deletions UserAccMgmtServ/routes/__test__/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ const request = require('supertest');

const bcrypt = require('bcrypt');
const axios = require('axios');
const User = require('../../models/user'); // adjust path as needed
const { CreateUser } = require('../../controllers/userController'); // adjust path as needed

const User = require('../../models/user');
const { CreateUser } = require('../../controllers/userController');
jest.mock('bcrypt');
jest.mock('axios');
jest.mock('../../models/user'); // adjust path as needed
jest.mock('../../models/user');

const app = require('../../app').app; // adjust path as needed
const app = require('../../app').app;

describe('Register User', () => {
it('should return 400 if username, password, email, or confirm password not provided', async () => {
Expand Down

0 comments on commit 12cf38d

Please sign in to comment.