1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { checkBadCountPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
14 setAccessTokensToServers
15 } from '@shared/server-commands'
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
24 // ---------------------------------------------------------------
26 before(async function () {
29 server = await createSingleServer(1)
31 await setAccessTokensToServers([ server ])
33 const { id, uuid } = await server.videos.upload()
34 watchingPath = '/api/v1/videos/' + uuid + '/watching'
38 describe('When notifying a user is watching a video', function () {
40 it('Should fail with an unauthenticated user', async function () {
41 const fields = { currentTime: 5 }
42 await makePutBodyRequest({ url: server.url, path: watchingPath, fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
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({
52 token: server.accessToken,
53 expectedStatus: HttpStatusCode.BAD_REQUEST_400
57 it('Should fail with an unknown video', async function () {
58 const fields = { currentTime: 5 }
59 const path = '/api/v1/videos/d91fff41-c24d-4508-8e13-3bd5902c3b02/watching'
61 await makePutBodyRequest({
65 token: server.accessToken,
66 expectedStatus: HttpStatusCode.NOT_FOUND_404
70 it('Should fail with a bad current time', async function () {
71 const fields = { currentTime: 'hello' }
72 await makePutBodyRequest({
76 token: server.accessToken,
77 expectedStatus: HttpStatusCode.BAD_REQUEST_400
81 it('Should succeed with the correct parameters', async function () {
82 const fields = { currentTime: 5 }
84 await makePutBodyRequest({
88 token: server.accessToken,
89 expectedStatus: HttpStatusCode.NO_CONTENT_204
94 describe('When listing user videos history', function () {
95 it('Should fail with a bad start pagination', async function () {
96 await checkBadStartPagination(server.url, myHistoryPath, server.accessToken)
99 it('Should fail with a bad count pagination', async function () {
100 await checkBadCountPagination(server.url, myHistoryPath, server.accessToken)
103 it('Should fail with an unauthenticated user', async function () {
104 await makeGetRequest({ url: server.url, path: myHistoryPath, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
107 it('Should succeed with the correct params', async function () {
108 await makeGetRequest({ url: server.url, token: server.accessToken, path: myHistoryPath, expectedStatus: HttpStatusCode.OK_200 })
112 describe('When removing a specific user video history element', function () {
116 path = myHistoryPath + '/' + videoId
119 it('Should fail with an unauthenticated user', async function () {
120 await makeDeleteRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
123 it('Should fail with a bad videoId parameter', async function () {
124 await makeDeleteRequest({
126 token: server.accessToken,
127 path: myHistoryRemove + '/hi',
128 expectedStatus: HttpStatusCode.BAD_REQUEST_400
132 it('Should succeed with the correct parameters', async function () {
133 await makeDeleteRequest({
135 token: server.accessToken,
137 expectedStatus: HttpStatusCode.NO_CONTENT_204
142 describe('When removing all user videos history', function () {
143 it('Should fail with an unauthenticated user', async function () {
144 await makePostBodyRequest({ url: server.url, path: myHistoryPath + '/remove', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
147 it('Should fail with a bad beforeDate parameter', async function () {
148 const body = { beforeDate: '15' }
149 await makePostBodyRequest({
151 token: server.accessToken,
152 path: myHistoryRemove,
154 expectedStatus: HttpStatusCode.BAD_REQUEST_400
158 it('Should succeed with a valid beforeDate param', async function () {
159 const body = { beforeDate: new Date().toISOString() }
160 await makePostBodyRequest({
162 token: server.accessToken,
163 path: myHistoryRemove,
165 expectedStatus: HttpStatusCode.NO_CONTENT_204
169 it('Should succeed without body', async function () {
170 await makePostBodyRequest({
172 token: server.accessToken,
173 path: myHistoryRemove,
174 expectedStatus: HttpStatusCode.NO_CONTENT_204
179 after(async function () {
180 await cleanupTests([ server ])