omg tests (#5232), and .create returns flag data now

v1.18.x
Julian Lam 8 years ago
parent 98a104564b
commit 9f7c4befea

@ -26,6 +26,10 @@ modsController.flags.list = function (req, res, next) {
}
flags.list({}, function (err, flags) {
if (err) {
return next(err);
}
res.render('flags/list', {
flags: flags
});

@ -64,7 +64,7 @@ Flags.list = function (filters, callback) {
'icon:text': userObj['icon:text']
}
}));
})
});
}
], function (err, flagObj) {
if (err) {
@ -110,6 +110,10 @@ Flags.create = function (type, id, uid, reason, callback) {
async.apply(Flags.exists, type, id, uid),
async.apply(Flags.targetExists, type, id)
], function (err, checks) {
if (err) {
return next(err);
}
if (checks[0]) {
return next(new Error('[[error:already-flagged]]'));
} else if (!checks[1]) {
@ -132,15 +136,16 @@ Flags.create = function (type, id, uid, reason, callback) {
})),
async.apply(db.sortedSetAdd.bind(db), 'flags:datetime', Date.now(), flagId),
async.apply(db.setObjectField.bind(db), 'flagHash:flagId', [type, id, uid].join(':'), flagId)
], next);
}
], function (err) {
], function (err, data) {
if (err) {
return callback(err);
return next(err);
}
callback();
next(null, flagId);
});
},
async.apply(Flags.get)
], callback);
// if (!parseInt(uid, 10) || !reason) {
// return callback();
// }

@ -0,0 +1,107 @@
'use strict';
/*globals require, before, after, describe, it*/
var assert = require('assert');
var async = require('async');
var db = require('./mocks/databasemock');
var Flags = require('../src/flags');
var Categories = require('../src/categories');
var Topics = require('../src/topics');
var User = require('../src/user');
describe('Flags', function () {
before(function (done) {
// Create some stuff to flag
async.waterfall([
async.apply(User.create, {username: 'testUser', password: 'abcdef', email: 'b@c.com'}),
function (uid, next) {
Categories.create({
name: 'test category'
}, function (err, category) {
if (err) {
return done(err);
}
Topics.post({
cid: category.cid,
uid: uid,
title: 'Topic to flag',
content: 'This is flaggable content'
}, next);
});
}
], done);
});
describe('.create()', function () {
it('should create a flag and return its data', function (done) {
Flags.create('post', 1, 1, 'Test flag', function (err, flagData) {
assert.ifError(err);
var compare = {
flagId: 1,
uid: 1,
targetId: 1,
type: 'post',
description: 'Test flag',
state: 'open'
};
for(var key in compare) {
if (compare.hasOwnProperty(key)) {
assert.ok(flagData[key]);
assert.strictEqual(flagData[key], compare[key]);
}
}
done();
});
});
});
describe('.get()', function () {
it('should retrieve and display a flag\'s data', function (done) {
Flags.get(1, function (err, flagData) {
assert.ifError(err);
var compare = {
flagId: 1,
uid: 1,
targetId: 1,
type: 'post',
description: 'Test flag',
state: 'open'
};
for(var key in compare) {
if (compare.hasOwnProperty(key)) {
assert.ok(flagData[key]);
assert.strictEqual(flagData[key], compare[key]);
}
}
done();
});
});
});
describe('.list()', function () {
it('should show a list of flags (with one item)', function (done) {
Flags.list({}, function (err, flags) {
assert.ifError(err);
assert.ok(Array.isArray(flags));
assert.equal(flags.length, 1);
Flags.get(flags[0].flagId, function (err, flagData) {
assert.ifError(err);
assert.equal(flags[0].flagId, flagData.flagId);
assert.equal(flags[0].description, flagData.description);
done();
});
});
});
});
after(function (done) {
db.emptydb(done);
});
});
Loading…
Cancel
Save