aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/videos/videos-history.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-24 13:36:47 +0100
committerChocobozzz <chocobozzz@cpy.re>2022-04-15 09:49:35 +0200
commitb211106695bb82f6c32e53306081b5262c3d109d (patch)
treefa187de1c33b0956665f5362e29af6b0f6d8bb57 /server/tests/api/videos/videos-history.ts
parent69d48ee30c9d47cddf0c3c047dc99a99dcb6e894 (diff)
downloadPeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.gz
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.zst
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.zip
Support video views/viewers stats in server
* Add "currentTime" and "event" body params to view endpoint * Merge watching and view endpoints * Introduce WatchAction AP activity * Add tables to store viewer information of local videos * Add endpoints to fetch video views/viewers stats of local videos * Refactor views/viewers handlers * Support "views" and "viewers" counters for both VOD and live videos
Diffstat (limited to 'server/tests/api/videos/videos-history.ts')
-rw-r--r--server/tests/api/videos/videos-history.ts73
1 files changed, 30 insertions, 43 deletions
diff --git a/server/tests/api/videos/videos-history.ts b/server/tests/api/videos/videos-history.ts
index 8648c97f0..b1b3ff10a 100644
--- a/server/tests/api/videos/videos-history.ts
+++ b/server/tests/api/videos/videos-history.ts
@@ -3,15 +3,8 @@
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { wait } from '@shared/core-utils' 5import { wait } from '@shared/core-utils'
6import { HttpStatusCode, Video } from '@shared/models' 6import { Video } from '@shared/models'
7import { 7import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
8 cleanupTests,
9 createSingleServer,
10 HistoryCommand,
11 killallServers,
12 PeerTubeServer,
13 setAccessTokensToServers
14} from '@shared/server-commands'
15 8
16const expect = chai.expect 9const expect = chai.expect
17 10
@@ -23,7 +16,6 @@ describe('Test videos history', function () {
23 let video3UUID: string 16 let video3UUID: string
24 let video3WatchedDate: Date 17 let video3WatchedDate: Date
25 let userAccessToken: string 18 let userAccessToken: string
26 let command: HistoryCommand
27 19
28 before(async function () { 20 before(async function () {
29 this.timeout(30000) 21 this.timeout(30000)
@@ -32,30 +24,26 @@ describe('Test videos history', function () {
32 24
33 await setAccessTokensToServers([ server ]) 25 await setAccessTokensToServers([ server ])
34 26
35 command = server.history 27 // 10 seconds long
28 const fixture = 'video_59fps.mp4'
36 29
37 { 30 {
38 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1' } }) 31 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } })
39 video1UUID = uuid 32 video1UUID = uuid
40 video1Id = id 33 video1Id = id
41 } 34 }
42 35
43 { 36 {
44 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } }) 37 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } })
45 video2UUID = uuid 38 video2UUID = uuid
46 } 39 }
47 40
48 { 41 {
49 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } }) 42 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } })
50 video3UUID = uuid 43 video3UUID = uuid
51 } 44 }
52 45
53 const user = { 46 userAccessToken = await server.users.generateUserAndToken('user_1')
54 username: 'user_1',
55 password: 'super password'
56 }
57 await server.users.create({ username: user.username, password: user.password })
58 userAccessToken = await server.login.getAccessToken(user)
59 }) 47 })
60 48
61 it('Should get videos, without watching history', async function () { 49 it('Should get videos, without watching history', async function () {
@@ -70,8 +58,8 @@ describe('Test videos history', function () {
70 }) 58 })
71 59
72 it('Should watch the first and second video', async function () { 60 it('Should watch the first and second video', async function () {
73 await command.watchVideo({ videoId: video2UUID, currentTime: 8 }) 61 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
74 await command.watchVideo({ videoId: video1UUID, currentTime: 3 }) 62 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 })
75 }) 63 })
76 64
77 it('Should return the correct history when listing, searching and getting videos', async function () { 65 it('Should return the correct history when listing, searching and getting videos', async function () {
@@ -124,9 +112,9 @@ describe('Test videos history', function () {
124 112
125 it('Should have these videos when listing my history', async function () { 113 it('Should have these videos when listing my history', async function () {
126 video3WatchedDate = new Date() 114 video3WatchedDate = new Date()
127 await command.watchVideo({ videoId: video3UUID, currentTime: 2 }) 115 await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 })
128 116
129 const body = await command.list() 117 const body = await server.history.list()
130 118
131 expect(body.total).to.equal(3) 119 expect(body.total).to.equal(3)
132 120
@@ -137,14 +125,14 @@ describe('Test videos history', function () {
137 }) 125 })
138 126
139 it('Should not have videos history on another user', async function () { 127 it('Should not have videos history on another user', async function () {
140 const body = await command.list({ token: userAccessToken }) 128 const body = await server.history.list({ token: userAccessToken })
141 129
142 expect(body.total).to.equal(0) 130 expect(body.total).to.equal(0)
143 expect(body.data).to.have.lengthOf(0) 131 expect(body.data).to.have.lengthOf(0)
144 }) 132 })
145 133
146 it('Should be able to search through videos in my history', async function () { 134 it('Should be able to search through videos in my history', async function () {
147 const body = await command.list({ search: '2' }) 135 const body = await server.history.list({ search: '2' })
148 expect(body.total).to.equal(1) 136 expect(body.total).to.equal(1)
149 137
150 const videos = body.data 138 const videos = body.data
@@ -152,11 +140,11 @@ describe('Test videos history', function () {
152 }) 140 })
153 141
154 it('Should clear my history', async function () { 142 it('Should clear my history', async function () {
155 await command.removeAll({ beforeDate: video3WatchedDate.toISOString() }) 143 await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() })
156 }) 144 })
157 145
158 it('Should have my history cleared', async function () { 146 it('Should have my history cleared', async function () {
159 const body = await command.list() 147 const body = await server.history.list()
160 expect(body.total).to.equal(1) 148 expect(body.total).to.equal(1)
161 149
162 const videos = body.data 150 const videos = body.data
@@ -168,7 +156,10 @@ describe('Test videos history', function () {
168 videosHistoryEnabled: false 156 videosHistoryEnabled: false
169 }) 157 })
170 158
171 await command.watchVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 }) 159 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
160
161 const { data } = await server.history.list()
162 expect(data[0].name).to.not.equal('video 2')
172 }) 163 })
173 164
174 it('Should re-enable videos history', async function () { 165 it('Should re-enable videos history', async function () {
@@ -176,14 +167,10 @@ describe('Test videos history', function () {
176 videosHistoryEnabled: true 167 videosHistoryEnabled: true
177 }) 168 })
178 169
179 await command.watchVideo({ videoId: video1UUID, currentTime: 8 }) 170 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
180 171
181 const body = await command.list() 172 const { data } = await server.history.list()
182 expect(body.total).to.equal(2) 173 expect(data[0].name).to.equal('video 2')
183
184 const videos = body.data
185 expect(videos[0].name).to.equal('video 1')
186 expect(videos[1].name).to.equal('video 3')
187 }) 174 })
188 175
189 it('Should not clean old history', async function () { 176 it('Should not clean old history', async function () {
@@ -197,7 +184,7 @@ describe('Test videos history', function () {
197 184
198 // Should still have history 185 // Should still have history
199 186
200 const body = await command.list() 187 const body = await server.history.list()
201 expect(body.total).to.equal(2) 188 expect(body.total).to.equal(2)
202 }) 189 })
203 190
@@ -210,25 +197,25 @@ describe('Test videos history', function () {
210 197
211 await wait(6000) 198 await wait(6000)
212 199
213 const body = await command.list() 200 const body = await server.history.list()
214 expect(body.total).to.equal(0) 201 expect(body.total).to.equal(0)
215 }) 202 })
216 203
217 it('Should delete a specific history element', async function () { 204 it('Should delete a specific history element', async function () {
218 { 205 {
219 await command.watchVideo({ videoId: video1UUID, currentTime: 4 }) 206 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 })
220 await command.watchVideo({ videoId: video2UUID, currentTime: 8 }) 207 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
221 } 208 }
222 209
223 { 210 {
224 const body = await command.list() 211 const body = await server.history.list()
225 expect(body.total).to.equal(2) 212 expect(body.total).to.equal(2)
226 } 213 }
227 214
228 { 215 {
229 await command.removeElement({ videoId: video1Id }) 216 await server.history.removeElement({ videoId: video1Id })
230 217
231 const body = await command.list() 218 const body = await server.history.list()
232 expect(body.total).to.equal(1) 219 expect(body.total).to.equal(1)
233 expect(body.data[0].uuid).to.equal(video2UUID) 220 expect(body.data[0].uuid).to.equal(video2UUID)
234 } 221 }