mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-04-08 10:41:30 +03:00
Added backend tests and made authentication more testable
This commit is contained in:
@@ -144,16 +144,18 @@ exports.registerUser = function(req, res) {
|
|||||||
************************************************/
|
************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
exports.login = async (username, password) => {
|
||||||
|
const user = users_db.get('users').find({name: username}).value();
|
||||||
|
if (!user) { logger.error(`User ${username} not found`); false }
|
||||||
|
if (user.auth_method && user.auth_method !== 'internal') { return false }
|
||||||
|
return await bcrypt.compare(password, user.passhash) ? user : false;
|
||||||
|
}
|
||||||
|
|
||||||
exports.passport.use(new LocalStrategy({
|
exports.passport.use(new LocalStrategy({
|
||||||
usernameField: 'username',
|
usernameField: 'username',
|
||||||
passwordField: 'password'},
|
passwordField: 'password'},
|
||||||
async function(username, password, done) {
|
async function(username, password, done) {
|
||||||
const user = users_db.get('users').find({name: username}).value();
|
return done(null, await exports.login(username, password));
|
||||||
if (!user) { logger.error(`User ${username} not found`); return done(null, false); }
|
|
||||||
if (user.auth_method && user.auth_method !== 'internal') { return done(null, false); }
|
|
||||||
if (user) {
|
|
||||||
return done(null, (await bcrypt.compare(password, user.passhash)) ? user : false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|||||||
1159
backend/package-lock.json
generated
1159
backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -44,6 +44,7 @@
|
|||||||
"lowdb": "^1.0.0",
|
"lowdb": "^1.0.0",
|
||||||
"md5": "^2.2.1",
|
"md5": "^2.2.1",
|
||||||
"merge-files": "^0.1.2",
|
"merge-files": "^0.1.2",
|
||||||
|
"mocha": "^8.4.0",
|
||||||
"moment": "^2.29.1",
|
"moment": "^2.29.1",
|
||||||
"multer": "^1.4.2",
|
"multer": "^1.4.2",
|
||||||
"node-fetch": "^2.6.1",
|
"node-fetch": "^2.6.1",
|
||||||
|
|||||||
94
backend/test/tests.js
Normal file
94
backend/test/tests.js
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
var assert = require('assert');
|
||||||
|
const low = require('lowdb')
|
||||||
|
var winston = require('winston');
|
||||||
|
|
||||||
|
process.chdir('./backend')
|
||||||
|
|
||||||
|
const FileSync = require('lowdb/adapters/FileSync');
|
||||||
|
|
||||||
|
const adapter = new FileSync('./appdata/db.json');
|
||||||
|
const db = low(adapter)
|
||||||
|
|
||||||
|
const users_adapter = new FileSync('./appdata/users.json');
|
||||||
|
const users_db = low(users_adapter);
|
||||||
|
|
||||||
|
const defaultFormat = winston.format.printf(({ level, message, label, timestamp }) => {
|
||||||
|
return `${timestamp} ${level.toUpperCase()}: ${message}`;
|
||||||
|
});
|
||||||
|
|
||||||
|
let debugMode = process.env.YTDL_MODE === 'debug';
|
||||||
|
|
||||||
|
const logger = winston.createLogger({
|
||||||
|
level: 'info',
|
||||||
|
format: winston.format.combine(winston.format.timestamp(), defaultFormat),
|
||||||
|
defaultMeta: {},
|
||||||
|
transports: [
|
||||||
|
//
|
||||||
|
// - Write to all logs with level `info` and below to `combined.log`
|
||||||
|
// - Write all logs error (and below) to `error.log`.
|
||||||
|
//
|
||||||
|
new winston.transports.File({ filename: 'appdata/logs/error.log', level: 'error' }),
|
||||||
|
new winston.transports.File({ filename: 'appdata/logs/combined.log' }),
|
||||||
|
new winston.transports.Console({level: !debugMode ? 'info' : 'debug', name: 'console'})
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
var auth_api = require('../authentication/auth');
|
||||||
|
var db_api = require('../db');
|
||||||
|
|
||||||
|
db_api.initialize(db, users_db, logger);
|
||||||
|
auth_api.initialize(db, users_db, logger);
|
||||||
|
|
||||||
|
describe('Multi User', async function() {
|
||||||
|
let user = null;
|
||||||
|
const user_to_test = 'admin';
|
||||||
|
before(async function() {
|
||||||
|
user = await auth_api.login('admin', 'pass');
|
||||||
|
console.log('hi')
|
||||||
|
});
|
||||||
|
describe('Authentication', function() {
|
||||||
|
it('login', async function() {
|
||||||
|
assert(user);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('Video player - normal', function() {
|
||||||
|
const video_to_test = 'ebbcfffb-d6f1-4510-ad25-d1ec82e0477e';
|
||||||
|
it('Get video', async function() {
|
||||||
|
const video_obj = db_api.getVideo(video_to_test, 'admin');
|
||||||
|
assert(video_obj);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Video access - disallowed', async function() {
|
||||||
|
await db_api.setVideoProperty(video_to_test, {sharingEnabled: false}, user_to_test);
|
||||||
|
const video_obj = auth_api.getUserVideo('admin', video_to_test, true);
|
||||||
|
assert(!video_obj);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Video access - allowed', async function() {
|
||||||
|
await db_api.setVideoProperty(video_to_test, {sharingEnabled: true}, user_to_test);
|
||||||
|
const video_obj = auth_api.getUserVideo('admin', video_to_test, true);
|
||||||
|
assert(video_obj);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
// describe('Video player - subscription', function() {
|
||||||
|
// const sub_to_test = '';
|
||||||
|
// const video_to_test = 'ebbcfffb-d6f1-4510-ad25-d1ec82e0477e';
|
||||||
|
// it('Get video', async function() {
|
||||||
|
// const video_obj = db_api.getVideo(video_to_test, 'admin', );
|
||||||
|
// assert(video_obj);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('Video access - disallowed', async function() {
|
||||||
|
// await db_api.setVideoProperty(video_to_test, {sharingEnabled: false}, user_to_test, sub_to_test);
|
||||||
|
// const video_obj = auth_api.getUserVideo('admin', video_to_test, true);
|
||||||
|
// assert(!video_obj);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('Video access - allowed', async function() {
|
||||||
|
// await db_api.setVideoProperty(video_to_test, {sharingEnabled: true}, user_to_test, sub_to_test);
|
||||||
|
// const video_obj = auth_api.getUserVideo('admin', video_to_test, true);
|
||||||
|
// assert(video_obj);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user