1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11 } from '@shared/server-commands'
13 describe('Test videos views', function () {
14 let servers: PeerTubeServer[]
15 let liveVideoId: string
17 let remoteVideoId: string
18 let userAccessToken: string
20 before(async function () {
23 servers = await createMultipleServers(2)
24 await setAccessTokensToServers(servers)
25 await setDefaultVideoChannel(servers)
27 await servers[0].config.enableLive({ allowReplay: false, transcoding: false });
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({
34 privacy: VideoPrivacy.PUBLIC,
35 channelId: servers[0].store.channel.id
39 userAccessToken = await servers[0].users.generateUserAndToken('user')
41 await doubleFollow(servers[0], servers[1])
44 describe('When viewing a video', async function () {
46 // TODO: implement it when we'll remove backward compatibility in REST API
47 it('Should fail without current time')
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 })
54 it('Should succeed with correct parameters', async function () {
55 await servers[0].views.view({ id: videoId, currentTime: 1 })
59 describe('When getting overall stats', function () {
61 it('Should fail with a remote video', async function () {
62 await servers[0].videoStats.getOverallStats({ videoId: remoteVideoId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
65 it('Should fail without token', async function () {
66 await servers[0].videoStats.getOverallStats({ videoId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
69 it('Should fail with another token', async function () {
70 await servers[0].videoStats.getOverallStats({
72 token: userAccessToken,
73 expectedStatus: HttpStatusCode.FORBIDDEN_403
77 it('Should fail with an invalid start date', async function () {
78 await servers[0].videoStats.getOverallStats({
80 startDate: 'fake' as any,
81 endDate: new Date().toISOString(),
82 expectedStatus: HttpStatusCode.BAD_REQUEST_400
86 it('Should fail with an invalid end date', async function () {
87 await servers[0].videoStats.getOverallStats({
89 startDate: new Date().toISOString(),
90 endDate: 'fake' as any,
91 expectedStatus: HttpStatusCode.BAD_REQUEST_400
95 it('Should succeed with the correct parameters', async function () {
96 await servers[0].videoStats.getOverallStats({
98 startDate: new Date().toISOString(),
99 endDate: new Date().toISOString()
104 describe('When getting timeserie stats', function () {
106 it('Should fail with a remote video', async function () {
107 await servers[0].videoStats.getTimeserieStats({
108 videoId: remoteVideoId,
110 expectedStatus: HttpStatusCode.FORBIDDEN_403
114 it('Should fail without token', async function () {
115 await servers[0].videoStats.getTimeserieStats({
119 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
123 it('Should fail with another token', async function () {
124 await servers[0].videoStats.getTimeserieStats({
126 token: userAccessToken,
128 expectedStatus: HttpStatusCode.FORBIDDEN_403
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 })
136 it('Should fail with an invalid start date', async function () {
137 await servers[0].videoStats.getTimeserieStats({
140 startDate: 'fake' as any,
142 expectedStatus: HttpStatusCode.BAD_REQUEST_400
146 it('Should fail with an invalid end date', async function () {
147 await servers[0].videoStats.getTimeserieStats({
150 startDate: new Date(),
151 endDate: 'fake' as any,
152 expectedStatus: HttpStatusCode.BAD_REQUEST_400
156 it('Should fail if start date is specified but not end date', async function () {
157 await servers[0].videoStats.getTimeserieStats({
160 startDate: new Date(),
161 expectedStatus: HttpStatusCode.BAD_REQUEST_400
165 it('Should fail if end date is specified but not start date', async function () {
166 await servers[0].videoStats.getTimeserieStats({
170 expectedStatus: HttpStatusCode.BAD_REQUEST_400
174 it('Should fail with a too big interval', async function () {
175 await servers[0].videoStats.getTimeserieStats({
178 startDate: new Date('2000-04-07T08:31:57.126Z'),
180 expectedStatus: HttpStatusCode.BAD_REQUEST_400
184 it('Should succeed with the correct parameters', async function () {
185 await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' })
189 describe('When getting retention stats', function () {
191 it('Should fail with a remote video', async function () {
192 await servers[0].videoStats.getRetentionStats({
193 videoId: remoteVideoId,
194 expectedStatus: HttpStatusCode.FORBIDDEN_403
198 it('Should fail without token', async function () {
199 await servers[0].videoStats.getRetentionStats({
202 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
206 it('Should fail with another token', async function () {
207 await servers[0].videoStats.getRetentionStats({
209 token: userAccessToken,
210 expectedStatus: HttpStatusCode.FORBIDDEN_403
214 it('Should fail on live video', async function () {
215 await servers[0].videoStats.getRetentionStats({ videoId: liveVideoId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
218 it('Should succeed with the correct parameters', async function () {
219 await servers[0].videoStats.getRetentionStats({ videoId })
223 after(async function () {
224 await cleanupTests(servers)