diff --git a/test/e2e/automated/a00-liveness.rest-e2e.js b/test/e2e/automated/a00-liveness.rest-e2e.js index b4981fb..6401503 100644 --- a/test/e2e/automated/a00-liveness.rest-e2e.js +++ b/test/e2e/automated/a00-liveness.rest-e2e.js @@ -4,8 +4,8 @@ // Public npm libraries import { assert } from 'chai' - import axios from 'axios' +import testUtils from '../../utils/test-utils.js' // const sinon = require('sinon') @@ -25,7 +25,20 @@ describe('#Check Server Liveness', () => { assert(response.status === 200, 'Server is running, continuing with E2E tests.') } catch (err) { console.log('\nServer is not running, exiting tests.') - console.log('Start the server with `npm start` before running E2E tests.\n') + console.log('Start the server with `npm run start:e2e:server` before running E2E tests.\n') + console.log('Ensure running npm run docs before running the test server') + process.exit(1) + } + }) + it('should confirm the server is running over test enviroment', async () => { + try { + const res = await testUtils.loginAdminUser() + assert.property(res, 'user') + assert.property(res, 'token') + assert.property(res, 'id') + } catch (err) { + console.log('\nServer is not running over test enviroment, exiting tests.') + console.log('Start the server with `npm run start:e2e:server` before running E2E tests.\n') process.exit(1) } }) diff --git a/test/e2e/automated/a02-users.rest-e2e.js b/test/e2e/automated/a02-users.rest-e2e.js index 222664a..7ce8ac5 100644 --- a/test/e2e/automated/a02-users.rest-e2e.js +++ b/test/e2e/automated/a02-users.rest-e2e.js @@ -166,64 +166,6 @@ if (!config.noMongo) { assert.property(result.data, 'token', 'Token property exists.') assert.equal(result.data.user.type, 'user') }) - it('should reject signup when DISABLE_NEW_ACCOUNTS is true', async () => { - try { - process.env.DISABLE_NEW_ACCOUNTS = true - const options = { - method: 'POST', - url: `${LOCALHOST}/users`, - data: { - email: 'test2@test.com', - password: 'supersecretpassword', - name: 'test3' - } - } - - await axios(options) - - assert(false, 'Unexpected result') - } catch (err) { - console.log(err) - assert(err.response.status === 401, 'Error code 401 expected.') - } - }) - it('admin can create a user when DISABLE_NEW_ACCOUNTS is true', async () => { - try { - process.env.DISABLE_NEW_ACCOUNTS = true - const options = { - method: 'post', - url: `${LOCALHOST}/users`, - headers: { - Authorization: `Bearer ${context.adminJWT}` - }, - data: { - user: { - email: 'fromAdmin@test.com', - password: 'supersecretpassword', - name: 'test3' - } - } - } - const result = await axios(options) - - context.user = result.data.user - context.token = result.data.token - - assert(result.status === 200, 'Status Code 200 expected.') - assert( - result.data.user.email === 'fromAdmin@test.com', - 'Email of test expected' - ) - assert( - result.data.user.password === undefined, - 'Password expected to be omited' - ) - assert.property(result.data, 'token', 'Token property exists.') - assert.equal(result.data.user.type, 'user') - } catch (error) { - assert.fail('Unexpected code path') - } - }) }) describe('GET /users', () => { diff --git a/test/e2e/automated/a09-admin.rest-e2e.js b/test/unit/adapters/admin.adapter.unit.js similarity index 84% rename from test/e2e/automated/a09-admin.rest-e2e.js rename to test/unit/adapters/admin.adapter.unit.js index ec31e0f..a78e5ba 100644 --- a/test/e2e/automated/a09-admin.rest-e2e.js +++ b/test/unit/adapters/admin.adapter.unit.js @@ -21,24 +21,10 @@ describe('Admin', () => { describe('loginAdmin()', () => { it('should logind admin', async () => { try { - const error = new Error('test error') - error.response = { - status: 422 - } - // sandbox.stub(uut.axios, 'request').onFirstCall().throws(error) + sandbox.stub(uut.axios, 'request').resolves(true) const result = await uut.loginAdmin() - const user = result.data.user - - assert.property(user, '_id') - assert.property(user, 'email') - assert.property(user, 'type') - - assert.isString(user._id) - assert.isString(user.email) - assert.isString(user.type) - - assert.equal(user.type, 'admin') + assert.isTrue(result) } catch (err) { assert(false, 'Unexpected result') } @@ -48,13 +34,13 @@ describe('Admin', () => { try { // Returns an erroneous password to force // an auth error + sandbox.stub(uut.axios, 'request').throws(new Error('test error')) sandbox.stub(uut.jsonFiles, 'readJSON').resolves({ password: 'wrong' }) await uut.loginAdmin() assert(false, 'Unexpected result') } catch (err) { - assert.equal(err.response.status, 401) - assert.include(err.response.data, 'Unauthorized') + assert.include(err.message, 'test error') } }) }) diff --git a/test/unit/controllers/rest-api/users/users.rest.router.unit.js b/test/unit/controllers/rest-api/users/users.rest.router.unit.js index 798264f..ef5fcb3 100644 --- a/test/unit/controllers/rest-api/users/users.rest.router.unit.js +++ b/test/unit/controllers/rest-api/users/users.rest.router.unit.js @@ -79,4 +79,32 @@ describe('#Users-REST-Router', () => { } }) }) + + describe('#createUser', () => { + it('should ignore admin validator when DISABLE_NEW_ACCOUNTS is not defined', async () => { + // Stub functions + const validationSpy = sandbox.stub(uut.validators, 'ensureAdmin').resolves(true) + sandbox.stub(uut.userRESTController, 'createUser').resolves(true) + + // Call function + await uut.createUser() + + // Assertions + assert.isTrue(validationSpy.notCalled, 'Admin validator should not be called') + }) + it('should ensure admin when DISABLE_NEW_ACCOUNTS is defined', async () => { + // Set environment variable + process.env.DISABLE_NEW_ACCOUNTS = true + + // Stub functions + const validationSpy = sandbox.stub(uut.validators, 'ensureAdmin').resolves(true) + sandbox.stub(uut.userRESTController, 'createUser').resolves(true) + + // Call function + await uut.createUser() + + // Assertions + assert.isTrue(validationSpy.calledOnce, 'Admin validator should be called') + }) + }) })