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