mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
refactor(auth): Back-ported email/username auth changes
This commit is contained in:
+3
-3
@@ -16,11 +16,11 @@ passport.deserializeUser(async (id, done) => {
|
||||
})
|
||||
|
||||
passport.use('local', new Strategy({
|
||||
usernameField: 'username',
|
||||
usernameField: 'email',
|
||||
passwordField: 'password'
|
||||
}, async (username, password, done) => {
|
||||
}, async (email, password, done) => {
|
||||
try {
|
||||
const user = await User.findOne({ username })
|
||||
const user = await User.findOne({ email })
|
||||
if (!user) { return done(null, false) }
|
||||
|
||||
try {
|
||||
|
||||
+3
-3
@@ -37,14 +37,14 @@ async function createSystemUser () {
|
||||
json: true,
|
||||
body: {
|
||||
user: {
|
||||
username: 'system',
|
||||
email: 'system@system.com',
|
||||
password: context.password
|
||||
}
|
||||
}
|
||||
}
|
||||
let result = await rp(options)
|
||||
|
||||
context.username = result.body.user.username
|
||||
context.email = result.body.user.email
|
||||
context.id = result.body.user._id
|
||||
context.token = result.body.token
|
||||
|
||||
@@ -131,7 +131,7 @@ async function loginAdmin () {
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'system',
|
||||
email: 'system@system.com',
|
||||
password: existingUser.password
|
||||
}
|
||||
}
|
||||
|
||||
+15
-2
@@ -6,8 +6,21 @@ const jwt = require('jsonwebtoken')
|
||||
const User = new mongoose.Schema({
|
||||
type: { type: String, default: 'user' },
|
||||
name: { type: String },
|
||||
username: { type: String, required: true, unique: true },
|
||||
password: { type: String, required: true }
|
||||
username: { type: String },
|
||||
password: { type: String, required: true },
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
validate: {
|
||||
validator: function (email) {
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)
|
||||
},
|
||||
message: props => `${props.value} is not a valid Email format!`
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
User.pre('save', function preSave (next) {
|
||||
|
||||
+33
-6
@@ -16,11 +16,11 @@ describe('Auth', () => {
|
||||
await app.startServer() // This should be second instruction.
|
||||
|
||||
const userObj = {
|
||||
username: 'test',
|
||||
email: 'test@test.com',
|
||||
password: 'pass'
|
||||
}
|
||||
const testUser = await utils.createUser(userObj)
|
||||
|
||||
console.log(`TestUser : ${testUser}`)
|
||||
context.user = testUser.user
|
||||
context.token = testUser.token
|
||||
})
|
||||
@@ -34,7 +34,7 @@ describe('Auth', () => {
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'test',
|
||||
email: 'test@test.com',
|
||||
password: 'wrongpassword'
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,33 @@ describe('Auth', () => {
|
||||
}
|
||||
}
|
||||
})
|
||||
it('should throw 422 if email is wrong format', async () => {
|
||||
try {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/auth`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
email: 'wrongEmail',
|
||||
password: 'wrongpassword'
|
||||
}
|
||||
}
|
||||
|
||||
await rp(options)
|
||||
assert(false, 'Unexpected result')
|
||||
} catch (err) {
|
||||
if (err.statusCode === 422) {
|
||||
assert(err.statusCode === 422, 'Error code 422 expected.')
|
||||
} else if (err.statusCode === 401) {
|
||||
assert(err.statusCode === 401, 'Error code 401 expected.')
|
||||
} else {
|
||||
console.error('Error: ', err)
|
||||
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
|
||||
throw err
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('should auth user', async () => {
|
||||
try {
|
||||
@@ -66,7 +93,7 @@ describe('Auth', () => {
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'test',
|
||||
email: 'test@test.com',
|
||||
password: 'pass'
|
||||
}
|
||||
}
|
||||
@@ -77,8 +104,8 @@ describe('Auth', () => {
|
||||
|
||||
assert(result.statusCode === 200, 'Status Code 200 expected.')
|
||||
assert(
|
||||
result.body.user.username === 'test',
|
||||
'Username of test expected'
|
||||
result.body.user.email === 'test@test.com',
|
||||
'Email of test expected'
|
||||
)
|
||||
assert(
|
||||
result.body.user.password === undefined,
|
||||
|
||||
+12
-9
@@ -16,7 +16,7 @@ describe('Users', () => {
|
||||
|
||||
// Create a second test user.
|
||||
const userObj = {
|
||||
username: 'test2',
|
||||
email: 'test2@test.com',
|
||||
password: 'pass2'
|
||||
}
|
||||
const testUser = await testUtils.createUser(userObj)
|
||||
@@ -47,7 +47,7 @@ describe('Users', () => {
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'supercoolname'
|
||||
email: 'test2@test.com'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,10 @@ describe('Users', () => {
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
user: { username: 'supercoolname', password: 'supersecretpassword' }
|
||||
user: {
|
||||
email: 'test3@test.com',
|
||||
password: 'supersecretpassword'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,8 +91,8 @@ describe('Users', () => {
|
||||
|
||||
assert(result.statusCode === 200, 'Status Code 200 expected.')
|
||||
assert(
|
||||
result.body.user.username === 'supercoolname',
|
||||
'Username of test expected'
|
||||
result.body.user.email === 'test3@test.com',
|
||||
'Email of test expected'
|
||||
)
|
||||
assert(
|
||||
result.body.user.password === undefined,
|
||||
@@ -274,7 +277,7 @@ describe('Users', () => {
|
||||
const user = result.body.user
|
||||
// console.log(`user: ${util.inspect(user)}`)
|
||||
|
||||
assert.hasAnyKeys(user, ['type', '_id', 'username'])
|
||||
assert.hasAnyKeys(user, ['type', '_id', 'email'])
|
||||
assert.equal(user._id, _id)
|
||||
assert.notProperty(
|
||||
user,
|
||||
@@ -343,7 +346,7 @@ describe('Users', () => {
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: {
|
||||
user: { username: 'updatedcoolname' }
|
||||
user: { email: 'testToUpdate@test.com' }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,14 +354,14 @@ describe('Users', () => {
|
||||
const user = result.body.user
|
||||
// console.log(`user: ${util.inspect(user)}`)
|
||||
|
||||
assert.hasAnyKeys(user, ['type', '_id', 'username'])
|
||||
assert.hasAnyKeys(user, ['type', '_id', 'email'])
|
||||
assert.equal(user._id, _id)
|
||||
assert.notProperty(
|
||||
user,
|
||||
'password',
|
||||
'Password property should not be returned'
|
||||
)
|
||||
assert.equal(user.username, 'updatedcoolname')
|
||||
assert.equal(user.email, 'testToUpdate@test.com')
|
||||
})
|
||||
|
||||
it('should not be able to update user type', async () => {
|
||||
|
||||
+3
-3
@@ -27,7 +27,7 @@ async function createUser (userObj) {
|
||||
json: true,
|
||||
body: {
|
||||
user: {
|
||||
username: userObj.username,
|
||||
email: userObj.email,
|
||||
password: userObj.password
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ async function loginTestUser () {
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'test',
|
||||
email: 'test@test.com',
|
||||
password: 'pass'
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ async function loginAdminUser () {
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: adminUserData.username,
|
||||
email: adminUserData.email,
|
||||
password: adminUserData.password
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ const mongoose = require('mongoose')
|
||||
|
||||
const config = require('../../config')
|
||||
|
||||
const USERNAME = 'test'
|
||||
const EMAIL = 'test@test.com'
|
||||
const PASSWORD = 'pass'
|
||||
|
||||
async function addUser () {
|
||||
@@ -14,7 +14,7 @@ async function addUser () {
|
||||
const User = require('../../src/models/users')
|
||||
|
||||
const userData = {
|
||||
username: USERNAME,
|
||||
email: EMAIL,
|
||||
password: PASSWORD
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ async function addUser () {
|
||||
|
||||
await mongoose.connection.close()
|
||||
|
||||
console.log(`User ${USERNAME} created.`)
|
||||
console.log(`User ${EMAIL} created.`)
|
||||
}
|
||||
addUser()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user