config.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { omit } from 'lodash'
  3. import 'mocha'
  4. import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
  5. import {
  6. cleanupTests,
  7. createUser,
  8. flushAndRunServer,
  9. immutableAssign,
  10. makeDeleteRequest,
  11. makeGetRequest,
  12. makePutBodyRequest,
  13. ServerInfo,
  14. setAccessTokensToServers,
  15. userLogin
  16. } from '../../../../shared/extra-utils'
  17. import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
  18. describe('Test config API validators', function () {
  19. const path = '/api/v1/config/custom'
  20. let server: ServerInfo
  21. let userAccessToken: string
  22. const updateParams: CustomConfig = {
  23. instance: {
  24. name: 'PeerTube updated',
  25. shortDescription: 'my short description',
  26. description: 'my super description',
  27. terms: 'my super terms',
  28. codeOfConduct: 'my super coc',
  29. creationReason: 'my super reason',
  30. moderationInformation: 'my super moderation information',
  31. administrator: 'Kuja',
  32. maintenanceLifetime: 'forever',
  33. businessModel: 'my super business model',
  34. hardwareInformation: '2vCore 3GB RAM',
  35. languages: [ 'en', 'es' ],
  36. categories: [ 1, 2 ],
  37. isNSFW: true,
  38. defaultClientRoute: '/videos/recently-added',
  39. defaultNSFWPolicy: 'blur',
  40. customizations: {
  41. javascript: 'alert("coucou")',
  42. css: 'body { background-color: red; }'
  43. }
  44. },
  45. theme: {
  46. default: 'default'
  47. },
  48. services: {
  49. twitter: {
  50. username: '@MySuperUsername',
  51. whitelisted: true
  52. }
  53. },
  54. cache: {
  55. previews: {
  56. size: 2
  57. },
  58. captions: {
  59. size: 3
  60. }
  61. },
  62. signup: {
  63. enabled: false,
  64. limit: 5,
  65. requiresEmailVerification: false
  66. },
  67. admin: {
  68. email: 'superadmin1@example.com'
  69. },
  70. contactForm: {
  71. enabled: false
  72. },
  73. user: {
  74. videoQuota: 5242881,
  75. videoQuotaDaily: 318742
  76. },
  77. transcoding: {
  78. enabled: true,
  79. allowAdditionalExtensions: true,
  80. allowAudioFiles: true,
  81. threads: 1,
  82. resolutions: {
  83. '0p': false,
  84. '240p': false,
  85. '360p': true,
  86. '480p': true,
  87. '720p': false,
  88. '1080p': false,
  89. '1440p': false,
  90. '2160p': false
  91. },
  92. webtorrent: {
  93. enabled: true
  94. },
  95. hls: {
  96. enabled: false
  97. }
  98. },
  99. live: {
  100. enabled: true,
  101. allowReplay: false,
  102. maxDuration: 30,
  103. maxInstanceLives: -1,
  104. maxUserLives: 50,
  105. transcoding: {
  106. enabled: true,
  107. threads: 4,
  108. resolutions: {
  109. '240p': true,
  110. '360p': true,
  111. '480p': true,
  112. '720p': true,
  113. '1080p': true,
  114. '1440p': true,
  115. '2160p': true
  116. }
  117. }
  118. },
  119. import: {
  120. videos: {
  121. http: {
  122. enabled: false
  123. },
  124. torrent: {
  125. enabled: false
  126. }
  127. }
  128. },
  129. autoBlacklist: {
  130. videos: {
  131. ofUsers: {
  132. enabled: false
  133. }
  134. }
  135. },
  136. followers: {
  137. instance: {
  138. enabled: false,
  139. manualApproval: true
  140. }
  141. },
  142. followings: {
  143. instance: {
  144. autoFollowBack: {
  145. enabled: true
  146. },
  147. autoFollowIndex: {
  148. enabled: true,
  149. indexUrl: 'https://index.example.com'
  150. }
  151. }
  152. },
  153. broadcastMessage: {
  154. enabled: true,
  155. dismissable: true,
  156. message: 'super message',
  157. level: 'warning'
  158. },
  159. search: {
  160. remoteUri: {
  161. users: true,
  162. anonymous: true
  163. },
  164. searchIndex: {
  165. enabled: true,
  166. url: 'https://search.joinpeertube.org',
  167. disableLocalSearch: true,
  168. isDefaultSearch: true
  169. }
  170. }
  171. }
  172. // ---------------------------------------------------------------
  173. before(async function () {
  174. this.timeout(30000)
  175. server = await flushAndRunServer(1)
  176. await setAccessTokensToServers([ server ])
  177. const user = {
  178. username: 'user1',
  179. password: 'password'
  180. }
  181. await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  182. userAccessToken = await userLogin(server, user)
  183. })
  184. describe('When getting the configuration', function () {
  185. it('Should fail without token', async function () {
  186. await makeGetRequest({
  187. url: server.url,
  188. path,
  189. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  190. })
  191. })
  192. it('Should fail if the user is not an administrator', async function () {
  193. await makeGetRequest({
  194. url: server.url,
  195. path,
  196. token: userAccessToken,
  197. statusCodeExpected: HttpStatusCode.FORBIDDEN_403
  198. })
  199. })
  200. })
  201. describe('When updating the configuration', function () {
  202. it('Should fail without token', async function () {
  203. await makePutBodyRequest({
  204. url: server.url,
  205. path,
  206. fields: updateParams,
  207. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  208. })
  209. })
  210. it('Should fail if the user is not an administrator', async function () {
  211. await makePutBodyRequest({
  212. url: server.url,
  213. path,
  214. fields: updateParams,
  215. token: userAccessToken,
  216. statusCodeExpected: HttpStatusCode.FORBIDDEN_403
  217. })
  218. })
  219. it('Should fail if it misses a key', async function () {
  220. const newUpdateParams = omit(updateParams, 'admin.email')
  221. await makePutBodyRequest({
  222. url: server.url,
  223. path,
  224. fields: newUpdateParams,
  225. token: server.accessToken,
  226. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  227. })
  228. })
  229. it('Should fail with a bad default NSFW policy', async function () {
  230. const newUpdateParams = immutableAssign(updateParams, {
  231. instance: {
  232. defaultNSFWPolicy: 'hello'
  233. }
  234. })
  235. await makePutBodyRequest({
  236. url: server.url,
  237. path,
  238. fields: newUpdateParams,
  239. token: server.accessToken,
  240. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  241. })
  242. })
  243. it('Should fail if email disabled and signup requires email verification', async function () {
  244. // opposite scenario - success when enable enabled - covered via tests/api/users/user-verification.ts
  245. const newUpdateParams = immutableAssign(updateParams, {
  246. signup: {
  247. enabled: true,
  248. limit: 5,
  249. requiresEmailVerification: true
  250. }
  251. })
  252. await makePutBodyRequest({
  253. url: server.url,
  254. path,
  255. fields: newUpdateParams,
  256. token: server.accessToken,
  257. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  258. })
  259. })
  260. it('Should fail with a disabled webtorrent & hls transcoding', async function () {
  261. const newUpdateParams = immutableAssign(updateParams, {
  262. transcoding: {
  263. hls: {
  264. enabled: false
  265. },
  266. webtorrent: {
  267. enabled: false
  268. }
  269. }
  270. })
  271. await makePutBodyRequest({
  272. url: server.url,
  273. path,
  274. fields: newUpdateParams,
  275. token: server.accessToken,
  276. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  277. })
  278. })
  279. it('Should success with the correct parameters', async function () {
  280. await makePutBodyRequest({
  281. url: server.url,
  282. path,
  283. fields: updateParams,
  284. token: server.accessToken,
  285. statusCodeExpected: HttpStatusCode.OK_200
  286. })
  287. })
  288. })
  289. describe('When deleting the configuration', function () {
  290. it('Should fail without token', async function () {
  291. await makeDeleteRequest({
  292. url: server.url,
  293. path,
  294. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  295. })
  296. })
  297. it('Should fail if the user is not an administrator', async function () {
  298. await makeDeleteRequest({
  299. url: server.url,
  300. path,
  301. token: userAccessToken,
  302. statusCodeExpected: HttpStatusCode.FORBIDDEN_403
  303. })
  304. })
  305. })
  306. after(async function () {
  307. await cleanupTests([ server ])
  308. })
  309. })