refactor(axios): Replaced request and request-promise with axios

This commit is contained in:
danielhumgon
2020-01-30 01:10:06 -04:00
parent 65002fd3fc
commit 46a1c5888e
6 changed files with 258 additions and 244 deletions
+16 -24
View File
@@ -1162,6 +1162,14 @@
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz", "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz",
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==" "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ=="
}, },
"axios": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
"requires": {
"follow-redirects": "1.5.10"
}
},
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@@ -3119,6 +3127,14 @@
"integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==", "integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==",
"dev": true "dev": true
}, },
"follow-redirects": {
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
"requires": {
"debug": "=3.1.0"
}
},
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@@ -10149,25 +10165,6 @@
} }
} }
}, },
"request-promise": {
"version": "4.2.4",
"resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.4.tgz",
"integrity": "sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==",
"requires": {
"bluebird": "^3.5.0",
"request-promise-core": "1.1.2",
"stealthy-require": "^1.1.1",
"tough-cookie": "^2.3.3"
}
},
"request-promise-core": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.2.tgz",
"integrity": "sha512-UHYyq1MO8GsefGEt7EprS8UrXsm1TxEvFUX1IMTuSLU2Rh7fTIdFtl8xD7JiEYiWU2dl+NYAjCTksTehQUxPag==",
"requires": {
"lodash": "^4.17.11"
}
},
"require-directory": { "require-directory": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
@@ -11105,11 +11102,6 @@
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
}, },
"stealthy-require": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz",
"integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks="
},
"stream-combiner2": { "stream-combiner2": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/stream-combiner2/-/stream-combiner2-1.1.1.tgz", "resolved": "https://registry.npmjs.org/stream-combiner2/-/stream-combiner2-1.1.1.tgz",
+1 -1
View File
@@ -32,6 +32,7 @@
"repository": "christroutner/koa-api-boilerplate", "repository": "christroutner/koa-api-boilerplate",
"dependencies": { "dependencies": {
"apidoc": "^0.20.0", "apidoc": "^0.20.0",
"axios": "^0.19.2",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"glob": "^7.0.0", "glob": "^7.0.0",
"jsonwebtoken": "^8.3.0", "jsonwebtoken": "^8.3.0",
@@ -49,7 +50,6 @@
"mongoose": "^5.5.12", "mongoose": "^5.5.12",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"request": "^2.85.0", "request": "^2.85.0",
"request-promise": "^4.2.2",
"winston": "^3.2.1", "winston": "^3.2.1",
"winston-daily-rotate-file": "^4.0.0" "winston-daily-rotate-file": "^4.0.0"
}, },
+16 -23
View File
@@ -10,8 +10,7 @@
*/ */
'use strict' 'use strict'
const axios = require('axios').default
const rp = require('request-promise')
const User = require('../models/users') const User = require('../models/users')
const jsonFiles = require('./utils/json-files') const jsonFiles = require('./utils/json-files')
const config = require('../../config') const config = require('../../config')
@@ -32,21 +31,19 @@ async function createSystemUser () {
const options = { const options = {
method: 'POST', method: 'POST',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true, data: {
json: true,
body: {
user: { user: {
email: 'system@system.com', email: 'system@system.com',
password: context.password password: context.password
} }
} }
} }
let result = await rp(options) let result = await axios(options)
context.email = result.body.user.email context.email = result.data.user.email
context.id = result.body.user._id context.id = result.data.user._id
context.token = result.body.token context.token = result.data.token
// Get the mongoDB entry // Get the mongoDB entry
const user = await User.findById(context.id) const user = await User.findById(context.id)
@@ -68,7 +65,7 @@ async function createSystemUser () {
return context return context
} catch (err) { } catch (err) {
// Handle existing system user. // Handle existing system user.
if (err.statusCode === 422) { if (err.response.status === 422) {
try { try {
// Delete the existing user // Delete the existing user
await deleteExistingSystemUser() await deleteExistingSystemUser()
@@ -92,23 +89,21 @@ async function deleteExistingSystemUser () {
try { try {
let result = await loginAdmin() let result = await loginAdmin()
const token = result.body.token const token = result.data.token
const id = result.body.user._id.toString() const id = result.data.user._id.toString()
// Delete the user. // Delete the user.
const options = { const options = {
method: 'DELETE', method: 'DELETE',
uri: `${LOCALHOST}/users/${id}`, url: `${LOCALHOST}/users/${id}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
} }
result = await rp(options) result = await axios(options)
// console.log(`result2: ${JSON.stringify(result, null, 2)}`) // console.log(`result2: ${JSON.stringify(result, null, 2)}`)
return result.body.success return result.data.success
} catch (err) { } catch (err) {
console.log(`Error in admin.js/deleteExistingSystemUser()`) console.log(`Error in admin.js/deleteExistingSystemUser()`)
throw err throw err
@@ -127,15 +122,13 @@ async function loginAdmin () {
// Log in as the user. // Log in as the user.
let options = { let options = {
method: 'POST', method: 'POST',
uri: `${LOCALHOST}/auth`, url: `${LOCALHOST}/auth`,
resolveWithFullResponse: true, data: {
json: true,
body: {
email: 'system@system.com', email: 'system@system.com',
password: existingUser.password password: existingUser.password
} }
} }
let result = await rp(options) let result = await axios(options)
// console.log(`result1: ${JSON.stringify(result, null, 2)}`) // console.log(`result1: ${JSON.stringify(result, null, 2)}`)
return result return result
+27 -34
View File
@@ -1,9 +1,10 @@
const app = require('../bin/server') const app = require('../bin/server')
const utils = require('./utils') const utils = require('./utils')
const config = require('../config') const config = require('../config')
const rp = require('request-promise')
const assert = require('chai').assert const assert = require('chai').assert
const axios = require('axios').default
// const request = supertest.agent(app.listen()) // const request = supertest.agent(app.listen())
const context = {} const context = {}
@@ -29,27 +30,25 @@ describe('Auth', () => {
it('should throw 401 if credentials are incorrect', async () => { it('should throw 401 if credentials are incorrect', async () => {
try { try {
const options = { const options = {
method: 'POST', method: 'post',
uri: `${LOCALHOST}/auth`, url: `${LOCALHOST}/auth`,
resolveWithFullResponse: true, data: {
json: true,
body: {
email: 'test@test.com', email: 'test@test.com',
password: 'wrongpassword' password: 'wrongpassword'
} }
} }
let result = await rp(options) let result = await axios(options)
// console.log(`result: ${JSON.stringify(result, null, 2)}`) // console.log(`result: ${JSON.stringify(result, null, 2)}`)
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) console.log(`result stringified: ${JSON.stringify(result.data, null, 2)}`)
assert(false, 'Unexpected result') assert(false, 'Unexpected result')
} catch (err) { } catch (err) {
if (err.statusCode === 422) { if (err.response.status === 422) {
assert(err.statusCode === 422, 'Error code 422 expected.') assert(err.response.status === 422, 'Error code 422 expected.')
} else if (err.statusCode === 401) { } else if (err.response.status === 401) {
assert(err.statusCode === 401, 'Error code 401 expected.') assert(err.response.status === 401, 'Error code 401 expected.')
} else { } else {
console.error('Error: ', err) console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2)) console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -60,23 +59,21 @@ describe('Auth', () => {
it('should throw 422 if email is wrong format', async () => { it('should throw 422 if email is wrong format', async () => {
try { try {
const options = { const options = {
method: 'POST', method: 'post',
uri: `${LOCALHOST}/auth`, url: `${LOCALHOST}/auth`,
resolveWithFullResponse: true, data: {
json: true,
body: {
email: 'wrongEmail', email: 'wrongEmail',
password: 'wrongpassword' password: 'wrongpassword'
} }
} }
await rp(options) await axios(options)
assert(false, 'Unexpected result') assert(false, 'Unexpected result')
} catch (err) { } catch (err) {
if (err.statusCode === 422) { if (err.response.status === 422) {
assert(err.statusCode === 422, 'Error code 422 expected.') assert(err.response.status === 422, 'Error code 422 expected.')
} else if (err.statusCode === 401) { } else if (err.response.status === 401) {
assert(err.statusCode === 401, 'Error code 401 expected.') assert(err.response.status === 401, 'Error code 401 expected.')
} else { } else {
console.error('Error: ', err) console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2)) console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -88,27 +85,23 @@ describe('Auth', () => {
it('should auth user', async () => { it('should auth user', async () => {
try { try {
const options = { const options = {
method: 'POST', method: 'post',
uri: `${LOCALHOST}/auth`, url: `${LOCALHOST}/auth`,
resolveWithFullResponse: true, data: {
json: true,
body: {
email: 'test@test.com', email: 'test@test.com',
password: 'pass' password: 'pass'
} }
} }
let result = await axios(options)
// console.log(`result: ${JSON.stringify(result.data, null, 2)}`)
let result = await rp(options) assert(result.status === 200, 'Status Code 200 expected.')
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert(result.statusCode === 200, 'Status Code 200 expected.')
assert( assert(
result.body.user.email === 'test@test.com', result.data.user.email === 'test@test.com',
'Email of test expected' 'Email of test expected'
) )
assert( assert(
result.body.user.password === undefined, result.data.user.password === undefined,
'Password expected to be omited' 'Password expected to be omited'
) )
} catch (err) { } catch (err) {
+178 -136
View File
@@ -1,7 +1,7 @@
const testUtils = require('./utils') const testUtils = require('./utils')
const rp = require('request-promise')
const assert = require('chai').assert const assert = require('chai').assert
const config = require('../config') const config = require('../config')
const axios = require('axios').default
const util = require('util') const util = require('util')
util.inspect.defaultOptions = { depth: 1 } util.inspect.defaultOptions = { depth: 1 }
@@ -42,24 +42,22 @@ describe('Users', () => {
it('should reject signup when data is incomplete', async () => { it('should reject signup when data is incomplete', async () => {
try { try {
const options = { const options = {
method: 'POST', method: 'post',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true, data: {
json: true,
body: {
email: 'test2@test.com' email: 'test2@test.com'
} }
} }
let result = await rp(options) let result = await axios(options)
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) console.log(`result stringified: ${JSON.stringify(result.data, null, 2)}`)
assert(false, 'Unexpected result') assert(false, 'Unexpected result')
} catch (err) { } catch (err) {
if (err.statusCode === 422) { if (err.response.status === 422) {
assert(err.statusCode === 422, 'Error code 422 expected.') assert(err.response.status === 422, 'Error code 422 expected.')
} else if (err.statusCode === 401) { } else if (err.response.status === 401) {
assert(err.statusCode === 401, 'Error code 401 expected.') assert(err.response.status === 401, 'Error code 401 expected.')
} else { } else {
console.error('Error: ', err) console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2)) console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -71,11 +69,9 @@ describe('Users', () => {
it('should sign up', async () => { it('should sign up', async () => {
try { try {
const options = { const options = {
method: 'POST', method: 'post',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true, data: {
json: true,
body: {
user: { user: {
email: 'test3@test.com', email: 'test3@test.com',
password: 'supersecretpassword' password: 'supersecretpassword'
@@ -83,22 +79,22 @@ describe('Users', () => {
} }
} }
let result = await rp(options) let result = await axios(options)
// console.log(`result: ${JSON.stringify(result, null, 2)}`) // console.log(`result: ${JSON.stringify(result, null, 2)}`)
context.user = result.body.user context.user = result.data.user
context.token = result.body.token context.token = result.data.token
assert(result.statusCode === 200, 'Status Code 200 expected.') assert(result.status === 200, 'Status Code 200 expected.')
assert( assert(
result.body.user.email === 'test3@test.com', result.data.user.email === 'test3@test.com',
'Email of test expected' 'Email of test expected'
) )
assert( assert(
result.body.user.password === undefined, result.data.user.password === undefined,
'Password expected to be omited' 'Password expected to be omited'
) )
assert.property(result.body, 'token', 'Token property exists.') assert.property(result.data, 'token', 'Token property exists.')
} catch (err) { } catch (err) {
console.log( console.log(
'Error authenticating test user: ' + JSON.stringify(err, null, 2) 'Error authenticating test user: ' + JSON.stringify(err, null, 2)
@@ -112,40 +108,52 @@ describe('Users', () => {
it('should not fetch users if the authorization header is missing', async () => { it('should not fetch users if the authorization header is missing', async () => {
try { try {
const options = { const options = {
method: 'GET', method: 'get',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json' Accept: 'application/json'
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
it('should not fetch users if the authorization header is missing the scheme', async () => { it('should not fetch users if the authorization header is missing the scheme', async () => {
try { try {
const options = { const options = {
method: 'GET', method: 'get',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: '1' Authorization: '1'
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -153,40 +161,52 @@ describe('Users', () => {
const { token } = context const { token } = context
try { try {
const options = { const options = {
method: 'GET', method: 'get',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Unknown ${token}` Authorization: `Unknown ${token}`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
it('should not fetch users if token is invalid', async () => { it('should not fetch users if token is invalid', async () => {
try { try {
const options = { const options = {
method: 'GET', method: 'get',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer 1` Authorization: `Bearer 1`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -194,21 +214,19 @@ describe('Users', () => {
const { token } = context const { token } = context
const options = { const options = {
method: 'GET', method: 'get',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
} }
const result = await rp(options) const result = await axios(options)
const users = result.body.users const users = result.data.users
// console.log(`users: ${util.inspect(users)}`) // console.log(`users: ${util.inspect(users)}`)
assert.hasAnyKeys(users[0], ['type', '_id', 'username']) assert.hasAnyKeys(users[0], ['type', '_id', 'email'])
assert.isNumber(users.length) assert.isNumber(users.length)
}) })
}) })
@@ -218,19 +236,25 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'GET', method: 'GET',
uri: `${LOCALHOST}/users/1`, url: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer 1` Authorization: `Bearer 1`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -240,19 +264,25 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'GET', method: 'GET',
uri: `${LOCALHOST}/users/1`, url: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 404) if (err.response.status) {
assert.equal(err.response.status, 404)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 404)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -264,17 +294,15 @@ describe('Users', () => {
const options = { const options = {
method: 'GET', method: 'GET',
uri: `${LOCALHOST}/users/${_id}`, url: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
} }
const result = await rp(options) const result = await axios(options)
const user = result.body.user const user = result.data.user
// console.log(`user: ${util.inspect(user)}`) // console.log(`user: ${util.inspect(user)}`)
assert.hasAnyKeys(user, ['type', '_id', 'email']) assert.hasAnyKeys(user, ['type', '_id', 'email'])
@@ -292,19 +320,25 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'PUT', method: 'PUT',
uri: `${LOCALHOST}/users/1`, url: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer 1` Authorization: `Bearer 1`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -314,19 +348,25 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'PUT', method: 'PUT',
uri: `${LOCALHOST}/users/1`, url: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -338,20 +378,18 @@ describe('Users', () => {
const options = { const options = {
method: 'PUT', method: 'PUT',
uri: `${LOCALHOST}/users/${_id}`, url: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
}, },
body: { data: {
user: { email: 'testToUpdate@test.com' } user: { email: 'testToUpdate@test.com' }
} }
} }
const result = await rp(options) const result = await axios(options)
const user = result.body.user const user = result.data.user
// console.log(`user: ${util.inspect(user)}`) // console.log(`user: ${util.inspect(user)}`)
assert.hasAnyKeys(user, ['type', '_id', 'email']) assert.hasAnyKeys(user, ['type', '_id', 'email'])
@@ -368,13 +406,11 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'PUT', method: 'PUT',
uri: `${LOCALHOST}/users/${context.user._id.toString()}`, url: `${LOCALHOST}/users/${context.user._id.toString()}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Authorization: `Bearer ${context.token}` Authorization: `Bearer ${context.token}`
}, },
body: { data: {
user: { user: {
name: 'new name', name: 'new name',
type: 'test' type: 'test'
@@ -382,12 +418,12 @@ describe('Users', () => {
} }
} }
let result = await rp(options) let result = await axios(options)
// console.log(`Users: ${JSON.stringify(result, null, 2)}`) // console.log(`Users: ${JSON.stringify(result.data, null, 2)}`)
assert(result.statusCode === 200, 'Status Code 200 expected.') assert(result.status === 200, 'Status Code 200 expected.')
assert(result.body.user.type === 'user', 'Type should be unchanged.') assert(result.data.user.type === 'user', 'Type should be unchanged.')
} catch (err) { } catch (err) {
console.error('Error: ', err) console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2)) console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -399,26 +435,26 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'PUT', method: 'PUT',
uri: `${LOCALHOST}/users/${context.user2._id.toString()}`, url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Authorization: `Bearer ${context.token}` Authorization: `Bearer ${context.token}`
}, },
body: { data: {
user: { user: {
name: 'This should not work' name: 'This should not work'
} }
} }
} }
let result = await rp(options) let result = await axios(options)
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) console.log(`result stringified: ${JSON.stringify(result, null, 2)}`)
assert(false, 'Unexpected result') assert(false, 'Unexpected result')
} catch (err) { } catch (err) {
if (err.statusCode === 401) { if (err.response.status) {
assert(err.statusCode === 401, 'Error code 401 expected.') assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else { } else {
console.error('Error: ', err) console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2)) console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -432,23 +468,21 @@ describe('Users', () => {
const options = { const options = {
method: 'PUT', method: 'PUT',
uri: `${LOCALHOST}/users/${context.user2._id.toString()}`, url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Authorization: `Bearer ${adminJWT}` Authorization: `Bearer ${adminJWT}`
}, },
body: { data: {
user: { user: {
name: 'This should work' name: 'This should work'
} }
} }
} }
let result = await rp(options) let result = await axios(options)
// console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) // console.log(`result stringified: ${JSON.stringify(result, null, 2)}`)
const userName = result.body.user.name const userName = result.data.user.name
assert.equal(userName, 'This should work') assert.equal(userName, 'This should work')
}) })
}) })
@@ -458,19 +492,25 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'DELETE', method: 'DELETE',
uri: `${LOCALHOST}/users/1`, url: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer 1` Authorization: `Bearer 1`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -480,19 +520,25 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'DELETE', method: 'DELETE',
uri: `${LOCALHOST}/users/1`, url: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
} }
await rp(options) await axios(options)
assert.equal(true, false, 'Unexpected behavior') assert.equal(true, false, 'Unexpected behavior')
} catch (err) { } catch (err) {
assert.equal(err.statusCode, 401) if (err.response.status) {
assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
} }
}) })
@@ -500,21 +546,21 @@ describe('Users', () => {
try { try {
const options = { const options = {
method: 'DELETE', method: 'DELETE',
uri: `${LOCALHOST}/users/${context.user2._id.toString()}`, url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Authorization: `Bearer ${context.token}` Authorization: `Bearer ${context.token}`
} }
} }
let result = await rp(options) let result = await axios(options)
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) console.log(`result stringified: ${JSON.stringify(result.data, null, 2)}`)
assert(false, 'Unexpected result') assert(false, 'Unexpected result')
} catch (err) { } catch (err) {
if (err.statusCode === 401) { if (err.response.status) {
assert(err.statusCode === 401, 'Error code 401 expected.') assert.equal(err.response.status, 401)
} else if (err.response.statusCode) {
assert.equal(err.response.statusCode, 401)
} else { } else {
console.error('Error: ', err) console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2)) console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -531,19 +577,17 @@ describe('Users', () => {
const options = { const options = {
method: 'DELETE', method: 'DELETE',
uri: `${LOCALHOST}/users/${_id}`, url: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
} }
const result = await rp(options) const result = await axios(options)
// console.log(`result: ${util.inspect(result.body)}`) // console.log(`result: ${util.inspect(result.data.success)}`)
assert.equal(result.body.success, true) assert.equal(result.data.success, true)
}) })
it('should be able to delete other users when admin', async () => { it('should be able to delete other users when admin', async () => {
@@ -552,19 +596,17 @@ describe('Users', () => {
const options = { const options = {
method: 'DELETE', method: 'DELETE',
uri: `${LOCALHOST}/users/${id}`, url: `${LOCALHOST}/users/${id}`,
resolveWithFullResponse: true,
json: true,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
Authorization: `Bearer ${adminJWT}` Authorization: `Bearer ${adminJWT}`
} }
} }
const result = await rp(options) const result = await axios(options)
// console.log(`result: ${util.inspect(result.body)}`) // console.log(`result: ${util.inspect(result.data)}`)
assert.equal(result.body.success, true) assert.equal(result.data.success, true)
}) })
}) })
}) })
+20 -26
View File
@@ -1,6 +1,6 @@
const mongoose = require('mongoose') const mongoose = require('mongoose')
const rp = require('request-promise')
const config = require('../config') const config = require('../config')
const axios = require('axios').default
const LOCALHOST = `http://localhost:${config.port}` const LOCALHOST = `http://localhost:${config.port}`
@@ -22,10 +22,8 @@ async function createUser (userObj) {
try { try {
const options = { const options = {
method: 'POST', method: 'POST',
uri: `${LOCALHOST}/users`, url: `${LOCALHOST}/users`,
resolveWithFullResponse: true, data: {
json: true,
body: {
user: { user: {
email: userObj.email, email: userObj.email,
password: userObj.password password: userObj.password
@@ -33,11 +31,11 @@ async function createUser (userObj) {
} }
} }
let result = await rp(options) let result = await axios(options)
const retObj = { const retObj = {
user: result.body.user, user: result.data.user,
token: result.body.token token: result.data.token
} }
return retObj return retObj
@@ -51,23 +49,21 @@ async function loginTestUser () {
try { try {
const options = { const options = {
method: 'POST', method: 'POST',
uri: `${LOCALHOST}/auth`, url: `${LOCALHOST}/auth`,
resolveWithFullResponse: true, data: {
json: true,
body: {
email: 'test@test.com', email: 'test@test.com',
password: 'pass' password: 'pass'
} }
} }
let result = await rp(options) let result = await axios(options)
// console.log(`result: ${JSON.stringify(result, null, 2)}`) // console.log(`result: ${JSON.stringify(result.data, null, 2)}`)
const retObj = { const retObj = {
token: result.body.token, token: result.data.token,
user: result.body.user.username, user: result.data.user.username,
id: result.body.user._id.toString() id: result.data.user._id.toString()
} }
return retObj return retObj
@@ -85,23 +81,21 @@ async function loginAdminUser () {
const options = { const options = {
method: 'POST', method: 'POST',
uri: `${LOCALHOST}/auth`, url: `${LOCALHOST}/auth`,
resolveWithFullResponse: true, data: {
json: true,
body: {
email: adminUserData.email, email: adminUserData.email,
password: adminUserData.password password: adminUserData.password
} }
} }
let result = await rp(options) let result = await axios(options)
// console.log(`result: ${JSON.stringify(result, null, 2)}`) // console.log(`result: ${JSON.stringify(result.data, null, 2)}`)
const retObj = { const retObj = {
token: result.body.token, token: result.data.token,
user: result.body.user.username, user: result.data.user.username,
id: result.body.user._id.toString() id: result.data.user._id.toString()
} }
return retObj return retObj