]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/views.ts
Move to new documentation links
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / views.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
4 import {
5 cleanupTests,
6 createMultipleServers,
7 doubleFollow,
8 PeerTubeServer,
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11 } from '@shared/server-commands'
12
13 describe('Test videos views', function () {
14 let servers: PeerTubeServer[]
15 let liveVideoId: string
16 let videoId: string
17 let remoteVideoId: string
18 let userAccessToken: string
19
20 before(async function () {
21 this.timeout(120000)
22
23 servers = await createMultipleServers(2)
24 await setAccessTokensToServers(servers)
25 await setDefaultVideoChannel(servers)
26
27 await servers[0].config.enableLive({ allowReplay: false, transcoding: false });
28
29 ({ uuid: videoId } = await servers[0].videos.quickUpload({ name: 'video' }));
30 ({ uuid: remoteVideoId } = await servers[1].videos.quickUpload({ name: 'video' }));
31 ({ uuid: liveVideoId } = await servers[0].live.create({
32 fields: {
33 name: 'live',
34 privacy: VideoPrivacy.PUBLIC,
35 channelId: servers[0].store.channel.id
36 }
37 }))
38
39 userAccessToken = await servers[0].users.generateUserAndToken('user')
40
41 await doubleFollow(servers[0], servers[1])
42 })
43
44 describe('When viewing a video', async function () {
45
46 // TODO: implement it when we'll remove backward compatibility in REST API
47 it('Should fail without current time')
48
49 it('Should fail with an invalid current time', async function () {
50 await servers[0].views.view({ id: videoId, currentTime: -1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
51 await servers[0].views.view({ id: videoId, currentTime: 10, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
52 })
53
54 it('Should succeed with correct parameters', async function () {
55 await servers[0].views.view({ id: videoId, currentTime: 1 })
56 })
57 })
58
59 describe('When getting overall stats', function () {
60
61 it('Should fail with a remote video', async function () {
62 await servers[0].videoStats.getOverallStats({ videoId: remoteVideoId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
63 })
64
65 it('Should fail without token', async function () {
66 await servers[0].videoStats.getOverallStats({ videoId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
67 })
68
69 it('Should fail with another token', async function () {
70 await servers[0].videoStats.getOverallStats({
71 videoId,
72 token: userAccessToken,
73 expectedStatus: HttpStatusCode.FORBIDDEN_403
74 })
75 })
76
77 it('Should fail with an invalid start date', async function () {
78 await servers[0].videoStats.getOverallStats({
79 videoId,
80 startDate: 'fake' as any,
81 endDate: new Date().toISOString(),
82 expectedStatus: HttpStatusCode.BAD_REQUEST_400
83 })
84 })
85
86 it('Should fail with an invalid end date', async function () {
87 await servers[0].videoStats.getOverallStats({
88 videoId,
89 startDate: new Date().toISOString(),
90 endDate: 'fake' as any,
91 expectedStatus: HttpStatusCode.BAD_REQUEST_400
92 })
93 })
94
95 it('Should succeed with the correct parameters', async function () {
96 await servers[0].videoStats.getOverallStats({
97 videoId,
98 startDate: new Date().toISOString(),
99 endDate: new Date().toISOString()
100 })
101 })
102 })
103
104 describe('When getting timeserie stats', function () {
105
106 it('Should fail with a remote video', async function () {
107 await servers[0].videoStats.getTimeserieStats({
108 videoId: remoteVideoId,
109 metric: 'viewers',
110 expectedStatus: HttpStatusCode.FORBIDDEN_403
111 })
112 })
113
114 it('Should fail without token', async function () {
115 await servers[0].videoStats.getTimeserieStats({
116 videoId,
117 token: null,
118 metric: 'viewers',
119 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
120 })
121 })
122
123 it('Should fail with another token', async function () {
124 await servers[0].videoStats.getTimeserieStats({
125 videoId,
126 token: userAccessToken,
127 metric: 'viewers',
128 expectedStatus: HttpStatusCode.FORBIDDEN_403
129 })
130 })
131
132 it('Should fail with an invalid metric', async function () {
133 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'hello' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
134 })
135
136 it('Should fail with an invalid start date', async function () {
137 await servers[0].videoStats.getTimeserieStats({
138 videoId,
139 metric: 'viewers',
140 startDate: 'fake' as any,
141 endDate: new Date(),
142 expectedStatus: HttpStatusCode.BAD_REQUEST_400
143 })
144 })
145
146 it('Should fail with an invalid end date', async function () {
147 await servers[0].videoStats.getTimeserieStats({
148 videoId,
149 metric: 'viewers',
150 startDate: new Date(),
151 endDate: 'fake' as any,
152 expectedStatus: HttpStatusCode.BAD_REQUEST_400
153 })
154 })
155
156 it('Should fail if start date is specified but not end date', async function () {
157 await servers[0].videoStats.getTimeserieStats({
158 videoId,
159 metric: 'viewers',
160 startDate: new Date(),
161 expectedStatus: HttpStatusCode.BAD_REQUEST_400
162 })
163 })
164
165 it('Should fail if end date is specified but not start date', async function () {
166 await servers[0].videoStats.getTimeserieStats({
167 videoId,
168 metric: 'viewers',
169 endDate: new Date(),
170 expectedStatus: HttpStatusCode.BAD_REQUEST_400
171 })
172 })
173
174 it('Should fail with a too big interval', async function () {
175 await servers[0].videoStats.getTimeserieStats({
176 videoId,
177 metric: 'viewers',
178 startDate: new Date('2000-04-07T08:31:57.126Z'),
179 endDate: new Date(),
180 expectedStatus: HttpStatusCode.BAD_REQUEST_400
181 })
182 })
183
184 it('Should succeed with the correct parameters', async function () {
185 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' })
186 })
187 })
188
189 describe('When getting retention stats', function () {
190
191 it('Should fail with a remote video', async function () {
192 await servers[0].videoStats.getRetentionStats({
193 videoId: remoteVideoId,
194 expectedStatus: HttpStatusCode.FORBIDDEN_403
195 })
196 })
197
198 it('Should fail without token', async function () {
199 await servers[0].videoStats.getRetentionStats({
200 videoId,
201 token: null,
202 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
203 })
204 })
205
206 it('Should fail with another token', async function () {
207 await servers[0].videoStats.getRetentionStats({
208 videoId,
209 token: userAccessToken,
210 expectedStatus: HttpStatusCode.FORBIDDEN_403
211 })
212 })
213
214 it('Should fail on live video', async function () {
215 await servers[0].videoStats.getRetentionStats({ videoId: liveVideoId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
216 })
217
218 it('Should succeed with the correct parameters', async function () {
219 await servers[0].videoStats.getRetentionStats({ videoId })
220 })
221 })
222
223 after(async function () {
224 await cleanupTests(servers)
225 })
226 })