paradigm-channels
Version:
1,318 lines (1,083 loc) • 35.8 kB
JavaScript
const Migrations = require('structure-migrations')
const MockHTTPServer = require('../helpers/mock-http-server')
const pluginsList = require('../helpers/plugins')
const jobPurgeChannel = require('../../src/jobs/purge')
const server = new MockHTTPServer()
const createOrgAndApp = async function() {
// getting an organization and application Ids
var res0 = await new MockHTTPServer()
.post(`/api/${process.env.API_VERSION}/organizations`)
.send({
title: 'work it'
})
const org = res0.body.pkg
const orgId = org.id
var app = await new MockHTTPServer()
.post(`/api/${process.env.API_VERSION}/applications`)
.set('organizationid', orgId)
.send({
desc: '',
title: 'App 45'
})
const appId = app.body.pkg.id
return {orgId, appId}
}
describe('Routes', function() {
before(function() {
this.sandbox = sinon.sandbox.create()
this.queueSpy = this.sandbox.spy()
this.sandbox.stub(jobPurgeChannel, 'queue', this.queueSpy)
})
beforeEach(function() {
this.migration = new Migrations({
db: 'test',
items: {tables: [{
action: 'create',
table: 'digital_assets',
indexes: [
'applicationId',
'updatedAt'
]
}]},
plugins: pluginsList
})
return this.migration.process()
})
afterEach(function() {
this.queueSpy.reset()
return this.migration.purge()
})
after(function() {
this.sandbox.restore()
})
it('should create an channel', async function() {
const {orgId, appId} = await createOrgAndApp()
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
var pkg = {
desc: '',
title: 'Trending',
categoryIds: [category.id]
}
var res = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
expect(res.body.pkg.title).to.equal('Trending')
expect(res.body.pkg.categoryIds).to.deep.equal([category.id])
expect(res.body.status).to.equal(201)
expect(this.queueSpy.calledOnce).to.be.true
})
it('should not create an channel; invalid categoryId scope', async function() {
const {orgId, appId} = await createOrgAndApp()
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
var pkg = {
desc: '',
title: 'Trending',
categoryIds: [category.id]
}
var res = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', '12345')
.send(pkg)
expect(res.body.status).to.equal(400)
expect(res.body.err.code).to.equal("ID_SCOPE_INVALID")
expect(this.queueSpy.calledOnce).to.be.false
})
it('should get a channel by Id', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
desc: '',
title: 'Trending 45'
}
const channelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
const channelId = channelRes.body.pkg.id
var res = await server
.get(`/api/${process.env.API_VERSION}/channels/${channelId}`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res.body.pkg.title).to.equal('Trending 45')
expect(res.body.status).to.equal(200)
})
it('should get a channel by slug', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
desc: '',
title: 'Trending 45'
}
var res0 = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
var channel = res0.body.pkg
var res = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res.body.pkg.title).to.equal('Trending 45')
expect(res.body.status).to.equal(200)
})
it.skip('should match a channel by slug', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
desc: '',
title: 'Trending 45'
}
var res0 = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
var channel = res0.body.pkg
var res = await server
.get(`/api/${process.env.API_VERSION}/channels/match/${channel.slug}`)
.set('organizationid', orgId)
.set('applicationid', appId)
const channels = res.body.pkg.channels
expect(channels[0].title).to.equal('Trending 45')
expect(res.body.status).to.equal(200)
})
it('should get all channels', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
desc: '',
title: 'Trending 46'
}
await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
var res = await server
.get(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res.body.pkg.channels.length).to.be.above(0)
expect(res.body.status).to.equal(200)
})
it('should get all channels of correct organization', async function() {
const res0 = await createOrgAndApp()
const org1Id = res0.orgId
const app1Id = res0.appId
const res1 = await createOrgAndApp()
const org2Id = res1.orgId
const app2Id = res1.appId
await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', org1Id)
.set('applicationid', app1Id)
.send({
desc: '',
title: 'Trending 46'
})
await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', org2Id)
.set('applicationid', app2Id)
.send({
desc: '',
title: 'Trending 47'
})
var res = await server
.get(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', org1Id)
.set('applicationid', app1Id)
expect(res.body.status).to.equal(200)
expect(res.body.pkg.channels.length).to.equal(1)
expect(res.body.pkg.channels[0].title).to.equal('Trending 46')
})
it('should create a channel once', async function() {
const {orgId, appId} = await createOrgAndApp()
await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
title: 'Trending More'
})
await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
title: 'Trending More'
})
var res = await server
.get(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res.body.pkg.channels.length).to.equal(1)
expect(res.body.status).to.equal(200)
})
it('should update an channel by Id', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
desc: '',
title: 'Trending 47'
}
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
const channelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
const channelId = channelRes.body.pkg.id
this.queueSpy.reset()
var res = await server
.patch(`/api/${process.env.API_VERSION}/channels/${channelId}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
title: 'Trending 48',
categoryIds: [category.id]
})
var res2 = await server
.get(`/api/${process.env.API_VERSION}/channels/${channelId}`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res2.body.pkg.title).to.equal('Trending 48')
expect(res2.body.pkg.categoryIds).to.deep.equal([category.id])
expect(res.body.status).to.equal(200)
expect(this.queueSpy.calledOnce).to.be.true
})
it('should not update an channel by Id; invalid categoryId scope', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
desc: '',
title: 'Trending 47'
}
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
const channelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
const channelId = channelRes.body.pkg.id
this.queueSpy.reset()
var res = await server
.patch(`/api/${process.env.API_VERSION}/channels/${channelId}`)
.set('organizationid', orgId)
.set('applicationid', '12345')
.send({
desc: '',
title: 'Trending 48',
categoryIds: [category.id]
})
expect(res.body.status).to.equal(400)
expect(res.body.err.code).to.equal("ID_SCOPE_INVALID")
expect(this.queueSpy.calledOnce).to.be.false
})
it('should get channel resources by id', async function() {
const {orgId, appId} = await createOrgAndApp()
const userRes = await server
.post(`/api/${process.env.API_VERSION}/users`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
email: 'd.umbridge@magic.ministry.gov.co.uk',
password: 'd3m3nt0rs4l1f3',
username: 'umbridge'
})
const user = userRes.body.pkg
const channelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'hogwarts',
title: 'Hogwarts Lately'
})
const channel = channelRes.body.pkg
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
status: 'published',
tags: ['love'],
title: '15 Love Potions that did not make the final cut',
slug: '15-love-potions',
userId: user.id
})
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [category.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
status: 'published',
tags: ['felix'],
title: 'Felix Potions Discontinued!',
slug: 'felix-potions',
userId: user.id
})
let resources = null
/*
First assertion:
Get documents by category
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'potions'
}
]
})
const res1 = await server
.get(`/api/${process.env.API_VERSION}/channels/${channel.id}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res1.body.pkg.resources)
expect(resources.get('potions').length).to.equal(1)
/*
Second assertion:
Get document by tag (and operator)
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'potions',
tags: ['felix'],
taxonomyOperator: 'and'
}
]
})
const res2 = await server
.get(`/api/${process.env.API_VERSION}/channels/${channel.id}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res2.body.pkg.resources)
expect(resources.get('potions').length).to.equal(1)
expect(resources.get('potions')[0].tags[0]).to.equal('felix')
/*
Third assertion:
Get document by tag (or operator)
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'potions',
tags: ['felix'],
//taxonomyOperator: 'and' - default is or
}
]
})
const res3 = await server
.get(`/api/${process.env.API_VERSION}/channels/${channel.id}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res3.body.pkg.resources)
expect(resources.get('potions').length).to.equal(1)
expect(resources.get('potions')[0].tags[0]).to.equal('felix') // Default order is by publishedAt descending
/*
Fourth assertion:
Get two sections
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'section_felix',
tags: ['felix'],
taxonomyOperator: 'and'
},
{
// categorySlugs: ['potions'],
name: 'section_love',
tags: ['love'],
// taxonomyOperator: 'and'
}
]
})
const res4 = await server
.get(`/api/${process.env.API_VERSION}/channels/${channel.id}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res4.body.pkg.resources)
expect(resources.get('section_felix').length).to.equal(1)
expect(resources.get('section_felix')[0].tags[0]).to.equal('felix')
expect(resources.get('section_love').length).to.equal(1)
expect(resources.get('section_love')[0].tags[0]).to.equal('love')
/*
Fifth assertion:
Prove that no duplicate documents are returned; document is returned in only first section possible
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'section_felix',
tags: ['felix'],
taxonomyOperator: 'and'
},
{
// categorySlugs: ['potions'],
name: 'potions',
tags: ['felix'],
// taxonomyOperator: 'and' - default is or
},
{
// categorySlugs: ['potions'],
name: 'section_love',
tags: ['love'],
// taxonomyOperator: 'and'
}
]
})
const res5 = await server
.get(`/api/${process.env.API_VERSION}/channels/${channel.id}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res5.body.pkg.resources)
expect(resources.get('section_felix').length).to.equal(1)
expect(resources.get('section_felix')[0].tags[0]).to.equal('felix')
expect(resources.get('potions')[0].tags[0]).to.equal('love')
expect(resources.get('section_love').length).to.equal(0) // 2nd section took this document
})
it('should get channel resources by slug', async function() {
const {orgId, appId} = await createOrgAndApp()
const userRes = await server
.post(`/api/${process.env.API_VERSION}/users`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
email: 'd.umbridge@magic.ministry.gov.co.uk',
password: 'd3m3nt0rs4l1f3',
username: 'umbridge'
})
const user = userRes.body.pkg
const channelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'hogwarts',
title: 'Hogwarts Lately'
})
const channel = channelRes.body.pkg
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [category.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
status: 'published',
tags: ['love'],
title: '15 Love Potions that did not make the final cut',
slug: '15-love-potions',
userId: user.id
})
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [category.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
status: 'published',
tags: ['felix'],
title: 'Felix Potions Discontinued!',
slug: 'felix-potions',
userId: user.id
})
let resources = null
/*
First assertion:
Get documents by category
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'potions'
}
]
})
const res1 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res1.body.pkg.resources)
expect(resources.get('potions').length).to.equal(2)
/*
Second assertion:
Get document by tag (and operator)
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'potions',
tags: ['felix'],
taxonomyOperator: 'and'
}
]
})
const res2 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res2.body.pkg.resources)
expect(resources.get('potions').length).to.equal(1)
expect(resources.get('potions')[0].tags[0]).to.equal('felix')
/*
Third assertion:
Get document by tag (or operator)
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'potions',
tags: ['felix'],
//taxonomyOperator: 'and' - default is or
}
]
})
const res3 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res3.body.pkg.resources)
expect(resources.get('potions').length).to.equal(2)
expect(resources.get('potions')[0].tags[0]).to.equal('felix')
/*
Fourth assertion:
Get two sections
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'section_felix',
tags: ['felix'],
taxonomyOperator: 'and'
},
{
categorySlugs: ['potions'],
name: 'section_love',
tags: ['love'],
taxonomyOperator: 'and'
}
]
})
const res4 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res4.body.pkg.resources)
expect(resources.get('section_felix').length).to.equal(1)
expect(resources.get('section_felix')[0].tags[0]).to.equal('felix')
expect(resources.get('section_love').length).to.equal(1)
expect(resources.get('section_love')[0].tags[0]).to.equal('love')
/*
Fifth assertion:
Prove that no duplicate documents are returned; document is returned in only first section possible
*/
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
name: 'section_felix',
tags: ['felix'],
taxonomyOperator: 'and'
},
{
categorySlugs: ['potions'],
name: 'potions',
tags: ['felix'],
//taxonomyOperator: 'and' - default is or
},
{
categorySlugs: ['potions'],
name: 'section_love',
tags: ['love'],
taxonomyOperator: 'and'
}
]
})
const res5 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources = new Map(res5.body.pkg.resources)
expect(resources.get('section_felix').length).to.equal(1)
expect(resources.get('section_felix')[0].tags[0]).to.equal('felix')
expect(resources.get('potions')[0].tags[0]).to.equal('love')
expect(resources.get('section_love').length).to.equal(0) // 2nd section took this document
})
it('should get channel RSS data by slug', async function() {
const {orgId, appId} = await createOrgAndApp()
const userRes = await server
.post(`/api/${process.env.API_VERSION}/users`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
email: 'd.umbridge@magic.ministry.gov.co.uk',
password: 'd3m3nt0rs4l1f3',
username: 'umbridge'
})
const user = userRes.body.pkg
const potionsCategoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const potionsCategory = potionsCategoryRes.body.pkg
const catCategoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'cats',
title: 'Cats'
})
const catCategory = catCategoryRes.body.pkg
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [potionsCategory.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
status: 'published',
tags: ['love'],
title: '15 Love Potions that did not make the final cut',
slug: '15-love-potions',
userId: user.id
})
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [potionsCategory.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
status: 'published',
tags: ['felix'],
title: 'Felix Potions Discontinued!',
slug: 'felix-potions',
userId: user.id
})
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [catCategory.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
status: 'published',
tags: ['felix'],
title: '15 Amazing Cat Facts',
slug: 'cat-facts',
userId: user.id
})
const potionsChannelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions Hogwarts Lately',
resources: [
{
categorySlugs: ['potions'],
name: 'latest'
}
]
})
const potionsChannel = potionsChannelRes.body.pkg
const indexChannelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'index',
title: 'Index',
resources: [
{
categorySlugs: ['potions', 'cats'],
name: 'latest'
}
]
})
const indexChannel = indexChannelRes.body.pkg
const res1 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${potionsChannel.slug}/rss`)
.set('organizationid', orgId)
.set('applicationid', appId)
const potionsResources = new Map(res1.body.pkg.resources)
expect(potionsResources.get('latest').length).to.equal(2)
expect(potionsResources.get('latest')[0].fields.length).to.equal(1)
expect(potionsResources.get('latest')[0].user).to.be.an('object')
const res2 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${indexChannel.slug}/rss`)
.set('organizationid', orgId)
.set('applicationid', appId)
const indexResources = new Map(res2.body.pkg.resources)
expect(indexResources.get('latest').length).to.equal(3)
expect(indexResources.get('latest')[0].fields.length).to.equal(1)
expect(indexResources.get('latest')[0].user).to.be.an('object')
})
it('should paginate selected channel resource by slug', async function() {
const {orgId, appId} = await createOrgAndApp()
const userRes = await server
.post(`/api/${process.env.API_VERSION}/users`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
email: 'd.umbridge@magic.ministry.gov.co.uk',
password: 'd3m3nt0rs4l1f3',
username: 'umbridge'
})
const user = userRes.body.pkg
const channelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'hogwarts',
title: 'Hogwarts Lately'
})
const channel = channelRes.body.pkg
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
publishedAt: new Date().toISOString(),
status: 'published',
tags: ['love'],
title: '15 Love Potions that did not make the final cut',
slug: '15-love-potions',
categoryIds: [],
userId: user.id
})
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [category.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
publishedAt: new Date().toISOString(),
status: 'published',
tags: ['felix'],
title: 'Felix Potions Discontinued!',
slug: 'felix-potions',
userId: user.id
})
let resources1 = null
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
limit: 1,
name: 'potions'
},
{
limit: 1,
name: 'love',
tags: ['love']
}
]
})
const res1 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources/potions`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources1 = new Map(res1.body.pkg.resources)
const doc2 = resources1.get('potions')[0]
expect(resources1.get('potions').length).to.equal(1)
expect(doc2.title).to.equal('Felix Potions Discontinued!')
let resources2 = null
const res2 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources/love`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources2 = new Map(res2.body.pkg.resources)
const doc1 = resources2.get('love')[0]
expect(resources2.get('love').length).to.equal(1)
expect(doc1.title).to.equal('15 Love Potions that did not make the final cut')
})
it('should paginate selected channel resource by slug (page count)', async function() {
const {orgId, appId} = await createOrgAndApp()
const userRes = await server
.post(`/api/${process.env.API_VERSION}/users`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
email: 'd.umbridge@magic.ministry.gov.co.uk',
password: 'd3m3nt0rs4l1f3',
username: 'umbridge'
})
const user = userRes.body.pkg
const channelRes = await server
.post(`/api/${process.env.API_VERSION}/channels`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'hogwarts',
title: 'Hogwarts Lately'
})
const channel = channelRes.body.pkg
const categoryRes = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
slug: 'potions',
title: 'Potions'
})
const category = categoryRes.body.pkg
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
publishedAt: new Date().toISOString(),
status: 'published',
tags: ['love'],
title: '15 Love Potions that did not make the final cut',
slug: '15-love-potions',
categoryIds: [],
userId: user.id
})
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [category.id],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
publishedAt: new Date().toISOString(),
status: 'published',
tags: ['felix'],
title: 'Felix Potions Discontinued!',
slug: 'felix-potions',
userId: user.id
})
await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
categoryIds: [],
desc: '',
fields: [{foo: 'bar'}],
organizationId: orgId,
publishedAt: new Date().toISOString(),
status: 'published',
tags: ['love'],
title: '20 discontinued love potions',
slug: '20-discontinued-love-potions',
userId: user.id
})
let resources1 = null
await server
.patch(`/api/${process.env.API_VERSION}/channels/${channel.id}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
resources: [
{
categorySlugs: ['potions'],
limit: 1,
name: 'potions'
},
{
limit: 1,
name: 'love',
tags: ['love']
}
]
})
const res1 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources1 = new Map(res1.body.pkg.resources)
const doc2 = resources1.get('potions')[0]
expect(resources1.get('potions').length).to.equal(1)
expect(doc2.title).to.equal('Felix Potions Discontinued!')
let resources2 = null
const res2 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources/love?page=1&limit=1`)
.set('organizationid', orgId)
.set('applicationid', appId)
resources2 = new Map(res2.body.pkg.resources)
const doc1 = resources2.get('love')[0]
expect(resources2.get('love').length).to.equal(1)
expect(doc1.title).to.equal('20 discontinued love potions')
const res3 = await server
.get(`/api/${process.env.API_VERSION}/channels/slug/${channel.slug}/resources/love?page=2&limit=1`)
.set('organizationid', orgId)
.set('applicationid', appId)
const resources3 = new Map(res3.body.pkg.resources)
const doc3 = resources3.get('love')[0]
expect(resources3.get('love').length).to.equal(1)
expect(doc3.title).to.equal('15 Love Potions that did not make the final cut')
})
})