Commit 88d68337 authored by Tamlyn Rhodes's avatar Tamlyn Rhodes

Combine alternative fragmentType validations

Move fragment title property into base validations as it is populated in the constructor Remove duplicate fragment API test
parent 9028ef34
Pipeline #2687 passed with stage
in 4 minutes 33 seconds
......@@ -9,14 +9,20 @@ module.exports = {
sse: false
},
validations: {
fragment: {
source: Joi.string(),
kind: Joi.string(),
title: Joi.string(),
presentation: Joi.string(),
published: Joi.boolean(),
filtered: Joi.string()
},
fragment: [
{
fragmentType: Joi.valid('blogpost').required(),
source: Joi.string(),
kind: Joi.string(),
presentation: Joi.string(),
published: Joi.boolean(),
filtered: Joi.string()
},
{
fragmentType: Joi.valid('file').required(),
path: Joi.string().required()
}
],
collection: {
published: Joi.boolean(),
nonPublicProperty: Joi.string(),
......
......@@ -13,6 +13,7 @@ let validations = {
id: Joi.string().guid().required(),
type: Joi.string().required(),
fragmentType: Joi.string().required(),
title: Joi.string(),
rev: Joi.string(),
fragments: Joi.array().items(Joi.string().guid()),
owners: Joi.array().items(Joi.string().guid())
......@@ -57,15 +58,18 @@ let validations = {
}
let allValidations = function (type, config) {
let configurableValidations
let extraValidations = {}
if (config.validations && config.validations[type]) {
configurableValidations = config.validations[type]
extraValidations = config.validations[type]
}
return Joi.object().keys(
Object.assign({}, validations[type], configurableValidations)
)
if (Array.isArray(extraValidations)) {
const alternatives = extraValidations.map(extra => ({...validations[type], ...extra}))
return Joi.alternatives().try(...alternatives)
}
return Joi.object().keys({...validations[type], ...extraValidations})
}
module.exports = function (config) {
......
......@@ -45,42 +45,6 @@ describe('Fragments API', () => {
expect(fragment.filtered).toEqual('example')
})
it('should allow an admin user to retrieve only some fields of a fragment', async () => {
const adminToken = await authenticateAdmin()
// create a fragment
await api.fragments.post({
fragment: fixtures.fragment, token: adminToken
}).expect(STATUS.CREATED)
// list all the fragments, asking for all fields
const fragments = await api.fragments.get({token: adminToken})
.expect(STATUS.OK)
.then(res => res.body)
expect(fragments).toHaveLength(1)
const fragment = fragments[0]
expect(fragment).toHaveProperty('id')
expect(fragment).toHaveProperty('type')
expect(fragment).toHaveProperty('title')
expect(fragment).toHaveProperty('source')
// list all the fragments, asking for only two fields
const filteredFragments = await api.fragments.get({
token: adminToken, fields: ['type', 'title']
}).expect(STATUS.OK)
.then(res => res.body)
expect(filteredFragments).toHaveLength(1)
const filteredFragment = filteredFragments[0]
expect(filteredFragment).toHaveProperty('id') // the ID field must always be present
expect(filteredFragment).toHaveProperty('type')
expect(filteredFragment).toHaveProperty('title')
expect(filteredFragment).not.toHaveProperty('source')
})
it('should allow an admin user to update a fragment (without filtering properties)', async () => {
const adminToken = await authenticateAdmin()
......@@ -134,7 +98,6 @@ describe('Fragments API', () => {
fragment: fixtures.fragment,
token: adminToken
}).expect(STATUS.CREATED)
.then(res => res.body)
const fragments = await api.fragments.get({
token: adminToken
......
......@@ -3,6 +3,7 @@ const STATUS = require('http-status-codes')
const Model = require('../src/models/Model')
const User = require('../src/models/User')
const Fragment = require('../src/models/Fragment')
const Collection = require('../src/models/Collection')
const dbCleaner = require('./helpers/db_cleaner')
const fixtures = require('./fixtures/fixtures')
......@@ -95,6 +96,25 @@ describe('Model', function () {
})
})
it('rejects a fragment with wrong fragmentType', () => {
const fragment = new Fragment(fixtures.fragment)
fragment.fragmentType = 'file'
expect.hasAssertions()
return fragment.save().catch(err => {
expect(err.name).toEqual('ValidationError')
expect(err.message).toEqual(
'child "fragmentType" fails because ["fragmentType" must be one of [blogpost]], child "path" fails because ["path" is required]'
)
})
})
it('accepts a fragment with alternative fragmentType', () => {
const fragment = new Fragment({fragmentType: 'file', path: '/one/two'})
return fragment.save()
})
it('saving the same object multiple times in parallel throws conflict error', async () => {
expect.hasAssertions()
try {
......
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