Skip to content

Commit

Permalink
hot fix: return beforeEach
Browse files Browse the repository at this point in the history
  • Loading branch information
YehiaFarghaly committed Jan 2, 2024
1 parent c7386bb commit fb1e339
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 53 deletions.
71 changes: 28 additions & 43 deletions authentication/src/tests/api-tests/AuthenticationAPI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import request from 'supertest';
import app from '../../../app.js';
import {
connectDBTest,
disconnectDBTest,
dropDBTest
disconnectDBTest
} from '../../utils/TestingUtils.js';
import User from '../../database/models/Users.js';
import { describe, beforeAll, afterAll, expect, it, jest } from '@jest/globals';
import { describe, beforeEach, afterEach, expect, it, jest } from '@jest/globals';
import generateUser from '../model-generators/generateUser.js';
import { faker } from '@faker-js/faker';
import axios from 'axios';
Expand All @@ -15,7 +14,7 @@ jest.mock('axios');
import bcrypt from 'bcrypt'
describe('POST /signup/:request', () => {

beforeAll(async () => {
beforeEach(async () => {
await connectDBTest();
});

Expand Down Expand Up @@ -49,19 +48,15 @@ describe('POST /signup/:request', () => {
expect(JSON.parse(res.text).message).toBe(DUB_EMAIL_ERROR_MESSAGE);
});

afterAll(async () => {
afterEach(async () => {
await disconnectDBTest();
})

afterAll(async () => {
await dropDBTest();
})
})


describe('DELETE /users/:id', () => {

beforeAll(async () => {
beforeEach(async () => {
await connectDBTest();
});

Expand Down Expand Up @@ -90,19 +85,15 @@ describe('DELETE /users/:id', () => {
expect(newDataBaseRecord.length).toBe(oldDataBaseRecord.length);
});

afterAll(async () => {
afterEach(async () => {
await disconnectDBTest();
})

afterAll(async () => {
await dropDBTest();
})

})

describe('POST /doctors', () => {

beforeAll(async () => {
beforeEach(async () => {
await connectDBTest();
});

Expand All @@ -117,23 +108,15 @@ describe('POST /doctors', () => {
expect(databaseRecord[databaseRecord.length - 1].userName).toBe(userName);
});

afterAll(async () => {
afterEach(async () => {
await disconnectDBTest();
})

afterAll(async () => {
await dropDBTest();
})

afterAll(async () => {
await dropDBTest();
})

})

describe('POST /pharmacists', () => {

beforeAll(async () => {
beforeEach(async () => {
await connectDBTest();
});

Expand All @@ -148,19 +131,15 @@ describe('POST /pharmacists', () => {
expect(databaseRecord[databaseRecord.length - 1].userName).toBe(userName);
});

afterAll(async () => {
afterEach(async () => {
await disconnectDBTest();
})

afterAll(async () => {
await dropDBTest();
})

})

describe('POST /admins/:request', () => {

beforeAll(async () => {
beforeEach(async () => {
await connectDBTest();
});

Expand Down Expand Up @@ -203,19 +182,15 @@ describe('POST /admins/:request', () => {
expect(JSON.parse(res.text).message).toBe(DUB_USERNAME_ERROR_MESSAGE);
});

afterAll(async () => {
afterEach(async () => {
await disconnectDBTest();
})

afterAll(async () => {
await dropDBTest();
})

})

describe('POST /login/:request', () => {

beforeAll(async () => {
beforeEach(async () => {
await connectDBTest();

});
Expand Down Expand Up @@ -250,12 +225,22 @@ describe('POST /login/:request', () => {
expect(JSON.parse(res.text).message).toBe(INCORRECT_PASSWORD_ERROR_MESSAGE);
});

afterAll(async () => {
await disconnectDBTest();
})
it('should return 200 the correct user', async () => {
const userId = faker.database.mongodbObjectId();
const email = faker.internet.email();
const userName = faker.internet.userName();
const password = faker.internet.password();
const salt = await bcrypt.genSalt();
const HashedPassword = await bcrypt.hash(password, salt);
const user = new User(generateUser(userId, email, userName, PATIENT_ENUM, HashedPassword));
await user.save();
const res = await request(app).post(`/login/${CLINIC_REQ}`).send({ userName, password });
expect(res.status).toBe(OK_REQUEST_CODE_200);
expect(JSON.parse(res.text).userName).toBe(userName);
});

afterAll(async () => {
await dropDBTest();
afterEach(async () => {
await disconnectDBTest();
})

})
13 changes: 3 additions & 10 deletions authentication/src/utils/TestingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ const connectDBTest = async () => {
try{
const mongoURL = process.env.MONGO_URI_TEST;
await mongoose.connect(mongoURL);
await mongoose.connection.db.dropDatabase();
} catch(err){
console.error('Error connecting to the database:', err.message);
}
};

const disconnectDBTest = async () => {
try{
await mongoose.connection.db.dropDatabase();
await mongoose.disconnect();
} catch(err){
console.error('Error connecting to the database:', err.message);
}
};

const dropDBTest = async () => {
try {
await mongoose.connection.db.dropDatabase();

} catch (err) {
console.error('error dropping the db', err)
}
}

export { connectDBTest, disconnectDBTest, dropDBTest };
export { connectDBTest, disconnectDBTest };

0 comments on commit fb1e339

Please sign in to comment.