]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos-history.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/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 createSingleServer,
9 makeGetRequest,
10 makePostBodyRequest,
11 makePutBodyRequest,
12 PeerTubeServer,
13 setAccessTokensToServers
14 } from '@shared/extra-utils'
15 import { HttpStatusCode } from '@shared/models'
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: PeerTubeServer
22
23 // ---------------------------------------------------------------
24
25 before(async function () {
26 this.timeout(30000)
27
28 server = await createSingleServer(1)
29
30 await setAccessTokensToServers([ server ])
31
32 const { uuid } = await server.videos.upload()
33 watchingPath = '/api/v1/videos/' + uuid + '/watching'
34 })
35
36 describe('When notifying a user is watching a video', function () {
37
38 it('Should fail with an unauthenticated user', async function () {
39 const fields = { currentTime: 5 }
40 await makePutBodyRequest({ url: server.url, path: watchingPath, fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
41 })
42
43 it('Should fail with an incorrect video id', async function () {
44 const fields = { currentTime: 5 }
45 const path = '/api/v1/videos/blabla/watching'
46 await makePutBodyRequest({
47 url: server.url,
48 path,
49 fields,
50 token: server.accessToken,
51 expectedStatus: HttpStatusCode.BAD_REQUEST_400
52 })
53 })
54
55 it('Should fail with an unknown video', async function () {
56 const fields = { currentTime: 5 }
57 const path = '/api/v1/videos/d91fff41-c24d-4508-8e13-3bd5902c3b02/watching'
58
59 await makePutBodyRequest({
60 url: server.url,
61 path,
62 fields,
63 token: server.accessToken,
64 expectedStatus: HttpStatusCode.NOT_FOUND_404
65 })
66 })
67
68 it('Should fail with a bad current time', async function () {
69 const fields = { currentTime: 'hello' }
70 await makePutBodyRequest({
71 url: server.url,
72 path: watchingPath,
73 fields,
74 token: server.accessToken,
75 expectedStatus: HttpStatusCode.BAD_REQUEST_400
76 })
77 })
78
79 it('Should succeed with the correct parameters', async function () {
80 const fields = { currentTime: 5 }
81
82 await makePutBodyRequest({
83 url: server.url,
84 path: watchingPath,
85 fields,
86 token: server.accessToken,
87 expectedStatus: HttpStatusCode.NO_CONTENT_204
88 })
89 })
90 })
91
92 describe('When listing user videos history', function () {
93 it('Should fail with a bad start pagination', async function () {
94 await checkBadStartPagination(server.url, myHistoryPath, server.accessToken)
95 })
96
97 it('Should fail with a bad count pagination', async function () {
98 await checkBadCountPagination(server.url, myHistoryPath, server.accessToken)
99 })
100
101 it('Should fail with an unauthenticated user', async function () {
102 await makeGetRequest({ url: server.url, path: myHistoryPath, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
103 })
104
105 it('Should succeed with the correct params', async function () {
106 await makeGetRequest({ url: server.url, token: server.accessToken, path: myHistoryPath, expectedStatus: HttpStatusCode.OK_200 })
107 })
108 })
109
110 describe('When removing user videos history', function () {
111 it('Should fail with an unauthenticated user', async function () {
112 await makePostBodyRequest({ url: server.url, path: myHistoryPath + '/remove', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
113 })
114
115 it('Should fail with a bad beforeDate parameter', async function () {
116 const body = { beforeDate: '15' }
117 await makePostBodyRequest({
118 url: server.url,
119 token: server.accessToken,
120 path: myHistoryRemove,
121 fields: body,
122 expectedStatus: HttpStatusCode.BAD_REQUEST_400
123 })
124 })
125
126 it('Should succeed with a valid beforeDate param', async function () {
127 const body = { beforeDate: new Date().toISOString() }
128 await makePostBodyRequest({
129 url: server.url,
130 token: server.accessToken,
131 path: myHistoryRemove,
132 fields: body,
133 expectedStatus: HttpStatusCode.NO_CONTENT_204
134 })
135 })
136
137 it('Should succeed without body', async function () {
138 await makePostBodyRequest({
139 url: server.url,
140 token: server.accessToken,
141 path: myHistoryRemove,
142 expectedStatus: HttpStatusCode.NO_CONTENT_204
143 })
144 })
145 })
146
147 after(async function () {
148 await cleanupTests([ server ])
149 })
150 })