Skip to content

Commit 8ac11b1

Browse files
authored
[db] userById fails on undefined (#18962)
1 parent ea29158 commit 8ac11b1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

components/gitpod-db/src/typeorm/user-db-impl.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
4949
import { DataCache } from "../data-cache";
5050
import { TransactionalDBImpl } from "./transactional-db-impl";
5151
import { TypeORM } from "./typeorm";
52+
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
5253

5354
// OAuth token expiry
5455
const tokenExpiryInFuture = new DateInterval("7d");
@@ -132,6 +133,9 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
132133
}
133134

134135
public async findUserById(id: string): Promise<MaybeUser> {
136+
if (!id || id.trim() === "") {
137+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Cannot find user without id");
138+
}
135139
return this.cache.get(getUserCacheKey(id), async () => {
136140
const userRepo = await this.getUserRepo();
137141
const result = await userRepo.findOne(id);

components/gitpod-db/src/user-db.spec.db.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ class UserDBSpec {
9797
expect(dbResult).to.deep.include(user);
9898
}
9999

100+
@test(timeout(10000))
101+
public async findUserById() {
102+
const user = await this.db.newUser();
103+
user.identities.push(this.IDENTITY1);
104+
await this.db.storeUser(user);
105+
106+
const foundUser = await this.db.findUserById(user.id);
107+
expect(foundUser!.id).to.eq(user.id);
108+
}
109+
110+
@test(timeout(10000))
111+
public async findUserById_undefined() {
112+
const user = await this.db.newUser();
113+
user.identities.push(this.IDENTITY1);
114+
await this.db.storeUser(user);
115+
116+
try {
117+
await this.db.findUserById(undefined!);
118+
expect.fail("Should have failed");
119+
} catch (error) {
120+
expect(error.code).to.eq(400);
121+
}
122+
try {
123+
await this.db.findUserById("");
124+
expect.fail("Should have failed");
125+
} catch (error) {
126+
expect(error.code).to.eq(400);
127+
}
128+
}
129+
100130
@test(timeout(10000))
101131
public async findUsersByEmail_multiple_users_identities() {
102132
let user1 = await this.db.newUser();

0 commit comments

Comments
 (0)