Skip to content

Commit

Permalink
fix: Avoid to autofill with paper from another contact
Browse files Browse the repository at this point in the history
If paper.cozyMetadata.sourceAccountIdentifier and contactEmail were
undefined, it was considered as a paper from the contact even if it
has no sense.

I added multiple tests to improve the function safety.
  • Loading branch information
zatteo committed Sep 17, 2024
1 parent 04ec8d3 commit a83d30b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
107 changes: 106 additions & 1 deletion libs/cozy/mapping.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CozyProfile } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";

import { selectDataWithCozyProfile } from "./mapping";
import { isPaperFromContact, selectDataWithCozyProfile } from "./mapping";

// PROFILES

Expand Down Expand Up @@ -41,6 +41,111 @@ const WORK_ONLY_ELEMENT = { phone: "2", label: "work" };
const WORK_AND_TYPE_ELEMENT = { phone: "3", label: "work", type: "Cozy Cloud" };

describe("mapping", () => {
describe("isPaperFromContact", () => {
it("should return true if referenced by same contact", () => {
const paper = {
relationships: {
referenced_by: {
data: [
{
id: "7a4a4166175d8bb5e69033669702390d",
type: "io.cozy.contacts",
},
],
},
},
cozyMetadata: {},
};

expect(
isPaperFromContact(paper, "7a4a4166175d8bb5e69033669702390d", "[email protected]", false),
).toEqual(true);
});

it("should return false if referenced by different contact", () => {
const paper = {
relationships: {
referenced_by: {
data: [
{
id: "9b2a1982738d8bb5e69033669700988a",
type: "io.cozy.contacts",
},
],
},
},
cozyMetadata: {},
};

expect(
isPaperFromContact(paper, "7a4a4166175d8bb5e69033669702390d", "[email protected]", false),
).toEqual(false);
});

it("should return true if source account id corresponds to email", () => {
const paper = {
relationships: {},
cozyMetadata: {
sourceAccountIdentifier: "[email protected]",
},
};

expect(
isPaperFromContact(paper, "7a4a4166175d8bb5e69033669702390d", "[email protected]", false),
).toEqual(true);
});

it("should return false if source account id does not correspond email", () => {
const paper = {
relationships: {},
cozyMetadata: {
sourceAccountIdentifier: "john123",
},
};

expect(
isPaperFromContact(paper, "7a4a4166175d8bb5e69033669702390d", "[email protected]", false),
).toEqual(false);
});

it("should return false if source account id is undefined and email is undefined", () => {
const paper = {
relationships: {},
cozyMetadata: {},
};

expect(
isPaperFromContact(paper, "7a4a4166175d8bb5e69033669702390d", undefined, false),
).toEqual(false);
});

it("should return true if paper from konnector and contact is 'me'", () => {
const paper = {
relationships: {},
cozyMetadata: {
sourceAccount: "123",
},
};

expect(
isPaperFromContact(paper, "7a4a4166175d8bb5e69033669702390d", "[email protected]", true),
).toEqual(true);
});

it("should return false if paper from konnector and contact is not 'me'", () => {
const paper = {
relationships: {},
cozyMetadata: {
sourceAccount: "123",
},
};

expect(
isPaperFromContact(paper, "7a4a4166175d8bb5e69033669702390d", "[email protected]", false),
).toEqual(false);
});
});

describe("selectDataWithCozyProfile", () => {
describe("with no element", () => {
it("should handle undefined array", () => {
Expand Down
22 changes: 15 additions & 7 deletions libs/cozy/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ export const selectDataWithCozyProfile = (data: any[] | undefined, cozyProfile?:
return data[0];
};

const isReferencedByContact = (paper: any, contactId: string) => {
return paper?.relationships?.referenced_by?.data?.find(
const isReferencedByContact = (paper: any, contactId: string): boolean => {
return paper?.relationships?.referenced_by?.data?.some(
(reference: any) => reference.id === contactId && reference.type === "io.cozy.contacts",
);
};
Expand All @@ -354,11 +354,19 @@ const isReferencedByContact = (paper: any, contactId: string) => {
* @param {boolean} me - A flag indicating whether to check the contact is "me".
* @returns {boolean} Returns true if the paper is from the specified contact.
*/
const isPaperFromContact = (paper: any, contactId: string | undefined, contactEmail: string | undefined, me: boolean) => {
return isReferencedByContact(paper, contactId) ||
paper.cozyMetadata?.sourceAccountIdentifier === contactEmail || // konnector login is equal to contact primary email
(paper.cozyMetadata?.sourceAccount && me) // by default, we assign papers to "me"
}
export const isPaperFromContact = (
paper: any,
contactId: string | undefined,
contactEmail: string | undefined,
me: boolean,
) => {
return (
isReferencedByContact(paper, contactId) ||
(paper.cozyMetadata?.sourceAccountIdentifier &&
paper.cozyMetadata.sourceAccountIdentifier === contactEmail) || // konnector login is equal to contact primary email
!!(paper.cozyMetadata?.sourceAccount && me) // by default, we assign papers to "me"
);
};

const makeYearFilterFunction = (field: AutofillField) => {
const filter = FILTERS.yearFilter;
Expand Down

0 comments on commit a83d30b

Please sign in to comment.