@proconnect-gouv/proconnect.identite 1.7.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,97 +1,106 @@
1
1
  //
2
2
 
3
+ import { context, emptyDatabase, migrate, pg } from "#testing";
3
4
  import assert from "node:assert/strict";
4
- import { describe, it, mock } from "node:test";
5
+ import { before, beforeEach, describe, it } from "node:test";
5
6
  import { assignUserVerificationTypeToDomainFactory } from "./assign-user-verification-type-to-domain.js";
6
7
 
7
8
  //
8
9
 
10
+ const assignUserVerificationTypeToDomain =
11
+ assignUserVerificationTypeToDomainFactory(context);
12
+
9
13
  describe("assignUserVerificationTypeToDomain", () => {
14
+ before(migrate);
15
+ beforeEach(emptyDatabase);
16
+
10
17
  it("should update some organization members", async () => {
11
- const updateUserOrganizationLink = mock.fn(() =>
12
- Promise.resolve({} as any),
13
- );
14
- const assignUserVerificationTypeToDomain =
15
- assignUserVerificationTypeToDomainFactory({
16
- getUsers: () =>
17
- Promise.resolve([
18
- { email: "foo@bar.world" },
19
- { email: "foo@darkangels.world" },
20
- {
21
- email: "foo@darkangels.world",
22
- verification_type: "verified",
23
- },
24
- {
25
- id: 42,
26
- email: "lion.eljonson@darkangels.world",
27
- verification_type: "no_validation_means_available",
28
- },
29
- {
30
- id: 43,
31
- email: "cat.eljonson@darkangels.world",
32
- verification_type:
33
- "no_verification_means_for_entreprise_unipersonnelle",
34
- },
35
- {
36
- id: 44,
37
- email: "tiger.eljonson@darkangels.world",
38
- verification_type: "no_verification_means_for_small_association",
39
- },
40
- {
41
- id: 45,
42
- email: "mouse.eljonson@darkangels.world",
43
- verification_type: "domain_not_verified_yet",
44
- },
45
- ] as any),
46
- updateUserOrganizationLink,
47
- });
18
+ await pg.sql`
19
+ INSERT INTO organizations
20
+ (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
21
+ VALUES
22
+ (1, 'Dark Angels', 'Dark Angels Legion', '🦁', '4444-04-04', '4444-04-04')
23
+ ;
24
+ `;
25
+ // user from a different domain — should not be updated
26
+ await pg.sql`
27
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
28
+ VALUES (1, 'foo@bar.world', '4444-04-04', '4444-04-04', 'Foo', 'Bar', '0', 'None')
29
+ `;
30
+ // user matching domain but already "verified" — should not be updated
31
+ await pg.sql`
32
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
33
+ VALUES (2, 'foo@darkangels.world', '4444-04-04', '4444-04-04', 'Foo', 'DA', '0', 'None')
34
+ `;
35
+ // user with no_validation_means_available — should be updated
36
+ await pg.sql`
37
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
38
+ VALUES (3, 'lion.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'Lion', 'El''Jonson', '0', 'Primarque')
39
+ `;
40
+ // user with no_verification_means_for_entreprise_unipersonnelle — should be updated
41
+ await pg.sql`
42
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
43
+ VALUES (4, 'cat.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'Cat', 'El''Jonson', '0', 'Scout')
44
+ `;
45
+ // user with no_verification_means_for_small_association — should be updated
46
+ await pg.sql`
47
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
48
+ VALUES (5, 'tiger.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'Tiger', 'El''Jonson', '0', 'Scout')
49
+ `;
50
+ // user with domain_not_verified_yet — should be updated
51
+ await pg.sql`
52
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
53
+ VALUES (6, 'mouse.eljonson@darkangels.world', '4444-04-04', '4444-04-04', 'Mouse', 'El''Jonson', '0', 'Scout')
54
+ `;
48
55
 
49
- await assignUserVerificationTypeToDomain(111, "darkangels.world");
56
+ // link all users to the organization
57
+ await pg.sql`
58
+ INSERT INTO users_organizations
59
+ (user_id, organization_id, created_at, updated_at, is_external, verification_type, needs_official_contact_email_verification, official_contact_email_verification_token, official_contact_email_verification_sent_at)
60
+ VALUES
61
+ (1, 1, '4444-04-04', '4444-04-04', false, 'no_validation_means_available', false, null, null),
62
+ (2, 1, '4444-04-04', '4444-04-04', false, 'verified', false, null, null),
63
+ (3, 1, '4444-04-04', '4444-04-04', false, 'no_validation_means_available', false, null, null),
64
+ (4, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_for_entreprise_unipersonnelle', false, null, null),
65
+ (5, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_for_small_association', false, null, null),
66
+ (6, 1, '4444-04-04', '4444-04-04', false, 'domain_not_verified_yet', false, null, null)
67
+ ;
68
+ `;
50
69
 
51
- const [lion, cat, tiger, mouse] = updateUserOrganizationLink.mock.calls;
52
- assert.deepEqual(lion.arguments, [
53
- 111,
54
- 42,
55
- {
56
- verification_type: "domain",
57
- },
58
- ]);
59
- assert.deepEqual(cat.arguments, [
60
- 111,
61
- 43,
62
- {
63
- verification_type: "domain",
64
- },
65
- ]);
66
- assert.deepEqual(tiger.arguments, [
67
- 111,
68
- 44,
69
- {
70
- verification_type: "domain",
71
- },
72
- ]);
73
- assert.deepEqual(mouse.arguments, [
74
- 111,
75
- 45,
76
- {
77
- verification_type: "domain",
78
- },
70
+ await assignUserVerificationTypeToDomain(1, "darkangels.world");
71
+
72
+ // verify that the 4 matching users were updated to "domain"
73
+ const { rows } = await pg.sql`
74
+ SELECT user_id, verification_type
75
+ FROM users_organizations
76
+ WHERE organization_id = 1
77
+ ORDER BY user_id
78
+ `;
79
+
80
+ assert.deepEqual(rows, [
81
+ { user_id: 1, verification_type: "no_validation_means_available" }, // different domain
82
+ { user_id: 2, verification_type: "verified" }, // already verified
83
+ { user_id: 3, verification_type: "domain" },
84
+ { user_id: 4, verification_type: "domain" },
85
+ { user_id: 5, verification_type: "domain" },
86
+ { user_id: 6, verification_type: "domain" },
79
87
  ]);
80
- assert.equal(updateUserOrganizationLink.mock.callCount(), 4);
81
88
  });
82
89
 
83
90
  it("❎ should update nothing if no organization members", async () => {
84
- const updateUserOrganizationLink = mock.fn(() =>
85
- Promise.reject(new Error("💣")),
86
- );
87
- const assignUserVerificationTypeToDomain =
88
- assignUserVerificationTypeToDomainFactory({
89
- getUsers: () => Promise.resolve([]),
90
- updateUserOrganizationLink,
91
- });
91
+ await pg.sql`
92
+ INSERT INTO organizations
93
+ (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
94
+ VALUES
95
+ (1, 'Empty', 'Empty Org', '🫥', '4444-04-04', '4444-04-04')
96
+ ;
97
+ `;
92
98
 
93
- await assignUserVerificationTypeToDomain(42, "darkangels.world");
99
+ await assignUserVerificationTypeToDomain(1, "darkangels.world");
94
100
 
95
- assert.deepEqual(updateUserOrganizationLink.mock.calls, []);
101
+ const { rows } = await pg.sql`
102
+ SELECT * FROM users_organizations WHERE organization_id = 1
103
+ `;
104
+ assert.deepEqual(rows, []);
96
105
  });
97
106
  });
@@ -1,27 +1,20 @@
1
1
  //
2
2
 
3
- import type { GetUsersByOrganizationHandler } from "#src/repositories/organization";
4
- import type { UpdateUserOrganizationLinkHandler } from "#src/repositories/user";
3
+ import type { Context } from "#src/connectors";
5
4
  import { LinkTypes, SuperWeakLinkTypes, UnverifiedLinkTypes } from "#src/types";
6
5
  import { getEmailDomain } from "@proconnect-gouv/proconnect.core/services/email";
7
6
  import { match } from "ts-pattern";
8
7
 
9
8
  //
10
9
 
11
- type FactoryDependencies = {
12
- getUsers: GetUsersByOrganizationHandler;
13
- updateUserOrganizationLink: UpdateUserOrganizationLinkHandler;
14
- };
15
-
16
10
  export function assignUserVerificationTypeToDomainFactory({
17
- getUsers,
18
- updateUserOrganizationLink,
19
- }: FactoryDependencies) {
11
+ repository: { organizations, users_organizations },
12
+ }: Context) {
20
13
  return async function assignUserVerificationTypeToDomain(
21
14
  organization_id: number,
22
15
  domain: string,
23
16
  ) {
24
- const usersInOrganization = await getUsers(organization_id);
17
+ const usersInOrganization = await organizations.getUsers(organization_id);
25
18
 
26
19
  await Promise.all(
27
20
  usersInOrganization.map(
@@ -33,7 +26,7 @@ export function assignUserVerificationTypeToDomainFactory({
33
26
  .with(...UnverifiedLinkTypes, ...SuperWeakLinkTypes, () => true)
34
27
  .otherwise(() => false)
35
28
  ) {
36
- return updateUserOrganizationLink(organization_id, id, {
29
+ return users_organizations.update(organization_id, id, {
37
30
  verification_type: LinkTypes.enum.domain,
38
31
  });
39
32
  }
@@ -44,7 +37,3 @@ export function assignUserVerificationTypeToDomainFactory({
44
37
  );
45
38
  };
46
39
  }
47
-
48
- export type AssignUserVerificationTypeToDomainFactoryHandler = ReturnType<
49
- typeof assignUserVerificationTypeToDomainFactory
50
- >;
package/testing/index.ts CHANGED
@@ -4,10 +4,12 @@ import { PGlite } from "@electric-sql/pglite";
4
4
  import { noop } from "lodash-es";
5
5
  import { runner } from "node-pg-migrate";
6
6
  import { join } from "path";
7
+ import { createContext } from "../src/connectors/index.js";
7
8
 
8
9
  //
9
10
 
10
11
  export const pg = new PGlite();
12
+ export const context = createContext(pg as any);
11
13
 
12
14
  export function migrate() {
13
15
  return runner({