Commit dbdd7b42 authored by Tamlyn Rhodes's avatar Tamlyn Rhodes

Minor test refactoring

Split up a few big tests
Fixed a few tests that could accidentally pass when they should fail
Removed some `var`s
`async`ified some setup functions
Removed some redundant calls to cleanDb
parent f43bd376
Pipeline #2747 passed with stage
in 4 minutes and 38 seconds
......@@ -14,25 +14,15 @@ describe('authenticated api', function () {
let user
let collection
beforeEach(() => {
beforeEach(async () => {
// Create collection with admin user and one non-admin user
return dbCleaner().then(
createBasicCollection
).then(
(userAndCol) => {
({ user, collection } = userAndCol)
}
).then(
() => {
// Create another user without any roles
otherUser = new User(fixtures.updatedUser)
return otherUser.save()
}
)
await dbCleaner()
;({user, collection} = await createBasicCollection())
// Create another user without any roles
otherUser = new User(fixtures.updatedUser)
await otherUser.save()
})
afterEach(dbCleaner)
it(`fails to create a fragment in a protected
collection if authenticated as user without permissions`, () => {
return api.users.authenticate.post(
......@@ -123,7 +113,7 @@ describe('authenticated api', function () {
})
describe('actions on a fragment owned by a different user', () => {
var fragment
let fragment
beforeEach(async () => {
const Fragment = require('../src/models/Fragment')
......
const express = require('express')
const app = express()
var api = require('../src')(app)
const api = require('../src')(app)
const User = require('../src/models/User')
const fixtures = require('./fixtures/fixtures')
const cleanDB = require('./helpers/db_cleaner')
describe('api/app locals', () => {
beforeEach(() => {
return cleanDB().then(
() => { return new User(fixtures.adminUser).save() }
)
beforeEach(async () => {
await cleanDB()
await new User(fixtures.adminUser).save()
})
afterEach(cleanDB)
it('exposes models', () => {
expect(api.locals.models.User.type).toEqual('user')
expect(api.locals.models.Team.type).toEqual('team')
......
const STATUS = require('http-status-codes')
var api = require('./helpers/api')
const api = require('./helpers/api')
const createBasicCollection = require('./helpers/basic_collection')
const createFragment = require('./helpers/fragment')
const cleanDB = require('./helpers/db_cleaner')
const Collection = require('../src/models/Collection')
describe('unauthenticated/public api', () => {
var fragment
var unpublishedFragment
var collection
afterEach(cleanDB)
const setNewFragment = (opts) => cleanDB().then(
createBasicCollection
).then(
userAndCol => {
collection = userAndCol.collection
return createFragment(opts, collection)
}
).then(
newfragment => { fragment = newfragment }
).then(
() => createFragment({}, collection)
).then(
fragment => { unpublishedFragment = fragment }
)
let fragment
let unpublishedFragment
let collection
beforeEach(cleanDB)
async function setNewFragment (opts) {
const userAndCollection = await createBasicCollection()
collection = userAndCollection.collection
fragment = await createFragment(opts, collection)
unpublishedFragment = await createFragment({}, collection)
}
describe('published fragment', () => {
beforeEach(() => setNewFragment({ published: true }))
......
......@@ -156,7 +156,7 @@ async function authenticatedUser (user, operation, object, context) {
return unauthenticatedUser(operation, object)
}
var authsomeMode = async function (userId, operation, object, context) {
const authsomeMode = async function (userId, operation, object, context) {
if (!userId) {
return unauthenticatedUser(operation, object)
}
......
......@@ -9,24 +9,17 @@ const dbCleaner = require('./helpers/db_cleaner')
const fixtures = require('./fixtures/fixtures')
describe('Model', function () {
var user
var otherUser
beforeEach(function () {
return dbCleaner().then(
() => {
user = new User(fixtures.user)
return user.save()
}
).then(
() => {
otherUser = new User(fixtures.updatedUser)
return otherUser.save()
}
)
let user
let otherUser
beforeEach(async () => {
await dbCleaner()
user = await new User(fixtures.user).save()
otherUser = await new User(fixtures.updatedUser).save()
})
it('raises an error if trying to find on a destroyed database', () => {
expect.hasAssertions()
return global.db.destroy().then(
() => Model.findByField('field', 'value')
).catch(err => {
......@@ -37,6 +30,7 @@ describe('Model', function () {
})
it('raises an error if trying to find all on a destroyed database', () => {
expect.hasAssertions()
return global.db.destroy().then(
() => User.all()
).catch(err => {
......@@ -47,6 +41,7 @@ describe('Model', function () {
})
it('raises an error if trying to save on a destroyed database', () => {
expect.hasAssertions()
return global.db.destroy().then(
() => user.save()
).catch(err => {
......@@ -56,41 +51,46 @@ describe('Model', function () {
})
})
it('initially has no owner', () => {
const collection = new Collection(fixtures.collection)
expect(collection.owners).toBeUndefined()
expect(collection.isOwner(user.id)).toBe(false)
expect(collection.isOwner(otherUser.id)).toBe(false)
})
it('can set the owners of a Collection', () => {
var collection = new Collection(fixtures.collection)
return collection.save().then((collection) => {
expect(collection.isOwner(user.id)).toBe(false)
expect(collection.isOwner(otherUser.id)).toBe(false)
collection.setOwners([otherUser.id])
expect(collection.owners).toEqual([otherUser.id])
expect(collection.isOwner(user.id)).toBe(false)
expect(collection.isOwner(otherUser.id)).toBe(true)
collection.setOwners([user.id, otherUser.id])
expect(collection.owners.sort()).toEqual([user.id, otherUser.id].sort())
expect(collection.isOwner(user.id)).toBe(true)
expect(collection.isOwner(otherUser.id)).toBe(true)
collection.setOwners([user.id])
expect(collection.owners).toEqual([user.id])
expect(collection.isOwner(user.id)).toBe(true)
expect(collection.isOwner(otherUser.id)).toBe(false)
try {
collection.setOwners('notAnArray')
} catch (err) {
expect(err.name).toEqual('ValidationError')
expect(err.message).toEqual('owners should be an array')
}
})
const collection = new Collection(fixtures.collection)
collection.setOwners([otherUser.id])
expect(collection.owners).toEqual([otherUser.id])
expect(collection.isOwner(user.id)).toBe(false)
expect(collection.isOwner(otherUser.id)).toBe(true)
collection.setOwners([user.id, otherUser.id])
expect(collection.owners.sort()).toEqual([user.id, otherUser.id].sort())
expect(collection.isOwner(user.id)).toBe(true)
expect(collection.isOwner(otherUser.id)).toBe(true)
})
it('cannot set owners to non-array', () => {
const collection = new Collection(fixtures.collection)
expect.hasAssertions()
try {
collection.setOwners('notAnArray')
} catch (err) {
expect(err.name).toEqual('ValidationError')
expect(err.message).toEqual('owners should be an array')
}
})
it('can validate an object', () => {
var user = new User(fixtures.user)
const user = new User(fixtures.user)
user.email = 'notanemail'
user.save().catch(err => {
expect.hasAssertions()
return user.save().catch(err => {
expect(err.name).toEqual('ValidationError')
expect(err.message).toEqual('child "email" fails because ["email" must be a valid email]')
})
......
......@@ -2,28 +2,25 @@ const Fragment = require('../src/models/Fragment')
const validations = require('../src/models/validations')(require('config'))
describe('Validations export', function () {
it('exports useable combined validations (static + configurable)', () => {
expect(Object.keys(validations).sort()).toEqual(
['user', 'fragment', 'team', 'collection'].sort()
)
it('has validations for each type', () => {
expect(Object.keys(validations).sort()).toEqual(['collection', 'fragment', 'team', 'user'])
})
var fragment = new Fragment({
it('allows fragment with required fields', () => {
const fragment = new Fragment({
title: 'Testing',
fragmentType: 'blogpost',
owners: ['d56153c3-0ddf-44fd-9bec-c4151329ef0a']
fragmentType: 'blogpost'
})
expect(fragment.validate()).toBe(true)
})
var message
try {
fragment.type = undefined
fragment.validate()
} catch (error) {
message = error.message
}
it('rejects fragment with missing type', () => {
const fragment = new Fragment({
title: 'Testing'
})
fragment.type = undefined
expect(message).toBeTruthy()
expect(() => fragment.validate()).toThrow('"type" is required')
})
})
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