codecombat/spec/server/unit/user_handler.spec.coffee
Scott Erickson 7bab895dee Generalize new I18N view system
Previously, when diplomats submit translations, the system
would try to figure out whether it should be a 'patch' or a 'change',
and then would either create a patch for an admin or artisan to
review and accept or reject, or would apply the changes immediately
and they would be live. This was done as a compromise between
getting translations live quickly, but also preventing already-translated
text from getting overwritten without oversight.

But having the client handle this added logical complexity. So
this makes all diplomats submit patches, no matter what. The server
is then in charge of deciding if it should auto-accept the patch or not.
Either way, a patch is created.

There was also much refactoring. This commit includes:

* Update jsondiffpatch so changes within array items are handled correctly
* Refactor posting patches to use the new auto-accepting logic, and out of Patch model
* Refactor POST /db/patch/:handle/status so that it doesn't rely on handlers
* Refactor patch stat handling to ensure auto-accepted patches are counted
* Refactor User.incrementStat to use mongodb update commands, to avoid race conditions
* Refactor Patch tests
2016-09-09 10:59:26 -07:00

51 lines
1.8 KiB
CoffeeScript

User = require '../../../server/models/User'
request = require '../request'
utils = require '../utils'
Promise = require 'bluebird'
UserHandler = require '../../../server/handlers/user_handler'
describe 'UserHandler', ->
describe '.recalculateStats(statName, done)', ->
it 'recalculates amount of submitted and accepted patches', utils.wrap (done) ->
creator = yield utils.initAdmin()
yield utils.loginUser(creator)
article = yield utils.makeArticle()
user = yield utils.initUser()
yield utils.loginUser(user)
json = {
commitMessage: 'Accept this patch!'
delta: {name: ['test']}
target: {id: article.id, collection: 'article'}
}
url = utils.getURL('/db/patch')
[res, body] = yield request.postAsync { url, json }
patchID = body._id
expect(res.statusCode).toBe(201)
yield utils.loginUser(creator)
url = utils.getURL("/db/patch/#{patchID}/status")
[res, body] = yield request.putAsync {url, json: {status: 'accepted'}}
expect(res.statusCode).toBe(200)
user = yield User.findById(user.id)
expect(user.get 'stats.patchesSubmitted').toBe 1
statsBefore = user.get('stats')
yield user.update({$unset: {stats: ''}})
user = yield User.findById(user.id)
expect(user.get 'stats').toBeUndefined()
recalculateStatsAsync = Promise.promisify(UserHandler.recalculateStats)
yield [
recalculateStatsAsync 'patchesContributed'
recalculateStatsAsync 'patchesSubmitted'
recalculateStatsAsync 'totalMiscPatches'
recalculateStatsAsync 'totalTranslationPatches'
recalculateStatsAsync 'articleMiscPatches'
]
user = yield User.findById(user.id)
statsAfter = user.get('stats')
expect(_.isEqual(statsBefore, statsAfter)).toBe(true)
done()