Add API limits configurable in settings.json

This commit is contained in:
Gaël Métais
2014-12-12 20:26:19 +01:00
parent 84faabca86
commit 0da2ad0c10
7 changed files with 202 additions and 152 deletions
+69 -94
View File
@@ -1,13 +1,11 @@
var should = require('chai').should();
var request = require('request');
var jwt = require('jwt-simple');
var Q = require('q');
var config = {
"authorizedKeys": {
"1234567890": "contact@gaelmetais.com"
},
"tokenSalt": "lake-city",
"authorizedApplications": ["frontend"]
}
};
var apiUrl = 'http://localhost:8387/api';
@@ -16,26 +14,7 @@ var wwwUrl = 'http://localhost:8388';
describe('api', function() {
var runId;
it('should not accept a query if there is no key in headers', function(done) {
this.timeout(5000);
request({
method: 'POST',
url: apiUrl + '/runs',
body: {
url: wwwUrl + '/simple-page.html',
waitForResponse: false
},
json: true
}, function(error, response, body) {
if (!error && response.statusCode === 401) {
done();
} else {
done(error || response.statusCode);
}
});
});
it('should refuse a query with an invalid key', function(done) {
this.timeout(5000);
@@ -87,85 +66,81 @@ describe('api', function() {
});
});
it('should refuse an expired token', function(done) {
this.timeout(5000);
it('should accept up to 24 anonymous runs to the API', function(done) {
this.timeout(15000);
request({
method: 'POST',
url: apiUrl + '/runs',
body: {
url: wwwUrl + '/simple-page.html',
waitForResponse: false
},
json: true,
headers: {
'X-Api-Token': jwt.encode({
application: config.authorizedApplications[0],
expire: Date.now() - 60000
}, config.tokenSalt)
}
}, function(error, response, body) {
if (!error && response.statusCode === 401) {
done();
} else {
done(error || response.statusCode);
}
});
});
function launchRun() {
var deferred = Q.defer();
it('should refuse a token from an unknown app', function(done) {
this.timeout(5000);
request({
method: 'POST',
url: apiUrl + '/runs',
body: {
url: wwwUrl + '/simple-page.html',
waitForResponse: false
},
json: true
}, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(response, body);
}
});
request({
method: 'POST',
url: apiUrl + '/runs',
body: {
url: wwwUrl + '/simple-page.html',
waitForResponse: false
},
json: true,
headers: {
'X-Api-Token': jwt.encode({
application: 'unknown-app',
expire: Date.now() + 60000
}, config.tokenSalt)
}
}, function(error, response, body) {
if (!error && response.statusCode === 401) {
done();
} else {
done(error || response.statusCode);
}
});
});
return deferred.promise;
}
it('should accept a good token', function(done) {
this.timeout(5000);
launchRun()
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
request({
method: 'POST',
url: apiUrl + '/runs',
body: {
url: wwwUrl + '/simple-page.html',
waitForResponse: false
},
json: true,
headers: {
'X-Api-Token': jwt.encode({
application: config.authorizedApplications[0],
expire: Date.now() + 60000
}, config.tokenSalt)
}
}, function(error, response, body) {
if (!error && response.statusCode === 200) {
.then(function(response, body) {
// Here should still be ok
response.statusCode.should.equal(200);
runId = body.runId;
runId.should.be.a('string');
launchRun()
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(launchRun)
.then(function(response, body) {
// It should fail now
response.statusCode.should.equal(429);
done();
} else {
done(error || response.statusCode);
}
})
.fail(function(error) {
done(error);
});
}).fail(function(error) {
done(error);
});
});
});