config.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. createUser, flushTests, killallServers, makeDeleteRequest, makeGetRequest, makePutBodyRequest, flushAndRunServer, ServerInfo,
  7. setAccessTokensToServers, userLogin, immutableAssign
  8. } from '../../../../shared/extra-utils'
  9. describe('Test config API validators', function () {
  10. const path = '/api/v1/config/custom'
  11. let server: ServerInfo
  12. let userAccessToken: string
  13. const updateParams: CustomConfig = {
  14. instance: {
  15. name: 'PeerTube updated',
  16. shortDescription: 'my short description',
  17. description: 'my super description',
  18. terms: 'my super terms',
  19. isNSFW: true,
  20. defaultClientRoute: '/videos/recently-added',
  21. defaultNSFWPolicy: 'blur',
  22. customizations: {
  23. javascript: 'alert("coucou")',
  24. css: 'body { background-color: red; }'
  25. }
  26. },
  27. services: {
  28. twitter: {
  29. username: '@MySuperUsername',
  30. whitelisted: true
  31. }
  32. },
  33. cache: {
  34. previews: {
  35. size: 2
  36. },
  37. captions: {
  38. size: 3
  39. }
  40. },
  41. signup: {
  42. enabled: false,
  43. limit: 5,
  44. requiresEmailVerification: false
  45. },
  46. admin: {
  47. email: 'superadmin1@example.com'
  48. },
  49. contactForm: {
  50. enabled: false
  51. },
  52. user: {
  53. videoQuota: 5242881,
  54. videoQuotaDaily: 318742
  55. },
  56. transcoding: {
  57. enabled: true,
  58. allowAdditionalExtensions: true,
  59. threads: 1,
  60. resolutions: {
  61. '240p': false,
  62. '360p': true,
  63. '480p': true,
  64. '720p': false,
  65. '1080p': false
  66. },
  67. hls: {
  68. enabled: false
  69. }
  70. },
  71. import: {
  72. videos: {
  73. http: {
  74. enabled: false
  75. },
  76. torrent: {
  77. enabled: false
  78. }
  79. }
  80. },
  81. autoBlacklist: {
  82. videos: {
  83. ofUsers: {
  84. enabled: false
  85. }
  86. }
  87. },
  88. followers: {
  89. instance: {
  90. enabled: false,
  91. manualApproval: true
  92. }
  93. }
  94. }
  95. // ---------------------------------------------------------------
  96. before(async function () {
  97. this.timeout(30000)
  98. server = await flushAndRunServer(1)
  99. await setAccessTokensToServers([ server ])
  100. const user = {
  101. username: 'user1',
  102. password: 'password'
  103. }
  104. await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  105. userAccessToken = await userLogin(server, user)
  106. })
  107. describe('When getting the configuration', function () {
  108. it('Should fail without token', async function () {
  109. await makeGetRequest({
  110. url: server.url,
  111. path,
  112. statusCodeExpected: 401
  113. })
  114. })
  115. it('Should fail if the user is not an administrator', async function () {
  116. await makeGetRequest({
  117. url: server.url,
  118. path,
  119. token: userAccessToken,
  120. statusCodeExpected: 403
  121. })
  122. })
  123. })
  124. describe('When updating the configuration', function () {
  125. it('Should fail without token', async function () {
  126. await makePutBodyRequest({
  127. url: server.url,
  128. path,
  129. fields: updateParams,
  130. statusCodeExpected: 401
  131. })
  132. })
  133. it('Should fail if the user is not an administrator', async function () {
  134. await makePutBodyRequest({
  135. url: server.url,
  136. path,
  137. fields: updateParams,
  138. token: userAccessToken,
  139. statusCodeExpected: 403
  140. })
  141. })
  142. it('Should fail if it misses a key', async function () {
  143. const newUpdateParams = omit(updateParams, 'admin.email')
  144. await makePutBodyRequest({
  145. url: server.url,
  146. path,
  147. fields: newUpdateParams,
  148. token: server.accessToken,
  149. statusCodeExpected: 400
  150. })
  151. })
  152. it('Should fail with a bad default NSFW policy', async function () {
  153. const newUpdateParams = immutableAssign(updateParams, {
  154. instance: {
  155. defaultNSFWPolicy: 'hello'
  156. }
  157. })
  158. await makePutBodyRequest({
  159. url: server.url,
  160. path,
  161. fields: newUpdateParams,
  162. token: server.accessToken,
  163. statusCodeExpected: 400
  164. })
  165. })
  166. it('Should fail if email disabled and signup requires email verification', async function () {
  167. // opposite scenario - succcess when enable enabled - covered via tests/api/users/user-verification.ts
  168. const newUpdateParams = immutableAssign(updateParams, {
  169. signup: {
  170. enabled: true,
  171. limit: 5,
  172. requiresEmailVerification: true
  173. }
  174. })
  175. await makePutBodyRequest({
  176. url: server.url,
  177. path,
  178. fields: newUpdateParams,
  179. token: server.accessToken,
  180. statusCodeExpected: 400
  181. })
  182. })
  183. it('Should success with the correct parameters', async function () {
  184. await makePutBodyRequest({
  185. url: server.url,
  186. path,
  187. fields: updateParams,
  188. token: server.accessToken,
  189. statusCodeExpected: 200
  190. })
  191. })
  192. })
  193. describe('When deleting the configuration', function () {
  194. it('Should fail without token', async function () {
  195. await makeDeleteRequest({
  196. url: server.url,
  197. path,
  198. statusCodeExpected: 401
  199. })
  200. })
  201. it('Should fail if the user is not an administrator', async function () {
  202. await makeDeleteRequest({
  203. url: server.url,
  204. path,
  205. token: userAccessToken,
  206. statusCodeExpected: 403
  207. })
  208. })
  209. })
  210. after(function () {
  211. killallServers([ server ])
  212. })
  213. })