video-channels.ts 3.8 KB

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