From 7fed637506043e4432cbebe041ada0625171cceb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 22 Apr 2020 16:07:04 +0200 Subject: Begin auth plugin support --- server/tests/api/users/users.ts | 14 +++-- .../peertube-plugin-test-id-pass-auth-one/main.js | 61 +++++++++++++++++++ .../package.json | 20 +++++++ .../main.js | 37 ++++++++++++ .../package.json | 20 +++++++ .../peertube-plugin-test-id-pass-auth-two/main.js | 36 +++++++++++ .../package.json | 20 +++++++ server/tests/plugins/id-and-pass-auth.ts | 69 ++++++++++++++++++++++ server/tests/plugins/index.ts | 1 + 9 files changed, 274 insertions(+), 4 deletions(-) create mode 100644 server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js create mode 100644 server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/package.json create mode 100644 server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/main.js create mode 100644 server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/package.json create mode 100644 server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/main.js create mode 100644 server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/package.json create mode 100644 server/tests/plugins/id-and-pass-auth.ts (limited to 'server/tests') diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index db82e8fc2..7ba04a4ca 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -40,7 +40,7 @@ import { getVideoAbusesList, updateCustomSubConfig, getCustomConfig, waitJobs } from '../../../../shared/extra-utils' import { follow } from '../../../../shared/extra-utils/server/follows' -import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login' +import { setAccessTokensToServers, logout } from '../../../../shared/extra-utils/users/login' import { getMyVideos } from '../../../../shared/extra-utils/videos/videos' import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model' import { CustomConfig } from '@shared/models/server' @@ -205,11 +205,17 @@ describe('Test users', function () { }) describe('Logout', function () { - it('Should logout (revoke token)') + it('Should logout (revoke token)', async function () { + await logout(server.url, server.accessToken) + }) - it('Should not be able to get the user information') + it('Should not be able to get the user information', async function () { + await getMyUserInformation(server.url, server.accessToken, 401) + }) - it('Should not be able to upload a video') + it('Should not be able to upload a video', async function () { + await uploadVideo(server.url, server.accessToken, { name: 'video' }, 401) + }) it('Should not be able to remove a video') diff --git a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js new file mode 100644 index 000000000..4755ed643 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/main.js @@ -0,0 +1,61 @@ +async function register ({ + registerIdAndPassAuth, + peertubeHelpers +}) { + registerIdAndPassAuth({ + type: 'id-and-pass', + + onLogout: () => { + peertubeHelpers.logger.info('On logout for auth 1 - 1') + }, + + getWeight: () => 15, + + login (body) { + if (body.id === 'spyro' && body.password === 'spyro password') { + return Promise.resolve({ + username: 'spyro', + email: 'spyro@example.com', + role: 0, + displayName: 'Spyro the Dragon' + }) + } + + return null + } + }) + + registerIdAndPassAuth({ + type: 'id-and-pass', + + onLogout: () => { + peertubeHelpers.logger.info('On logout for auth 1 - 2') + }, + + getWeight: () => 50, + + login (body) { + if (body.id === 'crash' && body.password === 'crash password') { + return Promise.resolve({ + username: 'crash', + email: 'crash@example.com', + role: 2, + displayName: 'Crash Bandicoot' + }) + } + + return null + } + }) +} + +async function unregister () { + return +} + +module.exports = { + register, + unregister +} + +// ########################################################################### diff --git a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/package.json b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/package.json new file mode 100644 index 000000000..f8ad18a90 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-one/package.json @@ -0,0 +1,20 @@ +{ + "name": "peertube-plugin-test-id-pass-auth-one", + "version": "0.0.1", + "description": "Id and pass auth one", + "engine": { + "peertube": ">=1.3.0" + }, + "keywords": [ + "peertube", + "plugin" + ], + "homepage": "https://github.com/Chocobozzz/PeerTube", + "author": "Chocobozzz", + "bugs": "https://github.com/Chocobozzz/PeerTube/issues", + "library": "./main.js", + "staticDirs": {}, + "css": [], + "clientScripts": [], + "translations": {} +} diff --git a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/main.js b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/main.js new file mode 100644 index 000000000..2a15b3754 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/main.js @@ -0,0 +1,37 @@ +async function register ({ + registerIdAndPassAuth, + peertubeHelpers +}) { + registerIdAndPassAuth({ + type: 'id-and-pass', + + onLogout: () => { + peertubeHelpers.logger.info('On logout for auth 3 - 1') + }, + + getWeight: () => 5, + + login (body) { + if (body.id === 'laguna' && body.password === 'laguna password') { + return Promise.resolve({ + username: 'laguna', + email: 'laguna@example.com', + displayName: 'Laguna Loire' + }) + } + + return null + } + }) +} + +async function unregister () { + return +} + +module.exports = { + register, + unregister +} + +// ########################################################################### diff --git a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/package.json b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/package.json new file mode 100644 index 000000000..f9f107b1a --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-three/package.json @@ -0,0 +1,20 @@ +{ + "name": "peertube-plugin-test-id-pass-auth-three", + "version": "0.0.1", + "description": "Id and pass auth three", + "engine": { + "peertube": ">=1.3.0" + }, + "keywords": [ + "peertube", + "plugin" + ], + "homepage": "https://github.com/Chocobozzz/PeerTube", + "author": "Chocobozzz", + "bugs": "https://github.com/Chocobozzz/PeerTube/issues", + "library": "./main.js", + "staticDirs": {}, + "css": [], + "clientScripts": [], + "translations": {} +} diff --git a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/main.js b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/main.js new file mode 100644 index 000000000..edfc870c0 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/main.js @@ -0,0 +1,36 @@ +async function register ({ + registerIdAndPassAuth, + peertubeHelpers +}) { + registerIdAndPassAuth({ + type: 'id-and-pass', + + onLogout: () => { + peertubeHelpers.logger.info('On logout for auth 2 - 1') + }, + + getWeight: () => 30, + + login (body) { + if (body.id === 'laguna' && body.password === 'laguna password') { + return Promise.resolve({ + username: 'laguna', + email: 'laguna@example.com' + }) + } + + return null + } + }) +} + +async function unregister () { + return +} + +module.exports = { + register, + unregister +} + +// ########################################################################### diff --git a/server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/package.json b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/package.json new file mode 100644 index 000000000..5df15fac1 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-id-pass-auth-two/package.json @@ -0,0 +1,20 @@ +{ + "name": "peertube-plugin-test-id-pass-auth-two", + "version": "0.0.1", + "description": "Id and pass auth two", + "engine": { + "peertube": ">=1.3.0" + }, + "keywords": [ + "peertube", + "plugin" + ], + "homepage": "https://github.com/Chocobozzz/PeerTube", + "author": "Chocobozzz", + "bugs": "https://github.com/Chocobozzz/PeerTube/issues", + "library": "./main.js", + "staticDirs": {}, + "css": [], + "clientScripts": [], + "translations": {} +} diff --git a/server/tests/plugins/id-and-pass-auth.ts b/server/tests/plugins/id-and-pass-auth.ts new file mode 100644 index 000000000..5b4d1a1db --- /dev/null +++ b/server/tests/plugins/id-and-pass-auth.ts @@ -0,0 +1,69 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' +import { getPluginTestPath, installPlugin, setAccessTokensToServers } from '../../../shared/extra-utils' + +describe('Test id and pass auth plugins', function () { + let server: ServerInfo + + before(async function () { + this.timeout(30000) + + server = await flushAndRunServer(1) + await setAccessTokensToServers([ server ]) + + await installPlugin({ + url: server.url, + accessToken: server.accessToken, + path: getPluginTestPath('-id-pass-auth-one') + }) + + await installPlugin({ + url: server.url, + accessToken: server.accessToken, + path: getPluginTestPath('-id-pass-auth-two') + }) + }) + + it('Should not login', async function() { + + }) + + it('Should login Spyro, create the user and use the token', async function() { + + }) + + it('Should login Crash, create the user and use the token', async function() { + + }) + + it('Should login the first Laguna, create the user and use the token', async function() { + + }) + + it('Should update Crash profile', async function () { + + }) + + it('Should logout Crash', async function () { + + // test token + }) + + it('Should have logged the Crash logout', async function () { + + }) + + it('Should login Crash and keep the old existing profile', async function () { + + }) + + it('Should uninstall the plugin one and do not login existing Crash', async function () { + + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/plugins/index.ts b/server/tests/plugins/index.ts index 1414e7e58..8aa30654a 100644 --- a/server/tests/plugins/index.ts +++ b/server/tests/plugins/index.ts @@ -1,4 +1,5 @@ import './action-hooks' +import './id-and-pass-auth' import './filter-hooks' import './translations' import './video-constants' -- cgit v1.2.3