Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/src/team-db.spec.db.ts
2497 views
1
/**
2
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import * as chai from "chai";
8
const expect = chai.expect;
9
import { suite, test, timeout } from "@testdeck/mocha";
10
import { v4 as uuidv4 } from "uuid";
11
12
import { testContainer } from "./test-container";
13
import { TeamDBImpl } from "./typeorm/team-db-impl";
14
import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";
15
import { TypeORM } from "./typeorm/typeorm";
16
import { Connection } from "typeorm";
17
import { resetDB } from "./test/reset-db";
18
19
@suite
20
class TeamDBSpec {
21
db = testContainer.get<TeamDBImpl>(TeamDBImpl);
22
userDb = testContainer.get<TypeORMUserDBImpl>(TypeORMUserDBImpl);
23
24
async before() {
25
await this.wipeRepo();
26
}
27
28
async after() {
29
await this.wipeRepo();
30
}
31
32
async wipeRepo() {
33
const typeorm = testContainer.get<TypeORM>(TypeORM);
34
await resetDB(typeorm);
35
}
36
37
@test(timeout(10000))
38
public async createAndFindATeam() {
39
const user = await this.userDb.newUser();
40
let dbResult = await this.db.findTeamsByUser(user.id);
41
expect(dbResult.length).to.eq(0);
42
await this.db.createTeam(user.id, "Ground Control");
43
dbResult = await this.db.findTeamsByUser(user.id);
44
expect(dbResult.length).to.eq(1);
45
expect(dbResult[0].name).to.eq("Ground Control");
46
}
47
48
@test(timeout(10000))
49
public async findTeamMembers() {
50
const user = await this.userDb.newUser();
51
user.identities.push({
52
authProviderId: "GitHub",
53
authId: "1234",
54
authName: "Major Tom",
55
primaryEmail: "[email protected]",
56
});
57
await this.userDb.storeUser(user);
58
const team = await this.db.createTeam(user.id, "Flight Crew");
59
const members = await this.db.findMembersByTeam(team.id);
60
expect(members.length).to.eq(1);
61
expect(members[0].userId).to.eq(user.id);
62
}
63
64
@test(timeout(15000))
65
public async findTeamWhenUserIsSoleOwner() {
66
const user = await this.userDb.newUser();
67
user.identities.push({
68
authProviderId: "GitHub",
69
authId: "2345",
70
authName: "Nana",
71
primaryEmail: "[email protected]",
72
});
73
await this.userDb.storeUser(user);
74
75
const ownTeam = await this.db.createTeam(user.id, "My Own Team");
76
77
const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);
78
79
expect(teams.length).to.eq(1);
80
expect(teams[0].id).to.eq(ownTeam.id);
81
}
82
83
@test(timeout(10000))
84
public async findTeamWhenUserIsSoleOwnerWithMembers() {
85
const user = await this.userDb.newUser();
86
user.identities.push({
87
authProviderId: "GitHub",
88
authId: "2345",
89
authName: "Nana",
90
primaryEmail: "[email protected]",
91
});
92
await this.userDb.storeUser(user);
93
const user2 = await this.userDb.newUser();
94
user2.identities.push({
95
authProviderId: "GitLab",
96
authId: "4567",
97
authName: "Dudu",
98
primaryEmail: "[email protected]",
99
});
100
await this.userDb.storeUser(user2);
101
102
const ownTeam = await this.db.createTeam(user.id, "My Own Team With Members");
103
await this.db.addMemberToTeam(user2.id, ownTeam.id);
104
const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);
105
106
expect(teams.length).to.eq(1);
107
expect(teams[0].id).to.eq(ownTeam.id);
108
}
109
110
@test(timeout(10000))
111
public async findNoTeamWhenCoOwned() {
112
const user = await this.userDb.newUser();
113
user.identities.push({
114
authProviderId: "GitHub",
115
authId: "2345",
116
authName: "Nana",
117
primaryEmail: "[email protected]",
118
});
119
await this.userDb.storeUser(user);
120
const user2 = await this.userDb.newUser();
121
user2.identities.push({
122
authProviderId: "GitLab",
123
authId: "4567",
124
authName: "Dudu",
125
primaryEmail: "[email protected]",
126
});
127
await this.userDb.storeUser(user2);
128
129
const jointTeam = await this.db.createTeam(user.id, "Joint Team");
130
await this.db.addMemberToTeam(user2.id, jointTeam.id);
131
await this.db.setTeamMemberRole(user2.id, jointTeam.id, "owner");
132
133
const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);
134
135
expect(teams.length).to.eq(0);
136
}
137
138
@test(timeout(10000))
139
public async findTeams() {
140
const user = await this.userDb.newUser();
141
const t1 = await this.db.createTeam(user.id, "First Team");
142
await this.db.createTeam(user.id, "Second Team");
143
144
let searchTerm = "first";
145
let result = await this.db.findTeams(0, 10, "creationTime", "DESC", searchTerm);
146
expect(result.rows.length).to.eq(1);
147
148
searchTerm = "team";
149
result = await this.db.findTeams(0, 10, "creationTime", "DESC", searchTerm);
150
expect(result.rows.length).to.eq(2);
151
152
await this.db.deleteTeam(t1.id);
153
result = await this.db.findTeams(0, 10, "creationTime", "DESC", searchTerm);
154
expect(result.rows.length).to.eq(1);
155
}
156
157
@test(timeout(10000))
158
public async test_hasActiveSSO() {
159
expect((await this.db.findTeams(0, 1, "creationTime", "ASC")).total, "case 1: empty db").to.be.eq(0);
160
161
const user = await this.userDb.newUser();
162
const org = await this.db.createTeam(user.id, "Some Org");
163
await this.db.createTeam(user.id, "Another Org");
164
expect(await this.db.hasActiveSSO(org.id), "case 2: org without sso").to.be.false;
165
166
const id = uuidv4();
167
await this.exec(async (c) => {
168
await c.query(
169
"INSERT INTO d_b_oidc_client_config (id, issuer, organizationId, data, active) VALUES (?,?,?,?,?)",
170
[id, "https://issuer.local", org.id, "{}", 0],
171
);
172
});
173
expect(await this.db.hasActiveSSO(org.id), "case 3: org with inactive sso").to.be.false;
174
175
await this.exec(async (c) => {
176
await c.query("UPDATE d_b_oidc_client_config set active = ? where id = ?", [1, id]);
177
});
178
expect(await this.db.hasActiveSSO(org.id), "case 4: org with active sso").to.be.true;
179
180
await this.db.deleteTeam(org.id);
181
expect(await this.db.hasActiveSSO(org.id), "case 5: deleted org").to.be.false;
182
}
183
184
protected async exec(queryFn: (connection: Connection) => Promise<void>) {
185
const typeorm = testContainer.get<TypeORM>(TypeORM);
186
const connection = await typeorm.getConnection();
187
await queryFn(connection);
188
}
189
}
190
191
module.exports = new TeamDBSpec();
192
193