config.ts 6.8 KB

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