tests: refactor to use async

pull/5853/head
webzwo0i 2023-07-29 21:41:42 +02:00
parent cd6da7335b
commit 92f8610085
2 changed files with 43 additions and 40 deletions

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const common = require('../../common'); const common = require('../../common');
const assert = require('assert').strict;
let agent; let agent;
const apiKey = common.apiKey; const apiKey = common.apiKey;
@ -15,14 +16,14 @@ describe(__filename, function () {
before(async function () { agent = await common.init(); }); before(async function () { agent = await common.init(); });
describe('API Versioning', function () { describe('API Versioning', function () {
it('errors if can not connect', function (done) { it('errors if can not connect', async function () {
agent.get('/api/') await agent.get('/api/')
.expect((res) => { .expect((res) => {
apiVersion = res.body.currentVersion; apiVersion = res.body.currentVersion;
if (!res.body.currentVersion) throw new Error('No version set in API'); if (!res.body.currentVersion) throw new Error('No version set in API');
return; return;
}) })
.expect(200, done); .expect(200);
}); });
}); });
@ -38,20 +39,18 @@ describe(__filename, function () {
-> getChatHistory(padID) -> getChatHistory(padID)
*/ */
describe('createPad', function () { describe('Chat functionality', function () {
it('creates a new Pad', function (done) { it('creates a new Pad', async function () {
agent.get(`${endPoint('createPad')}&padID=${padID}`) await agent.get(`${endPoint('createPad')}&padID=${padID}`)
.expect((res) => { .expect((res) => {
if (res.body.code !== 0) throw new Error('Unable to create new Pad'); if (res.body.code !== 0) throw new Error('Unable to create new Pad');
}) })
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200, done); .expect(200);
}); });
});
describe('createAuthor', function () { it('Creates an author with a name set', async function () {
it('Creates an author with a name set', function (done) { await agent.get(endPoint('createAuthor'))
agent.get(endPoint('createAuthor'))
.expect((res) => { .expect((res) => {
if (res.body.code !== 0 || !res.body.data.authorID) { if (res.body.code !== 0 || !res.body.data.authorID) {
throw new Error('Unable to create author'); throw new Error('Unable to create author');
@ -59,47 +58,51 @@ describe(__filename, function () {
authorID = res.body.data.authorID; // we will be this author for the rest of the tests authorID = res.body.data.authorID; // we will be this author for the rest of the tests
}) })
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200, done); .expect(200);
}); });
});
describe('appendChatMessage', function () { it('Gets the head of chat before the first chat msg', async function () {
it('Adds a chat message to the pad', function (done) { await agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` + .expect((res) => {
if (res.body.data.chatHead !== -1) throw new Error('Chat Head Length is wrong');
if (res.body.code !== 0) throw new Error('Unable to get chat head');
})
.expect('Content-Type', /json/)
.expect(200);
});
it('Adds a chat message to the pad', async function () {
await agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` +
`&authorID=${authorID}&time=${timestamp}`) `&authorID=${authorID}&time=${timestamp}`)
.expect((res) => { .expect((res) => {
if (res.body.code !== 0) throw new Error('Unable to create chat message'); if (res.body.code !== 0) throw new Error('Unable to create chat message');
}) })
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200, done); .expect(200);
}); });
});
it('Gets the head of chat', async function () {
describe('getChatHead', function () { await agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
it('Gets the head of chat', function (done) {
agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
.expect((res) => { .expect((res) => {
if (res.body.data.chatHead !== 0) throw new Error('Chat Head Length is wrong'); if (res.body.data.chatHead !== 0) throw new Error('Chat Head Length is wrong');
if (res.body.code !== 0) throw new Error('Unable to get chat head'); if (res.body.code !== 0) throw new Error('Unable to get chat head');
}) })
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200, done); .expect(200);
}); });
});
describe('getChatHistory', function () { it('Gets Chat History of a Pad', async function () {
it('Gets Chat History of a Pad', function (done) { await agent.get(`${endPoint('getChatHistory')}&padID=${padID}`)
agent.get(`${endPoint('getChatHistory')}&padID=${padID}`)
.expect((res) => {
if (res.body.data.messages.length !== 1) {
throw new Error('Chat History Length is wrong');
}
if (res.body.code !== 0) throw new Error('Unable to get chat history');
})
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200, done); .expect(200)
.expect((res) => {
assert.equal(res.body.code, 0, 'Unable to get chat history');
assert.equal(res.body.data.messages.length, 1, 'Chat History Length is wrong');
assert.equal(res.body.data.messages[0].text, 'blalblalbha', 'Chat text does not match');
assert.equal(res.body.data.messages[0].userId, authorID, 'Message author does not match');
assert.equal(res.body.data.messages[0].time, timestamp.toString(), 'Message time does not match');
});
}); });
}); });
}); });

View File

@ -17,16 +17,16 @@ describe(__filename, function () {
before(async function () { agent = await common.init(); }); before(async function () { agent = await common.init(); });
describe('Connectivity for instance-level API tests', function () { describe('Connectivity for instance-level API tests', function () {
it('can connect', function (done) { it('can connect', async function () {
agent.get('/api/') await agent.get('/api/')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200, done); .expect(200);
}); });
}); });
describe('getStats', function () { describe('getStats', function () {
it('Gets the stats of a running instance', function (done) { it('Gets the stats of a running instance', async function () {
agent.get(endPoint('getStats')) await agent.get(endPoint('getStats'))
.expect((res) => { .expect((res) => {
if (res.body.code !== 0) throw new Error('getStats() failed'); if (res.body.code !== 0) throw new Error('getStats() failed');
@ -48,7 +48,7 @@ describe(__filename, function () {
} }
}) })
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200, done); .expect(200);
}); });
}); });
}); });