codecombat/test/server/integration/models/plugins.spec.coffee

329 lines
11 KiB
CoffeeScript
Raw Normal View History

2014-02-03 15:55:09 -05:00
require '../../common'
2014-01-03 13:32:13 -05:00
describe 'NamePlugin', ->
article = new Article(
2014-06-30 22:16:26 -04:00
name: 'Alpha'
2014-01-03 13:32:13 -05:00
body: 'What does it mean?'
)
it 'clears things first', (done) ->
clearModels [Article], (err) ->
expect(err).toBeNull()
done()
it 'saves', (done) ->
article.save (err) ->
throw err if err
done()
it 'does not allow name conflicts', (done) ->
c2 = new Article(
2014-06-30 22:16:26 -04:00
name: 'Alpha'
2014-01-03 13:32:13 -05:00
body: 'The misunderstood Greek character.'
)
c2.save (err) ->
expect(err.code).toBe(409)
done()
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
it 'prevents slugs from being valid ObjectIds', (done) ->
c2 = new Article(
2014-06-30 22:16:26 -04:00
name: '522e0f149aaa330000000002'
2014-01-03 13:32:13 -05:00
body: '... fish.'
)
c2.save (err) ->
expect(err.code).toBe(422)
done()
describe 'VersionedPlugin', ->
it 'clears things first', (done) ->
clearModels [Article], (err) ->
expect(err).toBeNull()
done()
it 'can create new major versions', (done) ->
2014-06-30 22:16:26 -04:00
firstArticle = new Article(name: 'List Comp1', body: "A programmer's best friend.")
2014-01-03 13:32:13 -05:00
firstArticle.original = firstArticle._id
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
firstArticle.save (err) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
secondObject = firstArticle.toObject()
2014-06-30 22:16:26 -04:00
secondObject['body'] = 'Not as good as lambda.'
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMajorVersion secondObject, (err, secondArticle) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
secondArticle.save (err) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
thirdObject = secondArticle.toObject()
2014-06-30 22:16:26 -04:00
thirdObject['body'] = '...'
2014-01-03 13:32:13 -05:00
secondArticle.makeNewMajorVersion thirdObject, (err, thirdArticle) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
thirdArticle.save ->
2014-06-30 22:16:26 -04:00
Article.find {original: firstArticle.original}, (err, results) ->
2014-01-03 13:32:13 -05:00
expect(results.length).toBe(3)
expect(results[0].version.major).toBe(2)
expect(results[1].version.major).toBe(1)
expect(results[2].version.major).toBe(0)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[0].version.minor).toBe(0)
expect(results[1].version.minor).toBe(0)
expect(results[2].version.minor).toBe(0)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.isLatestMajor).toBe(false)
expect(results[1].version.isLatestMajor).toBe(false)
expect(results[0].version.isLatestMajor).toBe(true)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.isLatestMinor).toBe(true)
expect(results[1].version.isLatestMinor).toBe(true)
expect(results[0].version.isLatestMinor).toBe(true)
expect(results[2].index).toBeUndefined()
expect(results[1].index).toBeUndefined()
expect(results[0].index).toBe(true)
done()
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
it 'works if you do not successfully save the new major version', (done) ->
2014-06-30 22:16:26 -04:00
firstArticle = new Article(name: 'List Comp2', body: "A programmer's best friend.")
2014-01-03 13:32:13 -05:00
firstArticle.original = firstArticle._id
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
firstArticle.save (err) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
secondObject = firstArticle.toObject()
2014-06-30 22:16:26 -04:00
secondObject['body'] = 'Not as good as lambda.'
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMajorVersion secondObject, (err, forgottenSecondArticle) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMajorVersion secondObject, (err, realSecondArticle) ->
throw err if err
expect(realSecondArticle.version.major).toBe(1)
done()
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
it 'can create new minor versions', (done) ->
2014-06-30 22:16:26 -04:00
firstArticle = new Article(name: 'List Comp3', body: "A programmer's best friend.")
2014-01-03 13:32:13 -05:00
firstArticle.original = firstArticle._id
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
firstArticle.save (err) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
secondObject = firstArticle.toObject()
2014-06-30 22:16:26 -04:00
secondObject['body'] = 'Not as good as lambda.'
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMinorVersion secondObject, 0, (err, secondArticle) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
secondArticle.save (err) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
thirdObject = secondArticle.toObject()
2014-06-30 22:16:26 -04:00
thirdObject['body'] = '...'
2014-01-03 13:32:13 -05:00
secondArticle.makeNewMinorVersion thirdObject, 0, (err, thirdArticle) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
thirdArticle.save ->
2014-06-30 22:16:26 -04:00
Article.find {original: firstArticle.original}, (err, results) ->
2014-01-03 13:32:13 -05:00
expect(results.length).toBe(3)
expect(results[0].version.major).toBe(0)
expect(results[1].version.major).toBe(0)
expect(results[2].version.major).toBe(0)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.minor).toBe(0)
expect(results[1].version.minor).toBe(1)
expect(results[0].version.minor).toBe(2)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.isLatestMajor).toBe(false)
expect(results[1].version.isLatestMajor).toBe(false)
expect(results[0].version.isLatestMajor).toBe(true)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.isLatestMinor).toBe(false)
expect(results[1].version.isLatestMinor).toBe(false)
expect(results[0].version.isLatestMinor).toBe(true)
expect(results[2].index).toBeUndefined()
expect(results[1].index).toBeUndefined()
expect(results[0].index).toBe(true)
done()
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
it 'works if you do not successfully save the new minor version', (done) ->
firstArticle = new Article(
2014-06-30 22:16:26 -04:00
name: 'List Comp4',
body: "A programmer's best friend."
2014-01-03 13:32:13 -05:00
index: true
)
firstArticle.original = firstArticle._id
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
firstArticle.save (err) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
secondObject = firstArticle.toObject()
2014-06-30 22:16:26 -04:00
secondObject['body'] = 'Not as good as lambda.'
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMinorVersion secondObject, 0, (err, forgottenSecondArticle) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMinorVersion secondObject, 0, (err, realSecondArticle) ->
throw err if err
expect(realSecondArticle.version.minor).toBe(1)
done()
it 'works if you add a new minor version for an old major version', (done) ->
2014-06-30 22:16:26 -04:00
firstArticle = new Article(name: 'List Comp4.5', body: "A programmer's best friend.")
2014-01-03 13:32:13 -05:00
firstArticle.original = firstArticle._id
firstArticle.save (err) ->
throw err if err
secondObject = firstArticle.toObject()
2014-06-30 22:16:26 -04:00
secondObject['body'] = 'Not as good as lambda.'
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMajorVersion secondObject, (err, secondArticle) ->
throw err if err
secondArticle.save (err) ->
throw err if err
thirdObject = secondArticle.toObject()
2014-06-30 22:16:26 -04:00
thirdObject['body'] = '...'
2014-01-03 13:32:13 -05:00
Article.findOne {_id: firstArticle._id}, (err, firstArticle) ->
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMinorVersion thirdObject, 0, (err, thirdArticle) ->
throw err if err
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
thirdArticle.save ->
2014-06-30 22:16:26 -04:00
Article.find {original: firstArticle.original}, (err, results) ->
2014-01-03 13:32:13 -05:00
expect(results.length).toBe(3)
expect(results[2].version.major).toBe(0)
expect(results[1].version.major).toBe(0)
expect(results[0].version.major).toBe(1)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.minor).toBe(0)
expect(results[1].version.minor).toBe(1)
expect(results[0].version.minor).toBe(0)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.isLatestMajor).toBe(false)
expect(results[1].version.isLatestMajor).toBe(false)
expect(results[0].version.isLatestMajor).toBe(true)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].version.isLatestMinor).toBe(false)
expect(results[1].version.isLatestMinor).toBe(true)
expect(results[0].version.isLatestMinor).toBe(true)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
expect(results[2].index).toBeUndefined()
expect(results[1].index).toBeUndefined()
expect(results[0].index).toBe(true)
done()
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
it 'only keeps slugs for the absolute latest versions', (done) ->
2014-06-30 22:16:26 -04:00
firstArticle = new Article(name: 'List Comp4.6', body: "A programmer's best friend.")
2014-01-03 13:32:13 -05:00
firstArticle.original = firstArticle._id
firstArticle.save (err) ->
throw err if err
secondObject = firstArticle.toObject()
2014-06-30 22:16:26 -04:00
secondObject['body'] = 'Not as good as lambda.'
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMajorVersion secondObject, (err, secondArticle) ->
throw err if err
secondArticle.save (err) ->
throw err if err
thirdObject = secondArticle.toObject()
2014-06-30 22:16:26 -04:00
thirdObject['body'] = '...'
2014-01-03 13:32:13 -05:00
Article.findOne {_id: firstArticle._id}, (err, firstArticle) ->
firstArticle.makeNewMinorVersion thirdObject, 0, (err, thirdArticle) ->
throw err if err
thirdArticle.save ->
2014-06-30 22:16:26 -04:00
Article.find {original: firstArticle.original}, (err, results) ->
2014-01-03 13:32:13 -05:00
expect(results.length).toBe(3)
expect(results[2].slug).toBeUndefined()
expect(results[1].slug).toBeUndefined()
2014-01-15 14:36:59 -05:00
expect(results[0].slug).toBeDefined()
2014-01-03 13:32:13 -05:00
done()
describe 'SearchablePlugin', ->
it 'clears things first', (done) ->
clearModels [Article], (err) ->
expect(err).toBeNull()
done()
it 'can do a text search', (done) ->
# absolutely does not work at all if you don't save an article first
firstArticle = new Article(
2014-06-30 22:16:26 -04:00
name: 'List Comp5',
body: "A programmer's best friend.",
index: true
2014-01-03 13:32:13 -05:00
)
firstArticle.original = firstArticle._id
firstArticle.save (err) ->
throw err if err
Article.find {$text: {$search: 'best'}, index: true}, (err, results) ->
2014-01-03 13:32:13 -05:00
expect(err).toBeNull()
if results
expect(results.length).toBeGreaterThan(0)
2014-01-03 13:32:13 -05:00
else
console.log('ERROR:', err)
done()
it 'keeps the index property up to date', (done) ->
2014-06-30 22:16:26 -04:00
firstArticle = new Article(name: 'List Comp6', body: "A programmer's best friend.")
2014-01-03 13:32:13 -05:00
firstArticle.original = firstArticle._id
firstArticle.save (err) ->
throw err if err
secondObject = firstArticle.toObject()
2014-06-30 22:16:26 -04:00
secondObject['body'] = 'Not as good as lambda.'
2014-01-03 13:32:13 -05:00
firstArticle.makeNewMinorVersion secondObject, 0, (err, secondArticle) ->
throw err if err
secondArticle.save (err) ->
throw err if err
thirdObject = secondArticle.toObject()
2014-06-30 22:16:26 -04:00
thirdObject['body'] = '...'
2014-01-03 13:32:13 -05:00
secondArticle.makeNewMajorVersion thirdObject, (err, thirdArticle) ->
throw err if err
thirdArticle.save ->
throw err if err
2014-06-30 22:16:26 -04:00
Article.find {original: firstArticle.original}, (err, results) ->
2014-01-03 13:32:13 -05:00
expect(results[2].index).toBeUndefined()
expect(results[1].index).toBeUndefined()
expect(results[0].index).toBe(true)
done()
raw =
2014-06-30 22:16:26 -04:00
name: 'Battlefield 1942'
description: 'Vacation all over the world!'
permissions: [
target: 'not_the_public'
access: 'owner'
2014-01-03 13:32:13 -05:00
]