Skip to content

Instantly share code, notes, and snippets.

@isheraz
Created January 4, 2023 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isheraz/7fad9935b828d5f07cc0b211db70933b to your computer and use it in GitHub Desktop.
Save isheraz/7fad9935b828d5f07cc0b211db70933b to your computer and use it in GitHub Desktop.
const request = require('supertest');
const {
randomNumber,
randomStr,
} = require('../helpers');
const {
app,
server
} = require("../server");
const PASSWORDS = {
weak: 'asdf1234',
medium: 'AsDfGh123456',
strong: 'AsDfGhJ!2#4%6&8'
};
const PWD_CRITERIA = ['Minimum 1 uppercase letter.',
'Minimum 1 lowercase letter.',
'Minimum 1 special character.',
'Minimum 1 number.',
'Minimum 6 characters.',
'Maximum 30 characters.'
];
const mockPayload = {
firstName: 'Muhammad',
lastName: 'Abdullah',
email: 'm.adb@invo.zone',
password: PASSWORDS.strong,
dob: new Date('1993-12-29')
}
const mockWrongEmail = '@invozone.com'
const fixtureEmailErrors = {
notUnique: 'email unique violation',
validation: `${mockWrongEmail} does not comply RFC2822 Standards`
}
const mockRegister = {
message: 'User registered successfully'
}
const mockLogin = {
message: `logged in successfully`
}
describe('NodeJS JWT Authentication', () => {
beforeEach(() => {
jest.setTimeout(10000);
});
beforeAll(async () => {
const res = await request(app)
.post('/api/register')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(mockPayload);
console.table({
initialUser: res.body
});
});
describe('API Endpoints Register', () => {
it('POST /api/register missing parameter error', async () => {
const payload = mockPayload;
const modPayload = JSON.parse(JSON.stringify(payload));
const deleteIdx = randomNumber(0, 3);
delete modPayload[(Object.keys(payload))[deleteIdx]]
const res = await request(app)
.post('/api/register')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(modPayload);
expect(res.statusCode).toEqual(400);
expect(res.body.error).toEqual([Object.keys(payload)[deleteIdx]]);
});
it('POST /api/register weak password error', async () => {
const payload = {
...mockPayload,
password: PASSWORDS.weak
};
const res = await request(app)
.post('/api/register')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(payload);
expect(res.statusCode).toEqual(400);
expect(res.body.error).toEqual(PWD_CRITERIA);
});
it('POST /api/register invalid email error', async () => {
const payload = {
...mockPayload,
email: mockWrongEmail,
};
const res = await request(app)
.post('/api/register')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(payload);
expect(res.statusCode).toEqual(400);
expect(res.body.error).toEqual(fixtureEmailErrors.validation);
});
it('POST /api/register unique email address violation', async () => {
const res = await request(app)
.post('/api/register')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(mockPayload);
expect(res.statusCode).toEqual(400);
expect(res.body.error).toEqual(fixtureEmailErrors.notUnique);
});
it('POST /api/register should register user successfully and return authToken', async () => {
const payload = {
...mockPayload,
firstName: randomStr(6),
email: `${randomStr(5)}@${randomStr(4)}.com`
}
const res = await request(app)
.post('/api/register')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(payload);
expect(res.statusCode).toEqual(200);
expect(res.body.message).toEqual(mockRegister.message);
expect(res.body.accessToken).toBeTruthy();
// expect(res.body.refreshToken).notNull();
})
});
describe('API Endpoints Login', () => {
it('POST /api/login login successfully', async () => {
const payload = {
email: 'm.adb@invo.zone',
password: PASSWORDS.strong,
};
const res = await request(app)
.post('/api/login')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(payload);
expect(res.statusCode).toEqual(200);
expect(res.body.message).toEqual(mockLogin.message);
expect(res.body.accessToken).toBeTruthy();
});
});
xdescribe('API Endpoints Reset Password', () => {
it('POST /api/reset send email for password reset', async () => {
const payload = {
email: 'm.adb@invo.zone',
};
const res = await request(app)
.post('/api/login')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(payload);
expect(res.statusCode).toEqual(200);
expect(res.body.message).toEqual(mockLogin.message);
expect(res.body.accessToken).toBeTruthy();
});
});
afterEach(async () => {
server.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment