Commit 67f7d03a authored by Tim Kinnane's avatar Tim Kinnane

test(api): API scripts for test setup/teardown

parent 73c43685
......@@ -2,3 +2,4 @@
/yarn-error.log
/.nyc_output
/.DS_Store
/.env
......@@ -47,6 +47,7 @@
"cz-conventional-changelog": "^2.1.0",
"husky": "^0.14.3",
"mocha": "^5.0.1",
"node-rest-client": "^3.1.0",
"nyc": "^11.4.1",
"rimraf": "^2.6.2",
"sinon": "^4.4.2",
......@@ -63,7 +64,7 @@
"@types/node": "^9.4.6",
"@types/ws": "^4.0.1",
"asteroid": "^2.0.3",
"dotenv": "^5.0.0",
"dotenv": "^5.0.1",
"lru-cache": "^4.1.1",
"ws": "^4.0.0"
},
......
const { Client } = require('node-rest-client')
const { apiHost } = require('./config')
const api = new Client()
// Prepare shortcuts for API requests / error handling
const basicHeaders = { 'Content-Type': 'application/json' }
const authHeaders = { 'X-Auth-Token': null, 'X-User-Id': null }
const handle = (err) => console.error('ERROR (API):', err)
const debug = (process.env.LOG_LEVEL === 'debug')
// Populate auth headers from response data
function setAuth (authData) {
authHeaders['X-Auth-Token'] = authData.authToken
authHeaders['X-User-Id'] = authData.userId
}
// Join basic headers with auth headers if required
function getHeaders (authRequired=false) {
if (!authRequired) return basicHeaders
return Object.assign({}, basicHeaders, authHeaders)
}
// Do a POST request to an API endpoint
// If it happens to come back with a token, keep the token
// If it needs a token, use the token it kept (merges headers with auth)
const post = (endpoint, data, auth) => {
let headers = getHeaders(auth)
if (debug) console.log(`POST: ${endpoint}`, data)
return new Promise((resolve, reject) => {
api.post(apiHost+endpoint, { headers, data }, (result) => {
if (result.status !== 'success') {
reject(result.message)
} else {
if (result.data.hasOwnProperty('authToken')) setAuth(result.data)
if (debug) console.log('RESULT:', result)
resolve(result.data)
}
})
}).catch(handle)
}
// Do a GET request to an API endpoint
const get = (endpoint, auth) => {
let headers = getHeaders(auth)
if (debug) console.log(`GET: ${endpoint}`)
return new Promise((resolve, reject) => {
api.get(apiHost+endpoint, { headers }, (result) => {
if (result.status !== 'success') {
reject(result.message)
} else {
if (debug) console.log('RESULT:', result)
resolve(result.data)
}
})
}).catch(handle)
}
module.exports = {
get,
post,
handle
}
\ No newline at end of file
require('dotenv').config()
// The API location, requires a running Rocket.Chat instance
const apiHost = process.env.ROCKETCHAT_URL || 'http://localhost:3000'
// The API user, should be provisioned on build with local Rocket.Chat
const apiUser = {
username: process.env.ADMIN_USERNAME || 'admin',
password: process.env.ADMIN_PASS || 'pass'
}
// The Bot user, will attempt to login and run methods in tests
const botUser = {
email: 'bot@localhost',
name: 'Bot',
password: process.env.ROCKETCHAT_PASSWORD || 'pass',
username: process.env.ROCKETCHAT_USER || 'bot'
}
module.exports = {
apiHost,
apiUser,
botUser
}
\ No newline at end of file
// READY!
const { get, post, handle } = require('./api')
const { apiUser, botUser } = require('./config')
// SET!
async function setup () {
try {
// Login - stores auth token
await post('/api/v1/login', apiUser)
// Logout - invalidates token
await get('/api/v1/logout', true)
//
} catch (err) {
handle(err)
}
}
// GO!
setup()
\ No newline at end of file
const { get, post } = require('./api')
const { apiUser, botUser } = require('./config')
// Logout - invalidates token
get('/api/v1/logout', true)
\ No newline at end of file
......@@ -490,6 +490,12 @@ debug@^2.6.8:
dependencies:
ms "2.0.0"
debug@~2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.2.0.tgz#f87057e995b1a1f6ae6a4960664137bc56f039da"
dependencies:
ms "0.7.1"
decamelize@^1.0.0, decamelize@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
......@@ -537,9 +543,9 @@ doctrine@^0.7.2:
esutils "^1.1.6"
isarray "0.0.1"
dotenv@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.0.tgz#0206eb5b336639bf377618a2a304ff00c6a1fddb"
dotenv@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef"
error-ex@^1.2.0:
version "1.3.1"
......@@ -677,6 +683,12 @@ findup-sync@0.4.2:
micromatch "^2.3.7"
resolve-dir "^0.1.0"
follow-redirects@>=1.2.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.4.1.tgz#d8120f4518190f55aac65bb6fc7b85fcd666d6aa"
dependencies:
debug "^3.1.0"
for-in@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
......@@ -1272,6 +1284,10 @@ mocha@^5.0.1:
mkdirp "0.5.1"
supports-color "4.4.0"
ms@0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"
ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
......@@ -1290,6 +1306,14 @@ nise@^1.2.0:
path-to-regexp "^1.7.0"
text-encoding "^0.6.4"
node-rest-client@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/node-rest-client/-/node-rest-client-3.1.0.tgz#e0beb6dda7b20cc0b67a7847cf12c5fc419c37c3"
dependencies:
debug "~2.2.0"
follow-redirects ">=1.2.0"
xml2js ">=0.2.4"
normalize-package-data@^2.3.2:
version "2.4.0"
resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.4.0.tgz#12f95a307d58352075a04907b84ac8be98ac012f"
......@@ -1654,6 +1678,10 @@ samsam@1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.3.0.tgz#8d1d9350e25622da30de3e44ba692b5221ab7c50"
sax@>=0.6.0:
version "1.2.4"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
"semver@2 || 3 || 4 || 5", semver@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"
......@@ -2053,6 +2081,17 @@ ws@^4.0.0:
async-limiter "~1.0.0"
safe-buffer "~5.1.0"
xml2js@>=0.2.4:
version "0.4.19"
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.19.tgz#686c20f213209e94abf0d1bcf1efaa291c7827a7"
dependencies:
sax ">=0.6.0"
xmlbuilder "~9.0.1"
xmlbuilder@~9.0.1:
version "9.0.7"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d"
y18n@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment