users.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /* tslint:disable:no-unused-expression */
  2. import * as chai from 'chai'
  3. import 'mocha'
  4. import { User, UserRole } from '../../../../shared/index'
  5. import {
  6. blockUser,
  7. createUser,
  8. deleteMe,
  9. flushTests,
  10. getAccountRatings,
  11. getBlacklistedVideosList,
  12. getMyUserInformation,
  13. getMyUserVideoQuotaUsed,
  14. getMyUserVideoRating,
  15. getUserInformation,
  16. getUsersList,
  17. getUsersListPaginationAndSort,
  18. getVideosList,
  19. killallServers,
  20. login,
  21. makePutBodyRequest,
  22. rateVideo,
  23. registerUser,
  24. removeUser,
  25. removeVideo,
  26. flushAndRunServer,
  27. ServerInfo,
  28. testImage,
  29. unblockUser,
  30. updateMyAvatar,
  31. updateMyUser,
  32. updateUser,
  33. uploadVideo,
  34. userLogin
  35. } from '../../../../shared/extra-utils'
  36. import { follow } from '../../../../shared/extra-utils/server/follows'
  37. import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
  38. import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
  39. import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
  40. const expect = chai.expect
  41. describe('Test users', function () {
  42. let server: ServerInfo
  43. let accessToken: string
  44. let accessTokenUser: string
  45. let videoId: number
  46. let userId: number
  47. const user = {
  48. username: 'user_1',
  49. password: 'super password'
  50. }
  51. before(async function () {
  52. this.timeout(30000)
  53. server = await flushAndRunServer(1)
  54. await setAccessTokensToServers([ server ])
  55. })
  56. describe('OAuth client', function () {
  57. it('Should create a new client')
  58. it('Should return the first client')
  59. it('Should remove the last client')
  60. it('Should not login with an invalid client id', async function () {
  61. const client = { id: 'client', secret: server.client.secret }
  62. const res = await login(server.url, client, server.user, 400)
  63. expect(res.body.error).to.contain('client is invalid')
  64. })
  65. it('Should not login with an invalid client secret', async function () {
  66. const client = { id: server.client.id, secret: 'coucou' }
  67. const res = await login(server.url, client, server.user, 400)
  68. expect(res.body.error).to.contain('client is invalid')
  69. })
  70. })
  71. describe('Login', function () {
  72. it('Should not login with an invalid username', async function () {
  73. const user = { username: 'captain crochet', password: server.user.password }
  74. const res = await login(server.url, server.client, user, 400)
  75. expect(res.body.error).to.contain('credentials are invalid')
  76. })
  77. it('Should not login with an invalid password', async function () {
  78. const user = { username: server.user.username, password: 'mew_three' }
  79. const res = await login(server.url, server.client, user, 400)
  80. expect(res.body.error).to.contain('credentials are invalid')
  81. })
  82. it('Should not be able to upload a video', async function () {
  83. accessToken = 'my_super_token'
  84. const videoAttributes = {}
  85. await uploadVideo(server.url, accessToken, videoAttributes, 401)
  86. })
  87. it('Should not be able to follow', async function () {
  88. accessToken = 'my_super_token'
  89. await follow(server.url, [ 'http://example.com' ], accessToken, 401)
  90. })
  91. it('Should not be able to unfollow')
  92. it('Should be able to login', async function () {
  93. const res = await login(server.url, server.client, server.user, 200)
  94. accessToken = res.body.access_token
  95. })
  96. })
  97. describe('Upload', function () {
  98. it('Should upload the video with the correct token', async function () {
  99. const videoAttributes = {}
  100. await uploadVideo(server.url, accessToken, videoAttributes)
  101. const res = await getVideosList(server.url)
  102. const video = res.body.data[ 0 ]
  103. expect(video.account.name).to.equal('root')
  104. videoId = video.id
  105. })
  106. it('Should upload the video again with the correct token', async function () {
  107. const videoAttributes = {}
  108. await uploadVideo(server.url, accessToken, videoAttributes)
  109. })
  110. })
  111. describe('Ratings', function () {
  112. it('Should retrieve a video rating', async function () {
  113. await rateVideo(server.url, accessToken, videoId, 'like')
  114. const res = await getMyUserVideoRating(server.url, accessToken, videoId)
  115. const rating = res.body
  116. expect(rating.videoId).to.equal(videoId)
  117. expect(rating.rating).to.equal('like')
  118. })
  119. it('Should retrieve ratings list', async function () {
  120. await rateVideo(server.url, accessToken, videoId, 'like')
  121. const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
  122. const ratings = res.body
  123. expect(ratings.total).to.equal(1)
  124. expect(ratings.data[ 0 ].video.id).to.equal(videoId)
  125. expect(ratings.data[ 0 ].rating).to.equal('like')
  126. })
  127. it('Should retrieve ratings list by rating type', async function () {
  128. {
  129. const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like')
  130. const ratings = res.body
  131. expect(ratings.data.length).to.equal(1)
  132. }
  133. {
  134. const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike')
  135. const ratings = res.body
  136. expect(ratings.data.length).to.equal(0)
  137. }
  138. })
  139. })
  140. describe('Remove video', function () {
  141. it('Should not be able to remove the video with an incorrect token', async function () {
  142. await removeVideo(server.url, 'bad_token', videoId, 401)
  143. })
  144. it('Should not be able to remove the video with the token of another account')
  145. it('Should be able to remove the video with the correct token', async function () {
  146. await removeVideo(server.url, accessToken, videoId)
  147. })
  148. })
  149. describe('Logout', function () {
  150. it('Should logout (revoke token)')
  151. it('Should not be able to get the user information')
  152. it('Should not be able to upload a video')
  153. it('Should not be able to remove a video')
  154. it('Should not be able to rate a video', async function () {
  155. const path = '/api/v1/videos/'
  156. const data = {
  157. rating: 'likes'
  158. }
  159. const options = {
  160. url: server.url,
  161. path: path + videoId,
  162. token: 'wrong token',
  163. fields: data,
  164. statusCodeExpected: 401
  165. }
  166. await makePutBodyRequest(options)
  167. })
  168. it('Should be able to login again')
  169. it('Should have an expired access token')
  170. it('Should refresh the token')
  171. it('Should be able to upload a video again')
  172. })
  173. describe('Creating a user', function () {
  174. it('Should be able to create a new user', async function () {
  175. await createUser({
  176. url: server.url,
  177. accessToken: accessToken,
  178. username: user.username,
  179. password: user.password,
  180. videoQuota: 2 * 1024 * 1024,
  181. adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
  182. })
  183. })
  184. it('Should be able to login with this user', async function () {
  185. accessTokenUser = await userLogin(server, user)
  186. })
  187. it('Should be able to get user information', async function () {
  188. const res1 = await getMyUserInformation(server.url, accessTokenUser)
  189. const userMe: User = res1.body
  190. const res2 = await getUserInformation(server.url, server.accessToken, userMe.id)
  191. const userGet: User = res2.body
  192. for (const user of [ userMe, userGet ]) {
  193. expect(user.username).to.equal('user_1')
  194. expect(user.email).to.equal('user_1@example.com')
  195. expect(user.nsfwPolicy).to.equal('display')
  196. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  197. expect(user.roleLabel).to.equal('User')
  198. expect(user.id).to.be.a('number')
  199. expect(user.account.displayName).to.equal('user_1')
  200. expect(user.account.description).to.be.null
  201. }
  202. expect(userMe.adminFlags).to.be.undefined
  203. expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)
  204. })
  205. })
  206. describe('My videos & quotas', function () {
  207. it('Should be able to upload a video with this user', async function () {
  208. this.timeout(5000)
  209. const videoAttributes = {
  210. name: 'super user video',
  211. fixture: 'video_short.webm'
  212. }
  213. await uploadVideo(server.url, accessTokenUser, videoAttributes)
  214. })
  215. it('Should have video quota updated', async function () {
  216. const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
  217. const data = res.body
  218. expect(data.videoQuotaUsed).to.equal(218910)
  219. const resUsers = await getUsersList(server.url, server.accessToken)
  220. const users: User[] = resUsers.body.data
  221. const tmpUser = users.find(u => u.username === user.username)
  222. expect(tmpUser.videoQuotaUsed).to.equal(218910)
  223. })
  224. it('Should be able to list my videos', async function () {
  225. const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
  226. expect(res.body.total).to.equal(1)
  227. const videos = res.body.data
  228. expect(videos).to.have.lengthOf(1)
  229. expect(videos[ 0 ].name).to.equal('super user video')
  230. })
  231. })
  232. describe('Users listing', function () {
  233. it('Should list all the users', async function () {
  234. const res = await getUsersList(server.url, server.accessToken)
  235. const result = res.body
  236. const total = result.total
  237. const users = result.data
  238. expect(total).to.equal(2)
  239. expect(users).to.be.an('array')
  240. expect(users.length).to.equal(2)
  241. const user = users[ 0 ]
  242. expect(user.username).to.equal('user_1')
  243. expect(user.email).to.equal('user_1@example.com')
  244. expect(user.nsfwPolicy).to.equal('display')
  245. const rootUser = users[ 1 ]
  246. expect(rootUser.username).to.equal('root')
  247. expect(rootUser.email).to.equal('admin1@example.com')
  248. expect(user.nsfwPolicy).to.equal('display')
  249. userId = user.id
  250. })
  251. it('Should list only the first user by username asc', async function () {
  252. const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
  253. const result = res.body
  254. const total = result.total
  255. const users = result.data
  256. expect(total).to.equal(2)
  257. expect(users.length).to.equal(1)
  258. const user = users[ 0 ]
  259. expect(user.username).to.equal('root')
  260. expect(user.email).to.equal('admin1@example.com')
  261. expect(user.roleLabel).to.equal('Administrator')
  262. expect(user.nsfwPolicy).to.equal('display')
  263. })
  264. it('Should list only the first user by username desc', async function () {
  265. const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
  266. const result = res.body
  267. const total = result.total
  268. const users = result.data
  269. expect(total).to.equal(2)
  270. expect(users.length).to.equal(1)
  271. const user = users[ 0 ]
  272. expect(user.username).to.equal('user_1')
  273. expect(user.email).to.equal('user_1@example.com')
  274. expect(user.nsfwPolicy).to.equal('display')
  275. })
  276. it('Should list only the second user by createdAt desc', async function () {
  277. const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
  278. const result = res.body
  279. const total = result.total
  280. const users = result.data
  281. expect(total).to.equal(2)
  282. expect(users.length).to.equal(1)
  283. const user = users[ 0 ]
  284. expect(user.username).to.equal('user_1')
  285. expect(user.email).to.equal('user_1@example.com')
  286. expect(user.nsfwPolicy).to.equal('display')
  287. })
  288. it('Should list all the users by createdAt asc', async function () {
  289. const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
  290. const result = res.body
  291. const total = result.total
  292. const users = result.data
  293. expect(total).to.equal(2)
  294. expect(users.length).to.equal(2)
  295. expect(users[ 0 ].username).to.equal('root')
  296. expect(users[ 0 ].email).to.equal('admin1@example.com')
  297. expect(users[ 0 ].nsfwPolicy).to.equal('display')
  298. expect(users[ 1 ].username).to.equal('user_1')
  299. expect(users[ 1 ].email).to.equal('user_1@example.com')
  300. expect(users[ 1 ].nsfwPolicy).to.equal('display')
  301. })
  302. it('Should search user by username', async function () {
  303. const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
  304. const users = res.body.data as User[]
  305. expect(res.body.total).to.equal(1)
  306. expect(users.length).to.equal(1)
  307. expect(users[ 0 ].username).to.equal('root')
  308. })
  309. it('Should search user by email', async function () {
  310. {
  311. const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
  312. const users = res.body.data as User[]
  313. expect(res.body.total).to.equal(1)
  314. expect(users.length).to.equal(1)
  315. expect(users[ 0 ].username).to.equal('user_1')
  316. expect(users[ 0 ].email).to.equal('user_1@example.com')
  317. }
  318. {
  319. const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
  320. const users = res.body.data as User[]
  321. expect(res.body.total).to.equal(2)
  322. expect(users.length).to.equal(2)
  323. expect(users[ 0 ].username).to.equal('root')
  324. expect(users[ 1 ].username).to.equal('user_1')
  325. }
  326. })
  327. })
  328. describe('Update my account', function () {
  329. it('Should update my password', async function () {
  330. await updateMyUser({
  331. url: server.url,
  332. accessToken: accessTokenUser,
  333. currentPassword: 'super password',
  334. newPassword: 'new password'
  335. })
  336. user.password = 'new password'
  337. await userLogin(server, user, 200)
  338. })
  339. it('Should be able to change the NSFW display attribute', async function () {
  340. await updateMyUser({
  341. url: server.url,
  342. accessToken: accessTokenUser,
  343. nsfwPolicy: 'do_not_list'
  344. })
  345. const res = await getMyUserInformation(server.url, accessTokenUser)
  346. const user = res.body
  347. expect(user.username).to.equal('user_1')
  348. expect(user.email).to.equal('user_1@example.com')
  349. expect(user.nsfwPolicy).to.equal('do_not_list')
  350. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  351. expect(user.id).to.be.a('number')
  352. expect(user.account.displayName).to.equal('user_1')
  353. expect(user.account.description).to.be.null
  354. })
  355. it('Should be able to change the autoPlayVideo attribute', async function () {
  356. await updateMyUser({
  357. url: server.url,
  358. accessToken: accessTokenUser,
  359. autoPlayVideo: false
  360. })
  361. const res = await getMyUserInformation(server.url, accessTokenUser)
  362. const user = res.body
  363. expect(user.autoPlayVideo).to.be.false
  364. })
  365. it('Should be able to change the email display attribute', async function () {
  366. await updateMyUser({
  367. url: server.url,
  368. accessToken: accessTokenUser,
  369. email: 'updated@example.com'
  370. })
  371. const res = await getMyUserInformation(server.url, accessTokenUser)
  372. const user = res.body
  373. expect(user.username).to.equal('user_1')
  374. expect(user.email).to.equal('updated@example.com')
  375. expect(user.nsfwPolicy).to.equal('do_not_list')
  376. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  377. expect(user.id).to.be.a('number')
  378. expect(user.account.displayName).to.equal('user_1')
  379. expect(user.account.description).to.be.null
  380. })
  381. it('Should be able to update my avatar', async function () {
  382. const fixture = 'avatar.png'
  383. await updateMyAvatar({
  384. url: server.url,
  385. accessToken: accessTokenUser,
  386. fixture
  387. })
  388. const res = await getMyUserInformation(server.url, accessTokenUser)
  389. const user = res.body
  390. await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
  391. })
  392. it('Should be able to update my display name', async function () {
  393. await updateMyUser({
  394. url: server.url,
  395. accessToken: accessTokenUser,
  396. displayName: 'new display name'
  397. })
  398. const res = await getMyUserInformation(server.url, accessTokenUser)
  399. const user = res.body
  400. expect(user.username).to.equal('user_1')
  401. expect(user.email).to.equal('updated@example.com')
  402. expect(user.nsfwPolicy).to.equal('do_not_list')
  403. expect(user.videoQuota).to.equal(2 * 1024 * 1024)
  404. expect(user.id).to.be.a('number')
  405. expect(user.account.displayName).to.equal('new display name')
  406. expect(user.account.description).to.be.null
  407. })
  408. it('Should be able to update my description', async function () {
  409. await updateMyUser({
  410. url: server.url,
  411. accessToken: accessTokenUser,
  412. description: 'my super description updated'
  413. })
  414. const res = await getMyUserInformation(server.url, accessTokenUser)
  415. const user = res.body
  416. expect(user.username).to.equal('user_1')
  417. expect(user.email).to.equal('updated@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('new display name')
  422. expect(user.account.description).to.equal('my super description updated')
  423. })
  424. })
  425. describe('Updating another user', function () {
  426. it('Should be able to update another user', async function () {
  427. await updateUser({
  428. url: server.url,
  429. userId,
  430. accessToken,
  431. email: 'updated2@example.com',
  432. emailVerified: true,
  433. videoQuota: 42,
  434. role: UserRole.MODERATOR,
  435. adminFlags: UserAdminFlag.NONE
  436. })
  437. const res = await getUserInformation(server.url, accessToken, userId)
  438. const user = res.body
  439. expect(user.username).to.equal('user_1')
  440. expect(user.email).to.equal('updated2@example.com')
  441. expect(user.emailVerified).to.be.true
  442. expect(user.nsfwPolicy).to.equal('do_not_list')
  443. expect(user.videoQuota).to.equal(42)
  444. expect(user.roleLabel).to.equal('Moderator')
  445. expect(user.id).to.be.a('number')
  446. expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
  447. })
  448. it('Should have removed the user token', async function () {
  449. await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
  450. accessTokenUser = await userLogin(server, user)
  451. })
  452. it('Should be able to update another user password', async function () {
  453. await updateUser({
  454. url: server.url,
  455. userId,
  456. accessToken,
  457. password: 'password updated'
  458. })
  459. await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
  460. await userLogin(server, user, 400)
  461. user.password = 'password updated'
  462. accessTokenUser = await userLogin(server, user)
  463. })
  464. })
  465. describe('Video blacklists', function () {
  466. it('Should be able to list video blacklist by a moderator', async function () {
  467. await getBlacklistedVideosList({ url: server.url, token: accessTokenUser })
  468. })
  469. })
  470. describe('Remove a user', function () {
  471. it('Should be able to remove this user', async function () {
  472. await removeUser(server.url, userId, accessToken)
  473. })
  474. it('Should not be able to login with this user', async function () {
  475. await userLogin(server, user, 400)
  476. })
  477. it('Should not have videos of this user', async function () {
  478. const res = await getVideosList(server.url)
  479. expect(res.body.total).to.equal(1)
  480. const video = res.body.data[ 0 ]
  481. expect(video.account.name).to.equal('root')
  482. })
  483. })
  484. describe('Registering a new user', function () {
  485. it('Should register a new user', async function () {
  486. await registerUser(server.url, 'user_15', 'my super password')
  487. })
  488. it('Should be able to login with this registered user', async function () {
  489. const user15 = {
  490. username: 'user_15',
  491. password: 'my super password'
  492. }
  493. accessToken = await userLogin(server, user15)
  494. })
  495. it('Should have the correct video quota', async function () {
  496. const res = await getMyUserInformation(server.url, accessToken)
  497. const user = res.body
  498. expect(user.videoQuota).to.equal(5 * 1024 * 1024)
  499. })
  500. it('Should remove me', async function () {
  501. {
  502. const res = await getUsersList(server.url, server.accessToken)
  503. expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
  504. }
  505. await deleteMe(server.url, accessToken)
  506. {
  507. const res = await getUsersList(server.url, server.accessToken)
  508. expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
  509. }
  510. })
  511. })
  512. describe('User blocking', function () {
  513. it('Should block and unblock a user', async function () {
  514. const user16 = {
  515. username: 'user_16',
  516. password: 'my super password'
  517. }
  518. const resUser = await createUser({
  519. url: server.url,
  520. accessToken: server.accessToken,
  521. username: user16.username,
  522. password: user16.password
  523. })
  524. const user16Id = resUser.body.user.id
  525. accessToken = await userLogin(server, user16)
  526. await getMyUserInformation(server.url, accessToken, 200)
  527. await blockUser(server.url, user16Id, server.accessToken)
  528. await getMyUserInformation(server.url, accessToken, 401)
  529. await userLogin(server, user16, 400)
  530. await unblockUser(server.url, user16Id, server.accessToken)
  531. accessToken = await userLogin(server, user16)
  532. await getMyUserInformation(server.url, accessToken, 200)
  533. })
  534. })
  535. after(function () {
  536. killallServers([ server ])
  537. })
  538. })