From 09cababd79f9d445aa027c93cdfe823745fa041a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 28 Feb 2018 18:04:46 +0100 Subject: Add stats route --- server/tests/api/check-params/config.ts | 2 +- server/tests/api/index-fast.ts | 3 +- server/tests/api/index-slow.ts | 1 - server/tests/api/server/config.ts | 4 +- server/tests/api/server/stats.ts | 102 ++++++++++++++++++++++++++++++++ server/tests/utils/server/config.ts | 2 +- server/tests/utils/server/stats.ts | 17 ++++++ 7 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 server/tests/api/server/stats.ts create mode 100644 server/tests/utils/server/stats.ts (limited to 'server/tests') diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index c1c0a3f59..a66e51a6a 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -2,7 +2,7 @@ import { omit } from 'lodash' import 'mocha' -import { CustomConfig } from '../../../../shared/models/config/custom-config.model' +import { CustomConfig } from '../../../../shared/models/server/custom-config.model' import { createUser, flushTests, killallServers, makeDeleteRequest, makeGetRequest, makePutBodyRequest, runServer, ServerInfo, diff --git a/server/tests/api/index-fast.ts b/server/tests/api/index-fast.ts index 9f52310dd..464dcb5e0 100644 --- a/server/tests/api/index-fast.ts +++ b/server/tests/api/index-fast.ts @@ -1,5 +1,5 @@ // Order of the tests we want to execute -import './server/config' +import './server/stats' import './check-params' import './users/users' import './videos/single-server' @@ -10,3 +10,4 @@ import './videos/video-description' import './videos/video-privacy' import './videos/services' import './server/email' +import './server/config' diff --git a/server/tests/api/index-slow.ts b/server/tests/api/index-slow.ts index 0082bcb56..cde546856 100644 --- a/server/tests/api/index-slow.ts +++ b/server/tests/api/index-slow.ts @@ -1,5 +1,4 @@ // Order of the tests we want to execute -// import './multiple-servers' import './videos/video-transcoder' import './videos/multiple-servers' import './server/follows' diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index 048135a34..3d90580d8 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -2,8 +2,8 @@ import 'mocha' import * as chai from 'chai' -import { About } from '../../../../shared/models/config/about.model' -import { CustomConfig } from '../../../../shared/models/config/custom-config.model' +import { About } from '../../../../shared/models/server/about.model' +import { CustomConfig } from '../../../../shared/models/server/custom-config.model' import { deleteCustomConfig, getAbout, killallServers, reRunServer } from '../../utils' const expect = chai.expect diff --git a/server/tests/api/server/stats.ts b/server/tests/api/server/stats.ts new file mode 100644 index 000000000..71d54c0ab --- /dev/null +++ b/server/tests/api/server/stats.ts @@ -0,0 +1,102 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { ServerStats } from '../../../../shared/models/server/server-stats.model' +import { + createUser, + doubleFollow, + flushAndRunMultipleServers, + follow, + killallServers, + ServerInfo, + uploadVideo, + viewVideo, + wait +} from '../../utils' +import { flushTests, setAccessTokensToServers } from '../../utils/index' +import { getStats } from '../../utils/server/stats' +import { addVideoCommentThread } from '../../utils/videos/video-comments' + +const expect = chai.expect + +describe('Test stats', function () { + let servers: ServerInfo[] = [] + + before(async function () { + this.timeout(60000) + + await flushTests() + servers = await flushAndRunMultipleServers(3) + await setAccessTokensToServers(servers) + + await doubleFollow(servers[0], servers[1]) + + const user = { + username: 'user1', + password: 'super_password' + } + await createUser(servers[0].url, servers[0].accessToken, user.username, user.password) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, {}) + const videoUUID = resVideo.body.video.uuid + + await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment') + + await viewVideo(servers[0].url, videoUUID) + + await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) + await wait(5000) + }) + + it('Should have the correct stats on instance 1', async function () { + const res = await getStats(servers[0].url) + const data: ServerStats = res.body + + expect(data.totalLocalVideoComments).to.equal(1) + expect(data.totalLocalVideos).to.equal(1) + expect(data.totalLocalVideoViews).to.equal(1) + expect(data.totalUsers).to.equal(2) + expect(data.totalVideoComments).to.equal(1) + expect(data.totalVideos).to.equal(1) + expect(data.totalInstanceFollowers).to.equal(2) + expect(data.totalInstanceFollowing).to.equal(1) + }) + + it('Should have the correct stats on instance 2', async function () { + const res = await getStats(servers[1].url) + const data: ServerStats = res.body + + expect(data.totalLocalVideoComments).to.equal(0) + expect(data.totalLocalVideos).to.equal(0) + expect(data.totalLocalVideoViews).to.equal(0) + expect(data.totalUsers).to.equal(1) + expect(data.totalVideoComments).to.equal(1) + expect(data.totalVideos).to.equal(1) + expect(data.totalInstanceFollowers).to.equal(1) + expect(data.totalInstanceFollowing).to.equal(1) + }) + + it('Should have the correct stats on instance 3', async function () { + const res = await getStats(servers[2].url) + const data: ServerStats = res.body + + expect(data.totalLocalVideoComments).to.equal(0) + expect(data.totalLocalVideos).to.equal(0) + expect(data.totalLocalVideoViews).to.equal(0) + expect(data.totalUsers).to.equal(1) + expect(data.totalVideoComments).to.equal(1) + expect(data.totalVideos).to.equal(1) + expect(data.totalInstanceFollowing).to.equal(1) + expect(data.totalInstanceFollowers).to.equal(0) + }) + + after(async function () { + killallServers(servers) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/utils/server/config.ts b/server/tests/utils/server/config.ts index e5411117a..57f95a603 100644 --- a/server/tests/utils/server/config.ts +++ b/server/tests/utils/server/config.ts @@ -1,5 +1,5 @@ import { makeDeleteRequest, makeGetRequest, makePutBodyRequest } from '../' -import { CustomConfig } from '../../../../shared/models/config/custom-config.model' +import { CustomConfig } from '../../../../shared/models/server/custom-config.model' function getConfig (url: string) { const path = '/api/v1/config' diff --git a/server/tests/utils/server/stats.ts b/server/tests/utils/server/stats.ts new file mode 100644 index 000000000..9cdec6cff --- /dev/null +++ b/server/tests/utils/server/stats.ts @@ -0,0 +1,17 @@ +import { makeGetRequest } from '../' + +function getStats (url: string) { + const path = '/api/v1/server/stats' + + return makeGetRequest({ + url, + path, + statusCodeExpected: 200 + }) +} + +// --------------------------------------------------------------------------- + +export { + getStats +} -- cgit v1.2.3