codecombat/spec/server/unit/user.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

78 lines
2.9 KiB
CoffeeScript

GLOBAL._ = require 'lodash'
User = require '../../../server/models/User'
utils = require '../utils'
describe 'User', ->
it 'uses the schema defaults to fill in email preferences', (done) ->
user = new User()
expect(user.isEmailSubscriptionEnabled('generalNews')).toBeTruthy()
expect(user.isEmailSubscriptionEnabled('anyNotes')).toBeTruthy()
expect(user.isEmailSubscriptionEnabled('recruitNotes')).toBeTruthy()
expect(user.isEmailSubscriptionEnabled('archmageNews')).toBeFalsy()
done()
it 'uses old subs if they\'re around', (done) ->
user = new User()
user.set 'emailSubscriptions', ['tester']
expect(user.isEmailSubscriptionEnabled('adventurerNews')).toBeTruthy()
expect(user.isEmailSubscriptionEnabled('generalNews')).toBeFalsy()
done()
it 'maintains the old subs list if it\'s around', (done) ->
user = new User()
user.set 'emailSubscriptions', ['tester']
user.setEmailSubscription('artisanNews', true)
expect(JSON.stringify(user.get('emailSubscriptions'))).toBe(JSON.stringify(['tester', 'level_creator']))
done()
describe '.updateServiceSettings()', ->
makeMC = (callback) ->
it 'uses emails to determine what to send to MailChimp', (done) ->
spyOn(mc.lists, 'subscribe').and.callFake (params) ->
expect(JSON.stringify(params.merge_vars.groupings[0].groups)).toBe(JSON.stringify(['Announcements']))
done()
user = new User({emailSubscriptions: ['announcement'], email: 'tester@gmail.com'})
User.updateServiceSettings(user)
describe '.isAdmin()', ->
it 'returns true if user has "admin" permission', (done) ->
adminUser = new User()
adminUser.set('permissions', ['whatever', 'admin', 'user'])
expect(adminUser.isAdmin()).toBeTruthy()
done()
it 'returns false if user has no permissions', (done) ->
myUser = new User()
myUser.set('permissions', [])
expect(myUser.isAdmin()).toBeFalsy()
done()
it 'returns false if user has other permissions', (done) ->
classicUser = new User()
classicUser.set('permissions', ['user'])
expect(classicUser.isAdmin()).toBeFalsy()
done()
describe '.verificationCode(timestamp)', ->
it 'returns a timestamp and a hash', (done) ->
user = new User()
now = new Date()
code = user.verificationCode(now.getTime())
expect(code).toMatch(/[0-9]{13}:[0-9a-f]{64}/)
[timestamp, hash] = code.split(':')
expect(new Date(parseInt(timestamp))).toEqual(now)
done()
describe '.incrementStatAsync()', ->
it 'records nested stats', utils.wrap (done) ->
user = yield utils.initUser()
yield User.incrementStatAsync user.id, 'stats.testNumber'
yield User.incrementStatAsync user.id, 'stats.concepts.basic', {inc: 10}
user = yield User.findById(user.id)
expect(user.get('stats.testNumber')).toBe(1)
expect(user.get('stats.concepts.basic')).toBe(10)
done()