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",
"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": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@@ -3119,6 +3127,14 @@
"integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==",
"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": {
"version": "1.0.2",
"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": {
"version": "2.1.1",
"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",
"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": {
"version": "1.1.1",
"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",
"dependencies": {
"apidoc": "^0.20.0",
"axios": "^0.19.2",
"bcryptjs": "^2.4.3",
"glob": "^7.0.0",
"jsonwebtoken": "^8.3.0",
@@ -49,7 +50,6 @@
"mongoose": "^5.5.12",
"passport-local": "^1.0.0",
"request": "^2.85.0",
"request-promise": "^4.2.2",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^4.0.0"
},
+16 -23
View File
@@ -10,8 +10,7 @@
*/
'use strict'
const rp = require('request-promise')
const axios = require('axios').default
const User = require('../models/users')
const jsonFiles = require('./utils/json-files')
const config = require('../../config')
@@ -32,21 +31,19 @@ async function createSystemUser () {
const options = {
method: 'POST',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
body: {
url: `${LOCALHOST}/users`,
data: {
user: {
email: 'system@system.com',
password: context.password
}
}
}
let result = await rp(options)
let result = await axios(options)
context.email = result.body.user.email
context.id = result.body.user._id
context.token = result.body.token
context.email = result.data.user.email
context.id = result.data.user._id
context.token = result.data.token
// Get the mongoDB entry
const user = await User.findById(context.id)
@@ -68,7 +65,7 @@ async function createSystemUser () {
return context
} catch (err) {
// Handle existing system user.
if (err.statusCode === 422) {
if (err.response.status === 422) {
try {
// Delete the existing user
await deleteExistingSystemUser()
@@ -92,23 +89,21 @@ async function deleteExistingSystemUser () {
try {
let result = await loginAdmin()
const token = result.body.token
const id = result.body.user._id.toString()
const token = result.data.token
const id = result.data.user._id.toString()
// Delete the user.
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/${id}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${id}`,
headers: {
Authorization: `Bearer ${token}`
}
}
result = await rp(options)
result = await axios(options)
// console.log(`result2: ${JSON.stringify(result, null, 2)}`)
return result.body.success
return result.data.success
} catch (err) {
console.log(`Error in admin.js/deleteExistingSystemUser()`)
throw err
@@ -127,15 +122,13 @@ async function loginAdmin () {
// Log in as the user.
let options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
url: `${LOCALHOST}/auth`,
data: {
email: 'system@system.com',
password: existingUser.password
}
}
let result = await rp(options)
let result = await axios(options)
// console.log(`result1: ${JSON.stringify(result, null, 2)}`)
return result
+27 -34
View File
@@ -1,9 +1,10 @@
const app = require('../bin/server')
const utils = require('./utils')
const config = require('../config')
const rp = require('request-promise')
const assert = require('chai').assert
const axios = require('axios').default
// const request = supertest.agent(app.listen())
const context = {}
@@ -29,27 +30,25 @@ describe('Auth', () => {
it('should throw 401 if credentials are incorrect', async () => {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
method: 'post',
url: `${LOCALHOST}/auth`,
data: {
email: 'test@test.com',
password: 'wrongpassword'
}
}
let result = await rp(options)
let result = await axios(options)
// 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')
} 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.')
if (err.response.status === 422) {
assert(err.response.status === 422, 'Error code 422 expected.')
} else if (err.response.status === 401) {
assert(err.response.status === 401, 'Error code 401 expected.')
} else {
console.error('Error: ', err)
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 () => {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
method: 'post',
url: `${LOCALHOST}/auth`,
data: {
email: 'wrongEmail',
password: 'wrongpassword'
}
}
await rp(options)
await axios(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.')
if (err.response.status === 422) {
assert(err.response.status === 422, 'Error code 422 expected.')
} else if (err.response.status === 401) {
assert(err.response.status === 401, 'Error code 401 expected.')
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -88,27 +85,23 @@ describe('Auth', () => {
it('should auth user', async () => {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
method: 'post',
url: `${LOCALHOST}/auth`,
data: {
email: 'test@test.com',
password: 'pass'
}
}
let result = await axios(options)
// console.log(`result: ${JSON.stringify(result.data, null, 2)}`)
let result = await rp(options)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert(result.statusCode === 200, 'Status Code 200 expected.')
assert(result.status === 200, 'Status Code 200 expected.')
assert(
result.body.user.email === 'test@test.com',
result.data.user.email === 'test@test.com',
'Email of test expected'
)
assert(
result.body.user.password === undefined,
result.data.user.password === undefined,
'Password expected to be omited'
)
} catch (err) {
+178 -136
View File
@@ -1,7 +1,7 @@
const testUtils = require('./utils')
const rp = require('request-promise')
const assert = require('chai').assert
const config = require('../config')
const axios = require('axios').default
const util = require('util')
util.inspect.defaultOptions = { depth: 1 }
@@ -42,24 +42,22 @@ describe('Users', () => {
it('should reject signup when data is incomplete', async () => {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
body: {
method: 'post',
url: `${LOCALHOST}/users`,
data: {
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')
} 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.')
if (err.response.status === 422) {
assert(err.response.status === 422, 'Error code 422 expected.')
} else if (err.response.status === 401) {
assert(err.response.status === 401, 'Error code 401 expected.')
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -71,11 +69,9 @@ describe('Users', () => {
it('should sign up', async () => {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
body: {
method: 'post',
url: `${LOCALHOST}/users`,
data: {
user: {
email: 'test3@test.com',
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)}`)
context.user = result.body.user
context.token = result.body.token
context.user = result.data.user
context.token = result.data.token
assert(result.statusCode === 200, 'Status Code 200 expected.')
assert(result.status === 200, 'Status Code 200 expected.')
assert(
result.body.user.email === 'test3@test.com',
result.data.user.email === 'test3@test.com',
'Email of test expected'
)
assert(
result.body.user.password === undefined,
result.data.user.password === undefined,
'Password expected to be omited'
)
assert.property(result.body, 'token', 'Token property exists.')
assert.property(result.data, 'token', 'Token property exists.')
} catch (err) {
console.log(
'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 () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
method: 'get',
url: `${LOCALHOST}/users`,
headers: {
Accept: 'application/json'
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
method: 'get',
url: `${LOCALHOST}/users`,
headers: {
Accept: 'application/json',
Authorization: '1'
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
method: 'get',
url: `${LOCALHOST}/users`,
headers: {
Accept: 'application/json',
Authorization: `Unknown ${token}`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
method: 'get',
url: `${LOCALHOST}/users`,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
method: 'get',
url: `${LOCALHOST}/users`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
const result = await rp(options)
const users = result.body.users
const result = await axios(options)
const users = result.data.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)
})
})
@@ -218,19 +236,25 @@ describe('Users', () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 = {
method: 'GET',
uri: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${_id}`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
const result = await rp(options)
const user = result.body.user
const result = await axios(options)
const user = result.data.user
// console.log(`user: ${util.inspect(user)}`)
assert.hasAnyKeys(user, ['type', '_id', 'email'])
@@ -292,19 +320,25 @@ describe('Users', () => {
try {
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 {
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 = {
method: 'PUT',
uri: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${_id}`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
},
body: {
data: {
user: { email: 'testToUpdate@test.com' }
}
}
const result = await rp(options)
const user = result.body.user
const result = await axios(options)
const user = result.data.user
// console.log(`user: ${util.inspect(user)}`)
assert.hasAnyKeys(user, ['type', '_id', 'email'])
@@ -368,13 +406,11 @@ describe('Users', () => {
try {
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/${context.user._id.toString()}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${context.user._id.toString()}`,
headers: {
Authorization: `Bearer ${context.token}`
},
body: {
data: {
user: {
name: 'new name',
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.body.user.type === 'user', 'Type should be unchanged.')
assert(result.status === 200, 'Status Code 200 expected.')
assert(result.data.user.type === 'user', 'Type should be unchanged.')
} catch (err) {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
@@ -399,26 +435,26 @@ describe('Users', () => {
try {
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/${context.user2._id.toString()}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
headers: {
Authorization: `Bearer ${context.token}`
},
body: {
data: {
user: {
name: 'This should not work'
}
}
}
let result = await rp(options)
let result = await axios(options)
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`)
assert(false, 'Unexpected result')
} catch (err) {
if (err.statusCode === 401) {
assert(err.statusCode === 401, 'Error code 401 expected.')
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))
@@ -432,23 +468,21 @@ describe('Users', () => {
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/${context.user2._id.toString()}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
headers: {
Authorization: `Bearer ${adminJWT}`
},
body: {
data: {
user: {
name: 'This should work'
}
}
}
let result = await rp(options)
let result = await axios(options)
// 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')
})
})
@@ -458,19 +492,25 @@ describe('Users', () => {
try {
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 {
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
await rp(options)
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} 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 {
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/${context.user2._id.toString()}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
headers: {
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')
} catch (err) {
if (err.statusCode === 401) {
assert(err.statusCode === 401, 'Error code 401 expected.')
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))
@@ -531,19 +577,17 @@ describe('Users', () => {
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${_id}`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
const result = await rp(options)
// console.log(`result: ${util.inspect(result.body)}`)
const result = await axios(options)
// 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 () => {
@@ -552,19 +596,17 @@ describe('Users', () => {
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/${id}`,
resolveWithFullResponse: true,
json: true,
url: `${LOCALHOST}/users/${id}`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${adminJWT}`
}
}
const result = await rp(options)
// console.log(`result: ${util.inspect(result.body)}`)
const result = await axios(options)
// 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 rp = require('request-promise')
const config = require('../config')
const axios = require('axios').default
const LOCALHOST = `http://localhost:${config.port}`
@@ -22,10 +22,8 @@ async function createUser (userObj) {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
body: {
url: `${LOCALHOST}/users`,
data: {
user: {
email: userObj.email,
password: userObj.password
@@ -33,11 +31,11 @@ async function createUser (userObj) {
}
}
let result = await rp(options)
let result = await axios(options)
const retObj = {
user: result.body.user,
token: result.body.token
user: result.data.user,
token: result.data.token
}
return retObj
@@ -51,23 +49,21 @@ async function loginTestUser () {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
url: `${LOCALHOST}/auth`,
data: {
email: 'test@test.com',
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 = {
token: result.body.token,
user: result.body.user.username,
id: result.body.user._id.toString()
token: result.data.token,
user: result.data.user.username,
id: result.data.user._id.toString()
}
return retObj
@@ -85,23 +81,21 @@ async function loginAdminUser () {
const options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
url: `${LOCALHOST}/auth`,
data: {
email: adminUserData.email,
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 = {
token: result.body.token,
user: result.body.user.username,
id: result.body.user._id.toString()
token: result.data.token,
user: result.data.user.username,
id: result.data.user._id.toString()
}
return retObj