unread tests

v1.18.x
Baris Usakli 8 years ago
parent d1dd0cd6df
commit 69b5bb39ec

@ -55,66 +55,66 @@ unreadController.get = function (req, res, next) {
cutoff: cutoff,
}, next);
},
], function (err, data) {
if (err) {
return next(err);
}
data.pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
data.pagination = pagination.create(page, data.pageCount, req.query);
if (settings.usePagination && (page < 1 || page > data.pageCount)) {
req.query.page = Math.max(1, Math.min(data.pageCount, page));
return helpers.redirect(res, '/unread?' + querystring.stringify(req.query));
}
data.categories = results.watchedCategories.categories;
data.selectedCategory = results.watchedCategories.selectedCategory;
if (req.path.startsWith('/api/unread') || req.path.startsWith('/unread')) {
data.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[unread:title]]' }]);
}
data.title = '[[pages:unread]]';
data.filters = [{
name: '[[unread:all-topics]]',
url: 'unread',
selected: filter === '',
filter: '',
}, {
name: '[[unread:new-topics]]',
url: 'unread/new',
selected: filter === 'new',
filter: 'new',
}, {
name: '[[unread:watched-topics]]',
url: 'unread/watched',
selected: filter === 'watched',
filter: 'watched',
}];
data.selectedFilter = data.filters.find(function (filter) {
return filter && filter.selected;
});
data.querystring = cid ? ('?cid=' + validator.escape(String(cid))) : '';
res.render('unread', data);
});
function (data) {
data.pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
data.pagination = pagination.create(page, data.pageCount, req.query);
if (settings.usePagination && (page < 1 || page > data.pageCount)) {
req.query.page = Math.max(1, Math.min(data.pageCount, page));
return helpers.redirect(res, '/unread?' + querystring.stringify(req.query));
}
data.categories = results.watchedCategories.categories;
data.selectedCategory = results.watchedCategories.selectedCategory;
if (req.path.startsWith('/api/unread') || req.path.startsWith('/unread')) {
data.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[unread:title]]' }]);
}
data.title = '[[pages:unread]]';
data.filters = [{
name: '[[unread:all-topics]]',
url: 'unread',
selected: filter === '',
filter: '',
}, {
name: '[[unread:new-topics]]',
url: 'unread/new',
selected: filter === 'new',
filter: 'new',
}, {
name: '[[unread:watched-topics]]',
url: 'unread/watched',
selected: filter === 'watched',
filter: 'watched',
}];
data.selectedFilter = data.filters.find(function (filter) {
return filter && filter.selected;
});
data.querystring = cid ? ('?cid=' + validator.escape(String(cid))) : '';
res.render('unread', data);
},
], next);
};
unreadController.unreadTotal = function (req, res, next) {
var filter = req.params.filter || '';
if (!validFilter[filter]) {
return next();
}
topics.getTotalUnread(req.uid, filter, function (err, data) {
if (err) {
return next(err);
}
res.json(data);
});
async.waterfall([
function (next) {
plugins.fireHook('filter:unread.getValidFilters', { filters: validFilter }, next);
},
function (data, _next) {
if (!validFilter[filter]) {
return next();
}
topics.getTotalUnread(req.uid, filter, _next);
},
function (data) {
res.json(data);
},
], next);
};

@ -1564,6 +1564,51 @@ describe('Controllers', function () {
});
});
describe('unread', function () {
var jar;
before(function (done) {
helpers.loginUser('foo', 'barbar', function (err, _jar) {
assert.ifError(err);
jar = _jar;
done();
});
});
it('should 404 if filter is invalid', function (done) {
request(nconf.get('url') + '/api/unread/doesnotexist', { jar: jar }, function (err, res) {
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
});
});
it('should 404 if filter is invalid', function (done) {
request(nconf.get('url') + '/api/unread/doesnotexist/total', { jar: jar }, function (err, res) {
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
});
});
it('should return total unread count', function (done) {
request(nconf.get('url') + '/api/unread/new/total', { jar: jar }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.equal(body, 0);
done();
});
});
it('should redirect if page is out of bounds', function (done) {
request(nconf.get('url') + '/api/unread?page=-1', { jar: jar }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 308);
assert.equal(body, '"/unread?page=1"');
done();
});
});
});
after(function (done) {
var analytics = require('../src/analytics');
analytics.writeData(function (err) {

Loading…
Cancel
Save