video-channels.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import * as request from 'supertest'
  2. import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
  3. import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
  4. import { makeGetRequest, updateAvatarRequest } from '../requests/requests'
  5. import { ServerInfo } from '../server/servers'
  6. import { User } from '../../models/users/user.model'
  7. import { getMyUserInformation } from '../users/users'
  8. function getVideoChannelsList (url: string, start: number, count: number, sort?: string) {
  9. const path = '/api/v1/video-channels'
  10. const req = request(url)
  11. .get(path)
  12. .query({ start: start })
  13. .query({ count: count })
  14. if (sort) req.query({ sort })
  15. return req.set('Accept', 'application/json')
  16. .expect(200)
  17. .expect('Content-Type', /json/)
  18. }
  19. function getAccountVideoChannelsList (parameters: {
  20. url: string,
  21. accountName: string,
  22. start?: number,
  23. count?: number,
  24. sort?: string,
  25. specialStatus?: number
  26. }) {
  27. const { url, accountName, start, count, sort = 'createdAt', specialStatus = 200 } = parameters
  28. const path = '/api/v1/accounts/' + accountName + '/video-channels'
  29. return makeGetRequest({
  30. url,
  31. path,
  32. query: {
  33. start,
  34. count,
  35. sort
  36. },
  37. statusCodeExpected: specialStatus
  38. })
  39. }
  40. function addVideoChannel (
  41. url: string,
  42. token: string,
  43. videoChannelAttributesArg: VideoChannelCreate,
  44. expectedStatus = 200
  45. ) {
  46. const path = '/api/v1/video-channels/'
  47. // Default attributes
  48. let attributes = {
  49. displayName: 'my super video channel',
  50. description: 'my super channel description',
  51. support: 'my super channel support'
  52. }
  53. attributes = Object.assign(attributes, videoChannelAttributesArg)
  54. return request(url)
  55. .post(path)
  56. .send(attributes)
  57. .set('Accept', 'application/json')
  58. .set('Authorization', 'Bearer ' + token)
  59. .expect(expectedStatus)
  60. }
  61. function updateVideoChannel (
  62. url: string,
  63. token: string,
  64. channelName: string,
  65. attributes: VideoChannelUpdate,
  66. expectedStatus = 204
  67. ) {
  68. const body: any = {}
  69. const path = '/api/v1/video-channels/' + channelName
  70. if (attributes.displayName) body.displayName = attributes.displayName
  71. if (attributes.description) body.description = attributes.description
  72. if (attributes.support) body.support = attributes.support
  73. if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate
  74. return request(url)
  75. .put(path)
  76. .send(body)
  77. .set('Accept', 'application/json')
  78. .set('Authorization', 'Bearer ' + token)
  79. .expect(expectedStatus)
  80. }
  81. function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = 204) {
  82. const path = '/api/v1/video-channels/' + channelName
  83. return request(url)
  84. .delete(path)
  85. .set('Accept', 'application/json')
  86. .set('Authorization', 'Bearer ' + token)
  87. .expect(expectedStatus)
  88. }
  89. function getVideoChannel (url: string, channelName: string) {
  90. const path = '/api/v1/video-channels/' + channelName
  91. return request(url)
  92. .get(path)
  93. .set('Accept', 'application/json')
  94. .expect(200)
  95. .expect('Content-Type', /json/)
  96. }
  97. function updateVideoChannelAvatar (options: {
  98. url: string,
  99. accessToken: string,
  100. fixture: string,
  101. videoChannelName: string | number
  102. }) {
  103. const path = '/api/v1/video-channels/' + options.videoChannelName + '/avatar/pick'
  104. return updateAvatarRequest(Object.assign(options, { path }))
  105. }
  106. function setDefaultVideoChannel (servers: ServerInfo[]) {
  107. const tasks: Promise<any>[] = []
  108. for (const server of servers) {
  109. const p = getMyUserInformation(server.url, server.accessToken)
  110. .then(res => server.videoChannel = (res.body as User).videoChannels[0])
  111. tasks.push(p)
  112. }
  113. return Promise.all(tasks)
  114. }
  115. // ---------------------------------------------------------------------------
  116. export {
  117. updateVideoChannelAvatar,
  118. getVideoChannelsList,
  119. getAccountVideoChannelsList,
  120. addVideoChannel,
  121. updateVideoChannel,
  122. deleteVideoChannel,
  123. getVideoChannel,
  124. setDefaultVideoChannel
  125. }