video-nsfw.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
  4. import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models'
  5. function createOverviewRes (overview: VideosOverview) {
  6. const videos = overview.categories[0].videos
  7. return { data: videos, total: videos.length }
  8. }
  9. describe('Test video NSFW policy', function () {
  10. let server: PeerTubeServer
  11. let userAccessToken: string
  12. let customConfig: CustomConfig
  13. async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
  14. const user = await server.users.getMyInfo()
  15. const channelName = user.videoChannels[0].name
  16. const accountName = user.account.name + '@' + user.account.host
  17. const hasQuery = Object.keys(query).length !== 0
  18. let promises: Promise<ResultList<Video>>[]
  19. if (token) {
  20. promises = [
  21. server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
  22. server.videos.listWithToken({ token, ...query }),
  23. server.videos.listByAccount({ token, handle: accountName, ...query }),
  24. server.videos.listByChannel({ token, handle: channelName, ...query })
  25. ]
  26. // Overviews do not support video filters
  27. if (!hasQuery) {
  28. const p = server.overviews.getVideos({ page: 1, token })
  29. .then(res => createOverviewRes(res))
  30. promises.push(p)
  31. }
  32. return Promise.all(promises)
  33. }
  34. promises = [
  35. server.search.searchVideos({ search: 'n', sort: '-publishedAt' }),
  36. server.videos.list(),
  37. server.videos.listByAccount({ token: null, handle: accountName }),
  38. server.videos.listByChannel({ token: null, handle: channelName })
  39. ]
  40. // Overviews do not support video filters
  41. if (!hasQuery) {
  42. const p = server.overviews.getVideos({ page: 1 })
  43. .then(res => createOverviewRes(res))
  44. promises.push(p)
  45. }
  46. return Promise.all(promises)
  47. }
  48. before(async function () {
  49. this.timeout(50000)
  50. server = await createSingleServer(1)
  51. // Get the access tokens
  52. await setAccessTokensToServers([ server ])
  53. {
  54. const attributes = { name: 'nsfw', nsfw: true, category: 1 }
  55. await server.videos.upload({ attributes })
  56. }
  57. {
  58. const attributes = { name: 'normal', nsfw: false, category: 1 }
  59. await server.videos.upload({ attributes })
  60. }
  61. customConfig = await server.config.getCustomConfig()
  62. })
  63. describe('Instance default NSFW policy', function () {
  64. it('Should display NSFW videos with display default NSFW policy', async function () {
  65. const serverConfig = await server.config.getConfig()
  66. expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
  67. for (const body of await getVideosFunctions()) {
  68. expect(body.total).to.equal(2)
  69. const videos = body.data
  70. expect(videos).to.have.lengthOf(2)
  71. expect(videos[0].name).to.equal('normal')
  72. expect(videos[1].name).to.equal('nsfw')
  73. }
  74. })
  75. it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
  76. customConfig.instance.defaultNSFWPolicy = 'do_not_list'
  77. await server.config.updateCustomConfig({ newCustomConfig: customConfig })
  78. const serverConfig = await server.config.getConfig()
  79. expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
  80. for (const body of await getVideosFunctions()) {
  81. expect(body.total).to.equal(1)
  82. const videos = body.data
  83. expect(videos).to.have.lengthOf(1)
  84. expect(videos[0].name).to.equal('normal')
  85. }
  86. })
  87. it('Should display NSFW videos with blur default NSFW policy', async function () {
  88. customConfig.instance.defaultNSFWPolicy = 'blur'
  89. await server.config.updateCustomConfig({ newCustomConfig: customConfig })
  90. const serverConfig = await server.config.getConfig()
  91. expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
  92. for (const body of await getVideosFunctions()) {
  93. expect(body.total).to.equal(2)
  94. const videos = body.data
  95. expect(videos).to.have.lengthOf(2)
  96. expect(videos[0].name).to.equal('normal')
  97. expect(videos[1].name).to.equal('nsfw')
  98. }
  99. })
  100. })
  101. describe('User NSFW policy', function () {
  102. it('Should create a user having the default nsfw policy', async function () {
  103. const username = 'user1'
  104. const password = 'my super password'
  105. await server.users.create({ username, password })
  106. userAccessToken = await server.login.getAccessToken({ username, password })
  107. const user = await server.users.getMyInfo({ token: userAccessToken })
  108. expect(user.nsfwPolicy).to.equal('blur')
  109. })
  110. it('Should display NSFW videos with blur user NSFW policy', async function () {
  111. customConfig.instance.defaultNSFWPolicy = 'do_not_list'
  112. await server.config.updateCustomConfig({ newCustomConfig: customConfig })
  113. for (const body of await getVideosFunctions(userAccessToken)) {
  114. expect(body.total).to.equal(2)
  115. const videos = body.data
  116. expect(videos).to.have.lengthOf(2)
  117. expect(videos[0].name).to.equal('normal')
  118. expect(videos[1].name).to.equal('nsfw')
  119. }
  120. })
  121. it('Should display NSFW videos with display user NSFW policy', async function () {
  122. await server.users.updateMe({ nsfwPolicy: 'display' })
  123. for (const body of await getVideosFunctions(server.accessToken)) {
  124. expect(body.total).to.equal(2)
  125. const videos = body.data
  126. expect(videos).to.have.lengthOf(2)
  127. expect(videos[0].name).to.equal('normal')
  128. expect(videos[1].name).to.equal('nsfw')
  129. }
  130. })
  131. it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
  132. await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
  133. for (const body of await getVideosFunctions(server.accessToken)) {
  134. expect(body.total).to.equal(1)
  135. const videos = body.data
  136. expect(videos).to.have.lengthOf(1)
  137. expect(videos[0].name).to.equal('normal')
  138. }
  139. })
  140. it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
  141. const { total, data } = await server.videos.listMyVideos()
  142. expect(total).to.equal(2)
  143. expect(data).to.have.lengthOf(2)
  144. expect(data[0].name).to.equal('normal')
  145. expect(data[1].name).to.equal('nsfw')
  146. })
  147. it('Should display NSFW videos when the nsfw param === true', async function () {
  148. for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
  149. expect(body.total).to.equal(1)
  150. const videos = body.data
  151. expect(videos).to.have.lengthOf(1)
  152. expect(videos[0].name).to.equal('nsfw')
  153. }
  154. })
  155. it('Should hide NSFW videos when the nsfw param === true', async function () {
  156. for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
  157. expect(body.total).to.equal(1)
  158. const videos = body.data
  159. expect(videos).to.have.lengthOf(1)
  160. expect(videos[0].name).to.equal('normal')
  161. }
  162. })
  163. it('Should display both videos when the nsfw param === both', async function () {
  164. for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
  165. expect(body.total).to.equal(2)
  166. const videos = body.data
  167. expect(videos).to.have.lengthOf(2)
  168. expect(videos[0].name).to.equal('normal')
  169. expect(videos[1].name).to.equal('nsfw')
  170. }
  171. })
  172. })
  173. after(async function () {
  174. await cleanupTests([ server ])
  175. })
  176. })