0

use randomString/Number from lib in api tests (#2542)

This commit is contained in:
Meisam 2023-01-04 23:09:51 +01:00 committed by GitHub
parent 2607d90094
commit dad5b28cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 22 deletions

View File

@ -5,9 +5,10 @@ request = request('http://127.0.0.1:8080');
const registerChat = require('./lib/chat').registerChat;
const sendChatMessage = require('./lib/chat').sendChatMessage;
const getAdminResponse = require('./lib/admin').getAdminResponse;
const randomNumber = require('./lib/rand').randomNumber;
var userDisplayName;
const message = Math.floor(Math.random() * 100) + ' test 123';
const message = randomNumber(100) + ' test 123';
const testMessage = {
body: message,

View File

@ -8,9 +8,10 @@ const sendChatMessage = require('./lib/chat').sendChatMessage;
const getAdminResponse = require('./lib/admin').getAdminResponse;
const sendAdminPayload = require('./lib/admin').sendAdminPayload;
const sendAdminRequest = require('./lib/admin').sendAdminRequest;
const randomNumber = require('./lib/rand').randomNumber;
const testVisibilityMessage = {
body: 'message ' + Math.floor(Math.random() * 100),
body: 'message ' + randomNumber(100),
type: 'CHAT',
};

View File

@ -9,13 +9,13 @@ const sendChatMessage = require('./lib/chat').sendChatMessage;
const sendAdminRequest = require('./lib/admin').sendAdminRequest;
const sendAdminPayload = require('./lib/admin').sendAdminPayload;
const getAdminResponse = require('./lib/admin').getAdminResponse;
const randomNumber = require('./lib/rand').randomNumber;
const localIPAddressV4 = '127.0.0.1';
const localIPAddressV6 = '::1';
const testVisibilityMessage = {
body: 'message ' + Math.floor(Math.random() * 100),
body: 'message ' + randomNumber(100),
type: 'CHAT',
};

View File

@ -1,10 +1,10 @@
var request = require('supertest');
const Random = require('crypto-random');
const sendAdminRequest = require('./lib/admin').sendAdminRequest;
const failAdminRequest = require('./lib/admin').failAdminRequest;
const getAdminResponse = require('./lib/admin').getAdminResponse;
const randomString = require('./lib/rand').randomString;
const randomNumber = require('./lib/rand').randomNumber;
request = request('http://127.0.0.1:8080');
@ -82,7 +82,7 @@ const newStreamKeys = [
];
const newAdminPassword = randomString();
const latencyLevel = Random.range(0, 4);
const latencyLevel = randomNumber(4);
const appearanceValues = {
variable1: randomString(),
variable2: randomString(),
@ -457,10 +457,3 @@ test('verify frontend status', (done) => {
});
});
function randomString(length = 20) {
return Random.value().toString(16).substr(2, length);
}
function randomNumber() {
return Random.range(0, 5);
}

View File

@ -0,0 +1,16 @@
const crypto = require('crypto');
const Random = require('crypto-random');
//returns a random number in range of [1 - max]
function randomNumber(max = 5) {
return Random.range(1, max);
}
//returns a random hex string
function randomString(length = 20) {
const bytesCount = Math.ceil(length / 2);
return crypto.randomBytes(bytesCount).toString('hex').substring(0, length);
}
module.exports.randomNumber = randomNumber;
module.exports.randomString = randomString;

View File

@ -2,11 +2,11 @@ var request = require('supertest');
request = request('http://127.0.0.1:8080');
const fs = require('fs');
const path = require('path');
const randomString = require('./lib/rand').randomString;
const publicPath = path.resolve(__dirname, '../../../public');
const filename = randomString(20) + '.txt';
const fileContent = randomString(8);
const filename = randomString() + '.txt';
const fileContent = randomString();
test('random public static file does not exist', async (done) => {
request.get('/public/' + filename).expect(404);
@ -49,11 +49,6 @@ test('public static file is persistent and not locked', async (done) => {
done();
});
function randomString(length) {
return Math.random().toString(36).substr(2, length);
}
function writeFileToPublic() {
fs.writeFileSync(
path.join(publicPath, filename),