id-and-pass-auth.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { expect } from 'chai'
  4. import { wait } from '@shared/core-utils'
  5. import { HttpStatusCode, UserRole } from '@shared/models'
  6. import { cleanupTests, createSingleServer, PeerTubeServer, PluginsCommand, setAccessTokensToServers } from '@shared/server-commands'
  7. describe('Test id and pass auth plugins', function () {
  8. let server: PeerTubeServer
  9. let crashAccessToken: string
  10. let crashRefreshToken: string
  11. let lagunaAccessToken: string
  12. let lagunaRefreshToken: string
  13. before(async function () {
  14. this.timeout(30000)
  15. server = await createSingleServer(1)
  16. await setAccessTokensToServers([ server ])
  17. for (const suffix of [ 'one', 'two', 'three' ]) {
  18. await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) })
  19. }
  20. })
  21. it('Should display the correct configuration', async function () {
  22. const config = await server.config.getConfig()
  23. const auths = config.plugin.registeredIdAndPassAuths
  24. expect(auths).to.have.lengthOf(8)
  25. const crashAuth = auths.find(a => a.authName === 'crash-auth')
  26. expect(crashAuth).to.exist
  27. expect(crashAuth.npmName).to.equal('peertube-plugin-test-id-pass-auth-one')
  28. expect(crashAuth.weight).to.equal(50)
  29. })
  30. it('Should not login', async function () {
  31. await server.login.login({ user: { username: 'toto', password: 'password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  32. })
  33. it('Should login Spyro, create the user and use the token', async function () {
  34. const accessToken = await server.login.getAccessToken({ username: 'spyro', password: 'spyro password' })
  35. const body = await server.users.getMyInfo({ token: accessToken })
  36. expect(body.username).to.equal('spyro')
  37. expect(body.account.displayName).to.equal('Spyro the Dragon')
  38. expect(body.role).to.equal(UserRole.USER)
  39. })
  40. it('Should login Crash, create the user and use the token', async function () {
  41. {
  42. const body = await server.login.login({ user: { username: 'crash', password: 'crash password' } })
  43. crashAccessToken = body.access_token
  44. crashRefreshToken = body.refresh_token
  45. }
  46. {
  47. const body = await server.users.getMyInfo({ token: crashAccessToken })
  48. expect(body.username).to.equal('crash')
  49. expect(body.account.displayName).to.equal('Crash Bandicoot')
  50. expect(body.role).to.equal(UserRole.MODERATOR)
  51. }
  52. })
  53. it('Should login the first Laguna, create the user and use the token', async function () {
  54. {
  55. const body = await server.login.login({ user: { username: 'laguna', password: 'laguna password' } })
  56. lagunaAccessToken = body.access_token
  57. lagunaRefreshToken = body.refresh_token
  58. }
  59. {
  60. const body = await server.users.getMyInfo({ token: lagunaAccessToken })
  61. expect(body.username).to.equal('laguna')
  62. expect(body.account.displayName).to.equal('laguna')
  63. expect(body.role).to.equal(UserRole.USER)
  64. }
  65. })
  66. it('Should refresh crash token, but not laguna token', async function () {
  67. {
  68. const resRefresh = await server.login.refreshToken({ refreshToken: crashRefreshToken })
  69. crashAccessToken = resRefresh.body.access_token
  70. crashRefreshToken = resRefresh.body.refresh_token
  71. const body = await server.users.getMyInfo({ token: crashAccessToken })
  72. expect(body.username).to.equal('crash')
  73. }
  74. {
  75. await server.login.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  76. }
  77. })
  78. it('Should update Crash profile', async function () {
  79. await server.users.updateMe({
  80. token: crashAccessToken,
  81. displayName: 'Beautiful Crash',
  82. description: 'Mutant eastern barred bandicoot'
  83. })
  84. const body = await server.users.getMyInfo({ token: crashAccessToken })
  85. expect(body.account.displayName).to.equal('Beautiful Crash')
  86. expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
  87. })
  88. it('Should logout Crash', async function () {
  89. await server.login.logout({ token: crashAccessToken })
  90. })
  91. it('Should have logged out Crash', async function () {
  92. await server.servers.waitUntilLog('On logout for auth 1 - 2')
  93. await server.users.getMyInfo({ token: crashAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  94. })
  95. it('Should login Crash and keep the old existing profile', async function () {
  96. crashAccessToken = await server.login.getAccessToken({ username: 'crash', password: 'crash password' })
  97. const body = await server.users.getMyInfo({ token: crashAccessToken })
  98. expect(body.username).to.equal('crash')
  99. expect(body.account.displayName).to.equal('Beautiful Crash')
  100. expect(body.account.description).to.equal('Mutant eastern barred bandicoot')
  101. expect(body.role).to.equal(UserRole.MODERATOR)
  102. })
  103. it('Should reject token of laguna by the plugin hook', async function () {
  104. this.timeout(10000)
  105. await wait(5000)
  106. await server.users.getMyInfo({ token: lagunaAccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  107. })
  108. it('Should reject an invalid username, email, role or display name', async function () {
  109. const command = server.login
  110. await command.login({ user: { username: 'ward', password: 'ward password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  111. await server.servers.waitUntilLog('valid username')
  112. await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  113. await server.servers.waitUntilLog('valid display name')
  114. await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  115. await server.servers.waitUntilLog('valid role')
  116. await command.login({ user: { username: 'ellone', password: 'elonne password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  117. await server.servers.waitUntilLog('valid email')
  118. })
  119. it('Should unregister spyro-auth and do not login existing Spyro', async function () {
  120. await server.plugins.updateSettings({
  121. npmName: 'peertube-plugin-test-id-pass-auth-one',
  122. settings: { disableSpyro: true }
  123. })
  124. const command = server.login
  125. await command.login({ user: { username: 'spyro', password: 'spyro password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  126. await command.login({ user: { username: 'spyro', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  127. })
  128. it('Should have disabled this auth', async function () {
  129. const config = await server.config.getConfig()
  130. const auths = config.plugin.registeredIdAndPassAuths
  131. expect(auths).to.have.lengthOf(7)
  132. const spyroAuth = auths.find(a => a.authName === 'spyro-auth')
  133. expect(spyroAuth).to.not.exist
  134. })
  135. it('Should uninstall the plugin one and do not login existing Crash', async function () {
  136. await server.plugins.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' })
  137. await server.login.login({
  138. user: { username: 'crash', password: 'crash password' },
  139. expectedStatus: HttpStatusCode.BAD_REQUEST_400
  140. })
  141. })
  142. it('Should display the correct configuration', async function () {
  143. const config = await server.config.getConfig()
  144. const auths = config.plugin.registeredIdAndPassAuths
  145. expect(auths).to.have.lengthOf(6)
  146. const crashAuth = auths.find(a => a.authName === 'crash-auth')
  147. expect(crashAuth).to.not.exist
  148. })
  149. it('Should display plugin auth information in users list', async function () {
  150. const { data } = await server.users.list()
  151. const root = data.find(u => u.username === 'root')
  152. const crash = data.find(u => u.username === 'crash')
  153. const laguna = data.find(u => u.username === 'laguna')
  154. expect(root.pluginAuth).to.be.null
  155. expect(crash.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-one')
  156. expect(laguna.pluginAuth).to.equal('peertube-plugin-test-id-pass-auth-two')
  157. })
  158. after(async function () {
  159. await cleanupTests([ server ])
  160. })
  161. })