]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/plugins/external-auth.ts
External auth can update user on login
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / external-auth.ts
index c0834a14c5d840723b3347e18575fe5ffa3aeed3..e600f958f89bb68657713cadbf64f17387d2abc0 100644 (file)
@@ -1,18 +1,16 @@
 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import 'mocha'
 import { expect } from 'chai'
-import { HttpStatusCode } from '@shared/core-utils'
+import { wait } from '@shared/core-utils'
+import { HttpStatusCode, UserAdminFlag, UserRole } from '@shared/models'
 import {
   cleanupTests,
-  decodeQueryString,
   createSingleServer,
-  PluginsCommand,
+  decodeQueryString,
   PeerTubeServer,
-  setAccessTokensToServers,
-  wait
-} from '@shared/extra-utils'
-import { UserRole } from '@shared/models'
+  PluginsCommand,
+  setAccessTokensToServers
+} from '@shared/server-commands'
 
 async function loginExternal (options: {
   server: PeerTubeServer
@@ -20,15 +18,15 @@ async function loginExternal (options: {
   authName: string
   username: string
   query?: any
-  statusCodeExpected?: HttpStatusCode
-  statusCodeExpectedStep2?: HttpStatusCode
+  expectedStatus?: HttpStatusCode
+  expectedStatusStep2?: HttpStatusCode
 }) {
   const res = await options.server.plugins.getExternalAuth({
     npmName: options.npmName,
     npmVersion: '0.0.1',
     authName: options.authName,
     query: options.query,
-    expectedStatus: options.statusCodeExpected || HttpStatusCode.FOUND_302
+    expectedStatus: options.expectedStatus || HttpStatusCode.FOUND_302
   })
 
   if (res.status !== HttpStatusCode.FOUND_302) return
@@ -39,7 +37,7 @@ async function loginExternal (options: {
   const resLogin = await options.server.login.loginUsingExternalToken({
     username: options.username,
     externalAuthToken: externalAuthToken as string,
-    expectedStatus: options.statusCodeExpectedStep2
+    expectedStatus: options.expectedStatusStep2
   })
 
   return resLogin.body
@@ -53,13 +51,21 @@ describe('Test external auth plugins', function () {
 
   let kefkaAccessToken: string
   let kefkaRefreshToken: string
+  let kefkaId: number
 
   let externalAuthToken: string
 
   before(async function () {
     this.timeout(30000)
 
-    server = await createSingleServer(1)
+    server = await createSingleServer(1, {
+      rates_limit: {
+        login: {
+          max: 30
+        }
+      }
+    })
+
     await setAccessTokensToServers([ server ])
 
     for (const suffix of [ 'one', 'two', 'three' ]) {
@@ -71,7 +77,7 @@ describe('Test external auth plugins', function () {
     const config = await server.config.getConfig()
 
     const auths = config.plugin.registeredExternalAuths
-    expect(auths).to.have.lengthOf(8)
+    expect(auths).to.have.lengthOf(9)
 
     const auth2 = auths.find((a) => a.authName === 'external-auth-2')
     expect(auth2).to.exist
@@ -126,7 +132,7 @@ describe('Test external auth plugins', function () {
       expectedStatus: HttpStatusCode.BAD_REQUEST_400
     })
 
-    await server.servers.waitUntilLog('expired external auth token', 2)
+    await server.servers.waitUntilLog('expired external auth token', 4)
   })
 
   it('Should auto login Cyan, create the user and use the token', async function () {
@@ -150,7 +156,10 @@ describe('Test external auth plugins', function () {
       expect(body.username).to.equal('cyan')
       expect(body.account.displayName).to.equal('cyan')
       expect(body.email).to.equal('cyan@example.com')
-      expect(body.role).to.equal(UserRole.USER)
+      expect(body.role.id).to.equal(UserRole.USER)
+      expect(body.adminFlags).to.equal(UserAdminFlag.NONE)
+      expect(body.videoQuota).to.equal(5242880)
+      expect(body.videoQuotaDaily).to.equal(-1)
     }
   })
 
@@ -172,7 +181,12 @@ describe('Test external auth plugins', function () {
       expect(body.username).to.equal('kefka')
       expect(body.account.displayName).to.equal('Kefka Palazzo')
       expect(body.email).to.equal('kefka@example.com')
-      expect(body.role).to.equal(UserRole.ADMINISTRATOR)
+      expect(body.role.id).to.equal(UserRole.ADMINISTRATOR)
+      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
     }
   })
 
@@ -232,7 +246,38 @@ describe('Test external auth plugins', function () {
     expect(body.username).to.equal('cyan')
     expect(body.account.displayName).to.equal('Cyan Garamonde')
     expect(body.account.description).to.equal('Retainer to the king of Doma')
-    expect(body.role).to.equal(UserRole.USER)
+    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 () {
@@ -268,7 +313,7 @@ describe('Test external auth plugins', function () {
         username: 'kefka'
       },
       username: 'kefka',
-      statusCodeExpected: HttpStatusCode.NOT_FOUND_404
+      expectedStatus: HttpStatusCode.NOT_FOUND_404
     })
   })
 
@@ -276,7 +321,7 @@ describe('Test external auth plugins', function () {
     const config = await server.config.getConfig()
 
     const auths = config.plugin.registeredExternalAuths
-    expect(auths).to.have.lengthOf(7)
+    expect(auths).to.have.lengthOf(8)
 
     const auth1 = auths.find(a => a.authName === 'external-auth-2')
     expect(auth1).to.not.exist
@@ -293,7 +338,7 @@ describe('Test external auth plugins', function () {
         username: 'cyan'
       },
       username: 'cyan',
-      statusCodeExpected: HttpStatusCode.NOT_FOUND_404
+      expectedStatus: HttpStatusCode.NOT_FOUND_404
     })
 
     await server.login.login({ user: { username: 'cyan', password: null }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
@@ -307,7 +352,7 @@ describe('Test external auth plugins', function () {
       npmName: 'test-external-auth-two',
       authName: 'external-auth-4',
       username: 'kefka2',
-      statusCodeExpectedStep2: HttpStatusCode.BAD_REQUEST_400
+      expectedStatusStep2: HttpStatusCode.BAD_REQUEST_400
     })
 
     await loginExternal({
@@ -315,11 +360,11 @@ describe('Test external auth plugins', function () {
       npmName: 'test-external-auth-two',
       authName: 'external-auth-4',
       username: 'kefka',
-      statusCodeExpectedStep2: HttpStatusCode.BAD_REQUEST_400
+      expectedStatusStep2: HttpStatusCode.BAD_REQUEST_400
     })
   })
 
-  it('Should not login an existing user', async function () {
+  it('Should not login an existing user email', async function () {
     await server.users.create({ username: 'existing_user', password: 'super_password' })
 
     await loginExternal({
@@ -327,15 +372,37 @@ describe('Test external auth plugins', function () {
       npmName: 'test-external-auth-two',
       authName: 'external-auth-6',
       username: 'existing_user',
-      statusCodeExpectedStep2: HttpStatusCode.BAD_REQUEST_400
+      expectedStatusStep2: HttpStatusCode.BAD_REQUEST_400
     })
   })
 
+  it('Should be able to login an existing user username and channel', async function () {
+    await server.users.create({ username: 'existing_user2' })
+    await server.users.create({ username: 'existing_user2-1_channel' })
+
+    // Test twice to ensure we don't generate a username on every login
+    for (let i = 0; i < 2; i++) {
+      const res = await loginExternal({
+        server,
+        npmName: 'test-external-auth-two',
+        authName: 'external-auth-7',
+        username: 'existing_user2'
+      })
+
+      const token = res.access_token
+
+      const myInfo = await server.users.getMyInfo({ token })
+      expect(myInfo.username).to.equal('existing_user2-1')
+
+      expect(myInfo.videoChannels[0].name).to.equal('existing_user2-1_channel-1')
+    }
+  })
+
   it('Should display the correct configuration', async function () {
     const config = await server.config.getConfig()
 
     const auths = config.plugin.registeredExternalAuths
-    expect(auths).to.have.lengthOf(6)
+    expect(auths).to.have.lengthOf(7)
 
     const auth2 = auths.find((a) => a.authName === 'external-auth-2')
     expect(auth2).to.not.exist