2020-09-26 20:38:24 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const supertest = require(__dirname + '/../../../../src/node_modules/supertest');
|
|
|
|
const fs = require('fs');
|
|
|
|
const settings = require(__dirname + '/../../../../src/node/utils/Settings');
|
|
|
|
const api = supertest(`http://${settings.ip}:${settings.port}`);
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const filePath = path.join(__dirname, '../../../../APIKEY.txt');
|
|
|
|
|
|
|
|
const apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}).replace(/\n$/, '');
|
|
|
|
let apiVersion = 1;
|
|
|
|
let groupID = '';
|
|
|
|
let authorID = '';
|
|
|
|
let sessionID = '';
|
|
|
|
let padID = makeid();
|
2014-11-26 22:06:35 +01:00
|
|
|
|
|
|
|
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-26 22:19:55 +01:00
|
|
|
// BEGIN GROUP AND AUTHOR TESTS
|
|
|
|
/////////////////////////////////////
|
|
|
|
/////////////////////////////////////
|
|
|
|
|
2014-11-26 22:06:35 +01:00
|
|
|
/* Tests performed
|
|
|
|
-> createGroup() -- should return a groupID
|
|
|
|
-> listSessionsOfGroup(groupID) -- should be 0
|
|
|
|
-> deleteGroup(groupID)
|
|
|
|
-> createGroupIfNotExistsFor(groupMapper) -- should return a groupID
|
|
|
|
|
|
|
|
-> createAuthor([name]) -- should return an authorID
|
|
|
|
-> createAuthorIfNotExistsFor(authorMapper [, name]) -- should return an authorID
|
|
|
|
-> getAuthorName(authorID) -- should return a name IE "john"
|
|
|
|
|
|
|
|
-> createSession(groupID, authorID, validUntil)
|
|
|
|
-> getSessionInfo(sessionID)
|
|
|
|
-> listSessionsOfGroup(groupID) -- should be 1
|
|
|
|
-> deleteSession(sessionID)
|
|
|
|
-> getSessionInfo(sessionID) -- should have author id etc in
|
|
|
|
|
|
|
|
-> listPads(groupID) -- should be empty array
|
|
|
|
-> createGroupPad(groupID, padName [, text])
|
|
|
|
-> listPads(groupID) -- should be empty array
|
|
|
|
-> getPublicStatus(padId)
|
|
|
|
-> setPublicStatus(padId, status)
|
2014-11-26 22:36:57 +01:00
|
|
|
-> getPublicStatus(padId)
|
|
|
|
-> isPasswordProtected(padID) -- should be false
|
|
|
|
-> setPassword(padID, password)
|
|
|
|
-> isPasswordProtected(padID) -- should be true
|
2014-11-26 22:44:50 +01:00
|
|
|
|
|
|
|
-> listPadsOfAuthor(authorID)
|
2014-11-26 22:06:35 +01:00
|
|
|
*/
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
describe('API: Group creation and deletion', function() {
|
|
|
|
it('createGroup', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('createGroup'))
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Unable to create new Pad");
|
|
|
|
groupID = res.body.data.groupID;
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('listSessionsOfGroup for empty group', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('listSessionsOfGroup')+"&groupID="+groupID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || res.body.data !== null) throw new Error("Sessions show as existing for this group");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('deleteGroup', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('deleteGroup')+"&groupID="+groupID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0) throw new Error("Group failed to be deleted");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('createGroupIfNotExistsFor', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('createGroupIfNotExistsFor')+"&groupMapper=management")
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Sessions show as existing for this group");
|
2014-11-26 22:36:57 +01:00
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2020-09-26 21:20:47 +02:00
|
|
|
});
|
2014-11-26 22:36:57 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
describe('API: Author creation', function() {
|
|
|
|
it('createGroup', function(done) {
|
2014-11-26 22:36:57 +01:00
|
|
|
api.get(endPoint('createGroup'))
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Unable to create new Pad");
|
|
|
|
groupID = res.body.data.groupID;
|
2014-11-26 22:06:35 +01:00
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('createAuthor', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('createAuthor'))
|
|
|
|
.expect(function(res){
|
2014-11-26 22:44:50 +01:00
|
|
|
if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create author");
|
2014-11-26 22:06:35 +01:00
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('createAuthor with name', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('createAuthor')+"&name=john")
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create user with name set");
|
|
|
|
authorID = res.body.data.authorID; // we will be this author for the rest of the tests
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('createAuthorIfNotExistsFor', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('createAuthorIfNotExistsFor')+"&authorMapper=chris")
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create author with mapper");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('getAuthorName', function(done) {
|
2014-11-26 22:06:35 +01:00
|
|
|
api.get(endPoint('getAuthorName')+"&authorID="+authorID)
|
|
|
|
.expect(function(res){
|
2016-05-05 12:50:39 +02:00
|
|
|
if(res.body.code !== 0 || res.body.data !== "john") throw new Error("Unable to get Author Name from Author ID");
|
2014-11-26 22:06:35 +01:00
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2020-09-26 21:20:47 +02:00
|
|
|
});
|
2014-11-26 22:06:35 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
describe('API: Sessions', function() {
|
|
|
|
it('createSession', function(done) {
|
2014-11-26 22:19:55 +01:00
|
|
|
api.get(endPoint('createSession')+"&authorID="+authorID+"&groupID="+groupID+"&validUntil=999999999999")
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.sessionID) throw new Error("Unable to create Session");
|
|
|
|
sessionID = res.body.data.sessionID;
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2014-11-26 22:06:35 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('getSessionInfo', function(done) {
|
2014-11-26 22:19:55 +01:00
|
|
|
api.get(endPoint('getSessionInfo')+"&sessionID="+sessionID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.groupID || !res.body.data.authorID || !res.body.data.validUntil) throw new Error("Unable to get Session info");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2014-11-26 22:06:35 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('listSessionsOfGroup', function(done) {
|
2014-11-26 22:19:55 +01:00
|
|
|
api.get(endPoint('listSessionsOfGroup')+"&groupID="+groupID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || typeof res.body.data !== "object") throw new Error("Unable to get sessions of a group");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('deleteSession', function(done) {
|
2014-11-26 22:19:55 +01:00
|
|
|
api.get(endPoint('deleteSession')+"&sessionID="+sessionID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0) throw new Error("Unable to delete a session");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('getSessionInfo of deleted session', function(done) {
|
2014-11-26 22:19:55 +01:00
|
|
|
api.get(endPoint('getSessionInfo')+"&sessionID="+sessionID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 1) throw new Error("Session was not properly deleted");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2020-09-26 21:20:47 +02:00
|
|
|
});
|
2014-11-26 22:19:55 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
describe('API: Group pad management', function() {
|
|
|
|
it('listPads', function(done) {
|
2014-11-26 22:36:57 +01:00
|
|
|
api.get(endPoint('listPads')+"&groupID="+groupID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || res.body.data.padIDs.length !== 0) throw new Error("Group already had pads for some reason"+res.body.data.padIDs);
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2014-11-26 22:19:55 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('createGroupPad', function(done) {
|
2014-11-26 22:36:57 +01:00
|
|
|
api.get(endPoint('createGroupPad')+"&groupID="+groupID+"&padName="+padID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0) throw new Error("Unable to create group pad");
|
|
|
|
padID = res.body.data.padID;
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('listPads after creating a group pad', function(done) {
|
2014-11-26 22:36:57 +01:00
|
|
|
api.get(endPoint('listPads')+"&groupID="+groupID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || res.body.data.padIDs.length !== 1) throw new Error("Group isnt listing this pad");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2020-09-26 21:20:47 +02:00
|
|
|
});
|
2014-11-26 22:44:50 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
describe('API: Pad security', function() {
|
|
|
|
it('getPublicStatus', function(done) {
|
2014-11-26 22:36:57 +01:00
|
|
|
api.get(endPoint('getPublicStatus')+"&padID="+padID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || res.body.data.publicstatus) throw new Error("Unable to get public status of this pad");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('setPublicStatus', function(done) {
|
2014-11-26 22:36:57 +01:00
|
|
|
api.get(endPoint('setPublicStatus')+"&padID="+padID+"&publicStatus=true")
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0) throw new Error("Setting status did not work");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('getPublicStatus after changing public status', function(done) {
|
2014-11-26 22:36:57 +01:00
|
|
|
api.get(endPoint('getPublicStatus')+"&padID="+padID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.publicStatus) throw new Error("Setting public status of this pad did not work");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('isPasswordProtected', function(done) {
|
2014-11-26 22:44:50 +01:00
|
|
|
api.get(endPoint('isPasswordProtected')+"&padID="+padID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || res.body.data.isPasswordProtected) throw new Error("Pad is password protected by default");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2014-11-26 22:36:57 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('setPassword', function(done) {
|
2014-11-26 22:44:50 +01:00
|
|
|
api.get(endPoint('setPassword')+"&padID="+padID+"&password=test")
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0) throw new Error("Unabe to set password");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2014-11-26 22:06:35 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
it('isPasswordProtected after setting password', function(done) {
|
2014-11-26 22:44:50 +01:00
|
|
|
api.get(endPoint('isPasswordProtected')+"&padID="+padID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || !res.body.data.isPasswordProtected) throw new Error("Pad password protection has not applied");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2020-09-26 21:20:47 +02:00
|
|
|
});
|
2014-11-26 22:06:35 +01:00
|
|
|
|
2014-11-26 22:44:50 +01:00
|
|
|
// NOT SURE HOW TO POPULAT THIS /-_-\
|
|
|
|
///////////////////////////////////////
|
|
|
|
///////////////////////////////////////
|
2014-11-26 22:06:35 +01:00
|
|
|
|
2020-09-26 21:20:47 +02:00
|
|
|
describe('API: Misc', function() {
|
|
|
|
it('listPadsOfAuthor', function(done) {
|
2014-11-26 22:44:50 +01:00
|
|
|
api.get(endPoint('listPadsOfAuthor')+"&authorID="+authorID)
|
|
|
|
.expect(function(res){
|
|
|
|
if(res.body.code !== 0 || res.body.data.padIDs.length !== 0) throw new Error("Pad password protection has not applied");
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, done)
|
|
|
|
});
|
2020-09-26 21:20:47 +02:00
|
|
|
});
|
2014-11-26 22:06:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-26 20:38:24 +02:00
|
|
|
const endPoint = function(point) {
|
2014-11-26 20:28:49 +01:00
|
|
|
return '/api/'+apiVersion+'/'+point+'?apikey='+apiKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeid()
|
|
|
|
{
|
2020-09-26 20:38:24 +02:00
|
|
|
let text = '';
|
|
|
|
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
2014-11-26 20:28:49 +01:00
|
|
|
|
2020-09-26 20:38:24 +02:00
|
|
|
for (let i = 0; i < 5; i++) {
|
2014-11-26 20:28:49 +01:00
|
|
|
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|