Path: blob/main/components/gitpod-db/src/team-db.spec.db.ts
2497 views
/**1* Copyright (c) 2021 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 * as chai from "chai";7const expect = chai.expect;8import { suite, test, timeout } from "@testdeck/mocha";9import { v4 as uuidv4 } from "uuid";1011import { testContainer } from "./test-container";12import { TeamDBImpl } from "./typeorm/team-db-impl";13import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";14import { TypeORM } from "./typeorm/typeorm";15import { Connection } from "typeorm";16import { resetDB } from "./test/reset-db";1718@suite19class TeamDBSpec {20db = testContainer.get<TeamDBImpl>(TeamDBImpl);21userDb = testContainer.get<TypeORMUserDBImpl>(TypeORMUserDBImpl);2223async before() {24await this.wipeRepo();25}2627async after() {28await this.wipeRepo();29}3031async wipeRepo() {32const typeorm = testContainer.get<TypeORM>(TypeORM);33await resetDB(typeorm);34}3536@test(timeout(10000))37public async createAndFindATeam() {38const user = await this.userDb.newUser();39let dbResult = await this.db.findTeamsByUser(user.id);40expect(dbResult.length).to.eq(0);41await this.db.createTeam(user.id, "Ground Control");42dbResult = await this.db.findTeamsByUser(user.id);43expect(dbResult.length).to.eq(1);44expect(dbResult[0].name).to.eq("Ground Control");45}4647@test(timeout(10000))48public async findTeamMembers() {49const user = await this.userDb.newUser();50user.identities.push({51authProviderId: "GitHub",52authId: "1234",53authName: "Major Tom",54primaryEmail: "[email protected]",55});56await this.userDb.storeUser(user);57const team = await this.db.createTeam(user.id, "Flight Crew");58const members = await this.db.findMembersByTeam(team.id);59expect(members.length).to.eq(1);60expect(members[0].userId).to.eq(user.id);61}6263@test(timeout(15000))64public async findTeamWhenUserIsSoleOwner() {65const user = await this.userDb.newUser();66user.identities.push({67authProviderId: "GitHub",68authId: "2345",69authName: "Nana",70primaryEmail: "[email protected]",71});72await this.userDb.storeUser(user);7374const ownTeam = await this.db.createTeam(user.id, "My Own Team");7576const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);7778expect(teams.length).to.eq(1);79expect(teams[0].id).to.eq(ownTeam.id);80}8182@test(timeout(10000))83public async findTeamWhenUserIsSoleOwnerWithMembers() {84const user = await this.userDb.newUser();85user.identities.push({86authProviderId: "GitHub",87authId: "2345",88authName: "Nana",89primaryEmail: "[email protected]",90});91await this.userDb.storeUser(user);92const user2 = await this.userDb.newUser();93user2.identities.push({94authProviderId: "GitLab",95authId: "4567",96authName: "Dudu",97primaryEmail: "[email protected]",98});99await this.userDb.storeUser(user2);100101const ownTeam = await this.db.createTeam(user.id, "My Own Team With Members");102await this.db.addMemberToTeam(user2.id, ownTeam.id);103const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);104105expect(teams.length).to.eq(1);106expect(teams[0].id).to.eq(ownTeam.id);107}108109@test(timeout(10000))110public async findNoTeamWhenCoOwned() {111const user = await this.userDb.newUser();112user.identities.push({113authProviderId: "GitHub",114authId: "2345",115authName: "Nana",116primaryEmail: "[email protected]",117});118await this.userDb.storeUser(user);119const user2 = await this.userDb.newUser();120user2.identities.push({121authProviderId: "GitLab",122authId: "4567",123authName: "Dudu",124primaryEmail: "[email protected]",125});126await this.userDb.storeUser(user2);127128const jointTeam = await this.db.createTeam(user.id, "Joint Team");129await this.db.addMemberToTeam(user2.id, jointTeam.id);130await this.db.setTeamMemberRole(user2.id, jointTeam.id, "owner");131132const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);133134expect(teams.length).to.eq(0);135}136137@test(timeout(10000))138public async findTeams() {139const user = await this.userDb.newUser();140const t1 = await this.db.createTeam(user.id, "First Team");141await this.db.createTeam(user.id, "Second Team");142143let searchTerm = "first";144let result = await this.db.findTeams(0, 10, "creationTime", "DESC", searchTerm);145expect(result.rows.length).to.eq(1);146147searchTerm = "team";148result = await this.db.findTeams(0, 10, "creationTime", "DESC", searchTerm);149expect(result.rows.length).to.eq(2);150151await this.db.deleteTeam(t1.id);152result = await this.db.findTeams(0, 10, "creationTime", "DESC", searchTerm);153expect(result.rows.length).to.eq(1);154}155156@test(timeout(10000))157public async test_hasActiveSSO() {158expect((await this.db.findTeams(0, 1, "creationTime", "ASC")).total, "case 1: empty db").to.be.eq(0);159160const user = await this.userDb.newUser();161const org = await this.db.createTeam(user.id, "Some Org");162await this.db.createTeam(user.id, "Another Org");163expect(await this.db.hasActiveSSO(org.id), "case 2: org without sso").to.be.false;164165const id = uuidv4();166await this.exec(async (c) => {167await c.query(168"INSERT INTO d_b_oidc_client_config (id, issuer, organizationId, data, active) VALUES (?,?,?,?,?)",169[id, "https://issuer.local", org.id, "{}", 0],170);171});172expect(await this.db.hasActiveSSO(org.id), "case 3: org with inactive sso").to.be.false;173174await this.exec(async (c) => {175await c.query("UPDATE d_b_oidc_client_config set active = ? where id = ?", [1, id]);176});177expect(await this.db.hasActiveSSO(org.id), "case 4: org with active sso").to.be.true;178179await this.db.deleteTeam(org.id);180expect(await this.db.hasActiveSSO(org.id), "case 5: deleted org").to.be.false;181}182183protected async exec(queryFn: (connection: Connection) => Promise<void>) {184const typeorm = testContainer.get<TypeORM>(TypeORM);185const connection = await typeorm.getConnection();186await queryFn(connection);187}188}189190module.exports = new TeamDBSpec();191192193