Rework challenge integration test to be more autonomous

This commit is contained in:
Blade Barringer 2015-08-16 09:44:38 -05:00
parent 71a9f7caf4
commit ab9aab9262

View file

@ -10,7 +10,7 @@ describe "Challenges", ->
updateTodo = undefined
group = undefined
before (done) ->
beforeEach (done) ->
async.waterfall [
(cb) ->
registerNewUser(cb, true)
@ -23,68 +23,123 @@ describe "Challenges", ->
group = res.body
expect(group.members.length).to.equal 1
expect(group.leader).to.equal user._id
cb()
, (cb) ->
request.post(baseURL + "/challenges").send(
group: group._id
dailys: [
type: "daily"
text: "Challenge Daily"
]
todos: [{
type: "todo"
text: "Challenge Todo 1"
notes: "Challenge Notes"
}]
rewards: []
habits: []
).end (res) ->
challenge = res.body
done()
]
it "Creates a challenge", (done) ->
request.post(baseURL + "/challenges").send(
group: group._id
dailys: [
type: "daily"
text: "Challenge Daily"
]
todos: [{
type: "todo"
text: "Challenge Todo 1"
notes: "Challenge Notes"
}, {
type: "todo"
text: "Challenge Todo 2"
notes: "Challenge Notes"
}]
rewards: []
habits: []
official: true
).end (res) ->
expectCode res, 200
async.parallel [
(cb) ->
User.findById user._id, cb
(cb) ->
Challenge.findById res.body._id, cb
], (err, results) ->
_user = results[0]
challenge = results[1]
expect(_user.dailys[_user.dailys.length - 1].text).to.equal "Challenge Daily"
updateTodo = _user.todos[_user.todos.length - 1]
expect(updateTodo.text).to.equal "Challenge Todo 2"
expect(challenge.official).to.equal false
user = _user
done()
it "User updates challenge notes", (done) ->
updateTodo.notes = "User overriden notes"
request.put(baseURL + "/user/tasks/" + updateTodo.id).send(updateTodo).end (res) ->
done() # we'll do the check down below
it "Change challenge daily", (done) ->
challenge.dailys[0].text = "Updated Daily"
challenge.todos[0].notes = "Challenge Updated Todo Notes"
request.post(baseURL + "/challenges/" + challenge._id).send(challenge).end (res) ->
setTimeout (->
User.findById user._id, (err, _user) ->
expectCode res, 200
expect(_user.dailys[_user.dailys.length - 1].text).to.equal "Updated Daily"
expect(res.body.todos[0].notes).to.equal "Challenge Updated Todo Notes"
expect(_user.todos[_user.todos.length - 1].notes).to.equal "User overriden notes"
user = _user
describe 'POST /challenge', ->
it "Creates a challenge", (done) ->
request.post(baseURL + "/challenges").send(
group: group._id
dailys: [
type: "daily"
text: "Challenge Daily"
]
todos: [{
type: "todo"
text: "Challenge Todo 1"
notes: "Challenge Notes"
}, {
type: "todo"
text: "Challenge Todo 2"
notes: "Challenge Notes"
}]
rewards: []
habits: []
official: true
).end (res) ->
expectCode res, 200
async.parallel [
(cb) ->
User.findById user._id, cb
(cb) ->
Challenge.findById res.body._id, cb
], (err, results) ->
user = results[0]
challenge = results[1]
expect(user.dailys[user.dailys.length - 1].text).to.equal "Challenge Daily"
expect(challenge.official).to.equal false
done()
), 500 # we have to wait a while for users' tasks to be updated, called async on server
it "Shows user notes on challenge page", (done) ->
request.get(baseURL + "/challenges/" + challenge._id + "/member/" + user._id).end (res) ->
expect(res.body.todos[res.body.todos.length - 1].notes).to.equal "User overriden notes"
done()
describe 'POST /challenge/:cid', ->
it "updates the notes on user's version of a challenge task's note without updating the challenge", (done) ->
updateTodo = challenge.todos[0]
updateTodo.notes = "User overriden notes"
async.waterfall [
(cb) ->
request.put(baseURL + "/user/tasks/" + updateTodo.id).send(updateTodo).end (res) ->
cb()
, (cb) ->
Challenge.findById challenge._id, cb
, (chal, cb) ->
expect(chal.todos[0].notes).to.eql("Challenge Notes")
cb()
, (cb) ->
request.get(baseURL + "/user/tasks/" + updateTodo.id)
.end (res) ->
expect(res.body.notes).to.eql("User overriden notes")
done()
]
it "changes user's copy of challenge tasks when the challenge is updated", (done) ->
challenge.dailys[0].text = "Updated Daily"
request.post(baseURL + "/challenges/" + challenge._id)
.send(challenge)
.end (res) ->
challenge = res.body
expect(challenge.dailys[0].text).to.equal "Updated Daily"
User.findById user._id, (err, _user) ->
expectCode res, 200
expect(_user.dailys[_user.dailys.length - 1].text).to.equal "Updated Daily"
done()
it "does not changes user's notes on tasks when challenge task notes are updated", (done) ->
challenge.todos[0].notes = "Challenge Updated Todo Notes"
request.post(baseURL + "/challenges/" + challenge._id)
.send(challenge)
.end (res) ->
challenge = res.body
expect(challenge.todos[0].notes).to.equal "Challenge Updated Todo Notes"
User.findById user._id, (err, _user) ->
expectCode res, 200
expect(_user.todos[_user.todos.length - 1].notes).to.equal "Challenge Notes"
done()
it "shows user notes on challenge page", (done) ->
updateTodo = challenge.todos[0]
updateTodo.notes = "User overriden notes"
async.waterfall [
(cb) ->
request.put(baseURL + "/user/tasks/" + updateTodo.id).send(updateTodo).end (res) ->
cb()
, (cb) ->
Challenge.findById challenge._id, cb
, (chal, cb) ->
expect(chal.todos[0].notes).to.eql("Challenge Notes")
cb()
, (cb) ->
request.get(baseURL + "/challenges/" + challenge._id + "/member/" + user._id).end (res) ->
expect(res.body.todos[res.body.todos.length - 1].notes).to.equal "User overriden notes"
done()
]
it "Complete To-Dos", (done) ->
User.findById user._id, (err, _user) ->
@ -116,7 +171,7 @@ describe "Challenges", ->
done()
), 100 # we need to wait for challenge to update user, it's a background job for perf reasons
it "Admin creates a challenge", (done) ->
it "admin creates a challenge", (done) ->
User.findByIdAndUpdate user._id,
$set:
"contributor.admin": true