From 60b880acdfa85eab5c9ec09ba1283f82ae58ec85 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 30 Dec 2022 10:12:20 +0100 Subject: External auth can update user on login --- .../peertube-plugin-test-external-auth-one/main.js | 9 +++++- .../peertube-plugin-test-id-pass-auth-two/main.js | 13 ++++++++- server/tests/plugins/external-auth.ts | 34 ++++++++++++++++++++++ server/tests/plugins/id-and-pass-auth.ts | 34 ++++++++++++++++++++-- 4 files changed, 86 insertions(+), 4 deletions(-) (limited to 'server/tests') diff --git a/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js b/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js index cdbaf11ac..58bc27661 100644 --- a/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js +++ b/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js @@ -36,7 +36,14 @@ async function register ({ displayName: 'Kefka Palazzo', adminFlags: 1, videoQuota: 42000, - videoQuotaDaily: 42100 + videoQuotaDaily: 42100, + + // Always use new value except for videoQuotaDaily field + userUpdater: ({ fieldName, currentValue, newValue }) => { + if (fieldName === 'videoQuotaDaily') return currentValue + + return newValue + } }) }, hookTokenValidity: (options) => { 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 index ceab7b60d..fad5abf60 100644 --- 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 @@ -33,7 +33,18 @@ async function register ({ if (body.id === 'laguna' && body.password === 'laguna password') { return Promise.resolve({ username: 'laguna', - email: 'laguna@example.com' + email: 'laguna@example.com', + displayName: 'Laguna Loire', + adminFlags: 1, + videoQuota: 42000, + videoQuotaDaily: 42100, + + // Always use new value except for videoQuotaDaily field + userUpdater: ({ fieldName, currentValue, newValue }) => { + if (fieldName === 'videoQuotaDaily') return currentValue + + return newValue + } }) } diff --git a/server/tests/plugins/external-auth.ts b/server/tests/plugins/external-auth.ts index ee78ae5aa..e600f958f 100644 --- a/server/tests/plugins/external-auth.ts +++ b/server/tests/plugins/external-auth.ts @@ -51,6 +51,7 @@ describe('Test external auth plugins', function () { let kefkaAccessToken: string let kefkaRefreshToken: string + let kefkaId: number let externalAuthToken: string @@ -184,6 +185,8 @@ describe('Test external auth plugins', function () { expect(body.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST) expect(body.videoQuota).to.equal(42000) expect(body.videoQuotaDaily).to.equal(42100) + + kefkaId = body.id } }) @@ -246,6 +249,37 @@ describe('Test external auth plugins', function () { expect(body.role.id).to.equal(UserRole.USER) }) + it('Should login Kefka and update the profile', async function () { + { + await server.users.update({ userId: kefkaId, videoQuota: 43000, videoQuotaDaily: 43100 }) + await server.users.updateMe({ token: kefkaAccessToken, displayName: 'kefka updated' }) + + const body = await server.users.getMyInfo({ token: kefkaAccessToken }) + expect(body.username).to.equal('kefka') + expect(body.account.displayName).to.equal('kefka updated') + expect(body.videoQuota).to.equal(43000) + expect(body.videoQuotaDaily).to.equal(43100) + } + + { + const res = await loginExternal({ + server, + npmName: 'test-external-auth-one', + authName: 'external-auth-2', + username: 'kefka' + }) + + kefkaAccessToken = res.access_token + kefkaRefreshToken = res.refresh_token + + const body = await server.users.getMyInfo({ token: kefkaAccessToken }) + expect(body.username).to.equal('kefka') + expect(body.account.displayName).to.equal('Kefka Palazzo') + expect(body.videoQuota).to.equal(42000) + expect(body.videoQuotaDaily).to.equal(43100) + } + }) + it('Should not update an external auth email', async function () { await server.users.updateMe({ token: cyanAccessToken, diff --git a/server/tests/plugins/id-and-pass-auth.ts b/server/tests/plugins/id-and-pass-auth.ts index fc24a5656..10155c28b 100644 --- a/server/tests/plugins/id-and-pass-auth.ts +++ b/server/tests/plugins/id-and-pass-auth.ts @@ -13,6 +13,7 @@ describe('Test id and pass auth plugins', function () { let lagunaAccessToken: string let lagunaRefreshToken: string + let lagunaId: number before(async function () { this.timeout(30000) @@ -78,8 +79,10 @@ describe('Test id and pass auth plugins', function () { const body = await server.users.getMyInfo({ token: lagunaAccessToken }) expect(body.username).to.equal('laguna') - expect(body.account.displayName).to.equal('laguna') + expect(body.account.displayName).to.equal('Laguna Loire') expect(body.role.id).to.equal(UserRole.USER) + + lagunaId = body.id } }) @@ -132,6 +135,33 @@ describe('Test id and pass auth plugins', function () { expect(body.role.id).to.equal(UserRole.MODERATOR) }) + it('Should login Laguna and update the profile', async function () { + { + await server.users.update({ userId: lagunaId, videoQuota: 43000, videoQuotaDaily: 43100 }) + await server.users.updateMe({ token: lagunaAccessToken, displayName: 'laguna updated' }) + + const body = await server.users.getMyInfo({ token: lagunaAccessToken }) + expect(body.username).to.equal('laguna') + expect(body.account.displayName).to.equal('laguna updated') + expect(body.videoQuota).to.equal(43000) + expect(body.videoQuotaDaily).to.equal(43100) + } + + { + const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } }) + lagunaAccessToken = body.access_token + lagunaRefreshToken = body.refresh_token + } + + { + const body = await server.users.getMyInfo({ token: lagunaAccessToken }) + expect(body.username).to.equal('laguna') + expect(body.account.displayName).to.equal('Laguna Loire') + expect(body.videoQuota).to.equal(42000) + expect(body.videoQuotaDaily).to.equal(43100) + } + }) + it('Should reject token of laguna by the plugin hook', async function () { this.timeout(10000) @@ -147,7 +177,7 @@ describe('Test id and pass auth plugins', function () { await server.servers.waitUntilLog('valid username') await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) - await server.servers.waitUntilLog('valid display name') + await server.servers.waitUntilLog('valid displayName') await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await server.servers.waitUntilLog('valid role') -- cgit v1.2.3