Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

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

Meisam542ef8000b65
diff --git a/test/automated/api/chat.test.js b/test/automated/api/chat.test.js
index 9830ae060f..93faa33bb9 100644
--- a/test/automated/api/chat.test.js
+++ b/test/automated/api/chat.test.js
@@ -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,
diff --git a/test/automated/api/chatmoderation.test.js b/test/automated/api/chatmoderation.test.js
index 05b37f5258..c11b960128 100644
--- a/test/automated/api/chatmoderation.test.js
+++ b/test/automated/api/chatmoderation.test.js
@@ -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',
};
diff --git a/test/automated/api/chatusers.test.js b/test/automated/api/chatusers.test.js
index 88115100da..0a255c2ed7 100644
--- a/test/automated/api/chatusers.test.js
+++ b/test/automated/api/chatusers.test.js
@@ -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',
};
diff --git a/test/automated/api/configmanagement.test.js b/test/automated/api/configmanagement.test.js
index ba7a09ba63..0307dbcd94 100644
--- a/test/automated/api/configmanagement.test.js
+++ b/test/automated/api/configmanagement.test.js
@@ -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);
-}
diff --git a/test/automated/api/lib/rand.js b/test/automated/api/lib/rand.js
new file mode 100644
index 0000000000..3b45367505
--- /dev/null
+++ b/test/automated/api/lib/rand.js
@@ -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;
diff --git a/test/automated/api/publicstatic.test.js b/test/automated/api/publicstatic.test.js
index 38df15979a..189618cf9a 100644
--- a/test/automated/api/publicstatic.test.js
+++ b/test/automated/api/publicstatic.test.js
@@ -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),