Files
NodeBB/test/feeds.js

200 lines
6.0 KiB
JavaScript
Raw Normal View History

2017-05-21 19:45:25 -04:00
'use strict';
2021-02-04 00:06:15 -07:00
const assert = require('assert');
const async = require('async');
const request = require('request');
const nconf = require('nconf');
2017-05-21 19:45:25 -04:00
2021-02-04 00:06:15 -07:00
const db = require('./mocks/databasemock');
const topics = require('../src/topics');
const categories = require('../src/categories');
const groups = require('../src/groups');
const user = require('../src/user');
const meta = require('../src/meta');
const privileges = require('../src/privileges');
const helpers = require('./helpers');
2017-05-21 19:45:25 -04:00
2021-02-04 00:01:39 -07:00
describe('feeds', () => {
2021-02-04 00:06:15 -07:00
let tid;
let pid;
let fooUid;
let cid;
2021-02-04 00:01:39 -07:00
before((done) => {
2017-05-21 19:45:25 -04:00
meta.config['feeds:disableRSS'] = 1;
async.series({
category: function (next) {
categories.create({
name: 'Test Category',
description: 'Test category created by testing script',
}, next);
},
user: function (next) {
user.create({ username: 'foo', password: 'barbar', email: 'foo@test.com' }, next);
},
2021-02-04 00:01:39 -07:00
}, (err, results) => {
2017-05-21 19:45:25 -04:00
if (err) {
return done(err);
}
cid = results.category.cid;
fooUid = results.user;
2021-02-04 00:01:39 -07:00
topics.post({ uid: results.user, title: 'test topic title', content: 'test topic content', cid: results.category.cid }, (err, result) => {
2017-05-21 19:45:25 -04:00
tid = result.topicData.tid;
pid = result.postData.pid;
done(err);
});
});
});
2021-02-04 00:01:39 -07:00
it('should 404', (done) => {
2021-02-04 00:06:15 -07:00
const feedUrls = [
2021-02-03 23:59:08 -07:00
`${nconf.get('url')}/topic/${tid}.rss`,
`${nconf.get('url')}/category/${cid}.rss`,
`${nconf.get('url')}/topics.rss`,
`${nconf.get('url')}/recent.rss`,
`${nconf.get('url')}/top.rss`,
`${nconf.get('url')}/popular.rss`,
`${nconf.get('url')}/popular/day.rss`,
`${nconf.get('url')}/recentposts.rss`,
`${nconf.get('url')}/category/${cid}/recentposts.rss`,
`${nconf.get('url')}/user/foo/topics.rss`,
`${nconf.get('url')}/tags/nodebb.rss`,
2017-05-21 19:45:25 -04:00
];
2021-02-04 00:01:39 -07:00
async.eachSeries(feedUrls, (url, next) => {
request(url, (err, res) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
assert.equal(res.statusCode, 404);
next();
});
2021-02-04 00:01:39 -07:00
}, (err) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
meta.config['feeds:disableRSS'] = 0;
done();
});
});
2021-02-04 00:01:39 -07:00
it('should 404 if topic does not exist', (done) => {
request(`${nconf.get('url')}/topic/${1000}.rss`, (err, res) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
});
});
2021-02-04 00:01:39 -07:00
it('should 404 if category id is not a number', (done) => {
request(`${nconf.get('url')}/category/invalid.rss`, (err, res) => {
2018-07-31 14:26:12 -04:00
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
});
});
2021-02-04 00:01:39 -07:00
it('should redirect if we do not have read privilege', (done) => {
privileges.categories.rescind(['groups:topics:read'], cid, 'guests', (err) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
2021-02-04 00:01:39 -07:00
request(`${nconf.get('url')}/topic/${tid}.rss`, (err, res, body) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
assert(body.includes('Login to your account'));
privileges.categories.give(['groups:topics:read'], cid, 'guests', done);
2017-05-21 19:45:25 -04:00
});
});
});
2021-02-04 00:01:39 -07:00
it('should 404 if user is not found', (done) => {
request(`${nconf.get('url')}/user/doesnotexist/topics.rss`, (err, res) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
});
});
2021-02-04 00:01:39 -07:00
it('should redirect if we do not have read privilege', (done) => {
privileges.categories.rescind(['groups:read'], cid, 'guests', (err) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
2021-02-04 00:01:39 -07:00
request(`${nconf.get('url')}/category/${cid}.rss`, (err, res, body) => {
2017-05-21 19:45:25 -04:00
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
assert(body.includes('Login to your account'));
privileges.categories.give(['groups:read'], cid, 'guests', done);
2017-05-21 19:45:25 -04:00
});
});
});
2021-02-04 00:01:39 -07:00
describe('private feeds and tokens', () => {
2021-02-04 00:06:15 -07:00
let jar;
let rssToken;
before(async () => {
({ jar } = await helpers.loginUser('foo', 'barbar'));
});
2021-02-04 00:01:39 -07:00
it('should load feed if its not private', (done) => {
request(`${nconf.get('url')}/category/${cid}.rss`, { }, (err, res, body) => {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
done();
});
});
2021-02-04 00:01:39 -07:00
it('should not allow access if uid or token is missing', (done) => {
privileges.categories.rescind(['groups:read'], cid, 'guests', (err) => {
assert.ifError(err);
async.parallel({
test1: function (next) {
2021-02-03 23:59:08 -07:00
request(`${nconf.get('url')}/category/${cid}.rss?uid=${fooUid}`, { }, next);
},
test2: function (next) {
2021-02-03 23:59:08 -07:00
request(`${nconf.get('url')}/category/${cid}.rss?token=sometoken`, { }, next);
},
2021-02-04 00:01:39 -07:00
}, (err, results) => {
assert.ifError(err);
assert.equal(results.test1[0].statusCode, 200);
assert.equal(results.test2[0].statusCode, 200);
assert(results.test1[0].body.includes('Login to your account'));
assert(results.test2[0].body.includes('Login to your account'));
done();
});
});
});
2021-02-04 00:01:39 -07:00
it('should not allow access if token is wrong', (done) => {
request(`${nconf.get('url')}/category/${cid}.rss?uid=${fooUid}&token=sometoken`, { }, (err, res, body) => {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.includes('Login to your account'));
done();
});
});
2021-02-04 00:01:39 -07:00
it('should allow access if token is correct', (done) => {
request(`${nconf.get('url')}/api/category/${cid}`, { jar: jar, json: true }, (err, res, body) => {
assert.ifError(err);
rssToken = body.rssFeedUrl.split('token')[1].slice(1);
2021-02-04 00:01:39 -07:00
request(`${nconf.get('url')}/category/${cid}.rss?uid=${fooUid}&token=${rssToken}`, { }, (err, res, body) => {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.startsWith('<?xml version="1.0"'));
done();
});
});
});
2021-02-04 00:01:39 -07:00
it('should not allow access if token is correct but has no privilege', (done) => {
privileges.categories.rescind(['groups:read'], cid, 'registered-users', (err) => {
2017-06-22 13:04:24 -04:00
assert.ifError(err);
2021-02-04 00:01:39 -07:00
request(`${nconf.get('url')}/category/${cid}.rss?uid=${fooUid}&token=${rssToken}`, { }, (err, res, body) => {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.includes('Login to your account'));
done();
});
});
});
});
2017-05-21 19:45:25 -04:00
});