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

Loading…
Cancel
Save