mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-29 10:35:51 -05:00
9c39bf7656
Will remove deprecated properties after updating production.
106 lines
3.6 KiB
CoffeeScript
106 lines
3.6 KiB
CoffeeScript
require '../common'
|
|
|
|
describe '/db/prepaid', ->
|
|
prepaidURL = getURL('/db/prepaid')
|
|
prepaidCreateURL = getURL('/db/prepaid/-/create')
|
|
|
|
verifyPrepaid = (user, prepaid, done) ->
|
|
expect(prepaid.creator).toEqual(user.id)
|
|
expect(prepaid.type).toEqual('subscription')
|
|
expect(prepaid.maxRedeemers).toBeGreaterThan(0)
|
|
expect(prepaid.code).toMatch(/^\w{8}$/)
|
|
expect(prepaid.properties?.couponID).toEqual('free')
|
|
done()
|
|
|
|
it 'Clear database users and prepaids', (done) ->
|
|
clearModels [User, Prepaid], (err) ->
|
|
throw err if err
|
|
done()
|
|
|
|
it 'Anonymous creates prepaid code', (done) ->
|
|
createPrepaid 'subscription', 1, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(401)
|
|
done()
|
|
|
|
it 'Non-admin creates prepaid code', (done) ->
|
|
loginNewUser (user1) ->
|
|
expect(user1.isAdmin()).toEqual(false)
|
|
createPrepaid 'subscription', 4, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(403)
|
|
done()
|
|
|
|
it 'Admin creates prepaid code with type subscription', (done) ->
|
|
loginNewUser (user1) ->
|
|
user1.set('permissions', ['admin'])
|
|
user1.save (err, user1) ->
|
|
expect(err).toBeNull()
|
|
expect(user1.isAdmin()).toEqual(true)
|
|
createPrepaid 'subscription', 1, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(200)
|
|
verifyPrepaid user1, body, done
|
|
|
|
it 'Admin creates prepaid code with invalid type', (done) ->
|
|
loginNewUser (user1) ->
|
|
user1.set('permissions', ['admin'])
|
|
user1.save (err, user1) ->
|
|
expect(err).toBeNull()
|
|
expect(user1.isAdmin()).toEqual(true)
|
|
createPrepaid 'bulldozer', 1, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(403)
|
|
done()
|
|
|
|
it 'Admin creates prepaid code with no type specified', (done) ->
|
|
loginNewUser (user1) ->
|
|
user1.set('permissions', ['admin'])
|
|
user1.save (err, user1) ->
|
|
expect(err).toBeNull()
|
|
expect(user1.isAdmin()).toEqual(true)
|
|
createPrepaid null, 1, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(403)
|
|
done()
|
|
|
|
it 'Admin creates prepaid code with invalid maxRedeemers', (done) ->
|
|
loginNewUser (user1) ->
|
|
user1.set('permissions', ['admin'])
|
|
user1.save (err, user1) ->
|
|
expect(err).toBeNull()
|
|
expect(user1.isAdmin()).toEqual(true)
|
|
createPrepaid 'subscription', 0, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(403)
|
|
done()
|
|
|
|
it 'Non-admin requests /db/prepaid', (done) ->
|
|
loginNewUser (user1) ->
|
|
expect(user1.isAdmin()).toEqual(false)
|
|
request.get {uri: prepaidURL}, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(403)
|
|
done()
|
|
|
|
it 'Admin requests /db/prepaid', (done) ->
|
|
loginNewUser (user1) ->
|
|
user1.set('permissions', ['admin'])
|
|
user1.save (err, user1) ->
|
|
expect(err).toBeNull()
|
|
expect(user1.isAdmin()).toEqual(true)
|
|
createPrepaid 'subscription', 1, (err, res, prepaid) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(200)
|
|
request.get {uri: prepaidURL}, (err, res, body) ->
|
|
expect(err).toBeNull()
|
|
expect(res.statusCode).toBe(200)
|
|
prepaids = JSON.parse(body)
|
|
found = false
|
|
for p in prepaids
|
|
if p._id is prepaid._id
|
|
found = true
|
|
verifyPrepaid user1, p, done
|
|
break
|
|
expect(found).toEqual(true)
|
|
done() unless found
|