Move to eslint
[oweals/peertube.git] / server / tests / api / check-params / videos-history.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import {
5   checkBadCountPagination,
6   checkBadStartPagination,
7   cleanupTests,
8   flushAndRunServer,
9   makeGetRequest,
10   makePostBodyRequest,
11   makePutBodyRequest,
12   ServerInfo,
13   setAccessTokensToServers,
14   uploadVideo
15 } from '../../../../shared/extra-utils'
16
17 describe('Test videos history API validator', function () {
18   const myHistoryPath = '/api/v1/users/me/history/videos'
19   const myHistoryRemove = myHistoryPath + '/remove'
20   let watchingPath: string
21   let server: ServerInfo
22
23   // ---------------------------------------------------------------
24
25   before(async function () {
26     this.timeout(30000)
27
28     server = await flushAndRunServer(1)
29
30     await setAccessTokensToServers([ server ])
31
32     const res = await uploadVideo(server.url, server.accessToken, {})
33     const videoUUID = res.body.video.uuid
34
35     watchingPath = '/api/v1/videos/' + videoUUID + '/watching'
36   })
37
38   describe('When notifying a user is watching a video', function () {
39
40     it('Should fail with an unauthenticated user', async function () {
41       const fields = { currentTime: 5 }
42       await makePutBodyRequest({ url: server.url, path: watchingPath, fields, statusCodeExpected: 401 })
43     })
44
45     it('Should fail with an incorrect video id', async function () {
46       const fields = { currentTime: 5 }
47       const path = '/api/v1/videos/blabla/watching'
48       await makePutBodyRequest({ url: server.url, path, fields, token: server.accessToken, statusCodeExpected: 400 })
49     })
50
51     it('Should fail with an unknown video', async function () {
52       const fields = { currentTime: 5 }
53       const path = '/api/v1/videos/d91fff41-c24d-4508-8e13-3bd5902c3b02/watching'
54
55       await makePutBodyRequest({ url: server.url, path, fields, token: server.accessToken, statusCodeExpected: 404 })
56     })
57
58     it('Should fail with a bad current time', async function () {
59       const fields = { currentTime: 'hello' }
60       await makePutBodyRequest({ url: server.url, path: watchingPath, fields, token: server.accessToken, statusCodeExpected: 400 })
61     })
62
63     it('Should succeed with the correct parameters', async function () {
64       const fields = { currentTime: 5 }
65
66       await makePutBodyRequest({ url: server.url, path: watchingPath, fields, token: server.accessToken, statusCodeExpected: 204 })
67     })
68   })
69
70   describe('When listing user videos history', function () {
71     it('Should fail with a bad start pagination', async function () {
72       await checkBadStartPagination(server.url, myHistoryPath, server.accessToken)
73     })
74
75     it('Should fail with a bad count pagination', async function () {
76       await checkBadCountPagination(server.url, myHistoryPath, server.accessToken)
77     })
78
79     it('Should fail with an unauthenticated user', async function () {
80       await makeGetRequest({ url: server.url, path: myHistoryPath, statusCodeExpected: 401 })
81     })
82
83     it('Should succeed with the correct params', async function () {
84       await makeGetRequest({ url: server.url, token: server.accessToken, path: myHistoryPath, statusCodeExpected: 200 })
85     })
86   })
87
88   describe('When removing user videos history', function () {
89     it('Should fail with an unauthenticated user', async function () {
90       await makePostBodyRequest({ url: server.url, path: myHistoryPath + '/remove', statusCodeExpected: 401 })
91     })
92
93     it('Should fail with a bad beforeDate parameter', async function () {
94       const body = { beforeDate: '15' }
95       await makePostBodyRequest({
96         url: server.url,
97         token: server.accessToken,
98         path: myHistoryRemove,
99         fields: body,
100         statusCodeExpected: 400
101       })
102     })
103
104     it('Should succeed with a valid beforeDate param', async function () {
105       const body = { beforeDate: new Date().toISOString() }
106       await makePostBodyRequest({
107         url: server.url,
108         token: server.accessToken,
109         path: myHistoryRemove,
110         fields: body,
111         statusCodeExpected: 204
112       })
113     })
114
115     it('Should succeed without body', async function () {
116       await makePostBodyRequest({
117         url: server.url,
118         token: server.accessToken,
119         path: myHistoryRemove,
120         statusCodeExpected: 204
121       })
122     })
123   })
124
125   after(async function () {
126     await cleanupTests([ server ])
127   })
128 })