fix(spells): add some spells tests, don't send up body to spell paths

This commit is contained in:
Tyler Renelle 2014-01-22 00:16:52 -08:00
parent c6f7ab8a5c
commit e0646bb98d
4 changed files with 43 additions and 16 deletions

View file

@ -81,6 +81,7 @@
"expect.js": "~0.2.0",
"superagent": "~0.15.7",
"superagent-defaults": "~0.1.5",
"git-changelog": "colegleason/git-changelog"
"git-changelog": "colegleason/git-changelog",
"deep-diff": "~0.1.4"
}
}

View file

@ -167,9 +167,11 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
$event && ($event.stopPropagation(),$event.preventDefault());
if ($scope.spell.target != type) return Notification.text("Invalid target");
$scope.spell.cast(User.user, target);
var spell = $scope.spell;
User.save();
$http.post('/api/v2/user/class/cast/' + spell.key, {target:target, type:type}).success(function(){
var spell = $scope.spell;
var targetId = type == 'party' ? '' : type == 'task' ? target.id : target._id;
$http.post('/api/v2/user/class/cast/' + spell.key, {params:{targetType:type, targetId:targetId}})
.success(function(){
var msg = "You cast " + spell.text;
switch (type) {
case 'task': msg += ' on ' + target.text;break;

View file

@ -322,14 +322,8 @@ api.buyGemsPaypalIPN = function(req, res, next) {
*/
api.cast = function(req, res) {
var user = res.locals.user;
var type, target;
if (req.body.type) { // this is the method implemented in the app, it's not correct
type = req.body.type;
target = req.body.target;
} else if (req.query.targetType) { // this is the supported API method
type = req.query.targetType;
target = req.query.targetId;
}
var targetType = req.query.targetType;
var targetId = req.query.targetId;
var klass = shared.content.spells.special[req.params.spell] ? 'special' : user.stats.class
var spell = shared.content.spells[klass][req.params.spell];
@ -340,9 +334,9 @@ api.cast = function(req, res) {
res.json(saved);
}
switch (type) {
switch (targetType) {
case 'task':
spell.cast(user, user.tasks[target.id]);
spell.cast(user, user.tasks[targetId]);
user.save(done);
break;
@ -361,20 +355,20 @@ api.cast = function(req, res) {
// Solo player? let's just create a faux group for simpler code
var g = group ? group : {members:[user]};
var series = [], found;
if (type == 'party') {
if (targetType == 'party') {
spell.cast(user, g.members);
series = _.transform(g.members, function(m,v,k){
m.push(function(cb2){v.save(cb2)});
});
} else {
found = _.find(g.members, {_id: target._id})
found = _.find(g.members, {_id: targetId})
spell.cast(user, found);
series.push(function(cb2){found.save(cb2)});
}
if (group) {
series.push(function(cb2){
var message = '`'+user.profile.name+' casts '+spell.text + (type=='user' ? ' on '+found.profile.name : ' for the party')+'.`';
var message = '`'+user.profile.name+' casts '+spell.text + (targetType=='user' ? ' on '+found.profile.name : ' for the party')+'.`';
group.sendChat(message);
group.save(cb2);
})

View file

@ -5,6 +5,7 @@
var _ = require('lodash');
var expect = require('expect.js');
var async = require('async');
var diff = require('deep-diff');
var superagentDefaults = require('superagent-defaults');
var request = superagentDefaults();
@ -381,6 +382,35 @@ describe('API', function () {
})
});
it('Casts a spell', function(done){
var mp = user.stats.mp;
request.get(baseURL+'/members/'+party[0]._id).end(function(res){
party[0] = res.body;
request.post(baseURL+'/user/class/cast/snowball?targetType=user&targetId='+party[0]._id)
.end(function(res){
//expect(res.body.stats.mp).to.be.below(mp);
request.get(baseURL+'/members/'+party[0]._id).end(function(res){
var member = res.body;
expect(member.achievements.snowball).to.be(1);
expect(member.stats.buffs.snowball).to.be(true);
var difference = diff(member,party[0]);
expect(_.size(difference)).to.be(2);
// level up user so str is > 0
request.put(baseURL+'/user').send({'stats.lvl':5}).end(function(res){
request.post(baseURL+'/user/class/cast/valorousPresence?targetType=party').end(function(res){
request.get(baseURL+'/members/'+member._id).end(function(res){
expect(res.body.stats.buffs.str).to.be.above(0);
expect(diff(res.body,member).length).to.be(1);
done();
})
})
})
})
})
})
});
it("Doesn't include people who aren't participating",function(done){
request.get(baseURL+'/groups/'+group._id).end(function(res){
expect(_.size(res.body.quest.members)).to.be(3);