Skip to content

Commit

Permalink
cleaned up tests (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Jan 28, 2020
1 parent 9903f0a commit 54d7e73
Show file tree
Hide file tree
Showing 33 changed files with 630 additions and 559 deletions.
75 changes: 39 additions & 36 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,153 +14,156 @@
*/

/* jslint node: true */
/* global describe: false, before: false, after: false, it: false */
'use strict'

const NATS = require('../')
const nsc = require('./support/nats_server_control')
const should = require('should')
const after = require('mocha').after
const before = require('mocha').before
const describe = require('mocha').describe
const it = require('mocha').it

describe('Authorization', function () {
describe('Authorization', () => {
const PORT = 1421
const flags = ['--user', 'derek', '--pass', 'foobar']
const authUrl = 'nats://derek:foobar@localhost:' + PORT
const noAuthUrl = 'nats://localhost:' + PORT
let server

// Start up our own nats-server
before(function (done) {
before(done => {
server = nsc.startServer(PORT, flags, done)
})

// Shutdown our server after we are done
after(function (done) {
after(done => {
nsc.stopServer(server, done)
})

it('should fail to connect with no credentials ', function (done) {
it('should fail to connect with no credentials ', done => {
const nc = NATS.connect(PORT)
nc.on('error', function (err) {
nc.on('error', err => {
should.exist(err)
should.exist((/Authorization/).exec(err))
nc.close()
done()
})
})

it('should connect with proper credentials in url', function (done) {
it('should connect with proper credentials in url', done => {
const nc = NATS.connect(authUrl)
nc.on('connect', function (nc) {
setTimeout(function () {
nc.on('connect', nc => {
setTimeout(() => {
nc.close()
done()
}, 100)
})
})

it('should connect with proper credentials as options', function (done) {
it('should connect with proper credentials as options', done => {
const nc = NATS.connect({
url: noAuthUrl,
user: 'derek',
pass: 'foobar'
})
nc.on('connect', function (nc) {
setTimeout(function () {
nc.on('connect', nc => {
setTimeout(() => {
nc.close()
done()
}, 100)
})
})

it('should connect with proper credentials as server url', function (done) {
it('should connect with proper credentials as server url', done => {
const nc = NATS.connect({
servers: [authUrl]
})
nc.on('connect', function (nc) {
nc.on('connect', nc => {
nc.close()
setTimeout(done, 100)
})
})
})

describe('Token Authorization', function () {
describe('Token Authorization', () => {
const PORT = 1421
const flags = ['--auth', 'token1']
const authUrl = 'nats://token1@localhost:' + PORT
const noAuthUrl = 'nats://localhost:' + PORT
let server

// Start up our own nats-server
before(function (done) {
before(done => {
server = nsc.startServer(PORT, flags, done)
})

// Shutdown our server after we are done
after(function (done) {
after(done => {
nsc.stopServer(server, done)
})

it('should fail to connect with no credentials ', function (done) {
it('should fail to connect with no credentials ', done => {
const nc = NATS.connect(PORT)
nc.on('error', function (err) {
nc.on('error', err => {
should.exist(err)
should.exist((/Authorization/).exec(err))
nc.close()
done()
})
})

it('should connect with proper credentials in url', function (done) {
it('should connect with proper credentials in url', done => {
const nc = NATS.connect(authUrl)
nc.on('connect', function (nc) {
setTimeout(function () {
nc.on('connect', nc => {
setTimeout(() => {
nc.close()
done()
}, 100)
})
})

it('should connect with proper credentials as options', function (done) {
it('should connect with proper credentials as options', done => {
const nc = NATS.connect({
url: noAuthUrl,
token: 'token1'
})
nc.on('connect', function (nc) {
setTimeout(function () {
nc.on('connect', nc => {
setTimeout(() => {
nc.close()
done()
}, 100)
})
})

it('should connect with proper credentials as server url', function (done) {
it('should connect with proper credentials as server url', done => {
const nc = NATS.connect({
servers: [authUrl]
})
nc.on('connect', function (nc) {
nc.on('connect', nc => {
nc.close()
setTimeout(done, 100)
})
})
})

describe('tokenHandler Authorization', function () {
describe('tokenHandler Authorization', () => {
const PORT = 1421
const flags = ['--auth', 'token1']
const noAuthUrl = 'nats://localhost:' + PORT
let server

// Start up our own nats-server
before(function (done) {
before(done => {
server = nsc.startServer(PORT, flags, done)
})

// Shutdown our server after we are done
after(function (done) {
after(done => {
nsc.stopServer(server, done)
})

it('should connect using tokenHandler instead of plain old token', function (done) {
it('should connect using tokenHandler instead of plain old token', done => {
const nc = NATS.connect({
url: noAuthUrl,
tokenHandler: () => 'token1'
Expand All @@ -173,8 +176,8 @@ describe('tokenHandler Authorization', function () {
})
})

it('should fail to connect if tokenHandler is not a function', function (done) {
(function () {
it('should fail to connect if tokenHandler is not a function', done => {
(() => {
NATS.connect({
url: noAuthUrl,
tokenHandler: 'token1'
Expand All @@ -183,8 +186,8 @@ describe('tokenHandler Authorization', function () {
done()
})

it('should fail to connect if both token and tokenHandler are provided', function (done) {
(function () {
it('should fail to connect if both token and tokenHandler are provided', done => {
(() => {
NATS.connect({
url: noAuthUrl,
token: 'token1',
Expand All @@ -194,7 +197,7 @@ describe('tokenHandler Authorization', function () {
done()
})

it('should NOT connect if tokenHandler fails to return a token', function (done) {
it('should NOT connect if tokenHandler fails to return a token', done => {
const nc = NATS.connect({
url: noAuthUrl,
tokenHandler: () => {
Expand Down

0 comments on commit 54d7e73

Please sign in to comment.