]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/video-channels.ts
Add external login 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 }) {
36 const { url, accountName, start, count, sort = 'createdAt', specialStatus = 200, withStats = false } = parameters
37
38 const path = '/api/v1/accounts/' + accountName + '/video-channels'
39
40 return makeGetRequest({
41 url,
42 path,
43 query: {
44 start,
45 count,
46 sort,
47 withStats
48 },
49 statusCodeExpected: specialStatus
50 })
51 }
52
53 function addVideoChannel (
54 url: string,
55 token: string,
56 videoChannelAttributesArg: VideoChannelCreate,
57 expectedStatus = 200
58 ) {
59 const path = '/api/v1/video-channels/'
60
61 // Default attributes
62 let attributes = {
63 displayName: 'my super video channel',
64 description: 'my super channel description',
65 support: 'my super channel support'
66 }
67 attributes = Object.assign(attributes, videoChannelAttributesArg)
68
69 return request(url)
70 .post(path)
71 .send(attributes)
72 .set('Accept', 'application/json')
73 .set('Authorization', 'Bearer ' + token)
74 .expect(expectedStatus)
75 }
76
77 function updateVideoChannel (
78 url: string,
79 token: string,
80 channelName: string,
81 attributes: VideoChannelUpdate,
82 expectedStatus = 204
83 ) {
84 const body: any = {}
85 const path = '/api/v1/video-channels/' + channelName
86
87 if (attributes.displayName) body.displayName = attributes.displayName
88 if (attributes.description) body.description = attributes.description
89 if (attributes.support) body.support = attributes.support
90 if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate
91
92 return request(url)
93 .put(path)
94 .send(body)
95 .set('Accept', 'application/json')
96 .set('Authorization', 'Bearer ' + token)
97 .expect(expectedStatus)
98 }
99
100 function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = 204) {
101 const path = '/api/v1/video-channels/' + channelName
102
103 return request(url)
104 .delete(path)
105 .set('Accept', 'application/json')
106 .set('Authorization', 'Bearer ' + token)
107 .expect(expectedStatus)
108 }
109
110 function getVideoChannel (url: string, channelName: string) {
111 const path = '/api/v1/video-channels/' + channelName
112
113 return request(url)
114 .get(path)
115 .set('Accept', 'application/json')
116 .expect(200)
117 .expect('Content-Type', /json/)
118 }
119
120 function updateVideoChannelAvatar (options: {
121 url: string
122 accessToken: string
123 fixture: string
124 videoChannelName: string | number
125 }) {
126
127 const path = '/api/v1/video-channels/' + options.videoChannelName + '/avatar/pick'
128
129 return updateAvatarRequest(Object.assign(options, { path }))
130 }
131
132 function setDefaultVideoChannel (servers: ServerInfo[]) {
133 const tasks: Promise<any>[] = []
134
135 for (const server of servers) {
136 const p = getMyUserInformation(server.url, server.accessToken)
137 .then(res => { server.videoChannel = (res.body as User).videoChannels[0] })
138
139 tasks.push(p)
140 }
141
142 return Promise.all(tasks)
143 }
144
145 // ---------------------------------------------------------------------------
146
147 export {
148 updateVideoChannelAvatar,
149 getVideoChannelsList,
150 getAccountVideoChannelsList,
151 addVideoChannel,
152 updateVideoChannel,
153 deleteVideoChannel,
154 getVideoChannel,
155 setDefaultVideoChannel
156 }