Skip to content

[db] userById fails on undefined #18962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/gitpod-db/src/typeorm/user-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { DataCache } from "../data-cache";
import { TransactionalDBImpl } from "./transactional-db-impl";
import { TypeORM } from "./typeorm";
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems odd to me to use messaging errors in db layer, but the check is good.


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

public async findUserById(id: string): Promise<MaybeUser> {
if (!id || id.trim() === "") {
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Cannot find user without id");
}
return this.cache.get(getUserCacheKey(id), async () => {
const userRepo = await this.getUserRepo();
const result = await userRepo.findOne(id);
Expand Down
30 changes: 30 additions & 0 deletions components/gitpod-db/src/user-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,36 @@ class UserDBSpec {
expect(dbResult).to.deep.include(user);
}

@test(timeout(10000))
public async findUserById() {
const user = await this.db.newUser();
user.identities.push(this.IDENTITY1);
await this.db.storeUser(user);

const foundUser = await this.db.findUserById(user.id);
expect(foundUser!.id).to.eq(user.id);
}

@test(timeout(10000))
public async findUserById_undefined() {
const user = await this.db.newUser();
user.identities.push(this.IDENTITY1);
await this.db.storeUser(user);

try {
await this.db.findUserById(undefined!);
expect.fail("Should have failed");
} catch (error) {
expect(error.code).to.eq(400);
}
try {
await this.db.findUserById("");
expect.fail("Should have failed");
} catch (error) {
expect(error.code).to.eq(400);
}
}

@test(timeout(10000))
public async findUsersByEmail_multiple_users_identities() {
let user1 = await this.db.newUser();
Expand Down