✨ Simple User Quota (#367)
* ✨Simple User Quota * 👷Fix build with OpenSearch
This commit is contained in:
@@ -9,10 +9,16 @@ import { v1 as uuidv1 } from "uuid";
|
||||
import { TestDbService } from "../utils.prepare.db";
|
||||
import { DriveFile } from "../../../src/services/documents/entities/drive-file";
|
||||
import { FileVersion } from "../../../src/services/documents/entities/file-version";
|
||||
import { AccessTokenMockClass, DriveItemDetailsMockClass, SearchResultMockClass } from "./entities/mock_entities";
|
||||
import {
|
||||
AccessTokenMockClass,
|
||||
DriveItemDetailsMockClass,
|
||||
SearchResultMockClass,
|
||||
UserQuotaMockClass
|
||||
} from "./entities/mock_entities";
|
||||
import { logger } from "../../../src/core/platform/framework";
|
||||
import { expect } from "@jest/globals";
|
||||
import { publicAccessLevel } from "../../../src/services/documents/types";
|
||||
import { UserQuota } from "../../../src/services/user/web/types";
|
||||
|
||||
export default class TestHelpers {
|
||||
|
||||
@@ -191,22 +197,23 @@ export default class TestHelpers {
|
||||
parent_id = "root",
|
||||
) {
|
||||
const file = await this.uploadRandomFile();
|
||||
const item = {
|
||||
name: file.metadata.name,
|
||||
parent_id: parent_id,
|
||||
company_id: file.company_id,
|
||||
};
|
||||
|
||||
const version = {
|
||||
file_metadata: {
|
||||
name: file.metadata.name,
|
||||
size: file.upload_data?.size,
|
||||
thumbnails: [],
|
||||
external_id: file.id
|
||||
}
|
||||
}
|
||||
const doc = await this.createDocumentFromFile(file, parent_id);
|
||||
|
||||
const doc = await this.createDocument(item, version);
|
||||
expect(doc).toBeDefined();
|
||||
expect(doc).not.toBeNull();
|
||||
expect(doc.parent_id).toEqual(parent_id)
|
||||
|
||||
return doc;
|
||||
};
|
||||
|
||||
async createDocumentFromFilename(
|
||||
file_name: "sample.png",
|
||||
parent_id = "root",
|
||||
) {
|
||||
const file = await this.uploadFile(file_name);
|
||||
|
||||
const doc = await this.createDocumentFromFile(file, parent_id);
|
||||
|
||||
expect(doc).toBeDefined();
|
||||
expect(doc).not.toBeNull();
|
||||
@@ -338,5 +345,16 @@ export default class TestHelpers {
|
||||
response.body)
|
||||
};
|
||||
|
||||
async quota() {
|
||||
const url = "/internal/services/users/v1/users";
|
||||
|
||||
const response = await this.platform.app.inject({
|
||||
method: "GET",
|
||||
headers: { "authorization": `Bearer ${this.jwt}` },
|
||||
url: `${url}/${this.user.id}/quota?companyId=${this.platform.workspace.company_id}`,
|
||||
})
|
||||
|
||||
return deserialize<UserQuota>(UserQuotaMockClass, response.body)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {DriveFileAccessLevel, publicAccessLevel} from "../../../../src/services/documents/types";
|
||||
import { UserQuota } from "../../../../src/services/user/web/types";
|
||||
|
||||
export type MockAccessInformation = {
|
||||
public?: {
|
||||
@@ -45,6 +46,12 @@ export class SearchResultMockClass {
|
||||
entities: DriveFileMockClass[];
|
||||
}
|
||||
|
||||
export class UserQuotaMockClass implements UserQuota{
|
||||
remaining: number;
|
||||
total: number;
|
||||
used: number;
|
||||
}
|
||||
|
||||
export class AccessTokenMockClass {
|
||||
access_token: {
|
||||
time: 0;
|
||||
|
||||
@@ -4,7 +4,6 @@ import { AccessInformation, DriveFile } from "../../../src/services/documents/en
|
||||
import { FileVersion } from "../../../src/services/documents/entities/file-version";
|
||||
import { DriveFileAccessLevel, DriveItemDetails } from "../../../src/services/documents/types";
|
||||
import { init, TestPlatform } from "../setup";
|
||||
import { TestDbService } from "../utils.prepare.db";
|
||||
import { e2e_createDocument, e2e_updateDocument } from "./utils";
|
||||
import TestHelpers from "../common/common_test_helpers";
|
||||
import { AccessTokenMockClass } from "../common/entities/mock_entities";
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect} from "@jest/globals";
|
||||
import { init, TestPlatform } from "../setup";
|
||||
import TestHelpers from "../common/common_test_helpers";
|
||||
|
||||
describe("The /users/quota API", () => {
|
||||
let platform: TestPlatform;
|
||||
let currentUser: TestHelpers;
|
||||
|
||||
beforeEach(async () => {
|
||||
platform = await init();
|
||||
currentUser = await TestHelpers.getInstance(platform);
|
||||
}, 30000000);
|
||||
|
||||
afterEach(async () => {
|
||||
await platform.tearDown();
|
||||
platform = null;
|
||||
});
|
||||
|
||||
beforeAll(async () => {
|
||||
const platform = await init({
|
||||
services: [
|
||||
"database",
|
||||
"search",
|
||||
"message-queue",
|
||||
"websocket",
|
||||
"applications",
|
||||
"webserver",
|
||||
"user",
|
||||
"auth",
|
||||
"storage",
|
||||
"counter",
|
||||
"console",
|
||||
"workspaces",
|
||||
"statistics",
|
||||
"platform-services",
|
||||
],
|
||||
});
|
||||
}, 30000000);
|
||||
|
||||
afterAll(async () => {
|
||||
});
|
||||
|
||||
|
||||
test("should reutrn 200 with available quota", async () => {
|
||||
//given
|
||||
const userQuota = 200000000;
|
||||
const doc = await currentUser.createDocumentFromFilename("sample.png", "user_" + currentUser.user.id)
|
||||
|
||||
//when
|
||||
const quota = await currentUser.quota();
|
||||
|
||||
expect(quota.total).toBe(userQuota);
|
||||
expect(quota.remaining).toBe(userQuota - doc.size); //198346196 //198342406
|
||||
expect(quota.used).toBe(doc.size);
|
||||
}, 30000000);
|
||||
|
||||
test("should return 200 with all empty space", async () => {
|
||||
//given
|
||||
const userQuota = 200000000;
|
||||
|
||||
//when
|
||||
const quota = await currentUser.quota();
|
||||
|
||||
expect(quota.total).toBe(userQuota);
|
||||
expect(quota.remaining).toBe(userQuota); //198346196 //198342406
|
||||
expect(quota.used).toBe(0);
|
||||
});
|
||||
|
||||
});
|
||||
+1
-1
@@ -54,7 +54,7 @@ describe('The Postgres Connector module', () => {
|
||||
expect(normalizeWhitespace(dbQuerySpy.mock.calls[2][0])).toBe(normalizeWhitespace("SELECT table_name, column_name, data_type FROM information_schema.columns WHERE table_name = $1"));
|
||||
expect(dbQuerySpy.mock.calls[2][1]).toStrictEqual(["test_table"])
|
||||
expect(normalizeWhitespace(dbQuerySpy.mock.calls[3][0])).toBe(`ALTER TABLE "test_table" ADD COLUMN id UUID, ADD COLUMN is_in_trash BOOLEAN, ADD COLUMN tags TEXT, ADD COLUMN added BIGINT`)
|
||||
expect(normalizeWhitespace(dbQuerySpy.mock.calls[4][0])).toBe(`ALTER TABLE "test_table" ADD PRIMARY KEY ( company_id, id);`)
|
||||
expect(normalizeWhitespace(dbQuerySpy.mock.calls[4][0])).toBe(`do $$ begin IF NOT EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'test_table' and constraint_type = 'PRIMARY KEY') THEN ALTER TABLE "test_table" ADD PRIMARY KEY ( company_id, id); END IF; end $$;`)
|
||||
expect(normalizeWhitespace(dbQuerySpy.mock.calls[5][0])).toBe(`CREATE INDEX IF NOT EXISTS index_test_table_company_id_parent_id ON "test_table" ((company_id), parent_id)`)
|
||||
expect(normalizeWhitespace(dbQuerySpy.mock.calls[6][0])).toBe(`CREATE INDEX IF NOT EXISTS index_test_table_company_id_is_in_trash ON "test_table" ((company_id), is_in_trash)`)
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user