Wrote a couple simple tests
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
describe('POST /auth/login', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
|
||||
//fix util with jest (used by bcrypt's compare)
|
||||
jest.doMock('util', () => ({
|
||||
promisify: f => async () => f()
|
||||
}));
|
||||
|
||||
//mock out bcrypt
|
||||
jest.doMock('bcryptjs', () => ({
|
||||
genSalt: async amount => {
|
||||
expect(amount).toBe(11);
|
||||
return 'salt';
|
||||
},
|
||||
hash: async (password, salt) => {
|
||||
expect(password).toBe('password');
|
||||
return 'hashed-password';
|
||||
},
|
||||
compare: (lhs, rhs) => {
|
||||
return lhs === rhs;
|
||||
}
|
||||
}));
|
||||
|
||||
//mock out jsonwebtoken
|
||||
jest.doMock('jsonwebtoken', () => ({
|
||||
sign: (content, secretAccess, opts) => {
|
||||
return JSON.stringify(content);
|
||||
},
|
||||
|
||||
verify: (token, secretAccess, callback) => {
|
||||
return callback(null, JSON.parse(token));
|
||||
},
|
||||
}));
|
||||
|
||||
//mock out the sequelize library
|
||||
jest.doMock('sequelize', () => {
|
||||
return {
|
||||
Op: {
|
||||
//
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//mock out the database object
|
||||
jest.doMock('../../server/database', () => {
|
||||
const mSequelize = {
|
||||
authenticate: jest.fn(),
|
||||
define: jest.fn(),
|
||||
};
|
||||
|
||||
const actualSequelize = jest.requireActual('sequelize');
|
||||
return { Sequelize: jest.fn(() => mSequelize), DataTypes: actualSequelize.DataTypes };
|
||||
});
|
||||
|
||||
//mock out the database models
|
||||
jest.doMock('../../server/database/models', () => ({
|
||||
accounts: {
|
||||
findOne: async (config) => { //can't find any (signup state)
|
||||
expect(config?.where?.email).toBe('email@example.com');
|
||||
return {
|
||||
index: 0,
|
||||
email: config?.where?.email,
|
||||
username: 'username',
|
||||
type: 'alpha',
|
||||
admin: false,
|
||||
mod: false,
|
||||
};
|
||||
},
|
||||
|
||||
update: async (values, config) => {
|
||||
//Do nothing
|
||||
}
|
||||
},
|
||||
|
||||
tokens: {
|
||||
create: async (record) => {
|
||||
//Do nothing
|
||||
}
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
test('Basic valid login attempt', async () => {
|
||||
//arguments
|
||||
const req = {
|
||||
body: {
|
||||
email: 'email@example.com',
|
||||
password: 'password',
|
||||
}
|
||||
};
|
||||
|
||||
const res = {
|
||||
status: code => {
|
||||
expect(code).toBe(200);
|
||||
return {
|
||||
json: tokens => {
|
||||
//decode and analyze the JWT payload
|
||||
const accessToken = JSON.parse(tokens.accessToken);
|
||||
|
||||
expect(accessToken.email).toBe('email@example.com');
|
||||
expect(accessToken.username).toBe('username');
|
||||
},
|
||||
send: msg => { throw msg; },
|
||||
end: () => null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//test
|
||||
const route = require('../../server/auth/login');
|
||||
|
||||
const result = await route(req, res);
|
||||
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,98 @@
|
||||
describe('POST /auth/signup', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
|
||||
//mock out bcrypt
|
||||
jest.doMock('bcryptjs', () => ({
|
||||
genSalt: async amount => {
|
||||
expect(amount).toBe(11);
|
||||
return 'salt';
|
||||
},
|
||||
hash: async (password, salt) => {
|
||||
expect(password).toBe('password');
|
||||
return 'hashed-password';
|
||||
}
|
||||
}));
|
||||
|
||||
//mock out nodemailer
|
||||
jest.doMock('nodemailer', () => ({
|
||||
createTransport: jest.fn(config => {
|
||||
//TODO: test config?
|
||||
return { //return a fake transport object
|
||||
sendMail: async email => {
|
||||
expect(email.to).toBe('email@example.com');
|
||||
return { //return a fake info object
|
||||
accepted: [ email.to ]
|
||||
}
|
||||
}
|
||||
};
|
||||
}),
|
||||
}));
|
||||
|
||||
//mock out the sequelize library
|
||||
jest.doMock('sequelize', () => {
|
||||
return {
|
||||
Op: {
|
||||
//
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//mock out the database object
|
||||
jest.doMock('../../server/database', () => {
|
||||
const mSequelize = {
|
||||
authenticate: jest.fn(),
|
||||
define: jest.fn(),
|
||||
};
|
||||
|
||||
const actualSequelize = jest.requireActual('sequelize');
|
||||
return { Sequelize: jest.fn(() => mSequelize), DataTypes: actualSequelize.DataTypes };
|
||||
});
|
||||
|
||||
//mock out the database models
|
||||
jest.doMock('../../server/database/models', () => ({
|
||||
accounts: {
|
||||
findOne: () => null //can't find any (signup state)
|
||||
},
|
||||
|
||||
pendingSignups: {
|
||||
upsert: jest.fn(async record => {
|
||||
expect(record.email).toBe('email@example.com');
|
||||
expect(record.username).toBe('username');
|
||||
expect(record.hash).toBe('hashed-password');
|
||||
expect(record.contact).toBe(true);
|
||||
//token is a random UUID
|
||||
})
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
test('Basic valid signup attempt', async () => {
|
||||
//arguments
|
||||
const req = {
|
||||
body: {
|
||||
email: 'email@example.com',
|
||||
username: 'username',
|
||||
password: 'password',
|
||||
contact: true
|
||||
}
|
||||
};
|
||||
|
||||
const res = {
|
||||
status: code => {
|
||||
expect(code).toBe(200);
|
||||
return {
|
||||
send: msg => expect(msg).toBe('Validation email sent!'),
|
||||
end: () => null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//test
|
||||
const route = require('../../server/auth/signup');
|
||||
|
||||
const result = await route(req, res);
|
||||
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
});
|
||||
@@ -1,68 +0,0 @@
|
||||
#Signup
|
||||
POST https://dev-auth.krgamestudios.com/auth/signup HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"email": "example@example.com",
|
||||
"username": "Example",
|
||||
"password": "helloworld"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Login
|
||||
POST https://dev-auth.krgamestudios.com/auth/login HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"email": "example@example.com",
|
||||
"password": "helloworld"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Query data
|
||||
GET https://dev-auth.krgamestudios.com/auth/account HTTP/1.1
|
||||
Authorization: Bearer
|
||||
|
||||
###
|
||||
|
||||
#Logout
|
||||
DELETE https://dev-auth.krgamestudios.com/auth/logout HTTP/1.1
|
||||
Authorization: Bearer
|
||||
|
||||
{
|
||||
"token": ""
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Refresh
|
||||
POST https://dev-auth.krgamestudios.com/auth/token HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"token": ""
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Update account data
|
||||
PATCH https://dev-auth.krgamestudios.com/auth/update HTTP/1.1
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer
|
||||
|
||||
{
|
||||
"contact": "true"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Delete account
|
||||
DELETE https://dev-auth.krgamestudios.com/auth/deletion HTTP/1.1
|
||||
Authorization: Bearer
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"password": "helloworld"
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
#Signup
|
||||
POST http://127.0.0.1:3200/auth/signup HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"email": "example@example.com",
|
||||
"username": "Example",
|
||||
"password": "helloworld"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Login
|
||||
POST http://127.0.0.1:3200/auth/login HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"email": "example@example.com",
|
||||
"password": "helloworld"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Query data
|
||||
GET http://127.0.0.1:3200/auth/account HTTP/1.1
|
||||
Authorization: Bearer
|
||||
|
||||
###
|
||||
|
||||
#Logout
|
||||
DELETE http://127.0.0.1:3200/auth/logout HTTP/1.1
|
||||
Authorization: Bearer
|
||||
|
||||
{
|
||||
"token": ""
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Refresh
|
||||
POST http://127.0.0.1:3200/auth/token HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"token": ""
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Update account data
|
||||
PATCH http://127.0.0.1:3200/auth/update HTTP/1.1
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer
|
||||
|
||||
{
|
||||
"contact": "true"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
#Delete account
|
||||
DELETE http://127.0.0.1:3200/auth/deletion HTTP/1.1
|
||||
Authorization: Bearer
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"password": "helloworld"
|
||||
}
|
||||
|
||||
###
|
||||
Reference in New Issue
Block a user