Skip to content

Commit

Permalink
fix getPrimaryEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev committed Dec 6, 2023
1 parent 83c3d1a commit a93e35d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
43 changes: 43 additions & 0 deletions components/public-api/typescript-common/src/user-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

import { Timestamp } from "@bufbuild/protobuf";
import { Identity, User, User_ProfileDetails } from "@gitpod/public-api/lib/gitpod/v1/user_pb";
import * as chai from "chai";
import { getPrimaryEmail } from "./user-utils";

const expect = chai.expect;

describe("getPrimaryEmail", function () {
const user = new User({
organizationId: undefined,
profile: new User_ProfileDetails({
emailAddress: "[email protected]",
}),
identities: [
new Identity({
primaryEmail: "[email protected]",
}),
],
});
it(`should return email from profile exists`, () => {
const email = getPrimaryEmail(user);
expect(email).to.equal(user.profile!.emailAddress);
});
it(`should return email from SSO provider for org-owned accounts`, () => {
const ssoEmail = "[email protected]";
user.identities.unshift(
new Identity({
primaryEmail: ssoEmail,
// SSO identities have `lastSigninTime` set
lastSigninTime: Timestamp.fromDate(new Date()),
}),
);
user.organizationId = "any";
const email = getPrimaryEmail(user);
expect(email).to.equal(ssoEmail);
});
});
9 changes: 5 additions & 4 deletions components/public-api/typescript-common/src/user-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export function getPrimaryEmail(user: User | UserProtocol): string | undefined {

// In case of a personal account, check for the email stored by the user.
if (!isOrganizationOwned(user)) {
const emailAddress = UserProtocol.is(user)
? user.additionalData?.profile?.emailAddress
: user.profile?.emailAddress;
const emailAddress =
user instanceof User //
? user.profile?.emailAddress
: user.additionalData?.profile?.emailAddress;
if (emailAddress) {
return emailAddress;
}
Expand All @@ -56,7 +57,7 @@ export function getPrimaryEmail(user: User | UserProtocol): string | undefined {
}

export function getName(user: User | UserProtocol): string | undefined {
const name = /* user.fullName ||*/ user.name;
const name = user.name;
if (name) {
return name;
}
Expand Down

0 comments on commit a93e35d

Please sign in to comment.