etherpad-lite/tests/backend/specs/api/pad.js

284 lines
8.5 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
2014-11-26 23:10:56 +01:00
supertest = require(__dirname+'/../../../../src/node_modules/supertest'),
fs = require('fs'),
2014-11-25 23:47:22 +01:00
api = supertest('http://localhost:9001');
path = require('path');
2014-11-25 23:47:22 +01:00
2014-11-26 20:28:49 +01:00
var filePath = path.join(__dirname, '../../../../APIKEY.txt');
var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'});
2014-12-27 14:19:31 +01:00
apiKey = apiKey.replace(/\n$/, "");
var apiVersion = 1;
var testPadId = makeid();
2014-11-26 20:44:38 +01:00
var lastEdited = "";
2014-11-25 23:47:22 +01:00
describe('Connectivity', function(){
it('errors if can not connect', function(done) {
api.get('/api/')
.expect('Content-Type', /json/)
2014-11-25 23:47:22 +01:00
.expect(200, done)
});
})
describe('API Versioning', function(){
it('errors if can not connect', function(done) {
api.get('/api/')
.expect(function(res){
apiVersion = res.body.currentVersion;
if (!res.body.currentVersion) throw new Error("No version set in API");
return;
})
.expect(200, done)
});
})
2014-11-25 23:47:22 +01:00
describe('Permission', function(){
it('errors if can connect without correct APIKey', function(done) {
// This is broken because Etherpad doesn't handle HTTP codes properly see #2343
// If your APIKey is password you deserve to fail all tests anyway
2014-11-26 18:34:44 +01:00
var permErrorURL = '/api/'+apiVersion+'/createPad?apikey=password&padID=test';
api.get(permErrorURL)
.expect(401, done)
});
})
2014-11-25 23:47:22 +01:00
2014-11-26 18:53:31 +01:00
/* Pad Tests Order of execution
-> deletePad -- This gives us a guaranteed clear environment
-> createPad
2014-11-26 20:44:38 +01:00
-> getRevisions -- Should be 0
2014-11-26 18:53:31 +01:00
-> getHTML -- Should be the default pad text in HTML format
-> deletePad -- Should just delete a pad
-> getHTML -- Should return an error
-> createPad(withText)
-> getText -- Should have the text specified above as the pad text
-> setText
-> getText -- Should be the text set before
-> getRevisions -- Should be 0 still?
-> padUsersCount -- Should be 0
-> getReadOnlyId -- Should be a value
2014-11-26 20:44:38 +01:00
-> listAuthorsOfPad(padID) -- should be empty array?
-> getLastEdited(padID) -- Should be when pad was made
-> setText(padId)
-> getLastEdited(padID) -- Should be when setText was performed
2014-12-24 05:01:18 +01:00
-> padUsers(padID) -- Should be when setText was performed
2014-11-26 18:53:31 +01:00
*/
describe('deletePad', function(){
it('deletes a Pad', function(done) {
api.get(endPoint('deletePad')+"&padID="+testPadId)
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('createPad', function(){
2014-11-26 18:53:31 +01:00
it('creates a new Pad', function(done) {
api.get(endPoint('createPad')+"&padID="+testPadId)
2014-11-26 20:25:09 +01:00
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to create new Pad");
})
.expect('Content-Type', /json/)
.expect(200, done)
2014-11-25 23:47:22 +01:00
});
})
2014-11-26 20:25:09 +01:00
describe('getRevisionsCount', function(){
2014-11-26 18:53:31 +01:00
it('gets revision count of Pad', function(done) {
2014-11-26 20:25:09 +01:00
api.get(endPoint('getRevisionsCount')+"&padID="+testPadId)
.expect(function(res){
if(res.body.code !== 0) throw new Error("Unable to get Revision Count");
if(res.body.data.revisions !== 0) throw new Error("Incorrect Revision Count");
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getHTML', function(){
it('get the HTML of Pad', function(done) {
api.get(endPoint('getHTML')+"&padID="+testPadId)
2014-11-26 20:25:09 +01:00
.expect(function(res){
if(res.body.data.html.length <= 1) throw new Error("Unable to get Revision Count");
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('deletePad', function(){
it('deletes a Pad', function(done) {
api.get(endPoint('deletePad')+"&padID="+testPadId)
2014-11-26 20:25:09 +01:00
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Deletion failed")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getHTML', function(){
2014-11-26 20:25:09 +01:00
it('get the HTML of a Pad -- Should return a failure', function(done) {
2014-11-26 18:53:31 +01:00
api.get(endPoint('getHTML')+"&padID="+testPadId)
2014-11-26 20:25:09 +01:00
.expect(function(res){
if(res.body.code !== 1) throw new Error("Pad deletion failed")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('createPad', function(){
it('creates a new Pad with text', function(done) {
2014-11-26 20:25:09 +01:00
api.get(endPoint('createPad')+"&padID="+testPadId+"&text=testText")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad Creation failed")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getText', function(){
2014-11-26 20:25:09 +01:00
it('gets the Pad text and expect it to be testText with \n which is a line break', function(done) {
2014-11-26 18:53:31 +01:00
api.get(endPoint('getText')+"&padID="+testPadId)
2014-11-26 20:25:09 +01:00
.expect(function(res){
if(res.body.data.text !== "testText\n") throw new Error("Pad Creation with text")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('setText', function(){
it('creates a new Pad with text', function(done) {
2014-11-26 20:25:09 +01:00
api.get(endPoint('setText')+"&padID="+testPadId+"&text=testTextTwo")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad setting text failed");
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getText', function(){
it('gets the Pad text', function(done) {
api.get(endPoint('getText')+"&padID="+testPadId)
2014-11-26 20:25:09 +01:00
.expect(function(res){
if(res.body.data.text !== "testTextTwo\n") throw new Error("Setting Text")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
2014-11-26 20:25:09 +01:00
describe('getRevisionsCount', function(){
2014-11-26 18:53:31 +01:00
it('gets Revision Coutn of a Pad', function(done) {
2014-11-26 20:25:09 +01:00
api.get(endPoint('getRevisionsCount')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.revisions !== 1) throw new Error("Unable to set text revision count")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('padUsersCount', function(){
2014-11-26 20:44:38 +01:00
it('gets User Count of a Pad', function(done) {
2014-11-26 18:53:31 +01:00
api.get(endPoint('padUsersCount')+"&padID="+testPadId)
2014-11-26 20:25:09 +01:00
.expect(function(res){
if(res.body.data.padUsersCount !== 0) throw new Error("Incorrect Pad User count")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
2014-11-26 20:25:09 +01:00
describe('getReadOnlyID', function(){
2014-11-26 18:53:31 +01:00
it('Gets the Read Only ID of a Pad', function(done) {
2014-11-26 20:25:09 +01:00
api.get(endPoint('getReadOnlyID')+"&padID="+testPadId)
.expect(function(res){
if(!res.body.data.readOnlyID) throw new Error("No Read Only ID for Pad")
})
2014-11-26 18:53:31 +01:00
.expect('Content-Type', /json/)
.expect(200, done)
});
})
2014-11-26 20:44:38 +01:00
describe('listAuthorsOfPad', function(){
it('Get Authors of the Pad', function(done) {
api.get(endPoint('listAuthorsOfPad')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.authorIDs.length !== 0) throw new Error("# of Authors of pad is not 0")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getLastEdited', function(){
it('Get When Pad was left Edited', function(done) {
api.get(endPoint('getLastEdited')+"&padID="+testPadId)
.expect(function(res){
if(!res.body.data.lastEdited){
throw new Error("# of Authors of pad is not 0")
}else{
lastEdited = res.body.data.lastEdited;
}
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('setText', function(){
it('creates a new Pad with text', function(done) {
api.get(endPoint('setText')+"&padID="+testPadId+"&text=testTextTwo")
.expect(function(res){
if(res.body.code !== 0) throw new Error("Pad setting text failed");
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
describe('getLastEdited', function(){
it('Get When Pad was left Edited', function(done) {
api.get(endPoint('getLastEdited')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.lastEdited <= lastEdited){
throw new Error("Editing A Pad is not updating when it was last edited")
}
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
2014-12-24 05:01:18 +01:00
describe('padUsers', function(){
it('gets User Count of a Pad', function(done) {
api.get(endPoint('padUsers')+"&padID="+testPadId)
.expect(function(res){
if(res.body.data.padUsers.length !== 0) throw new Error("Incorrect Pad Users")
})
.expect('Content-Type', /json/)
.expect(200, done)
});
})
2014-11-26 20:44:38 +01:00
var endPoint = function(point){
return '/api/'+apiVersion+'/'+point+'?apikey='+apiKey;
}
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ ){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}