]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/plugins/external-auth.ts
Implement getServerListeningConfig plugin helper
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / external-auth.ts
index f3e018d437bfe8efbd861f93f6d68ebabd5bb541..437777e90e7a2dfb72f4a975f3809b074319a228 100644 (file)
@@ -1,17 +1,16 @@
 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import 'mocha'
 import { expect } from 'chai'
+import { wait } from '@shared/core-utils'
+import { HttpStatusCode, UserRole } from '@shared/models'
 import {
   cleanupTests,
   createSingleServer,
   decodeQueryString,
   PeerTubeServer,
   PluginsCommand,
-  setAccessTokensToServers,
-  wait
-} from '@shared/extra-utils'
-import { HttpStatusCode, UserRole } from '@shared/models'
+  setAccessTokensToServers
+} from '@shared/server-commands'
 
 async function loginExternal (options: {
   server: PeerTubeServer
@@ -58,7 +57,14 @@ describe('Test external auth plugins', function () {
   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' ]) {
@@ -70,7 +76,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
@@ -125,7 +131,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 () {
@@ -149,7 +155,7 @@ 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)
     }
   })
 
@@ -171,7 +177,7 @@ 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)
     }
   })
 
@@ -231,7 +237,7 @@ 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 not update an external auth email', async function () {
@@ -275,7 +281,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
@@ -318,7 +324,7 @@ describe('Test external auth plugins', function () {
     })
   })
 
-  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({
@@ -330,11 +336,33 @@ describe('Test external auth plugins', function () {
     })
   })
 
+  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