tests: Use async/await instead of callbacks, use assert

This commit is contained in:
Richard Hansen 2020-09-26 15:57:51 -04:00 committed by John McLear
parent 24345bf9a8
commit e01e575c86

View file

@ -1,4 +1,4 @@
const assert = require('assert'); const assert = require('assert').strict;
const supertest = require(__dirname + '/../../../../src/node_modules/supertest'); const supertest = require(__dirname + '/../../../../src/node_modules/supertest');
const fs = require('fs'); const fs = require('fs');
const settings = require(__dirname + '/../../../../src/node/utils/Settings'); const settings = require(__dirname + '/../../../../src/node/utils/Settings');
@ -14,17 +14,16 @@ let authorID = '';
let sessionID = ''; let sessionID = '';
let padID = makeid(); let padID = makeid();
describe('API Versioning', function(){ describe('API Versioning', function() {
it('errors if can not connect', function(done) { it('errors if can not connect', async function() {
api.get('/api/') await api.get('/api/')
.expect(function(res){ .expect(200)
apiVersion = res.body.currentVersion; .expect((res) => {
if (!res.body.currentVersion) throw new Error("No version set in API"); assert(res.body.currentVersion);
return; apiVersion = res.body.currentVersion;
}) });
.expect(200, done)
}); });
}) });
// BEGIN GROUP AND AUTHOR TESTS // BEGIN GROUP AND AUTHOR TESTS
///////////////////////////////////// /////////////////////////////////////
@ -60,224 +59,244 @@ describe('API Versioning', function(){
*/ */
describe('API: Group creation and deletion', function() { describe('API: Group creation and deletion', function() {
it('createGroup', function(done) { it('createGroup', async function() {
api.get(endPoint('createGroup')) await api.get(endPoint('createGroup'))
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Unable to create new Pad"); .expect('Content-Type', /json/)
groupID = res.body.data.groupID; .expect((res) => {
}) assert.equal(res.body.code, 0);
.expect('Content-Type', /json/) assert(res.body.data.groupID);
.expect(200, done) groupID = res.body.data.groupID;
});
}); });
it('listSessionsOfGroup for empty group', function(done) { it('listSessionsOfGroup for empty group', async function() {
api.get(endPoint('listSessionsOfGroup')+"&groupID="+groupID) await api.get(endPoint('listSessionsOfGroup') + `&groupID=${groupID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || res.body.data !== null) throw new Error("Sessions show as existing for this group"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert.equal(res.body.data, null);
});
}); });
it('deleteGroup', function(done) { it('deleteGroup', async function() {
api.get(endPoint('deleteGroup')+"&groupID="+groupID) await api.get(endPoint('deleteGroup') + `&groupID=${groupID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0) throw new Error("Group failed to be deleted"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) });
}); });
it('createGroupIfNotExistsFor', function(done) { it('createGroupIfNotExistsFor', async function() {
api.get(endPoint('createGroupIfNotExistsFor')+"&groupMapper=management") await api.get(endPoint('createGroupIfNotExistsFor') + '&groupMapper=management')
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Sessions show as existing for this group"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(res.body.data.groupID);
});
}); });
}); });
describe('API: Author creation', function() { describe('API: Author creation', function() {
it('createGroup', function(done) { it('createGroup', async function() {
api.get(endPoint('createGroup')) await api.get(endPoint('createGroup'))
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Unable to create new Pad"); .expect('Content-Type', /json/)
groupID = res.body.data.groupID; .expect((res) => {
}) assert.equal(res.body.code, 0);
.expect('Content-Type', /json/) assert(res.body.data.groupID);
.expect(200, done) groupID = res.body.data.groupID;
});
}); });
it('createAuthor', function(done) { it('createAuthor', async function() {
api.get(endPoint('createAuthor')) await api.get(endPoint('createAuthor'))
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create author"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(res.body.data.authorID);
});
}); });
it('createAuthor with name', function(done) { it('createAuthor with name', async function() {
api.get(endPoint('createAuthor')+"&name=john") await api.get(endPoint('createAuthor') + '&name=john')
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create user with name set"); .expect('Content-Type', /json/)
authorID = res.body.data.authorID; // we will be this author for the rest of the tests .expect((res) => {
}) assert.equal(res.body.code, 0);
.expect('Content-Type', /json/) assert(res.body.data.authorID);
.expect(200, done) authorID = res.body.data.authorID; // we will be this author for the rest of the tests
});
}); });
it('createAuthorIfNotExistsFor', function(done) { it('createAuthorIfNotExistsFor', async function() {
api.get(endPoint('createAuthorIfNotExistsFor')+"&authorMapper=chris") await api.get(endPoint('createAuthorIfNotExistsFor') + '&authorMapper=chris')
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create author with mapper"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(res.body.data.authorID);
});
}); });
it('getAuthorName', function(done) { it('getAuthorName', async function() {
api.get(endPoint('getAuthorName')+"&authorID="+authorID) await api.get(endPoint('getAuthorName') + `&authorID=${authorID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || res.body.data !== "john") throw new Error("Unable to get Author Name from Author ID"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert.equal(res.body.data, 'john');
});
}); });
}); });
describe('API: Sessions', function() { describe('API: Sessions', function() {
it('createSession', function(done) { it('createSession', async function() {
api.get(endPoint('createSession')+"&authorID="+authorID+"&groupID="+groupID+"&validUntil=999999999999") await api.get(endPoint('createSession') +
.expect(function(res){ `&authorID=${authorID}&groupID=${groupID}&validUntil=999999999999`)
if(res.body.code !== 0 || !res.body.data.sessionID) throw new Error("Unable to create Session"); .expect(200)
sessionID = res.body.data.sessionID; .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(res.body.data.sessionID);
sessionID = res.body.data.sessionID;
});
}); });
it('getSessionInfo', function(done) { it('getSessionInfo', async function() {
api.get(endPoint('getSessionInfo')+"&sessionID="+sessionID) await api.get(endPoint('getSessionInfo') + `&sessionID=${sessionID}`)
.expect(function(res){ .expect(200)
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((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(res.body.data.groupID);
assert(res.body.data.authorID);
assert(res.body.data.validUntil);
});
}); });
it('listSessionsOfGroup', function(done) { it('listSessionsOfGroup', async function() {
api.get(endPoint('listSessionsOfGroup')+"&groupID="+groupID) await api.get(endPoint('listSessionsOfGroup') + `&groupID=${groupID}`)
.expect(function(res){ .expect(200)
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((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert.equal(typeof res.body.data, 'object');
});
}); });
it('deleteSession', function(done) { it('deleteSession', async function() {
api.get(endPoint('deleteSession')+"&sessionID="+sessionID) await api.get(endPoint('deleteSession') + `&sessionID=${sessionID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0) throw new Error("Unable to delete a session"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) });
}); });
it('getSessionInfo of deleted session', function(done) { it('getSessionInfo of deleted session', async function() {
api.get(endPoint('getSessionInfo')+"&sessionID="+sessionID) await api.get(endPoint('getSessionInfo') + `&sessionID=${sessionID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 1) throw new Error("Session was not properly deleted"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 1);
.expect(200, done) });
}); });
}); });
describe('API: Group pad management', function() { describe('API: Group pad management', function() {
it('listPads', function(done) { it('listPads', async function() {
api.get(endPoint('listPads')+"&groupID="+groupID) await api.get(endPoint('listPads') + `&groupID=${groupID}`)
.expect(function(res){ .expect(200)
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((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert.equal(res.body.data.padIDs.length, 0);
});
}); });
it('createGroupPad', function(done) { it('createGroupPad', async function() {
api.get(endPoint('createGroupPad')+"&groupID="+groupID+"&padName="+padID) await api.get(endPoint('createGroupPad') + `&groupID=${groupID}&padName=${padID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0) throw new Error("Unable to create group pad"); .expect('Content-Type', /json/)
padID = res.body.data.padID; .expect((res) => {
}) assert.equal(res.body.code, 0);
.expect('Content-Type', /json/) padID = res.body.data.padID;
.expect(200, done) });
}); });
it('listPads after creating a group pad', function(done) { it('listPads after creating a group pad', async function() {
api.get(endPoint('listPads')+"&groupID="+groupID) await api.get(endPoint('listPads') + `&groupID=${groupID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || res.body.data.padIDs.length !== 1) throw new Error("Group isnt listing this pad"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert.equal(res.body.data.padIDs.length, 1);
});
}); });
}); });
describe('API: Pad security', function() { describe('API: Pad security', function() {
it('getPublicStatus', function(done) { it('getPublicStatus', async function() {
api.get(endPoint('getPublicStatus')+"&padID="+padID) await api.get(endPoint('getPublicStatus') + `&padID=${padID}`)
.expect(function(res){ .expect(200)
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((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(!res.body.data.publicstatus);
});
}); });
it('setPublicStatus', function(done) { it('setPublicStatus', async function() {
api.get(endPoint('setPublicStatus')+"&padID="+padID+"&publicStatus=true") await api.get(endPoint('setPublicStatus') + `&padID=${padID}&publicStatus=true`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0) throw new Error("Setting status did not work"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) });
}); });
it('getPublicStatus after changing public status', function(done) { it('getPublicStatus after changing public status', async function() {
api.get(endPoint('getPublicStatus')+"&padID="+padID) await api.get(endPoint('getPublicStatus') + `&padID=${padID}`)
.expect(function(res){ .expect(200)
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((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(res.body.data.publicStatus);
});
}); });
it('isPasswordProtected', function(done) { it('isPasswordProtected', async function() {
api.get(endPoint('isPasswordProtected')+"&padID="+padID) await api.get(endPoint('isPasswordProtected') + `&padID=${padID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || res.body.data.isPasswordProtected) throw new Error("Pad is password protected by default"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(!res.body.data.isPasswordProtected);
});
}); });
it('setPassword', function(done) { it('setPassword', async function() {
api.get(endPoint('setPassword')+"&padID="+padID+"&password=test") await api.get(endPoint('setPassword') + `&padID=${padID}&password=test`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0) throw new Error("Unabe to set password"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) });
}); });
it('isPasswordProtected after setting password', function(done) { it('isPasswordProtected after setting password', async function() {
api.get(endPoint('isPasswordProtected')+"&padID="+padID) await api.get(endPoint('isPasswordProtected') + `&padID=${padID}`)
.expect(function(res){ .expect(200)
if(res.body.code !== 0 || !res.body.data.isPasswordProtected) throw new Error("Pad password protection has not applied"); .expect('Content-Type', /json/)
}) .expect((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert(res.body.data.isPasswordProtected);
});
}); });
}); });
@ -286,21 +305,22 @@ describe('API: Pad security', function() {
/////////////////////////////////////// ///////////////////////////////////////
describe('API: Misc', function() { describe('API: Misc', function() {
it('listPadsOfAuthor', function(done) { it('listPadsOfAuthor', async function() {
api.get(endPoint('listPadsOfAuthor')+"&authorID="+authorID) await api.get(endPoint('listPadsOfAuthor') + `&authorID=${authorID}`)
.expect(function(res){ .expect(200)
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((res) => {
.expect('Content-Type', /json/) assert.equal(res.body.code, 0);
.expect(200, done) assert.equal(res.body.data.padIDs.length, 0);
});
}); });
}); });
const endPoint = function(point) { const endPoint = function(point) {
return '/api/'+apiVersion+'/'+point+'?apikey='+apiKey; return `/api/${apiVersion}/${point}?apikey=${apiKey}`;
} };
function makeid() function makeid()
{ {