Commit 9fb038e1 authored by Marco Flowers's avatar Marco Flowers

Added test database/tests for yp touch, Added bans to config

Dumped a test database for testing the api. Added new tests for
yp touch and pagination. Added the rest of the bans to the config.
Removed the logging module.
parent df3f088d
This diff is collapsed.
This diff is collapsed.
define({ "name": "Icecast Stream Directory API", "version": "0.0.0", "description": "Stream directory api", "title": "Icecast Stream Directory API", "url": "", "order": [ "Streams", "Genres", "Formats" ], "sampleUrl": false, "apidoc": "0.2.0", "generator": { "name": "apidoc", "time": "2015-08-11T10:02:36.841Z", "url": "http://apidocjs.com", "version": "0.13.1" }});
\ No newline at end of file
define({ "name": "Icecast Stream Directory API", "version": "0.0.0", "description": "Stream directory api", "title": "Icecast Stream Directory API", "url": "", "order": [ "Streams", "Genres", "Formats" ], "sampleUrl": false, "apidoc": "0.2.0", "generator": { "name": "apidoc", "time": "2015-08-19T08:03:01.948Z", "url": "http://apidocjs.com", "version": "0.13.1" }});
\ No newline at end of file
......
{ "name": "Icecast Stream Directory API", "version": "0.0.0", "description": "Stream directory api", "title": "Icecast Stream Directory API", "url": "", "order": [ "Streams", "Genres", "Formats" ], "sampleUrl": false, "apidoc": "0.2.0", "generator": { "name": "apidoc", "time": "2015-08-11T10:02:36.841Z", "url": "http://apidocjs.com", "version": "0.13.1" }}
\ No newline at end of file
{ "name": "Icecast Stream Directory API", "version": "0.0.0", "description": "Stream directory api", "title": "Icecast Stream Directory API", "url": "", "order": [ "Streams", "Genres", "Formats" ], "sampleUrl": false, "apidoc": "0.2.0", "generator": { "name": "apidoc", "time": "2015-08-19T08:03:01.948Z", "url": "http://apidocjs.com", "version": "0.13.1" }}
\ No newline at end of file
......
......@@ -9,37 +9,24 @@ var express = require('express'),
validator = require('validator'),
xmlbuilder = require('xmlbuilder');
querystring = require('querystring');
bunyan = require('bunyan');
// bunyan = require('bunyan');
var cache = cache_manager.caching({store: "memory", max: 100, ttl: 10});
var app = express();
var config = conf.all().config;
// if logging is needed
var log = bunyan.createLogger({
name: 'dev',
streams: [
{
level: 'info',
stream: process.stdout // log INFO and above to stdout
},
{
level: 'error',
path: __dirname + '/error.log' // log ERROR and above to a file
}
]
});
/* Controllers */
var stats = require('./controllers/stats.js')(query, cache, log);
var streamsFindBy = require('./controllers/stream-api.js')(query, cache, log);
var streamFindById = require('./controllers/stream-by-id.js')(query, cache, log);
var index = require('./controllers/index.js')(query, cache, streamsFindBy, stats, log);
var yp_cgi = require('./controllers/yp-cgi.js')(query, qs, validator, config, log);
var listen = require('./controllers/listen.js')(query, qs, xmlbuilder, streamFindById, log);
var search = require('./controllers/search.js')(query, cache, streamsFindBy, stats, log);
var stats = require('./controllers/stats.js')(query, cache);
var streamsFindBy = require('./controllers/stream-api.js')(query, cache);
var streamFindById = require('./controllers/stream-by-id.js')(query, cache);
var index = require('./controllers/index.js')(query, cache, streamsFindBy, stats);
var yp_cgi = require('./controllers/yp-cgi.js')(query, qs, validator, config);
var listen = require('./controllers/listen.js')(query, qs, xmlbuilder, streamFindById);
var search = require('./controllers/search.js')(query, cache, streamsFindBy, stats);
/*
To rerun api docs after modifying the apidoc comments use the command
......@@ -105,12 +92,12 @@ app.get('/reloadconfig/:password',function(req, res) {
* pagination to get close if the id passed in is deleted.
*
* @apiSuccess {List} streams List of stream objects(See get individual stream for an example)
* @apiSuccess {Object} data Contains starting_after and ending_before urls for this data
* @apiSuccess {Object} data Contains next_url and prev_url for this data
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "streams":[],
* "data":{"starting_after":"/streams?limit=2&starting_after=20"},
* "data":{"next_url":"/streams?limit=2&starting_after=20&order=0&last_listener_count=3","prev_url":"/streams?limit=2&ending_before=23&order=0&last_listener_count=5"},
* }
* View getting a single stream to see what the stream data will look like
* @apiError {String} error Contains an error message describing the issue
......
......@@ -53,6 +53,7 @@ body { padding-bottom: 70px; }
margin-left: auto;
margin-right: auto;
padding: 0 8px 0 8px;
border-collapse:separate !important;
}
#xiphbar a {
......
......@@ -6,15 +6,15 @@
},
"abuse":{
"reason": "Your server has been banned for abuse, have a nice day!",
"listenIps": ["92.246.30.112","222","333"]
"listenIps": ["92.246.30.112"]
},
"misconfigured":{
"reason": "The network range in which your server resides has been suspended due to a high number of wrongly configured servers! Please contact webmaster@xiph.org urgently!",
"listenUrls": ["hostingcenter.com"],
"listenIps": ["192.240.97.","192.240.102.","50.7.."]
"listenIps": ["192.240.97.","192.240.102.","50.7."]
},
"illegalListen2":{
"reason":"Whatever",
"reason":"Illegal listen_url. Incorrect <hostname> fails stream check and was blacklisted. Contact webmaster@icecast.org after correcting it on your side. 'listenurl' as shown in your admin interface MUST work!",
"listenUrls":["lazaradio.hu","ice.mwsc.tmt.de","host504.com","bristol.railroadradio.net","www.kolombiaestereo.com","radiowanderbuehne.org","live.raincitystream.com","roubaix.fr.shinsen-radio.com",
"lepesradio.hu","laradiohd.com","s-radio.whyza.net","213.240.254.147","radio.adrenalin.fm","99.93.26.131",
"delux-streams.eu","radio.status.ks.ua","campus.longmusic.com","stream1.getradio.ru",
......
var query, cache, streamsFindBy, stats, log;
var query, cache, streamsFindBy, stats;
function init(q, c, s, st, l) {
function init(q, c, s, st) {
query = q;
cache = c;
streamsFindBy = s;
stats = st;
log = l;
return index;
}
......
var query, cache, xmlbuilder, streamFindById, log;
var query, cache, xmlbuilder, streamFindById;
function init(q, c, x, s, l) {
function init(q, c, x, s) {
query = q;
cache = c;
streamFindById = s;
xmlbuilder = x;
log = l;
return getListen;
}
......
var query, cache, streamsFindBy, stats, log;
var query, cache, streamsFindBy, stats;
function init(q, c, s, st, l) {
function init(q, c, s, st) {
query = q;
cache = c;
streamsFindBy = s;
stats = st;
log = l;
return bySearch;
}
......@@ -41,7 +40,7 @@ function bySearch(req, res) {
// clear pagination params
delete params.ending_before;
delete params.starting_after;
delete params.last_count;
delete params.last_listener_count;
qstring = querystring.stringify(params);
home_url = req.path+'?'+qstring;
}
......@@ -66,7 +65,6 @@ function bySearch(req, res) {
delete params.starting_after;
params.ending_before = prev_id;
params.last_listener_count = prev_count;
qstring = querystring.stringify(params);
prev_url = req.path+'?'+qstring;
}
......
var query, cache, log;
var query, cache;
var async = require('async');
function init(q, c, l) {
function init(q, c) {
query = q;
cache = c;
log;
return getCachedStats;
}
......
var query, cache, log;
var query, cache;
function init(q, c, l) {
function init(q, c) {
query = q;
cache = c;
log = l;
return getCachedStreams;
}
......
var query, cache, log;
var query, cache;
function init(q, c, l) {
function init(q, c) {
query = q;
cache = c;
log = l;
return getCachedStreamById;
}
......
var query, qs, validator, config, log;
var query, qs, validator, config;
var async = require('async');
var foreign_key_violation = '23503';
var duplicate_key_violation = '23505';
function init(q, q_, v, c, l) {
function init(q, q_, v, c) {
query = q;
qs = q_;
validator = v;
config = c;
log = l;
return dispatcher;
}
......@@ -39,34 +38,38 @@ function checkPresent(toCheck, check)
function multiIndexOf(toCheck, check)
{
for(var i=0;i<check.length;i++)
{
if(toCheck.indexOf(check[i]) != -1) {
return false;
for(var i=0;i<check.length;i++)
{
if(toCheck.indexOf(check[i]) != -1) {
return false;
}
}
}
return true;
return true;
}
function ypAdd(req, res) {
var start = new Date().getTime();
//var start = new Date().getTime();
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var params = req.body;
var mandatoryArgs = ['sn', 'type', 'genre', 'listenurl'];
//need to add others
var defaultServerNames = ['Unspecified name','This is my server name',
'Stream Name','My Station name'];
'Stream Name','My Station name', '-', 'Auto-DJ',
'AutoDJ','Instreamer','Stream-128','Stream-64',
'Stream-96','Stream-256','stream-128','stream-64',
'stream-96','stream-256'];
if( checkPresent(params, mandatoryArgs) == false) {
ypRes(res, false, "Not enough arguments", -1, null);
ypRes(res, false, "Not enough arguments.", -1, null);
return;
}
if( validator.isURL(params.listenurl) == false) {
ypRes(res, false, "Not a real listenurl", -1, null);
ypRes(res, false, "Could not parse listen_url.", -1, null);
return;
}
// check ban groups
for(var ban in config.bans)
{
......@@ -79,6 +82,12 @@ function ypAdd(req, res) {
return;
}
}
if (listenIps != undefined) {
if( multiIndexOf(params.listenurl, listenIps) == false) {
ypRes(res, false, reason, -1, null);
return;
}
}
}
if( multiIndexOf(params.sn, defaultServerNames) == false) {
......@@ -139,7 +148,7 @@ function ypTouch(req, res) {
async.waterfall([
function start(cb) {
if(final.id == undefined) {
ypRes(res, false, "Not enough arguments", -1, null);
ypRes(res, false, "Not enough arguments.", -1, null);
return;
}
if(final.listeners === undefined) {
......@@ -153,7 +162,7 @@ function ypTouch(req, res) {
function(row,result, cb) {
if(result.rowCount != 1) {
// end with error
cb(1);
cb("update failed");
return;
}
params = [row[0].stream_id, final.songname,final.codec_sub_types];
......@@ -170,8 +179,14 @@ function ypTouch(req, res) {
],
function(err, result) {
if(err) {
console.log(err);
ypRes(res, false, "Server error", -1, null);
if(err == "update failed") {
ypRes(res, false, "SID does not exist. Check your firewall and icecast 'hostname' setting, your server may be unreachable.", -1, null);
}
else {
console.log(err);
ypRes(res, false, "Server error", -1, null);
}
}
});
}
......
This diff is collapsed.
......@@ -8,18 +8,24 @@ var app = require("../app.js").getApp;
var query = require("pg-query");
var config = conf.all().config;
var async = require('async');
var fs = require('fs');
var sqlCreateTestDB = fs.readFileSync(__dirname+'/init_test_database.sql').toString()
query.connectionParameters = config.db;
describe('GET /streams', function(){
beforeEach(function(done) {
before(function(done) {
// beforeEach cleanTables
var cleanDatabase= 'DELETE FROM server_mounts; DELETE FROM streams;';
query(cleanDatabase, function(err, rows) {
if(err) return done(err);
done();
query(sqlCreateTestDB, function(err, rows) {
if(err) return done(err);
done();
})
});
});
......@@ -35,10 +41,35 @@ describe('GET /streams', function(){
.end(function(err, res){
if (err) return done(err);
var result = JSON.parse(res.text);
//result.streams.length.should.equal(10)
result.streams.length.should.equal(10);
done()
});
});
it('starting after and ending before urls present', function(done){
var params = {"limit":10, "order":0, "genre":"Rock"}
var qstring = querystring.stringify(params);
request(app)
.get('/streams?'+qstring)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res){
if (err) return done(err);
var result = JSON.parse(res.text);
result.data.should.have.property('next_url');
result.data.should.have.property('prev_url');
done()
});
});
after(function(done) {
var cleanDatabase= 'DELETE FROM server_mounts; DELETE FROM streams;';
query(cleanDatabase, function(err, rows) {
if(err) return done(err);
done();
});
});
});
......@@ -32,12 +32,13 @@ describe('POST /cgi-bin/yp-cgi', function(){
});
});
/* Test Add */
it('add stream fails on not enough arguments', function(done){
var params = {action:"add"};
testPost(params, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('0');
res.headers.ypmessage.should.equal('Not enough arguments');
res.headers.ypmessage.should.equal('Not enough arguments.');
done();
});
});
......@@ -47,7 +48,7 @@ describe('POST /cgi-bin/yp-cgi', function(){
testPost(params, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('0');
res.headers.ypmessage.should.equal('Not a real listenurl');
res.headers.ypmessage.should.equal('Could not parse listen_url.');
done();
});
});
......@@ -63,11 +64,7 @@ describe('POST /cgi-bin/yp-cgi', function(){
testPost(params, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('0');
testPost({"action":"remove","sid":delsid}, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('1');
done();
});
done();
});
});
});
......@@ -78,11 +75,36 @@ describe('POST /cgi-bin/yp-cgi', function(){
testPost(params, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('1');
testPost({"action":"remove","sid":res.headers.sid}, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('1');
done()
});
done();
});
});
/* Test Touch */
it('test touch fails if not enough arguments', function(done){
var params = {"action":"touch"}
testPost(params, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('0');
res.headers.ypmessage.should.equal('Not enough arguments.');
done();
});
});
it('test touch fails if sid not in the db', function(done){
var params = {"action":"touch","sid":"0be387a8-0fab-4d35-a1b5-8a2222802a94"}
testPost(params, function(err, res) {
if (err) return done(err);
res.headers.ypresponse.should.equal('0');
res.headers.ypmessage.should.equal("SID does not exist. Check your firewall and icecast 'hostname' setting, your server may be unreachable.");
done();
});
});
after(function(done) {
var cleanDatabase= 'DELETE FROM server_mounts; DELETE FROM streams;';
query(cleanDatabase, function(err, rows) {
if(err) return done(err);
done();
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment