Tighten update method

This commit is contained in:
Chris Blanchard 2015-09-17 20:59:05 +01:00
parent 5f3885b765
commit 57d849f3f0
3 changed files with 91 additions and 5 deletions

View file

@ -7,12 +7,15 @@ var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var config = require(path.join(__dirname, "config/config.js"));
var env = process.env.NODE_ENV || "development";
// Load Models
require(path.join(__dirname, "db/index"));
// Initialise Steam Bot
require(path.join(__dirname, "lib/steam/bot"))(config.steamBot);
if (env !== "test") {
require(path.join(__dirname, "lib/steam/bot"))(config.steamBot);
}
// Configure express
require(path.join(__dirname, "config/express"))(app);

View file

@ -53,13 +53,23 @@ User.prototype.getHiveId = () => {
return index === 1 ? (tailId * 2) + 1 : tailId * 2;
};
var allowedAttributes = ["enslo", "division", "skill"];
var allowedAbilities = ["skulk", "lerk", "fade", "gorge", "onos", "commander"];
User.prototype.updateProfile = (data, callback) => {
let self = this;
Profile.update({
userId: self.id
}, data, error => {
Profile.findOne({userId: self.id}, (error, profile) => {
if (error) return callback(error);
Profile.findOne({userId: self.id}, (error, profile) => {
allowedAttributes.forEach(function (attr) {
if (data[attr] !== undefined) profile[attr] = data[attr];
});
if (data.abilities) {
allowedAbilities.forEach(function (attr) {
let newAbility = data.abilities[attr];
let abilities = profile.abilities;
if (newAbility !== undefined) abilities[attr] = newAbility;
});
}
profile.save(function (error, profile) {
if (error) return callback(error);
self.profile = profile.toJson();
return callback(error, profile);

View file

@ -3,6 +3,7 @@
var helper = require("./helpers/index.js");
var User = helper.User;
var assert = require("chai").assert;
var Profile = helper.Profile;
var async = require("async");
var userCount = 0;
@ -46,4 +47,76 @@ describe("User", () => {
assert.isNull(user.getSteamId());
});
});
describe("#updateProfile", () => {
var profile, user;
beforeEach(done => {
user = helper.createUser();
Profile.create({
userId: user.id
}, (error, result) => {
if (error) return done(error);
profile = result;
done();
});
});
after(done => helper.clearDb(done));
it ("updates profile", done => {
var attrs = {
enslo: 88,
division: "Foo",
skill: "Bar"
};
user.updateProfile(attrs, (error, profile) => {
if (error) return done(error);
assert.equal(profile.enslo, attrs.enslo);
assert.equal(profile.division, attrs.division);
assert.equal(profile.foo, attrs.foo);
done();
});
});
it ("updates abilities", done => {
var attrs = {
abilities: {
"skulk": true,
"lerk": true,
"fade": true,
"gorge": true,
"onos": true,
"commander": true
}
};
user.updateProfile(attrs, (error, profile) => {
if (error) return done(error);
for (let attr in attrs.abilities) {
assert.isTrue(profile.abilities[attr])
}
done();
});
});
it ("does not update userId", done => {
user.updateProfile({
userId: 80808080
}, (error, result) => {
if (error) return done(error);
assert.equal(result.userId, user.id);
done();
});
});
it ("does not update _id", done => {
user.updateProfile({
_id: "FOOOO"
}, (error, result) => {
if (error) return done(error);
assert.equal(result._id.toString(), profile._id.toString());
done();
});
});
});
});