aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/api/check-params/abuses.ts4
-rw-r--r--server/tests/api/check-params/index.ts11
-rw-r--r--server/tests/api/check-params/video-source.ts44
-rw-r--r--server/tests/api/check-params/videos.ts5
-rw-r--r--server/tests/api/live/live.ts4
-rw-r--r--server/tests/api/moderation/abuses.ts4
-rw-r--r--server/tests/api/notifications/user-notifications.ts2
-rw-r--r--server/tests/api/server/contact-form.ts2
-rw-r--r--server/tests/api/server/reverse-proxy.ts11
-rw-r--r--server/tests/api/server/services.ts34
-rw-r--r--server/tests/api/users/users.ts2
-rw-r--r--server/tests/api/videos/index.ts1
-rw-r--r--server/tests/api/videos/video-channels.ts19
-rw-r--r--server/tests/api/videos/video-privacy.ts2
-rw-r--r--server/tests/api/videos/video-source.ts39
-rw-r--r--server/tests/helpers/core-utils.ts65
16 files changed, 196 insertions, 53 deletions
diff --git a/server/tests/api/check-params/abuses.ts b/server/tests/api/check-params/abuses.ts
index c4b051723..bc2cc640f 100644
--- a/server/tests/api/check-params/abuses.ts
+++ b/server/tests/api/check-params/abuses.ts
@@ -269,7 +269,7 @@ describe('Test abuses API validators', function () {
269 await makePostBodyRequest({ url: server.url, path, token: userToken, fields }) 269 await makePostBodyRequest({ url: server.url, path, token: userToken, fields })
270 }) 270 })
271 271
272 it('Should succeed with the corret parameters (advanced)', async function () { 272 it('Should succeed with the correct parameters (advanced)', async function () {
273 const fields: AbuseCreate = { 273 const fields: AbuseCreate = {
274 video: { 274 video: {
275 id: server.store.videoCreated.id, 275 id: server.store.videoCreated.id,
@@ -333,7 +333,7 @@ describe('Test abuses API validators', function () {
333 await command.addMessage({ token: userToken, abuseId, message: 'a'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) 333 await command.addMessage({ token: userToken, abuseId, message: 'a'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
334 }) 334 })
335 335
336 it('Should suceed with the correct params', async function () { 336 it('Should succeed with the correct params', async function () {
337 const res = await command.addMessage({ token: userToken, abuseId, message }) 337 const res = await command.addMessage({ token: userToken, abuseId, message })
338 messageId = res.body.abuseMessage.id 338 messageId = res.body.abuseMessage.id
339 }) 339 })
diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts
index 259d7e783..a27bc8509 100644
--- a/server/tests/api/check-params/index.ts
+++ b/server/tests/api/check-params/index.ts
@@ -3,14 +3,14 @@ import './accounts'
3import './blocklist' 3import './blocklist'
4import './bulk' 4import './bulk'
5import './config' 5import './config'
6import './custom-pages'
7import './contact-form' 6import './contact-form'
7import './custom-pages'
8import './debug' 8import './debug'
9import './follows' 9import './follows'
10import './jobs' 10import './jobs'
11import './live'
11import './logs' 12import './logs'
12import './my-user' 13import './my-user'
13import './live'
14import './plugins' 14import './plugins'
15import './redundancy' 15import './redundancy'
16import './search' 16import './search'
@@ -25,12 +25,13 @@ import './video-blacklist'
25import './video-captions' 25import './video-captions'
26import './video-channels' 26import './video-channels'
27import './video-comments' 27import './video-comments'
28import './video-studio' 28import './video-files'
29import './video-imports' 29import './video-imports'
30import './video-playlists' 30import './video-playlists'
31import './videos' 31import './video-source'
32import './video-studio'
32import './videos-common-filters' 33import './videos-common-filters'
33import './video-files'
34import './videos-history' 34import './videos-history'
35import './videos-overviews' 35import './videos-overviews'
36import './videos'
36import './views' 37import './views'
diff --git a/server/tests/api/check-params/video-source.ts b/server/tests/api/check-params/video-source.ts
new file mode 100644
index 000000000..ca324bb9d
--- /dev/null
+++ b/server/tests/api/check-params/video-source.ts
@@ -0,0 +1,44 @@
1import { HttpStatusCode } from '@shared/models'
2import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
3
4describe('Test video sources API validator', function () {
5 let server: PeerTubeServer = null
6 let uuid: string
7 let userToken: string
8
9 before(async function () {
10 this.timeout(30000)
11
12 server = await createSingleServer(1)
13 await setAccessTokensToServers([ server ])
14
15 const created = await server.videos.quickUpload({ name: 'video' })
16 uuid = created.uuid
17
18 userToken = await server.users.generateUserAndToken('user')
19 })
20
21 it('Should fail without a valid uuid', async function () {
22 await server.videos.getSource({ id: '4da6fde3-88f7-4d16-b119-108df563d0b0', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
23 })
24
25 it('Should receive 404 when passing a non existing video id', async function () {
26 await server.videos.getSource({ id: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
27 })
28
29 it('Should not get the source as unauthenticated', async function () {
30 await server.videos.getSource({ id: uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401, token: null })
31 })
32
33 it('Should not get the source with another user', async function () {
34 await server.videos.getSource({ id: uuid, expectedStatus: HttpStatusCode.FORBIDDEN_403, token: userToken })
35 })
36
37 it('Should succeed with the correct parameters get the source as another user', async function () {
38 await server.videos.getSource({ id: uuid })
39 })
40
41 after(async function () {
42 await cleanupTests([ server ])
43 })
44})
diff --git a/server/tests/api/check-params/videos.ts b/server/tests/api/check-params/videos.ts
index 41064d2ff..5ff51d1ff 100644
--- a/server/tests/api/check-params/videos.ts
+++ b/server/tests/api/check-params/videos.ts
@@ -39,10 +39,7 @@ describe('Test videos API validator', function () {
39 39
40 await setAccessTokensToServers([ server ]) 40 await setAccessTokensToServers([ server ])
41 41
42 const username = 'user1' 42 userAccessToken = await server.users.generateUserAndToken('user1')
43 const password = 'my super password'
44 await server.users.create({ username: username, password: password })
45 userAccessToken = await server.login.getAccessToken({ username, password })
46 43
47 { 44 {
48 const body = await server.users.getMyInfo() 45 const body = await server.users.getMyInfo()
diff --git a/server/tests/api/live/live.ts b/server/tests/api/live/live.ts
index 5d354aad1..2d47c131b 100644
--- a/server/tests/api/live/live.ts
+++ b/server/tests/api/live/live.ts
@@ -610,7 +610,7 @@ describe('Test live', function () {
610 } 610 }
611 611
612 before(async function () { 612 before(async function () {
613 this.timeout(160000) 613 this.timeout(300000)
614 614
615 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false }) 615 liveVideoId = await createLiveWrapper({ saveReplay: false, permanent: false })
616 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false }) 616 liveVideoReplayId = await createLiveWrapper({ saveReplay: true, permanent: false })
@@ -654,7 +654,7 @@ describe('Test live', function () {
654 }) 654 })
655 655
656 it('Should save a non permanent live replay', async function () { 656 it('Should save a non permanent live replay', async function () {
657 this.timeout(120000) 657 this.timeout(240000)
658 658
659 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId }) 659 await commands[0].waitUntilPublished({ videoId: liveVideoReplayId })
660 660
diff --git a/server/tests/api/moderation/abuses.ts b/server/tests/api/moderation/abuses.ts
index 7bf49c7ec..568fbefcf 100644
--- a/server/tests/api/moderation/abuses.ts
+++ b/server/tests/api/moderation/abuses.ts
@@ -168,7 +168,7 @@ describe('Test abuses', function () {
168 expect(abuse2.reporterAccount.name).to.equal('root') 168 expect(abuse2.reporterAccount.name).to.equal('root')
169 expect(abuse2.reporterAccount.host).to.equal(servers[0].host) 169 expect(abuse2.reporterAccount.host).to.equal(servers[0].host)
170 170
171 expect(abuse2.video.id).to.equal(servers[1].store.videoCreated.id) 171 expect(abuse2.video.uuid).to.equal(servers[1].store.videoCreated.uuid)
172 172
173 expect(abuse2.comment).to.be.null 173 expect(abuse2.comment).to.be.null
174 174
@@ -530,7 +530,7 @@ describe('Test abuses', function () {
530 it('Should keep the comment abuse when deleting the comment', async function () { 530 it('Should keep the comment abuse when deleting the comment', async function () {
531 this.timeout(10000) 531 this.timeout(10000)
532 532
533 const commentServer2 = await getComment(servers[0], servers[1].store.videoCreated.id) 533 const commentServer2 = await getComment(servers[0], servers[1].store.videoCreated.uuid)
534 534
535 await servers[0].comments.delete({ videoId: servers[1].store.videoCreated.uuid, commentId: commentServer2.id }) 535 await servers[0].comments.delete({ videoId: servers[1].store.videoCreated.uuid, commentId: commentServer2.id })
536 536
diff --git a/server/tests/api/notifications/user-notifications.ts b/server/tests/api/notifications/user-notifications.ts
index a7cc529f8..a11289236 100644
--- a/server/tests/api/notifications/user-notifications.ts
+++ b/server/tests/api/notifications/user-notifications.ts
@@ -545,7 +545,7 @@ describe('Test user notifications', function () {
545 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port }) 545 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
546 }) 546 })
547 547
548 // PeerTube does not support accout -> account follows 548 // PeerTube does not support account -> account follows
549 // it('Should notify when a local account is following one of our channel', async function () { 549 // it('Should notify when a local account is following one of our channel', async function () {
550 // this.timeout(50000) 550 // this.timeout(50000)
551 // 551 //
diff --git a/server/tests/api/server/contact-form.ts b/server/tests/api/server/contact-form.ts
index 4f01f6fd5..d6165b293 100644
--- a/server/tests/api/server/contact-form.ts
+++ b/server/tests/api/server/contact-form.ts
@@ -61,7 +61,7 @@ describe('Test contact form', function () {
61 expect(email['text']).contains('my super message') 61 expect(email['text']).contains('my super message')
62 }) 62 })
63 63
64 it('Should not have duplicated email adress in text message', async function () { 64 it('Should not have duplicated email address in text message', async function () {
65 const text = emails[0]['text'] as string 65 const text = emails[0]['text'] as string
66 66
67 const matches = text.match(/toto@example.com/g) 67 const matches = text.match(/toto@example.com/g)
diff --git a/server/tests/api/server/reverse-proxy.ts b/server/tests/api/server/reverse-proxy.ts
index fa2063536..0a1565faf 100644
--- a/server/tests/api/server/reverse-proxy.ts
+++ b/server/tests/api/server/reverse-proxy.ts
@@ -7,6 +7,7 @@ import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServ
7 7
8describe('Test application behind a reverse proxy', function () { 8describe('Test application behind a reverse proxy', function () {
9 let server: PeerTubeServer 9 let server: PeerTubeServer
10 let userAccessToken: string
10 let videoId: string 11 let videoId: string
11 12
12 before(async function () { 13 before(async function () {
@@ -34,6 +35,8 @@ describe('Test application behind a reverse proxy', function () {
34 server = await createSingleServer(1, config) 35 server = await createSingleServer(1, config)
35 await setAccessTokensToServers([ server ]) 36 await setAccessTokensToServers([ server ])
36 37
38 userAccessToken = await server.users.generateUserAndToken('user')
39
37 const { uuid } = await server.videos.upload() 40 const { uuid } = await server.videos.upload()
38 videoId = uuid 41 videoId = uuid
39 }) 42 })
@@ -93,7 +96,7 @@ describe('Test application behind a reverse proxy', function () {
93 it('Should rate limit logins', async function () { 96 it('Should rate limit logins', async function () {
94 const user = { username: 'root', password: 'fail' } 97 const user = { username: 'root', password: 'fail' }
95 98
96 for (let i = 0; i < 19; i++) { 99 for (let i = 0; i < 18; i++) {
97 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) 100 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
98 } 101 }
99 102
@@ -141,6 +144,12 @@ describe('Test application behind a reverse proxy', function () {
141 await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 }) 144 await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
142 }) 145 })
143 146
147 it('Should rate limit API calls with a user but not with an admin', async function () {
148 await server.videos.get({ id: videoId, token: userAccessToken, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
149
150 await server.videos.get({ id: videoId, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
151 })
152
144 after(async function () { 153 after(async function () {
145 await cleanupTests([ server ]) 154 await cleanupTests([ server ])
146 }) 155 })
diff --git a/server/tests/api/server/services.ts b/server/tests/api/server/services.ts
index 5fd2abda4..2f95f953b 100644
--- a/server/tests/api/server/services.ts
+++ b/server/tests/api/server/services.ts
@@ -13,6 +13,21 @@ describe('Test services', function () {
13 let playlistDisplayName: string 13 let playlistDisplayName: string
14 let video: Video 14 let video: Video
15 15
16 const urlSuffixes = [
17 {
18 input: '',
19 output: ''
20 },
21 {
22 input: '?param=1',
23 output: ''
24 },
25 {
26 input: '?muted=1&warningTitle=0&toto=1',
27 output: '?muted=1&warningTitle=0'
28 }
29 ]
30
16 before(async function () { 31 before(async function () {
17 this.timeout(30000) 32 this.timeout(30000)
18 33
@@ -52,14 +67,15 @@ describe('Test services', function () {
52 67
53 it('Should have a valid oEmbed video response', async function () { 68 it('Should have a valid oEmbed video response', async function () {
54 for (const basePath of [ '/videos/watch/', '/w/' ]) { 69 for (const basePath of [ '/videos/watch/', '/w/' ]) {
55 for (const suffix of [ '', '?param=1' ]) { 70 for (const suffix of urlSuffixes) {
56 const oembedUrl = server.url + basePath + video.uuid + suffix 71 const oembedUrl = server.url + basePath + video.uuid + suffix.input
57 72
58 const res = await server.services.getOEmbed({ oembedUrl }) 73 const res = await server.services.getOEmbed({ oembedUrl })
59 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups" ' + 74 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups" ' +
60 `title="${video.name}" src="http://localhost:${server.port}/videos/embed/${video.uuid}" ` + 75 `title="${video.name}" src="http://${server.host}/videos/embed/${video.uuid}${suffix.output}" ` +
61 'frameborder="0" allowfullscreen></iframe>' 76 'frameborder="0" allowfullscreen></iframe>'
62 const expectedThumbnailUrl = 'http://localhost:' + server.port + video.previewPath 77
78 const expectedThumbnailUrl = 'http://' + server.host + video.previewPath
63 79
64 expect(res.body.html).to.equal(expectedHtml) 80 expect(res.body.html).to.equal(expectedHtml)
65 expect(res.body.title).to.equal(video.name) 81 expect(res.body.title).to.equal(video.name)
@@ -75,12 +91,12 @@ describe('Test services', function () {
75 91
76 it('Should have a valid playlist oEmbed response', async function () { 92 it('Should have a valid playlist oEmbed response', async function () {
77 for (const basePath of [ '/videos/watch/playlist/', '/w/p/' ]) { 93 for (const basePath of [ '/videos/watch/playlist/', '/w/p/' ]) {
78 for (const suffix of [ '', '?param=1' ]) { 94 for (const suffix of urlSuffixes) {
79 const oembedUrl = server.url + basePath + playlistUUID + suffix 95 const oembedUrl = server.url + basePath + playlistUUID + suffix.input
80 96
81 const res = await server.services.getOEmbed({ oembedUrl }) 97 const res = await server.services.getOEmbed({ oembedUrl })
82 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups" ' + 98 const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups" ' +
83 `title="${playlistDisplayName}" src="http://localhost:${server.port}/video-playlists/embed/${playlistUUID}" ` + 99 `title="${playlistDisplayName}" src="http://${server.host}/video-playlists/embed/${playlistUUID}${suffix.output}" ` +
84 'frameborder="0" allowfullscreen></iframe>' 100 'frameborder="0" allowfullscreen></iframe>'
85 101
86 expect(res.body.html).to.equal(expectedHtml) 102 expect(res.body.html).to.equal(expectedHtml)
@@ -97,14 +113,14 @@ describe('Test services', function () {
97 113
98 it('Should have a valid oEmbed response with small max height query', async function () { 114 it('Should have a valid oEmbed response with small max height query', async function () {
99 for (const basePath of [ '/videos/watch/', '/w/' ]) { 115 for (const basePath of [ '/videos/watch/', '/w/' ]) {
100 const oembedUrl = 'http://localhost:' + server.port + basePath + video.uuid 116 const oembedUrl = 'http://' + server.host + basePath + video.uuid
101 const format = 'json' 117 const format = 'json'
102 const maxHeight = 50 118 const maxHeight = 50
103 const maxWidth = 50 119 const maxWidth = 50
104 120
105 const res = await server.services.getOEmbed({ oembedUrl, format, maxHeight, maxWidth }) 121 const res = await server.services.getOEmbed({ oembedUrl, format, maxHeight, maxWidth })
106 const expectedHtml = '<iframe width="50" height="50" sandbox="allow-same-origin allow-scripts allow-popups" ' + 122 const expectedHtml = '<iframe width="50" height="50" sandbox="allow-same-origin allow-scripts allow-popups" ' +
107 `title="${video.name}" src="http://localhost:${server.port}/videos/embed/${video.uuid}" ` + 123 `title="${video.name}" src="http://${server.host}/videos/embed/${video.uuid}" ` +
108 'frameborder="0" allowfullscreen></iframe>' 124 'frameborder="0" allowfullscreen></iframe>'
109 125
110 expect(res.body.html).to.equal(expectedHtml) 126 expect(res.body.html).to.equal(expectedHtml)
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts
index 01b4c2eab..d15daeba5 100644
--- a/server/tests/api/users/users.ts
+++ b/server/tests/api/users/users.ts
@@ -380,7 +380,7 @@ describe('Test users', function () {
380 }) 380 })
381 381
382 it('Should disable webtorrent, enable HLS, and update my quota', async function () { 382 it('Should disable webtorrent, enable HLS, and update my quota', async function () {
383 this.timeout(60000) 383 this.timeout(160000)
384 384
385 { 385 {
386 const config = await server.config.getCustomConfig() 386 const config = await server.config.getCustomConfig()
diff --git a/server/tests/api/videos/index.ts b/server/tests/api/videos/index.ts
index 27b119f30..a0b6b01cf 100644
--- a/server/tests/api/videos/index.ts
+++ b/server/tests/api/videos/index.ts
@@ -16,3 +16,4 @@ import './video-schedule-update'
16import './videos-common-filters' 16import './videos-common-filters'
17import './videos-history' 17import './videos-history'
18import './videos-overview' 18import './videos-overview'
19import './video-source'
diff --git a/server/tests/api/videos/video-channels.ts b/server/tests/api/videos/video-channels.ts
index 6f495c42d..42e0cf431 100644
--- a/server/tests/api/videos/video-channels.ts
+++ b/server/tests/api/videos/video-channels.ts
@@ -478,6 +478,25 @@ describe('Test video channels', function () {
478 } 478 }
479 }) 479 })
480 480
481 it('Should report correct total views count', async function () {
482 // check if there's the property
483 {
484 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
485
486 for (const channel of data) {
487 expect(channel).to.haveOwnProperty('totalViews')
488 expect(channel.totalViews).to.be.a('number')
489 }
490 }
491
492 // Check if the totalViews count can be updated
493 {
494 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
495 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
496 expect(channelWithView.totalViews).to.equal(2)
497 }
498 })
499
481 it('Should report correct videos count', async function () { 500 it('Should report correct videos count', async function () {
482 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true }) 501 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
483 502
diff --git a/server/tests/api/videos/video-privacy.ts b/server/tests/api/videos/video-privacy.ts
index 3051a443d..1073aee8c 100644
--- a/server/tests/api/videos/video-privacy.ts
+++ b/server/tests/api/videos/video-privacy.ts
@@ -162,7 +162,7 @@ describe('Test video privacy', function () {
162 }) 162 })
163 163
164 it('Should not be able to get this unlisted video using its id', async function () { 164 it('Should not be able to get this unlisted video using its id', async function () {
165 await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) 165 await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
166 }) 166 })
167 167
168 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () { 168 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
diff --git a/server/tests/api/videos/video-source.ts b/server/tests/api/videos/video-source.ts
new file mode 100644
index 000000000..e34642300
--- /dev/null
+++ b/server/tests/api/videos/video-source.ts
@@ -0,0 +1,39 @@
1import 'mocha'
2import * as chai from 'chai'
3import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
4
5const expect = chai.expect
6
7describe('Test video source', () => {
8 let server: PeerTubeServer = null
9 const fixture = 'video_short.webm'
10
11 before(async function () {
12 this.timeout(30000)
13
14 server = await createSingleServer(1)
15 await setAccessTokensToServers([ server ])
16 })
17
18 it('Should get the source filename with legacy upload', async function () {
19 this.timeout(30000)
20
21 const { uuid } = await server.videos.upload({ attributes: { name: 'my video', fixture }, mode: 'legacy' })
22
23 const source = await server.videos.getSource({ id: uuid })
24 expect(source.filename).to.equal(fixture)
25 })
26
27 it('Should get the source filename with resumable upload', async function () {
28 this.timeout(30000)
29
30 const { uuid } = await server.videos.upload({ attributes: { name: 'my video', fixture }, mode: 'resumable' })
31
32 const source = await server.videos.getSource({ id: uuid })
33 expect(source.filename).to.equal(fixture)
34 })
35
36 after(async function () {
37 await cleanupTests([ server ])
38 })
39})
diff --git a/server/tests/helpers/core-utils.ts b/server/tests/helpers/core-utils.ts
index fa0a71341..e3e5eb45c 100644
--- a/server/tests/helpers/core-utils.ts
+++ b/server/tests/helpers/core-utils.ts
@@ -6,47 +6,64 @@ import { snakeCase } from 'lodash'
6import validator from 'validator' 6import validator from 'validator'
7import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils' 7import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils'
8import { VideoResolution } from '@shared/models' 8import { VideoResolution } from '@shared/models'
9import { objectConverter, parseBytes } from '../../helpers/core-utils' 9import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils'
10 10
11const expect = chai.expect 11const expect = chai.expect
12 12
13describe('Parse Bytes', function () { 13describe('Parse Bytes', function () {
14 14
15 it('Should pass when given valid value', async function () { 15 it('Should pass on valid value', async function () {
16 // just return it 16 // just return it
17 expect(parseBytes(1024)).to.be.eq(1024) 17 expect(parseBytes(-1024)).to.equal(-1024)
18 expect(parseBytes(1048576)).to.be.eq(1048576) 18 expect(parseBytes(1024)).to.equal(1024)
19 expect(parseBytes('1024')).to.be.eq(1024) 19 expect(parseBytes(1048576)).to.equal(1048576)
20 expect(parseBytes('1048576')).to.be.eq(1048576) 20 expect(parseBytes('1024')).to.equal(1024)
21 expect(parseBytes('1048576')).to.equal(1048576)
21 22
22 // sizes 23 // sizes
23 expect(parseBytes('1B')).to.be.eq(1024) 24 expect(parseBytes('1B')).to.equal(1024)
24 expect(parseBytes('1MB')).to.be.eq(1048576) 25 expect(parseBytes('1MB')).to.equal(1048576)
25 expect(parseBytes('1GB')).to.be.eq(1073741824) 26 expect(parseBytes('1GB')).to.equal(1073741824)
26 expect(parseBytes('1TB')).to.be.eq(1099511627776) 27 expect(parseBytes('1TB')).to.equal(1099511627776)
27 28
28 expect(parseBytes('5GB')).to.be.eq(5368709120) 29 expect(parseBytes('5GB')).to.equal(5368709120)
29 expect(parseBytes('5TB')).to.be.eq(5497558138880) 30 expect(parseBytes('5TB')).to.equal(5497558138880)
30 31
31 expect(parseBytes('1024B')).to.be.eq(1048576) 32 expect(parseBytes('1024B')).to.equal(1048576)
32 expect(parseBytes('1024MB')).to.be.eq(1073741824) 33 expect(parseBytes('1024MB')).to.equal(1073741824)
33 expect(parseBytes('1024GB')).to.be.eq(1099511627776) 34 expect(parseBytes('1024GB')).to.equal(1099511627776)
34 expect(parseBytes('1024TB')).to.be.eq(1125899906842624) 35 expect(parseBytes('1024TB')).to.equal(1125899906842624)
35 36
36 // with whitespace 37 // with whitespace
37 expect(parseBytes('1 GB')).to.be.eq(1073741824) 38 expect(parseBytes('1 GB')).to.equal(1073741824)
38 expect(parseBytes('1\tGB')).to.be.eq(1073741824) 39 expect(parseBytes('1\tGB')).to.equal(1073741824)
39 40
40 // sum value 41 // sum value
41 expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600) 42 expect(parseBytes('1TB 1024MB')).to.equal(1100585369600)
42 expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120) 43 expect(parseBytes('4GB 1024MB')).to.equal(5368709120)
43 expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880) 44 expect(parseBytes('4TB 1024GB')).to.equal(5497558138880)
44 expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880) 45 expect(parseBytes('4TB 1024GB 0MB')).to.equal(5497558138880)
45 expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224) 46 expect(parseBytes('1024TB 1024GB 1024MB')).to.equal(1127000492212224)
47 })
48
49 it('Should be invalid when given invalid value', async function () {
50 expect(parseBytes('6GB 1GB')).to.equal(6)
51 })
52})
53
54describe('Parse duration', function () {
55
56 it('Should pass when given valid value', async function () {
57 expect(parseDurationToMs(35)).to.equal(35)
58 expect(parseDurationToMs(-35)).to.equal(-35)
59 expect(parseDurationToMs('35 seconds')).to.equal(35 * 1000)
60 expect(parseDurationToMs('1 minute')).to.equal(60 * 1000)
61 expect(parseDurationToMs('1 hour')).to.equal(3600 * 1000)
62 expect(parseDurationToMs('35 hours')).to.equal(3600 * 35 * 1000)
46 }) 63 })
47 64
48 it('Should be invalid when given invalid value', async function () { 65 it('Should be invalid when given invalid value', async function () {
49 expect(parseBytes('6GB 1GB')).to.be.eq(6) 66 expect(parseBytes('35m 5s')).to.equal(35)
50 }) 67 })
51}) 68})
52 69