Federated video streaming platform using ActivityPub and P2P in the web browser with Angular. https://joinpeertube.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

908 lines
32 KiB

  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import * as chai from 'chai'
  4. import { testImage } from '@server/tests/shared'
  5. import { AbuseState, HttpStatusCode, OAuth2ErrorCode, UserAdminFlag, UserRole, Video, VideoPlaylistType } from '@shared/models'
  6. import {
  7. cleanupTests,
  8. createSingleServer,
  9. killallServers,
  10. makePutBodyRequest,
  11. PeerTubeServer,
  12. setAccessTokensToServers,
  13. waitJobs
  14. } from '@shared/server-commands'
  15. const expect = chai.expect
  16. describe('Test users', function () {
  17. let server: PeerTubeServer
  18. let token: string
  19. let userToken: string
  20. let videoId: number
  21. let userId: number
  22. const user = {
  23. username: 'user_1',
  24. password: 'super password'
  25. }
  26. before(async function () {
  27. this.timeout(30000)
  28. server = await createSingleServer(1, {
  29. rates_limit: {
  30. login: {
  31. max: 30
  32. }
  33. }
  34. })
  35. await setAccessTokensToServers([ server ])
  36. await server.plugins.install({ npmName: 'peertube-theme-background-red' })
  37. })
  38. describe('OAuth client', function () {
  39. it('Should create a new client')
  40. it('Should return the first client')
  41. it('Should remove the last client')
  42. it('Should not login with an invalid client id', async function () {
  43. const client = { id: 'client', secret: server.store.client.secret }
  44. const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  45. expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
  46. expect(body.error).to.contain('client is invalid')
  47. expect(body.type.startsWith('https://')).to.be.true
  48. expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
  49. })
  50. it('Should not login with an invalid client secret', async function () {
  51. const client = { id: server.store.client.id, secret: 'coucou' }
  52. const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  53. expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
  54. expect(body.error).to.contain('client is invalid')
  55. expect(body.type.startsWith('https://')).to.be.true
  56. expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
  57. })
  58. })
  59. describe('Login', function () {
  60. it('Should not login with an invalid username', async function () {
  61. const user = { username: 'captain crochet', password: server.store.user.password }
  62. const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  63. expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
  64. expect(body.error).to.contain('credentials are invalid')
  65. expect(body.type.startsWith('https://')).to.be.true
  66. expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
  67. })
  68. it('Should not login with an invalid password', async function () {
  69. const user = { username: server.store.user.username, password: 'mew_three' }
  70. const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  71. expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
  72. expect(body.error).to.contain('credentials are invalid')
  73. expect(body.type.startsWith('https://')).to.be.true
  74. expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
  75. })
  76. it('Should not be able to upload a video', async function () {
  77. token = 'my_super_token'
  78. await server.videos.upload({ token, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  79. })
  80. it('Should not be able to follow', async function () {
  81. token = 'my_super_token'
  82. await server.follows.follow({
  83. hosts: [ 'http://example.com' ],
  84. token,
  85. expectedStatus: HttpStatusCode.UNAUTHORIZED_401
  86. })
  87. })
  88. it('Should not be able to unfollow')
  89. it('Should be able to login', async function () {
  90. const body = await server.login.login({ expectedStatus: HttpStatusCode.OK_200 })
  91. token = body.access_token
  92. })
  93. it('Should be able to login with an insensitive username', async function () {
  94. const user = { username: 'RoOt', password: server.store.user.password }
  95. await server.login.login({ user, expectedStatus: HttpStatusCode.OK_200 })
  96. const user2 = { username: 'rOoT', password: server.store.user.password }
  97. await server.login.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 })
  98. const user3 = { username: 'ROOt', password: server.store.user.password }
  99. await server.login.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 })
  100. })
  101. })
  102. describe('Upload', function () {
  103. it('Should upload the video with the correct token', async function () {
  104. await server.videos.upload({ token })
  105. const { data } = await server.videos.list()
  106. const video = data[0]
  107. expect(video.account.name).to.equal('root')
  108. videoId = video.id
  109. })
  110. it('Should upload the video again with the correct token', async function () {
  111. await server.videos.upload({ token })
  112. })
  113. })
  114. describe('Ratings', function () {
  115. it('Should retrieve a video rating', async function () {
  116. await server.videos.rate({ id: videoId, rating: 'like' })
  117. const rating = await server.users.getMyRating({ token, videoId })
  118. expect(rating.videoId).to.equal(videoId)
  119. expect(rating.rating).to.equal('like')
  120. })
  121. it('Should retrieve ratings list', async function () {
  122. await server.videos.rate({ id: videoId, rating: 'like' })
  123. const body = await server.accounts.listRatings({ accountName: server.store.user.username })
  124. expect(body.total).to.equal(1)
  125. expect(body.data[0].video.id).to.equal(videoId)
  126. expect(body.data[0].rating).to.equal('like')
  127. })
  128. it('Should retrieve ratings list by rating type', async function () {
  129. {
  130. const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'like' })
  131. expect(body.data.length).to.equal(1)
  132. }
  133. {
  134. const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'dislike' })
  135. expect(body.data.length).to.equal(0)
  136. }
  137. })
  138. })
  139. describe('Remove video', function () {
  140. it('Should not be able to remove the video with an incorrect token', async function () {
  141. await server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  142. })
  143. it('Should not be able to remove the video with the token of another account')
  144. it('Should be able to remove the video with the correct token', async function () {
  145. await server.videos.remove({ token, id: videoId })
  146. })
  147. })
  148. describe('Logout', function () {
  149. it('Should logout (revoke token)', async function () {
  150. await server.login.logout({ token: server.accessToken })
  151. })
  152. it('Should not be able to get the user information', async function () {
  153. await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  154. })
  155. it('Should not be able to upload a video', async function () {
  156. await server.videos.upload({ attributes: { name: 'video' }, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  157. })
  158. it('Should not be able to rate a video', async function () {
  159. const path = '/api/v1/videos/'
  160. const data = {
  161. rating: 'likes'
  162. }
  163. const options = {
  164. url: server.url,
  165. path: path + videoId,
  166. token: 'wrong token',
  167. fields: data,
  168. expectedStatus: HttpStatusCode.UNAUTHORIZED_401
  169. }
  170. await makePutBodyRequest(options)
  171. })
  172. it('Should be able to login again', async function () {
  173. const body = await server.login.login()
  174. server.accessToken = body.access_token
  175. server.refreshToken = body.refresh_token
  176. })
  177. it('Should be able to get my user information again', async function () {
  178. await server.users.getMyInfo()
  179. })
  180. it('Should have an expired access token', async function () {
  181. this.timeout(60000)
  182. await server.sql.setTokenField(server.accessToken, 'accessTokenExpiresAt', new Date().toISOString())
  183. await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString())
  184. await killallServers([ server ])
  185. await server.run()
  186. await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  187. })
  188. it('Should not be able to refresh an access token with an expired refresh token', async function () {
  189. await server.login.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  190. })
  191. it('Should refresh the token', async function () {
  192. this.timeout(15000)
  193. const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString()
  194. await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', futureDate)
  195. await killallServers([ server ])
  196. await server.run()
  197. const res = await server.login.refreshToken({ refreshToken: server.refreshToken })
  198. server.accessToken = res.body.access_token
  199. server.refreshToken = res.body.refresh_token
  200. })
  201. it('Should be able to get my user information again', async function () {
  202. await server.users.getMyInfo()
  203. })
  204. })
  205. describe('Creating a user', function () {
  206. it('Should be able to create a new user', async function () {
  207. await server.users.create({ ...user, videoQuota: 2 * 1024 * 1024, adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST })
  208. })
  209. it('Should be able to login with this user', async function () {
  210. userToken = await server.login.getAccessToken(user)
  211. })
  212. it('Should be able to get user information', async function () {
  213. const userMe = await server.users.getMyInfo({ token: userToken })
  214. const userGet = await server.users.get({ userId: userMe.id, withStats: true })
  215. for (const user of [ userMe, userGet ]) {
  216. expect(user.username).to.equal('user_1')
  217. expect(user.email).to.equal('user_1@example.com')
  218. expect(user.nsfwPolicy).to.equal('display')
  219. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  220. expect(user.roleLabel).to.equal('User')
  221. expect(user.id).to.be.a('number')
  222. expect(user.account.displayName).to.equal('user_1')
  223. expect(user.account.description).to.be.null
  224. }
  225. expect(userMe.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
  226. expect(userGet.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
  227. expect(userMe.specialPlaylists).to.have.lengthOf(1)
  228. expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER)
  229. // Check stats are included with withStats
  230. expect(userGet.videosCount).to.be.a('number')
  231. expect(userGet.videosCount).to.equal(0)
  232. expect(userGet.videoCommentsCount).to.be.a('number')
  233. expect(userGet.videoCommentsCount).to.equal(0)
  234. expect(userGet.abusesCount).to.be.a('number')
  235. expect(userGet.abusesCount).to.equal(0)
  236. expect(userGet.abusesAcceptedCount).to.be.a('number')
  237. expect(userGet.abusesAcceptedCount).to.equal(0)
  238. })
  239. })
  240. describe('My videos & quotas', function () {
  241. it('Should be able to upload a video with this user', async function () {
  242. this.timeout(10000)
  243. const attributes = {
  244. name: 'super user video',
  245. fixture: 'video_short.webm'
  246. }
  247. await server.videos.upload({ token: userToken, attributes })
  248. await server.channels.create({ token: userToken, attributes: { name: 'other_channel' } })
  249. })
  250. it('Should have video quota updated', async function () {
  251. const quota = await server.users.getMyQuotaUsed({ token: userToken })
  252. expect(quota.videoQuotaUsed).to.equal(218910)
  253. const { data } = await server.users.list()
  254. const tmpUser = data.find(u => u.username === user.username)
  255. expect(tmpUser.videoQuotaUsed).to.equal(218910)
  256. })
  257. it('Should be able to list my videos', async function () {
  258. const { total, data } = await server.videos.listMyVideos({ token: userToken })
  259. expect(total).to.equal(1)
  260. expect(data).to.have.lengthOf(1)
  261. const video: Video = data[0]
  262. expect(video.name).to.equal('super user video')
  263. expect(video.thumbnailPath).to.not.be.null
  264. expect(video.previewPath).to.not.be.null
  265. })
  266. it('Should be able to filter by channel in my videos', async function () {
  267. const myInfo = await server.users.getMyInfo({ token: userToken })
  268. const mainChannel = myInfo.videoChannels.find(c => c.name !== 'other_channel')
  269. const otherChannel = myInfo.videoChannels.find(c => c.name === 'other_channel')
  270. {
  271. const { total, data } = await server.videos.listMyVideos({ token: userToken, channelId: mainChannel.id })
  272. expect(total).to.equal(1)
  273. expect(data).to.have.lengthOf(1)
  274. const video: Video = data[0]
  275. expect(video.name).to.equal('super user video')
  276. expect(video.thumbnailPath).to.not.be.null
  277. expect(video.previewPath).to.not.be.null
  278. }
  279. {
  280. const { total, data } = await server.videos.listMyVideos({ token: userToken, channelId: otherChannel.id })
  281. expect(total).to.equal(0)
  282. expect(data).to.have.lengthOf(0)
  283. }
  284. })
  285. it('Should be able to search in my videos', async function () {
  286. {
  287. const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'user video' })
  288. expect(total).to.equal(1)
  289. expect(data).to.have.lengthOf(1)
  290. }
  291. {
  292. const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'toto' })
  293. expect(total).to.equal(0)
  294. expect(data).to.have.lengthOf(0)
  295. }
  296. })
  297. it('Should disable webtorrent, enable HLS, and update my quota', async function () {
  298. this.timeout(60000)
  299. {
  300. const config = await server.config.getCustomConfig()
  301. config.transcoding.webtorrent.enabled = false
  302. config.transcoding.hls.enabled = true
  303. config.transcoding.enabled = true
  304. await server.config.updateCustomSubConfig({ newConfig: config })
  305. }
  306. {
  307. const attributes = {
  308. name: 'super user video 2',
  309. fixture: 'video_short.webm'
  310. }
  311. await server.videos.upload({ token: userToken, attributes })
  312. await waitJobs([ server ])
  313. }
  314. {
  315. const data = await server.users.getMyQuotaUsed({ token: userToken })
  316. expect(data.videoQuotaUsed).to.be.greaterThan(220000)
  317. }
  318. })
  319. })
  320. describe('Users listing', function () {
  321. it('Should list all the users', async function () {
  322. const { data, total } = await server.users.list()
  323. expect(total).to.equal(2)
  324. expect(data).to.be.an('array')
  325. expect(data.length).to.equal(2)
  326. const user = data[0]
  327. expect(user.username).to.equal('user_1')
  328. expect(user.email).to.equal('user_1@example.com')
  329. expect(user.nsfwPolicy).to.equal('display')
  330. const rootUser = data[1]
  331. expect(rootUser.username).to.equal('root')
  332. expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
  333. expect(user.nsfwPolicy).to.equal('display')
  334. expect(rootUser.lastLoginDate).to.exist
  335. expect(user.lastLoginDate).to.exist
  336. userId = user.id
  337. })
  338. it('Should list only the first user by username asc', async function () {
  339. const { total, data } = await server.users.list({ start: 0, count: 1, sort: 'username' })
  340. expect(total).to.equal(2)
  341. expect(data.length).to.equal(1)
  342. const user = data[0]
  343. expect(user.username).to.equal('root')
  344. expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
  345. expect(user.roleLabel).to.equal('Administrator')
  346. expect(user.nsfwPolicy).to.equal('display')
  347. })
  348. it('Should list only the first user by username desc', async function () {
  349. const { total, data } = await server.users.list({ start: 0, count: 1, sort: '-username' })
  350. expect(total).to.equal(2)
  351. expect(data.length).to.equal(1)
  352. const user = data[0]
  353. expect(user.username).to.equal('user_1')
  354. expect(user.email).to.equal('user_1@example.com')
  355. expect(user.nsfwPolicy).to.equal('display')
  356. })
  357. it('Should list only the second user by createdAt desc', async function () {
  358. const { data, total } = await server.users.list({ start: 0, count: 1, sort: '-createdAt' })
  359. expect(total).to.equal(2)
  360. expect(data.length).to.equal(1)
  361. const user = data[0]
  362. expect(user.username).to.equal('user_1')
  363. expect(user.email).to.equal('user_1@example.com')
  364. expect(user.nsfwPolicy).to.equal('display')
  365. })
  366. it('Should list all the users by createdAt asc', async function () {
  367. const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt' })
  368. expect(total).to.equal(2)
  369. expect(data.length).to.equal(2)
  370. expect(data[0].username).to.equal('root')
  371. expect(data[0].email).to.equal('admin' + server.internalServerNumber + '@example.com')
  372. expect(data[0].nsfwPolicy).to.equal('display')
  373. expect(data[1].username).to.equal('user_1')
  374. expect(data[1].email).to.equal('user_1@example.com')
  375. expect(data[1].nsfwPolicy).to.equal('display')
  376. })
  377. it('Should search user by username', async function () {
  378. const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'oot' })
  379. expect(total).to.equal(1)
  380. expect(data.length).to.equal(1)
  381. expect(data[0].username).to.equal('root')
  382. })
  383. it('Should search user by email', async function () {
  384. {
  385. const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'r_1@exam' })
  386. expect(total).to.equal(1)
  387. expect(data.length).to.equal(1)
  388. expect(data[0].username).to.equal('user_1')
  389. expect(data[0].email).to.equal('user_1@example.com')
  390. }
  391. {
  392. const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'example' })
  393. expect(total).to.equal(2)
  394. expect(data.length).to.equal(2)
  395. expect(data[0].username).to.equal('root')
  396. expect(data[1].username).to.equal('user_1')
  397. }
  398. })
  399. })
  400. describe('Update my account', function () {
  401. it('Should update my password', async function () {
  402. await server.users.updateMe({
  403. token: userToken,
  404. currentPassword: 'super password',
  405. password: 'new password'
  406. })
  407. user.password = 'new password'
  408. await server.login.login({ user })
  409. })
  410. it('Should be able to change the NSFW display attribute', async function () {
  411. await server.users.updateMe({
  412. token: userToken,
  413. nsfwPolicy: 'do_not_list'
  414. })
  415. const user = await server.users.getMyInfo({ token: userToken })
  416. expect(user.username).to.equal('user_1')
  417. expect(user.email).to.equal('user_1@example.com')
  418. expect(user.nsfwPolicy).to.equal('do_not_list')
  419. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  420. expect(user.id).to.be.a('number')
  421. expect(user.account.displayName).to.equal('user_1')
  422. expect(user.account.description).to.be.null
  423. })
  424. it('Should be able to change the autoPlayVideo attribute', async function () {
  425. await server.users.updateMe({
  426. token: userToken,
  427. autoPlayVideo: false
  428. })
  429. const user = await server.users.getMyInfo({ token: userToken })
  430. expect(user.autoPlayVideo).to.be.false
  431. })
  432. it('Should be able to change the autoPlayNextVideo attribute', async function () {
  433. await server.users.updateMe({
  434. token: userToken,
  435. autoPlayNextVideo: true
  436. })
  437. const user = await server.users.getMyInfo({ token: userToken })
  438. expect(user.autoPlayNextVideo).to.be.true
  439. })
  440. it('Should be able to change the p2p attribute', async function () {
  441. {
  442. await server.users.updateMe({
  443. token: userToken,
  444. webTorrentEnabled: false
  445. })
  446. const user = await server.users.getMyInfo({ token: userToken })
  447. expect(user.p2pEnabled).to.be.false
  448. }
  449. {
  450. await server.users.updateMe({
  451. token: userToken,
  452. p2pEnabled: true
  453. })
  454. const user = await server.users.getMyInfo({ token: userToken })
  455. expect(user.p2pEnabled).to.be.true
  456. }
  457. })
  458. it('Should be able to change the email attribute', async function () {
  459. await server.users.updateMe({
  460. token: userToken,
  461. currentPassword: 'new password',
  462. email: 'updated@example.com'
  463. })
  464. const user = await server.users.getMyInfo({ token: userToken })
  465. expect(user.username).to.equal('user_1')
  466. expect(user.email).to.equal('updated@example.com')
  467. expect(user.nsfwPolicy).to.equal('do_not_list')
  468. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  469. expect(user.id).to.be.a('number')
  470. expect(user.account.displayName).to.equal('user_1')
  471. expect(user.account.description).to.be.null
  472. })
  473. it('Should be able to update my avatar with a gif', async function () {
  474. const fixture = 'avatar.gif'
  475. await server.users.updateMyAvatar({ token: userToken, fixture })
  476. const user = await server.users.getMyInfo({ token: userToken })
  477. await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
  478. })
  479. it('Should be able to update my avatar with a gif, and then a png', async function () {
  480. for (const extension of [ '.png', '.gif' ]) {
  481. const fixture = 'avatar' + extension
  482. await server.users.updateMyAvatar({ token: userToken, fixture })
  483. const user = await server.users.getMyInfo({ token: userToken })
  484. await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
  485. }
  486. })
  487. it('Should be able to update my display name', async function () {
  488. await server.users.updateMe({ token: userToken, displayName: 'new display name' })
  489. const user = await server.users.getMyInfo({ token: userToken })
  490. expect(user.username).to.equal('user_1')
  491. expect(user.email).to.equal('updated@example.com')
  492. expect(user.nsfwPolicy).to.equal('do_not_list')
  493. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  494. expect(user.id).to.be.a('number')
  495. expect(user.account.displayName).to.equal('new display name')
  496. expect(user.account.description).to.be.null
  497. })
  498. it('Should be able to update my description', async function () {
  499. await server.users.updateMe({ token: userToken, description: 'my super description updated' })
  500. const user = await server.users.getMyInfo({ token: userToken })
  501. expect(user.username).to.equal('user_1')
  502. expect(user.email).to.equal('updated@example.com')
  503. expect(user.nsfwPolicy).to.equal('do_not_list')
  504. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  505. expect(user.id).to.be.a('number')
  506. expect(user.account.displayName).to.equal('new display name')
  507. expect(user.account.description).to.equal('my super description updated')
  508. expect(user.noWelcomeModal).to.be.false
  509. expect(user.noInstanceConfigWarningModal).to.be.false
  510. expect(user.noAccountSetupWarningModal).to.be.false
  511. })
  512. it('Should be able to update my theme', async function () {
  513. for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
  514. await server.users.updateMe({ token: userToken, theme })
  515. const user = await server.users.getMyInfo({ token: userToken })
  516. expect(user.theme).to.equal(theme)
  517. }
  518. })
  519. it('Should be able to update my modal preferences', async function () {
  520. await server.users.updateMe({
  521. token: userToken,
  522. noInstanceConfigWarningModal: true,
  523. noWelcomeModal: true,
  524. noAccountSetupWarningModal: true
  525. })
  526. const user = await server.users.getMyInfo({ token: userToken })
  527. expect(user.noWelcomeModal).to.be.true
  528. expect(user.noInstanceConfigWarningModal).to.be.true
  529. expect(user.noAccountSetupWarningModal).to.be.true
  530. })
  531. })
  532. describe('Updating another user', function () {
  533. it('Should be able to update another user', async function () {
  534. await server.users.update({
  535. userId,
  536. token,
  537. email: 'updated2@example.com',
  538. emailVerified: true,
  539. videoQuota: 42,
  540. role: UserRole.MODERATOR,
  541. adminFlags: UserAdminFlag.NONE,
  542. pluginAuth: 'toto'
  543. })
  544. const user = await server.users.get({ token, userId })
  545. expect(user.username).to.equal('user_1')
  546. expect(user.email).to.equal('updated2@example.com')
  547. expect(user.emailVerified).to.be.true
  548. expect(user.nsfwPolicy).to.equal('do_not_list')
  549. expect(user.videoQuota).to.equal(42)
  550. expect(user.roleLabel).to.equal('Moderator')
  551. expect(user.id).to.be.a('number')
  552. expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
  553. expect(user.pluginAuth).to.equal('toto')
  554. })
  555. it('Should reset the auth plugin', async function () {
  556. await server.users.update({ userId, token, pluginAuth: null })
  557. const user = await server.users.get({ token, userId })
  558. expect(user.pluginAuth).to.be.null
  559. })
  560. it('Should have removed the user token', async function () {
  561. await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  562. userToken = await server.login.getAccessToken(user)
  563. })
  564. it('Should be able to update another user password', async function () {
  565. await server.users.update({ userId, token, password: 'password updated' })
  566. await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  567. await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  568. user.password = 'password updated'
  569. userToken = await server.login.getAccessToken(user)
  570. })
  571. })
  572. describe('Video blacklists', function () {
  573. it('Should be able to list video blacklist by a moderator', async function () {
  574. await server.blacklist.list({ token: userToken })
  575. })
  576. })
  577. describe('Remove a user', function () {
  578. it('Should be able to remove this user', async function () {
  579. await server.users.remove({ userId, token })
  580. })
  581. it('Should not be able to login with this user', async function () {
  582. await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  583. })
  584. it('Should not have videos of this user', async function () {
  585. const { data, total } = await server.videos.list()
  586. expect(total).to.equal(1)
  587. const video = data[0]
  588. expect(video.account.name).to.equal('root')
  589. })
  590. })
  591. describe('Registering a new user', function () {
  592. let user15AccessToken
  593. it('Should register a new user', async function () {
  594. const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
  595. const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
  596. await server.users.register({ ...user, channel })
  597. })
  598. it('Should be able to login with this registered user', async function () {
  599. const user15 = {
  600. username: 'user_15',
  601. password: 'my super password'
  602. }
  603. user15AccessToken = await server.login.getAccessToken(user15)
  604. })
  605. it('Should have the correct display name', async function () {
  606. const user = await server.users.getMyInfo({ token: user15AccessToken })
  607. expect(user.account.displayName).to.equal('super user 15')
  608. })
  609. it('Should have the correct video quota', async function () {
  610. const user = await server.users.getMyInfo({ token: user15AccessToken })
  611. expect(user.videoQuota).to.equal(5 * 1024 * 1024)
  612. })
  613. it('Should have created the channel', async function () {
  614. const { displayName } = await server.channels.get({ channelName: 'my_user_15_channel' })
  615. expect(displayName).to.equal('my channel rocks')
  616. })
  617. it('Should remove me', async function () {
  618. {
  619. const { data } = await server.users.list()
  620. expect(data.find(u => u.username === 'user_15')).to.not.be.undefined
  621. }
  622. await server.users.deleteMe({ token: user15AccessToken })
  623. {
  624. const { data } = await server.users.list()
  625. expect(data.find(u => u.username === 'user_15')).to.be.undefined
  626. }
  627. })
  628. })
  629. describe('User blocking', function () {
  630. let user16Id
  631. let user16AccessToken
  632. const user16 = {
  633. username: 'user_16',
  634. password: 'my super password'
  635. }
  636. it('Should block a user', async function () {
  637. const user = await server.users.create({ ...user16 })
  638. user16Id = user.id
  639. user16AccessToken = await server.login.getAccessToken(user16)
  640. await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
  641. await server.users.banUser({ userId: user16Id })
  642. await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  643. await server.login.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  644. })
  645. it('Should search user by banned status', async function () {
  646. {
  647. const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: true })
  648. expect(total).to.equal(1)
  649. expect(data.length).to.equal(1)
  650. expect(data[0].username).to.equal(user16.username)
  651. }
  652. {
  653. const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: false })
  654. expect(total).to.equal(1)
  655. expect(data.length).to.equal(1)
  656. expect(data[0].username).to.not.equal(user16.username)
  657. }
  658. })
  659. it('Should unblock a user', async function () {
  660. await server.users.unbanUser({ userId: user16Id })
  661. user16AccessToken = await server.login.getAccessToken(user16)
  662. await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
  663. })
  664. })
  665. describe('User stats', function () {
  666. let user17Id
  667. let user17AccessToken
  668. it('Should report correct initial statistics about a user', async function () {
  669. const user17 = {
  670. username: 'user_17',
  671. password: 'my super password'
  672. }
  673. const created = await server.users.create({ ...user17 })
  674. user17Id = created.id
  675. user17AccessToken = await server.login.getAccessToken(user17)
  676. const user = await server.users.get({ userId: user17Id, withStats: true })
  677. expect(user.videosCount).to.equal(0)
  678. expect(user.videoCommentsCount).to.equal(0)
  679. expect(user.abusesCount).to.equal(0)
  680. expect(user.abusesCreatedCount).to.equal(0)
  681. expect(user.abusesAcceptedCount).to.equal(0)
  682. })
  683. it('Should report correct videos count', async function () {
  684. const attributes = { name: 'video to test user stats' }
  685. await server.videos.upload({ token: user17AccessToken, attributes })
  686. const { data } = await server.videos.list()
  687. videoId = data.find(video => video.name === attributes.name).id
  688. const user = await server.users.get({ userId: user17Id, withStats: true })
  689. expect(user.videosCount).to.equal(1)
  690. })
  691. it('Should report correct video comments for user', async function () {
  692. const text = 'super comment'
  693. await server.comments.createThread({ token: user17AccessToken, videoId, text })
  694. const user = await server.users.get({ userId: user17Id, withStats: true })
  695. expect(user.videoCommentsCount).to.equal(1)
  696. })
  697. it('Should report correct abuses counts', async function () {
  698. const reason = 'my super bad reason'
  699. await server.abuses.report({ token: user17AccessToken, videoId, reason })
  700. const body1 = await server.abuses.getAdminList()
  701. const abuseId = body1.data[0].id
  702. const user2 = await server.users.get({ userId: user17Id, withStats: true })
  703. expect(user2.abusesCount).to.equal(1) // number of incriminations
  704. expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
  705. await server.abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
  706. const user3 = await server.users.get({ userId: user17Id, withStats: true })
  707. expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
  708. })
  709. })
  710. after(async function () {
  711. await cleanupTests([ server ])
  712. })
  713. })