2014-04-08 22:26:19 -04:00
|
|
|
require '../common'
|
|
|
|
|
|
|
|
describe '/db/patch', ->
|
|
|
|
request = require 'request'
|
2014-07-23 14:00:28 -04:00
|
|
|
async = require 'async'
|
|
|
|
UserHandler = require '../../../server/users/user_handler'
|
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'clears the db first', (done) ->
|
|
|
|
clearModels [User, Article, Patch], (err) ->
|
|
|
|
throw err if err
|
|
|
|
done()
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
article = {name: 'Yo', body: 'yo ma'}
|
2014-04-08 22:26:19 -04:00
|
|
|
articleURL = getURL('/db/article')
|
|
|
|
articles = {}
|
|
|
|
|
|
|
|
patchURL = getURL('/db/patch')
|
|
|
|
patches = {}
|
|
|
|
patch =
|
2014-04-10 16:09:44 -04:00
|
|
|
commitMessage: 'Accept this patch!'
|
2014-06-30 22:16:26 -04:00
|
|
|
delta: {name: ['test']}
|
2014-04-17 20:09:01 -04:00
|
|
|
editPath: '/who/knows/yes'
|
2014-04-08 22:26:19 -04:00
|
|
|
target:
|
2014-06-30 22:16:26 -04:00
|
|
|
id: null
|
2014-04-08 22:26:19 -04:00
|
|
|
collection: 'article'
|
|
|
|
|
|
|
|
it 'creates an Article to patch', (done) ->
|
|
|
|
loginAdmin ->
|
2014-06-30 22:16:26 -04:00
|
|
|
request.post {uri: articleURL, json: article}, (err, res, body) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
articles[0] = body
|
|
|
|
patch.target.id = articles[0]._id
|
|
|
|
done()
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
it 'allows someone to submit a patch to something they don\'t control', (done) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
loginJoe (joe) ->
|
|
|
|
request.post {uri: patchURL, json: patch}, (err, res, body) ->
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
expect(body.target.original).toBeDefined()
|
|
|
|
expect(body.target.version.major).toBeDefined()
|
|
|
|
expect(body.target.version.minor).toBeDefined()
|
|
|
|
expect(body.status).toBe('pending')
|
|
|
|
expect(body.created).toBeDefined()
|
|
|
|
expect(body.creator).toBe(joe.id)
|
|
|
|
patches[0] = body
|
|
|
|
done()
|
2014-06-28 11:44:07 -04:00
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'adds a patch to the target document', (done) ->
|
|
|
|
Article.findOne({}).exec (err, article) ->
|
|
|
|
expect(article.toObject().patches[0]).toBeDefined()
|
|
|
|
done()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'shows up in patch requests', (done) ->
|
|
|
|
patchesURL = getURL("/db/article/#{articles[0]._id}/patches")
|
|
|
|
request.get {uri: patchesURL}, (err, res, body) ->
|
|
|
|
body = JSON.parse(body)
|
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
expect(body.length).toBe(1)
|
2014-06-30 22:16:26 -04:00
|
|
|
done()
|
|
|
|
|
2014-04-15 18:09:36 -04:00
|
|
|
it 'allows you to set yourself as watching', (done) ->
|
|
|
|
watchingURL = getURL("/db/article/#{articles[0]._id}/watch")
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: watchingURL, json: {on: true}}, (err, res, body) ->
|
2014-04-17 20:09:01 -04:00
|
|
|
expect(body.watchers[1]).toBeDefined()
|
2014-04-08 22:26:19 -04:00
|
|
|
done()
|
|
|
|
|
2014-04-15 18:09:36 -04:00
|
|
|
it 'added the watcher to the target document', (done) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
Article.findOne({}).exec (err, article) ->
|
2014-04-17 20:09:01 -04:00
|
|
|
expect(article.toObject().watchers[1]).toBeDefined()
|
2014-04-08 22:26:19 -04:00
|
|
|
done()
|
|
|
|
|
2014-04-15 18:09:36 -04:00
|
|
|
it 'does not add duplicate watchers', (done) ->
|
|
|
|
watchingURL = getURL("/db/article/#{articles[0]._id}/watch")
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: watchingURL, json: {on: true}}, (err, res, body) ->
|
2014-04-17 20:09:01 -04:00
|
|
|
expect(body.watchers.length).toBe(2)
|
2014-04-08 22:26:19 -04:00
|
|
|
done()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'allows removing yourself', (done) ->
|
2014-04-15 18:09:36 -04:00
|
|
|
watchingURL = getURL("/db/article/#{articles[0]._id}/watch")
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: watchingURL, json: {on: false}}, (err, res, body) ->
|
2014-04-17 20:09:01 -04:00
|
|
|
expect(body.watchers.length).toBe(1)
|
2014-04-08 22:26:19 -04:00
|
|
|
done()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'allows the submitter to withdraw the pull request', (done) ->
|
|
|
|
statusURL = getURL("/db/patch/#{patches[0]._id}/status")
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: statusURL, json: {status: 'withdrawn'}}, (err, res, body) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
Patch.findOne({}).exec (err, article) ->
|
|
|
|
expect(article.get('status')).toBe 'withdrawn'
|
|
|
|
Article.findOne({}).exec (err, article) ->
|
|
|
|
expect(article.toObject().patches.length).toBe(0)
|
|
|
|
done()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'does not allow the submitter to reject or accept the pull request', (done) ->
|
|
|
|
statusURL = getURL("/db/patch/#{patches[0]._id}/status")
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: statusURL, json: {status: 'rejected'}}, (err, res, body) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
expect(res.statusCode).toBe(403)
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: statusURL, json: {status: 'accepted'}}, (err, res, body) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
Patch.findOne({}).exec (err, article) ->
|
|
|
|
expect(article.get('status')).toBe 'withdrawn'
|
|
|
|
done()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'allows the recipient to accept or reject the pull request', (done) ->
|
|
|
|
statusURL = getURL("/db/patch/#{patches[0]._id}/status")
|
|
|
|
loginAdmin ->
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: statusURL, json: {status: 'rejected'}}, (err, res, body) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
Patch.findOne({}).exec (err, article) ->
|
|
|
|
expect(article.get('status')).toBe 'rejected'
|
2014-06-30 22:16:26 -04:00
|
|
|
request.put {uri: statusURL, json: {status: 'accepted'}}, (err, res, body) ->
|
2014-04-08 22:26:19 -04:00
|
|
|
expect(res.statusCode).toBe(200)
|
|
|
|
Patch.findOne({}).exec (err, article) ->
|
|
|
|
expect(article.get('status')).toBe 'accepted'
|
2014-08-15 13:35:35 -04:00
|
|
|
expect(article.get('acceptor')).toBeDefined()
|
2014-04-08 22:26:19 -04:00
|
|
|
done()
|
|
|
|
|
2014-06-28 11:44:07 -04:00
|
|
|
it 'keeps track of amount of submitted and accepted patches', (done) ->
|
|
|
|
loginJoe (joe) ->
|
2014-07-23 14:00:28 -04:00
|
|
|
User.findById joe.get('_id'), (err, guy) ->
|
2014-06-28 11:44:07 -04:00
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(guy.get 'stats.patchesSubmitted').toBe 1
|
|
|
|
expect(guy.get 'stats.patchesContributed').toBe 1
|
|
|
|
expect(guy.get 'stats.totalMiscPatches').toBe 1
|
|
|
|
expect(guy.get 'stats.articleMiscPatches').toBe 1
|
|
|
|
expect(guy.get 'stats.totalTranslationPatches').toBeUndefined()
|
|
|
|
done()
|
|
|
|
|
2014-07-23 14:00:28 -04:00
|
|
|
it 'recalculates amount of submitted and accepted patches', (done) ->
|
|
|
|
loginJoe (joe) ->
|
|
|
|
User.findById joe.get('_id'), (err, joe) ->
|
|
|
|
expect(joe.get 'stats.patchesSubmitted').toBe 1
|
|
|
|
joe.update {$unset: stats: ''}, (err) ->
|
|
|
|
UserHandler.modelClass.findById joe.get('_id'), (err, joe) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
expect(joe.get 'stats').toBeUndefined()
|
|
|
|
async.parallel [
|
|
|
|
(done) -> UserHandler.recalculateStats 'patchesContributed', done
|
|
|
|
(done) -> UserHandler.recalculateStats 'patchesSubmitted', done
|
2014-07-24 08:41:06 -04:00
|
|
|
(done) -> UserHandler.recalculateStats 'totalMiscPatches', done
|
|
|
|
(done) -> UserHandler.recalculateStats 'totalTranslationPatches', done
|
|
|
|
(done) -> UserHandler.recalculateStats 'articleMiscPatches', done
|
2014-07-23 14:00:28 -04:00
|
|
|
], (err) ->
|
|
|
|
expect(err).toBeNull()
|
|
|
|
UserHandler.modelClass.findById joe.get('_id'), (err, joe) ->
|
|
|
|
expect(joe.get 'stats.patchesSubmitted').toBe 1
|
2014-07-24 08:41:06 -04:00
|
|
|
expect(joe.get 'stats.patchesContributed').toBe 1
|
2014-07-24 13:42:43 -04:00
|
|
|
expect(joe.get 'stats.totalMiscPatches').toBe 1
|
|
|
|
expect(joe.get 'stats.articleMiscPatches').toBe 1
|
|
|
|
expect(joe.get 'stats.totalTranslationPatches').toBeUndefined()
|
2014-07-23 14:00:28 -04:00
|
|
|
done()
|
|
|
|
|
2014-04-08 22:26:19 -04:00
|
|
|
it 'does not allow the recipient to withdraw the pull request', (done) ->
|
2014-06-28 11:44:07 -04:00
|
|
|
loginAdmin ->
|
|
|
|
statusURL = getURL("/db/patch/#{patches[0]._id}/status")
|
|
|
|
request.put {uri: statusURL, json: {status:'withdrawn'}}, (err, res, body) ->
|
|
|
|
expect(res.statusCode).toBe(403)
|
|
|
|
Patch.findOne({}).exec (err, article) ->
|
|
|
|
expect(article.get('status')).toBe 'accepted'
|
|
|
|
done()
|
2014-07-23 14:00:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-24 08:41:06 -04:00
|
|
|
|