Path: blob/main/components/gitpod-db/src/project-db.spec.db.ts
2497 views
/**1* Copyright (c) 2022 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { Project } from "@gitpod/gitpod-protocol";7import { suite, test } from "@testdeck/mocha";8import * as chai from "chai";9import { testContainer } from "./test-container";10import { resetDB } from "./test/reset-db";11import { ProjectDBImpl } from "./typeorm/project-db-impl";12import { TypeORM } from "./typeorm/typeorm";13import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";14const expect = chai.expect;1516@suite17class ProjectDBSpec {18projectDb = testContainer.get<ProjectDBImpl>(ProjectDBImpl);19userDb = testContainer.get<TypeORMUserDBImpl>(TypeORMUserDBImpl);2021async before() {22await this.wipeRepo();23}2425async after() {26await this.wipeRepo();27}2829async wipeRepo() {30const typeorm = testContainer.get<TypeORM>(TypeORM);31await resetDB(typeorm);32}3334@test()35public async findProjectBySearchTerm() {36const user = await this.userDb.newUser();37user.identities.push({38authProviderId: "GitHub",39authId: "1234",40authName: "newUser",41primaryEmail: "[email protected]",42});43await this.userDb.storeUser(user);4445const project = Project.create({46name: "some-project",47cloneUrl: "some-random-clone-url",48teamId: "team-1",49appInstallationId: "app-1",50});51const searchTerm = "rand";52const storedProject = await this.projectDb.storeProject(project);53const foundProject = await this.projectDb.findProjectsBySearchTerm({54offset: 0,55limit: 10,56orderBy: "creationTime",57orderDir: "DESC",58searchTerm,59});6061expect(foundProject.rows[0].id).to.eq(storedProject.id);6263const foundProjectByName = await this.projectDb.findProjectsBySearchTerm({64offset: 0,65limit: 10,66orderBy: "creationTime",67orderDir: "DESC",68searchTerm: "some-proj",69});70expect(foundProjectByName.rows[0].id).to.eq(storedProject.id);7172const foundProjectEmptySearch = await this.projectDb.findProjectsBySearchTerm({73offset: 0,74limit: 10,75orderBy: "creationTime",76orderDir: "DESC",77searchTerm: " ",78});79expect(foundProjectEmptySearch.rows[0].id).to.eq(storedProject.id);80}8182@test()83public async findProjectBySearchTermPagniation() {84const user = await this.userDb.newUser();85user.identities.push({86authProviderId: "GitHub",87authId: "1234",88authName: "newUser",89primaryEmail: "[email protected]",90});91await this.userDb.storeUser(user);9293const project1 = Project.create({94name: "some-project",95cloneUrl: "some-random-clone-url",96teamId: "team-1",97appInstallationId: "",98});99const project2 = Project.create({100name: "some-project-2",101cloneUrl: "some-random-clone-url-2",102teamId: "team-1",103appInstallationId: "",104});105const project3 = Project.create({106name: "some-project-3",107cloneUrl: "some-random-clone-url-1",108teamId: "team-1",109appInstallationId: "",110});111const project4 = Project.create({112name: "some-project-4",113cloneUrl: "some-random-clone-url-1",114teamId: "team-1",115appInstallationId: "",116});117const project5 = Project.create({118name: "some-project-5",119cloneUrl: "some-random-clone-url-1",120teamId: "team-1",121appInstallationId: "",122});123const storedProject1 = await this.projectDb.storeProject(project1);124const storedProject2 = await this.projectDb.storeProject(project2);125const storedProject3 = await this.projectDb.storeProject(project3);126const storedProject4 = await this.projectDb.storeProject(project4);127const storedProject5 = await this.projectDb.storeProject(project5);128129const allResults = await this.projectDb.findProjectsBySearchTerm({130offset: 0,131limit: 10,132orderBy: "name",133orderDir: "ASC",134});135expect(allResults.total).equals(5);136expect(allResults.rows.length).equal(5);137expect(allResults.rows[0].id).to.eq(storedProject1.id);138expect(allResults.rows[1].id).to.eq(storedProject2.id);139expect(allResults.rows[2].id).to.eq(storedProject3.id);140expect(allResults.rows[3].id).to.eq(storedProject4.id);141expect(allResults.rows[4].id).to.eq(storedProject5.id);142143const pageSize = 3;144const page1 = await this.projectDb.findProjectsBySearchTerm({145offset: 0,146limit: pageSize,147orderBy: "name",148orderDir: "ASC",149});150expect(page1.total).equals(5);151expect(page1.rows.length).equal(3);152expect(page1.rows[0].id).to.eq(storedProject1.id);153expect(page1.rows[1].id).to.eq(storedProject2.id);154expect(page1.rows[2].id).to.eq(storedProject3.id);155156const page2 = await this.projectDb.findProjectsBySearchTerm({157offset: pageSize * 1,158limit: pageSize,159orderBy: "name",160orderDir: "ASC",161});162expect(page2.total).equals(5);163expect(page2.rows.length).equal(2);164expect(page2.rows[0].id).to.eq(storedProject4.id);165expect(page2.rows[1].id).to.eq(storedProject5.id);166}167168@test()169public async findProjectBySearchTermOrganizationId() {170const user = await this.userDb.newUser();171user.identities.push({172authProviderId: "GitHub",173authId: "1234",174authName: "newUser",175primaryEmail: "[email protected]",176});177await this.userDb.storeUser(user);178179const project1 = Project.create({180name: "some-project",181cloneUrl: "some-random-clone-url",182teamId: "team-1",183appInstallationId: "",184});185const project2 = Project.create({186name: "some-project-2",187cloneUrl: "some-random-clone-url-2",188teamId: "team-2",189appInstallationId: "",190});191const storedProject1 = await this.projectDb.storeProject(project1);192const storedProject2 = await this.projectDb.storeProject(project2);193194const team1Results = await this.projectDb.findProjectsBySearchTerm({195offset: 0,196limit: 10,197orderBy: "name",198orderDir: "ASC",199organizationId: "team-1",200});201expect(team1Results.total).equals(1);202expect(team1Results.rows[0].id).to.eq(storedProject1.id);203204const team2Results = await this.projectDb.findProjectsBySearchTerm({205offset: 0,206limit: 10,207orderBy: "name",208orderDir: "ASC",209organizationId: "team-2",210});211expect(team2Results.total).equals(1);212expect(team2Results.rows[0].id).to.eq(storedProject2.id);213214const noResults = await this.projectDb.findProjectsBySearchTerm({215offset: 0,216limit: 10,217orderBy: "name",218orderDir: "ASC",219organizationId: "does-not-exist",220});221expect(noResults.total).equals(0);222}223224@test()225public async findProjectBySearchTermPrebuildsEnabled() {226const user = await this.userDb.newUser();227user.identities.push({228authProviderId: "GitHub",229authId: "1234",230authName: "newUser",231primaryEmail: "[email protected]",232});233await this.userDb.storeUser(user);234235const noPrebuilds = Project.create({236name: "no-prebuilds",237cloneUrl: "some-random-clone-url",238teamId: "team-1",239appInstallationId: "",240settings: {241prebuilds: {242enable: false,243},244},245});246247const withPrebuilds = Project.create({248name: "with-prebuilds",249cloneUrl: "some-random-clone-url-2",250teamId: "team-1",251appInstallationId: "",252settings: {253prebuilds: {254enable: true,255},256},257});258259const storedNoPrebuilds = await this.projectDb.storeProject(noPrebuilds);260const storedWithPrebuilds = await this.projectDb.storeProject(withPrebuilds);261262const noPrebuildsResults = await this.projectDb.findProjectsBySearchTerm({263offset: 0,264limit: 10,265orderBy: "name",266orderDir: "ASC",267prebuildsEnabled: false,268});269expect(noPrebuildsResults.total).equals(1);270expect(noPrebuildsResults.rows[0].id).to.eq(storedNoPrebuilds.id);271272const withPrebuildsResults = await this.projectDb.findProjectsBySearchTerm({273offset: 0,274limit: 10,275orderBy: "name",276orderDir: "ASC",277prebuildsEnabled: true,278});279expect(withPrebuildsResults.total).equals(1);280expect(withPrebuildsResults.rows[0].id).to.eq(storedWithPrebuilds.id);281282const noPrebuildsFilterResults = await this.projectDb.findProjectsBySearchTerm({283offset: 0,284limit: 10,285orderBy: "name",286orderDir: "ASC",287prebuildsEnabled: undefined,288});289expect(noPrebuildsFilterResults.total).equals(2);290}291}292293module.exports = new ProjectDBSpec();294295296