1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
10 setAccessTokensToServers,
11 setDefaultVideoChannel
12 } from '@shared/server-commands'
14 describe('Test videos views', function () {
15 let servers: PeerTubeServer[]
16 let liveVideoId: string
18 let remoteVideoId: string
19 let userAccessToken: string
21 before(async function () {
24 servers = await createMultipleServers(2)
25 await setAccessTokensToServers(servers)
26 await setDefaultVideoChannel(servers)
28 await servers[0].config.enableLive({ allowReplay: false, transcoding: false });
30 ({ uuid: videoId } = await servers[0].videos.quickUpload({ name: 'video' }));
31 ({ uuid: remoteVideoId } = await servers[1].videos.quickUpload({ name: 'video' }));
32 ({ uuid: liveVideoId } = await servers[0].live.create({
35 privacy: VideoPrivacy.PUBLIC,
36 channelId: servers[0].store.channel.id
40 userAccessToken = await servers[0].users.generateUserAndToken('user')
42 await doubleFollow(servers[0], servers[1])
45 describe('When viewing a video', async function () {
47 // TODO: implement it when we'll remove backward compatibility in REST API
48 it('Should fail without current time')
50 it('Should fail with an invalid current time', async function () {
51 await servers[0].views.view({ id: videoId, currentTime: -1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
52 await servers[0].views.view({ id: videoId, currentTime: 10, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
55 it('Should succeed with correct parameters', async function () {
56 await servers[0].views.view({ id: videoId, currentTime: 1 })
60 describe('When getting overall stats', function () {
62 it('Should fail with a remote video', async function () {
63 await servers[0].videoStats.getOverallStats({ videoId: remoteVideoId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
66 it('Should fail without token', async function () {
67 await servers[0].videoStats.getOverallStats({ videoId: videoId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
70 it('Should fail with another token', async function () {
71 await servers[0].videoStats.getOverallStats({
73 token: userAccessToken,
74 expectedStatus: HttpStatusCode.FORBIDDEN_403
78 it('Should fail with an invalid start date', async function () {
79 await servers[0].videoStats.getOverallStats({
81 startDate: 'fake' as any,
82 endDate: new Date().toISOString(),
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400
87 it('Should fail with an invalid end date', async function () {
88 await servers[0].videoStats.getOverallStats({
90 startDate: new Date().toISOString(),
91 endDate: 'fake' as any,
92 expectedStatus: HttpStatusCode.BAD_REQUEST_400
96 it('Should succeed with the correct parameters', async function () {
97 await servers[0].videoStats.getOverallStats({
99 startDate: new Date().toISOString(),
100 endDate: new Date().toISOString()
105 describe('When getting timeserie stats', function () {
107 it('Should fail with a remote video', async function () {
108 await servers[0].videoStats.getTimeserieStats({
109 videoId: remoteVideoId,
111 expectedStatus: HttpStatusCode.FORBIDDEN_403
115 it('Should fail without token', async function () {
116 await servers[0].videoStats.getTimeserieStats({
120 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
124 it('Should fail with another token', async function () {
125 await servers[0].videoStats.getTimeserieStats({
127 token: userAccessToken,
129 expectedStatus: HttpStatusCode.FORBIDDEN_403
133 it('Should fail with an invalid metric', async function () {
134 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'hello' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
137 it('Should fail with an invalid start date', async function () {
138 await servers[0].videoStats.getTimeserieStats({
141 startDate: 'fake' as any,
143 expectedStatus: HttpStatusCode.BAD_REQUEST_400
147 it('Should fail with an invalid end date', async function () {
148 await servers[0].videoStats.getTimeserieStats({
151 startDate: new Date(),
152 endDate: 'fake' as any,
153 expectedStatus: HttpStatusCode.BAD_REQUEST_400
157 it('Should fail if start date is specified but not end date', async function () {
158 await servers[0].videoStats.getTimeserieStats({
161 startDate: new Date(),
162 expectedStatus: HttpStatusCode.BAD_REQUEST_400
166 it('Should fail if end date is specified but not start date', async function () {
167 await servers[0].videoStats.getTimeserieStats({
171 expectedStatus: HttpStatusCode.BAD_REQUEST_400
175 it('Should fail with a too big interval', async function () {
176 await servers[0].videoStats.getTimeserieStats({
179 startDate: new Date('2021-04-07T08:31:57.126Z'),
181 expectedStatus: HttpStatusCode.BAD_REQUEST_400
185 it('Should succeed with the correct parameters', async function () {
186 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' })
190 describe('When getting retention stats', function () {
192 it('Should fail with a remote video', async function () {
193 await servers[0].videoStats.getRetentionStats({
194 videoId: remoteVideoId,
195 expectedStatus: HttpStatusCode.FORBIDDEN_403
199 it('Should fail without token', async function () {
200 await servers[0].videoStats.getRetentionStats({
203 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
207 it('Should fail with another token', async function () {
208 await servers[0].videoStats.getRetentionStats({
210 token: userAccessToken,
211 expectedStatus: HttpStatusCode.FORBIDDEN_403
215 it('Should fail on live video', async function () {
216 await servers[0].videoStats.getRetentionStats({ videoId: liveVideoId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
219 it('Should succeed with the correct parameters', async function () {
220 await servers[0].videoStats.getRetentionStats({ videoId })
224 after(async function () {
225 await cleanupTests(servers)