Improved e2e tests

This commit is contained in:
Daniel Gonzalez
2025-05-30 11:53:57 -04:00
parent 8843b3e2b0
commit 34381e6e93
4 changed files with 47 additions and 78 deletions
+15 -2
View File
@@ -4,8 +4,8 @@
// Public npm libraries // Public npm libraries
import { assert } from 'chai' import { assert } from 'chai'
import axios from 'axios' import axios from 'axios'
import testUtils from '../../utils/test-utils.js'
// const sinon = require('sinon') // const sinon = require('sinon')
@@ -25,7 +25,20 @@ describe('#Check Server Liveness', () => {
assert(response.status === 200, 'Server is running, continuing with E2E tests.') assert(response.status === 200, 'Server is running, continuing with E2E tests.')
} catch (err) { } catch (err) {
console.log('\nServer is not running, exiting tests.') 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) process.exit(1)
} }
}) })
-58
View File
@@ -166,64 +166,6 @@ if (!config.noMongo) {
assert.property(result.data, 'token', 'Token property exists.') assert.property(result.data, 'token', 'Token property exists.')
assert.equal(result.data.user.type, 'user') 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', () => { describe('GET /users', () => {
@@ -21,24 +21,10 @@ describe('Admin', () => {
describe('loginAdmin()', () => { describe('loginAdmin()', () => {
it('should logind admin', async () => { it('should logind admin', async () => {
try { try {
const error = new Error('test error') sandbox.stub(uut.axios, 'request').resolves(true)
error.response = {
status: 422
}
// sandbox.stub(uut.axios, 'request').onFirstCall().throws(error)
const result = await uut.loginAdmin() const result = await uut.loginAdmin()
const user = result.data.user assert.isTrue(result)
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')
} catch (err) { } catch (err) {
assert(false, 'Unexpected result') assert(false, 'Unexpected result')
} }
@@ -48,13 +34,13 @@ describe('Admin', () => {
try { try {
// Returns an erroneous password to force // Returns an erroneous password to force
// an auth error // an auth error
sandbox.stub(uut.axios, 'request').throws(new Error('test error'))
sandbox.stub(uut.jsonFiles, 'readJSON').resolves({ password: 'wrong' }) sandbox.stub(uut.jsonFiles, 'readJSON').resolves({ password: 'wrong' })
await uut.loginAdmin() await uut.loginAdmin()
assert(false, 'Unexpected result') assert(false, 'Unexpected result')
} catch (err) { } catch (err) {
assert.equal(err.response.status, 401) assert.include(err.message, 'test error')
assert.include(err.response.data, 'Unauthorized')
} }
}) })
}) })
@@ -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')
})
})
}) })