Skip to content

Commit

Permalink
addressing PR feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoshouzi-gh committed Aug 25, 2023
1 parent 3f26008 commit c3a2c40
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
28 changes: 21 additions & 7 deletions spec/common/providers/identity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,34 +772,48 @@ describe("identity", () => {
});

describe("generateRequestPayload", () => {
const DISPLAY_NAME_FILED = "displayName";
const DISPLAY_NAME_FIELD = "displayName";
const TEST_RESPONSE = {
displayName: TEST_NAME,
recaptchaPassed: false,
} as identity.BeforeCreateResponse;

const EXPECT_PAYLOAD = {
userRecord: { displayName: TEST_NAME, updateMask: DISPLAY_NAME_FILED },
userRecord: { displayName: TEST_NAME, updateMask: DISPLAY_NAME_FIELD },
recaptchaPassed: false,
};

const TEST_RESPONSE_RECAPTCHA_TRUE = {
recaptchaPassed: true,
} as identity.BeforeCreateResponse;

const EXPECT_PAYLOAD_RECAPTCHA_TRUE = {
recaptchaPassed: true,
};

const TEST_RESPONSE_RECAPTCHA_UNDEFINED = {
displayName: TEST_NAME,
} as identity.BeforeSignInResponse;

const EXPECT_PAYLOAD_UNDEFINED = {
userRecord: { displayName: TEST_NAME, updateMask: DISPLAY_NAME_FILED },
userRecord: { displayName: TEST_NAME, updateMask: DISPLAY_NAME_FIELD },
};
it("should return empty string on undefined response", () => {
expect(identity.generateRequestPayload()).to.eq("");
it("should return empty object on undefined response", () => {
expect(identity.generateResponsePayload()).to.eql({});
});

it("should exclude recaptchaPass field from updateMask", () => {
expect(identity.generateRequestPayload(TEST_RESPONSE)).to.deep.equal(EXPECT_PAYLOAD);
expect(identity.generateResponsePayload(TEST_RESPONSE)).to.deep.equal(EXPECT_PAYLOAD);
});

it("should return recaptchaPass when it is true on response", () => {
expect(identity.generateResponsePayload(TEST_RESPONSE_RECAPTCHA_TRUE)).to.deep.equal(
EXPECT_PAYLOAD_RECAPTCHA_TRUE
);
});

it("should not return recaptchaPass if undefined", () => {
const payload = identity.generateRequestPayload(TEST_RESPONSE_RECAPTCHA_UNDEFINED);
const payload = identity.generateResponsePayload(TEST_RESPONSE_RECAPTCHA_UNDEFINED);
expect(payload.hasOwnProperty("recaptchaPassed")).to.be.false;
expect(payload).to.deep.equal(EXPECT_PAYLOAD_UNDEFINED);
});
Expand Down
24 changes: 18 additions & 6 deletions src/common/providers/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,17 @@ export interface DecodedPayload {
[key: string]: any;
}

/** @internal */
export interface ResponsePayload {
userRecord?: UserRecordResponsePayload;
recaptchaPassed?: boolean;
}

/** @internal */
export interface UserRecordResponsePayload extends Omit<BeforeSignInResponse, "recaptchaPassed"> {
updateMask?: string;
}

type HandlerV1 = (
user: AuthUserRecord,
context: AuthEventContext
Expand Down Expand Up @@ -651,18 +662,19 @@ function parseAdditionalUserInfo(decodedJWT: DecodedPayload): AdditionalUserInfo
};
}

/** Helper to generate payload to GCIP from client request.
/**
* Helper to generate a response from the blocking function to the Firebase Auth backend.
* @internal
*/
export function generateRequestPayload(
export function generateResponsePayload(
authResponse?: BeforeCreateResponse | BeforeSignInResponse
): any {
): ResponsePayload {
if (!authResponse) {
return "";
return {};
}

const { recaptchaPassed, ...formattedAuthResponse } = authResponse;
const result = {} as any;
const result = {} as ResponsePayload;
const updateMask = getUpdateMask(formattedAuthResponse);

if (updateMask.length !== 0) {
Expand Down Expand Up @@ -853,7 +865,7 @@ export function wrapHandler(eventType: AuthBlockingEventType, handler: HandlerV1
}

validateAuthResponse(eventType, authResponse);
const result = generateRequestPayload(authResponse);
const result = generateResponsePayload(authResponse);

res.status(200);
res.setHeader("Content-Type", "application/json");
Expand Down

0 comments on commit c3a2c40

Please sign in to comment.