1f5003bd32
* feat: basic structure * fix: detect regions out of sync * feat: added sync logic * fix: tests * feat: merge all strategy * fix: mr comments * fix: updatedAt in updates * feat: project updatedAt no longer is a hidden operation * feat: project region sync * feat: delete project on cross-region-move * removal: unnecesary permisions * fix: caching issue * fix: saved views on project cross region move
257 lines
8.5 KiB
TypeScript
257 lines
8.5 KiB
TypeScript
/* istanbul ignore file */
|
|
import { expect } from 'chai'
|
|
import { beforeEachContext, initializeTestServer } from '@/test/hooks'
|
|
import { createManyObjects } from '@/test/helpers'
|
|
|
|
import {
|
|
getTotalStreamCountFactory,
|
|
getTotalUserCountFactory
|
|
} from '@/modules/stats/repositories/index'
|
|
import { Scopes } from '@speckle/shared'
|
|
import { db } from '@/db/knex'
|
|
import {
|
|
createCommitByBranchIdFactory,
|
|
createCommitByBranchNameFactory
|
|
} from '@/modules/core/services/commit/management'
|
|
import {
|
|
createCommitFactory,
|
|
insertBranchCommitsFactory,
|
|
insertStreamCommitsFactory
|
|
} from '@/modules/core/repositories/commits'
|
|
import {
|
|
getBranchByIdFactory,
|
|
getStreamBranchByNameFactory,
|
|
markCommitBranchUpdatedFactory
|
|
} from '@/modules/core/repositories/branches'
|
|
import {
|
|
getObjectFactory,
|
|
storeObjectsIfNotFoundFactory
|
|
} from '@/modules/core/repositories/objects'
|
|
import { getEventBus } from '@/modules/shared/services/eventBus'
|
|
import { createPersonalAccessTokenFactory } from '@/modules/core/services/tokens'
|
|
import {
|
|
storeApiTokenFactory,
|
|
storePersonalApiTokenFactory,
|
|
storeTokenResourceAccessDefinitionsFactory,
|
|
storeTokenScopesFactory
|
|
} from '@/modules/core/repositories/tokens'
|
|
import { createObjectsFactory } from '@/modules/core/services/objects/management'
|
|
import type { BasicTestUser } from '@/test/authHelper'
|
|
import { createTestUser } from '@/test/authHelper'
|
|
import { createTestStream } from '@/test/speckle-helpers/streamHelper'
|
|
import { buildBasicTestProject } from '@/modules/core/tests/helpers/creation'
|
|
|
|
const getObject = getObjectFactory({ db })
|
|
const createCommitByBranchId = createCommitByBranchIdFactory({
|
|
createCommit: createCommitFactory({ db }),
|
|
getObject,
|
|
getBranchById: getBranchByIdFactory({ db }),
|
|
insertStreamCommits: insertStreamCommitsFactory({ db }),
|
|
insertBranchCommits: insertBranchCommitsFactory({ db }),
|
|
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
|
|
emitEvent: getEventBus().emit
|
|
})
|
|
|
|
const createCommitByBranchName = createCommitByBranchNameFactory({
|
|
createCommitByBranchId,
|
|
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
|
|
getBranchById: getBranchByIdFactory({ db })
|
|
})
|
|
|
|
const createPersonalAccessToken = createPersonalAccessTokenFactory({
|
|
storeApiToken: storeApiTokenFactory({ db }),
|
|
storeTokenScopes: storeTokenScopesFactory({ db }),
|
|
storeTokenResourceAccessDefinitions: storeTokenResourceAccessDefinitionsFactory({
|
|
db
|
|
}),
|
|
storePersonalApiToken: storePersonalApiTokenFactory({ db })
|
|
})
|
|
const createObjects = createObjectsFactory({
|
|
storeObjectsIfNotFoundFactory: storeObjectsIfNotFoundFactory({ db })
|
|
})
|
|
|
|
const params = { numUsers: 25, numStreams: 30, numObjects: 100, numCommits: 100 }
|
|
|
|
describe('Server stats services @stats-services', function () {
|
|
before(async function () {
|
|
this.timeout(15000)
|
|
await beforeEachContext()
|
|
await seedDb(params)
|
|
})
|
|
|
|
it('should return the total number of users on this server', async () => {
|
|
const res = await getTotalUserCountFactory({ db })()
|
|
expect(res).to.equal(params.numUsers)
|
|
})
|
|
|
|
it('should return the total number of streams on this server', async () => {
|
|
const res = await getTotalStreamCountFactory({ db })()
|
|
expect(res).to.equal(params.numStreams)
|
|
})
|
|
})
|
|
|
|
describe('Server stats api @stats-api', function () {
|
|
let sendRequest: Awaited<ReturnType<typeof initializeTestServer>>['sendRequest']
|
|
|
|
let adminUser: BasicTestUser & { goodToken?: string; badToken?: string }
|
|
let notAdminUser: BasicTestUser & { goodToken?: string; badToken?: string }
|
|
|
|
const fullQuery = `
|
|
query{
|
|
serverStats{
|
|
totalStreamCount
|
|
totalCommitCount
|
|
totalObjectCount
|
|
totalUserCount
|
|
streamHistory
|
|
commitHistory
|
|
objectHistory
|
|
userHistory
|
|
}
|
|
}
|
|
`
|
|
|
|
before(async function () {
|
|
this.timeout(15000)
|
|
const ctx = await beforeEachContext()
|
|
;({ sendRequest } = await initializeTestServer(ctx))
|
|
|
|
adminUser = await createTestUser({
|
|
name: 'Dimitrie',
|
|
password: 'TestPasswordSecure',
|
|
email: 'spam@spam.spam',
|
|
id: ''
|
|
})
|
|
adminUser.goodToken = `Bearer ${await createPersonalAccessToken(
|
|
adminUser.id,
|
|
'test token user A',
|
|
[Scopes.Server.Stats]
|
|
)}`
|
|
adminUser.badToken = `Bearer ${await createPersonalAccessToken(
|
|
adminUser.id,
|
|
'test token user A',
|
|
[Scopes.Streams.Read]
|
|
)}`
|
|
|
|
notAdminUser = await createTestUser({
|
|
name: 'Andrei',
|
|
password: 'TestPasswordSecure',
|
|
email: 'spasm@spam.spam',
|
|
id: ''
|
|
})
|
|
notAdminUser.goodToken = `Bearer ${await createPersonalAccessToken(
|
|
notAdminUser.id,
|
|
'test token user A',
|
|
[Scopes.Server.Stats]
|
|
)}`
|
|
notAdminUser.badToken = `Bearer ${await createPersonalAccessToken(
|
|
notAdminUser.id,
|
|
'test token user A',
|
|
[Scopes.Streams.Read]
|
|
)}`
|
|
|
|
await seedDb(params)
|
|
})
|
|
|
|
it('Should not get stats if user is not admin', async () => {
|
|
const res = await sendRequest(adminUser.badToken, { query: fullQuery })
|
|
expect(res.body.errors).to.exist
|
|
expect(res.body.errors[0].extensions.code).to.equal('FORBIDDEN')
|
|
})
|
|
|
|
it('Should not get stats if user is not admin even if the token has the correct scopes', async () => {
|
|
const res = await sendRequest(notAdminUser.goodToken, { query: fullQuery })
|
|
expect(res.body.errors).to.exist
|
|
expect(res.body.errors[0].extensions.code).to.equal('FORBIDDEN')
|
|
})
|
|
|
|
it('Should not get stats if token does not have required scope', async () => {
|
|
const res = await sendRequest(adminUser.badToken, { query: fullQuery })
|
|
expect(res).to.be.json
|
|
expect(res.body.errors).to.exist
|
|
expect(res.body.errors[0].extensions.code).to.equal('FORBIDDEN')
|
|
})
|
|
|
|
it('Should get server stats', async () => {
|
|
const res = await sendRequest(adminUser.goodToken, { query: fullQuery })
|
|
expect(res).to.be.json
|
|
expect(res.body.errors).to.not.exist
|
|
|
|
expect(res.body.data).to.have.property('serverStats')
|
|
expect(res.body.data.serverStats).to.have.property('totalStreamCount')
|
|
expect(res.body.data.serverStats).to.have.property('totalCommitCount')
|
|
expect(res.body.data.serverStats).to.have.property('totalObjectCount')
|
|
expect(res.body.data.serverStats).to.have.property('totalUserCount')
|
|
expect(res.body.data.serverStats).to.have.property('streamHistory')
|
|
expect(res.body.data.serverStats).to.have.property('commitHistory')
|
|
expect(res.body.data.serverStats).to.have.property('userHistory')
|
|
|
|
expect(res.body.data.serverStats.totalStreamCount).to.equal(0) // the endpoint is deprecated and we're now returning 0
|
|
expect(res.body.data.serverStats.totalCommitCount).to.equal(0) // the endpoint is deprecated and we're now returning 0
|
|
expect(res.body.data.serverStats.totalObjectCount).to.equal(0) // the endpoint is deprecated and we're now returning 0
|
|
expect(res.body.data.serverStats.totalUserCount).to.equal(0) // the endpoint is deprecated and we're now returning 0
|
|
|
|
expect(res.body.data.serverStats.streamHistory).to.be.an('array')
|
|
expect(res.body.data.serverStats.commitHistory).to.be.an('array')
|
|
expect(res.body.data.serverStats.userHistory).to.be.an('array')
|
|
})
|
|
})
|
|
|
|
async function seedDb({
|
|
numUsers = 10,
|
|
numStreams = 10,
|
|
numObjects = 10,
|
|
numCommits = 10
|
|
} = {}) {
|
|
// create users
|
|
const users = []
|
|
for (let i = 0; i < numUsers; i++) {
|
|
const user = await createTestUser({
|
|
name: `User ${i}`,
|
|
password: `SuperSecure${i}${i * 3.14}`,
|
|
email: `user${i}@speckle.systems`
|
|
})
|
|
users.push(user)
|
|
}
|
|
|
|
// create streams
|
|
const streamPromises: Array<Promise<{ id: string; ownerId: string }>> = []
|
|
for (let i = 0; i < numStreams; i++) {
|
|
const owner = users[i >= users.length ? users.length - 1 : i]
|
|
const promise = createTestStream(
|
|
buildBasicTestProject({
|
|
name: `Stream ${i}`
|
|
}),
|
|
owner
|
|
)
|
|
|
|
streamPromises.push(promise)
|
|
}
|
|
|
|
const streamData = await Promise.all(streamPromises)
|
|
|
|
// create a objects
|
|
const objs = await createObjects({
|
|
streamId: streamData[0].id,
|
|
objects: createManyObjects(numObjects - 1)
|
|
})
|
|
|
|
// create commits referencing those objects
|
|
const commitPromises = []
|
|
for (let i = 0; i < numCommits; i++) {
|
|
const promise = createCommitByBranchName({
|
|
streamId: streamData[0].id,
|
|
authorId: streamData[0].ownerId,
|
|
branchName: 'main',
|
|
sourceApplication: 'tests',
|
|
objectId: objs[i >= objs.length ? objs.length - 1 : i],
|
|
message: null,
|
|
totalChildrenCount: undefined,
|
|
parents: null
|
|
})
|
|
commitPromises.push(promise)
|
|
}
|
|
|
|
await Promise.all(commitPromises)
|
|
}
|