@proconnect-gouv/proconnect.identite.runtime 0.3.1 → 0.5.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.
- package/CHANGELOG.md +37 -0
- package/dist/activity/activity-context-schemas.d.ts +104 -0
- package/dist/activity/activity-context-schemas.d.ts.map +1 -1
- package/dist/activity/activity-context-schemas.js +6 -0
- package/dist/activity/types.d.ts +3 -0
- package/dist/activity/types.d.ts.map +1 -1
- package/dist/franceconnect/activities.d.ts +16 -0
- package/dist/franceconnect/activities.d.ts.map +1 -0
- package/dist/franceconnect/activities.js +17 -0
- package/dist/franceconnect/connect-franceconnect.d.ts +20 -0
- package/dist/franceconnect/connect-franceconnect.d.ts.map +1 -0
- package/dist/franceconnect/connect-franceconnect.js +59 -0
- package/dist/franceconnect/disconnect-franceconnect.d.ts +14 -0
- package/dist/franceconnect/disconnect-franceconnect.d.ts.map +1 -0
- package/dist/franceconnect/disconnect-franceconnect.js +29 -0
- package/dist/franceconnect/index.d.ts +4 -0
- package/dist/franceconnect/index.d.ts.map +1 -0
- package/dist/franceconnect/index.js +4 -0
- package/dist/journal/insert-activity.js +6 -0
- package/dist/memberships/activities.d.ts +27 -0
- package/dist/memberships/activities.d.ts.map +1 -1
- package/dist/memberships/activities.js +27 -0
- package/dist/memberships/add-membership.d.ts +22 -0
- package/dist/memberships/add-membership.d.ts.map +1 -0
- package/dist/memberships/add-membership.js +46 -0
- package/dist/memberships/index.d.ts +3 -0
- package/dist/memberships/index.d.ts.map +1 -1
- package/dist/memberships/index.js +3 -0
- package/dist/memberships/remove-membership.d.ts +19 -0
- package/dist/memberships/remove-membership.d.ts.map +1 -0
- package/dist/memberships/remove-membership.js +29 -0
- package/dist/memberships/update-membership.d.ts +31 -0
- package/dist/memberships/update-membership.d.ts.map +1 -0
- package/dist/memberships/update-membership.js +61 -0
- package/dist/moderations/activities.d.ts +11 -0
- package/dist/moderations/activities.d.ts.map +1 -0
- package/dist/moderations/activities.js +12 -0
- package/dist/moderations/index.d.ts +3 -0
- package/dist/moderations/index.d.ts.map +1 -0
- package/dist/moderations/index.js +3 -0
- package/dist/moderations/open-moderation.d.ts +18 -0
- package/dist/moderations/open-moderation.d.ts.map +1 -0
- package/dist/moderations/open-moderation.js +31 -0
- package/dist/runtime/create-identite-runtime.d.ts +152 -0
- package/dist/runtime/create-identite-runtime.d.ts.map +1 -1
- package/dist/runtime/create-identite-runtime.js +22 -0
- package/dist/users/activities.d.ts +62 -0
- package/dist/users/activities.d.ts.map +1 -0
- package/dist/users/activities.js +54 -0
- package/dist/users/change-password.d.ts +16 -0
- package/dist/users/change-password.d.ts.map +1 -0
- package/dist/users/change-password.js +38 -0
- package/dist/users/delete-user.d.ts +14 -0
- package/dist/users/delete-user.d.ts.map +1 -0
- package/dist/users/delete-user.js +39 -0
- package/dist/users/index.d.ts +6 -0
- package/dist/users/index.d.ts.map +1 -0
- package/dist/users/index.js +6 -0
- package/dist/users/reset-mfa.d.ts +14 -0
- package/dist/users/reset-mfa.d.ts.map +1 -0
- package/dist/users/reset-mfa.js +53 -0
- package/dist/users/update-personal-informations.d.ts +21 -0
- package/dist/users/update-personal-informations.d.ts.map +1 -0
- package/dist/users/update-personal-informations.js +53 -0
- package/package.json +5 -4
- package/src/activity/activity-context-schemas.ts +6 -0
- package/src/activity/types.ts +2 -1
- package/src/domains/mark-domain-as-verified.test.ts +162 -243
- package/src/franceconnect/activities.ts +20 -0
- package/src/franceconnect/connect-franceconnect.test.ts +197 -0
- package/src/franceconnect/connect-franceconnect.ts +96 -0
- package/src/franceconnect/disconnect-franceconnect.test.ts +94 -0
- package/src/franceconnect/disconnect-franceconnect.ts +53 -0
- package/src/franceconnect/index.ts +5 -0
- package/src/journal/insert-activity.ts +6 -0
- package/src/memberships/activities.ts +27 -0
- package/src/memberships/add-membership.test.ts +129 -0
- package/src/memberships/add-membership.ts +80 -0
- package/src/memberships/index.ts +3 -0
- package/src/memberships/remove-membership.test.ts +122 -0
- package/src/memberships/remove-membership.ts +59 -0
- package/src/memberships/update-membership.test.ts +202 -0
- package/src/memberships/update-membership.ts +106 -0
- package/src/moderations/activities.ts +15 -0
- package/src/moderations/index.ts +4 -0
- package/src/moderations/open-moderation.test.ts +99 -0
- package/src/moderations/open-moderation.ts +59 -0
- package/src/runtime/create-identite-runtime.test.ts +20 -51
- package/src/runtime/create-identite-runtime.ts +34 -0
- package/src/users/activities.ts +60 -0
- package/src/users/change-password.test.ts +91 -0
- package/src/users/change-password.ts +64 -0
- package/src/users/delete-user.test.ts +142 -0
- package/src/users/delete-user.ts +60 -0
- package/src/users/index.ts +7 -0
- package/src/users/reset-mfa.test.ts +128 -0
- package/src/users/reset-mfa.ts +79 -0
- package/src/users/update-personal-informations.test.ts +113 -0
- package/src/users/update-personal-informations.ts +97 -0
- package/testing/index.ts +25 -0
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { IdentiteDatabase } from "#src/database";
|
|
4
|
+
import { createIdentiteRuntime } from "#src/runtime";
|
|
5
|
+
import { createTestDatabase, readActivityLogs } from "#testing";
|
|
6
|
+
import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
7
|
+
import type { FranceConnectUserInfoResponse } from "@proconnect-gouv/proconnect.identite/types";
|
|
8
|
+
import { sql } from "drizzle-orm";
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import { describe, it } from "node:test";
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
|
|
14
|
+
const user_info: FranceConnectUserInfoResponse = {
|
|
15
|
+
birthcountry: "99100",
|
|
16
|
+
birthdate: new Date("1970-01-01"),
|
|
17
|
+
birthplace: "Toulon",
|
|
18
|
+
family_name: "Valjean",
|
|
19
|
+
gender: "male",
|
|
20
|
+
given_name: "Jean Valjean",
|
|
21
|
+
sub: "fc-sub-jean",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
async function setup() {
|
|
25
|
+
const db = await createTestDatabase();
|
|
26
|
+
|
|
27
|
+
await db.insert(schema.users).values([
|
|
28
|
+
{
|
|
29
|
+
created_at: "4444-04-04",
|
|
30
|
+
email: "jean.valjean@republic.fr",
|
|
31
|
+
family_name: "Fauchelevent",
|
|
32
|
+
given_name: "Jeannot",
|
|
33
|
+
id: 1,
|
|
34
|
+
updated_at: "4444-04-04",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
created_at: "4444-04-04",
|
|
38
|
+
email: "cosette@republic.fr",
|
|
39
|
+
family_name: "Fauchelevent",
|
|
40
|
+
given_name: "Cosette",
|
|
41
|
+
id: 2,
|
|
42
|
+
updated_at: "4444-04-04",
|
|
43
|
+
},
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
db,
|
|
48
|
+
runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
|
|
49
|
+
type: "user",
|
|
50
|
+
user_id: 1,
|
|
51
|
+
}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
describe("connectFranceConnect", () => {
|
|
56
|
+
it("✅ links the identity, copies the FranceConnect names and journals it", async () => {
|
|
57
|
+
const { db, runtime } = await setup();
|
|
58
|
+
|
|
59
|
+
const outcome = await runtime.connectFranceConnect({
|
|
60
|
+
user_id: 1,
|
|
61
|
+
user_info,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
assert.equal(outcome.kind, "franceconnect_connected");
|
|
65
|
+
if (outcome.kind !== "franceconnect_connected") return;
|
|
66
|
+
assert.deepEqual(outcome.user, {
|
|
67
|
+
family_name: "Valjean",
|
|
68
|
+
given_name: "Jean",
|
|
69
|
+
});
|
|
70
|
+
assert.deepEqual(await users(db), [
|
|
71
|
+
{ family_name: "Valjean", given_name: "Jean", id: 1 },
|
|
72
|
+
{ family_name: "Fauchelevent", given_name: "Cosette", id: 2 },
|
|
73
|
+
]);
|
|
74
|
+
assert.deepEqual(await identities(db), [
|
|
75
|
+
{
|
|
76
|
+
birthcountry: "99100",
|
|
77
|
+
birthdate: "1970-01-01 00:00:00+00",
|
|
78
|
+
birthplace: "Toulon",
|
|
79
|
+
family_name: "Valjean",
|
|
80
|
+
gender: "male",
|
|
81
|
+
given_name: "Jean Valjean",
|
|
82
|
+
sub: "fc-sub-jean",
|
|
83
|
+
user_id: 1,
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
assert.deepEqual(await readActivityLogs(db), [
|
|
87
|
+
{
|
|
88
|
+
action: "franceconnect.connected",
|
|
89
|
+
actor_ref: "1",
|
|
90
|
+
actor_type: "user",
|
|
91
|
+
app: "proconnect-identite",
|
|
92
|
+
cause_activity_id: null,
|
|
93
|
+
context: {
|
|
94
|
+
inputs: {},
|
|
95
|
+
outcome: { connected_at: outcome.connected_at },
|
|
96
|
+
},
|
|
97
|
+
interaction_cid: null,
|
|
98
|
+
moderation_id: null,
|
|
99
|
+
organization_id: null,
|
|
100
|
+
user_id: 1,
|
|
101
|
+
},
|
|
102
|
+
]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("✅ replaces an existing identity and keeps its creation date", async () => {
|
|
106
|
+
const { db, runtime } = await setup();
|
|
107
|
+
await db.insert(schema.franceconnect_userinfo).values({
|
|
108
|
+
created_at: "4444-04-04",
|
|
109
|
+
family_name: "Valjean",
|
|
110
|
+
given_name: "Jean",
|
|
111
|
+
sub: "old-sub",
|
|
112
|
+
updated_at: "4444-04-04",
|
|
113
|
+
user_id: 1,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const outcome = await runtime.connectFranceConnect({
|
|
117
|
+
user_id: 1,
|
|
118
|
+
user_info,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
assert.equal(outcome.kind, "franceconnect_connected");
|
|
122
|
+
if (outcome.kind !== "franceconnect_connected") return;
|
|
123
|
+
assert.match(outcome.connected_at, /^4444-04-04/);
|
|
124
|
+
assert.deepEqual(await identities(db), [
|
|
125
|
+
{
|
|
126
|
+
birthcountry: "99100",
|
|
127
|
+
birthdate: "1970-01-01 00:00:00+00",
|
|
128
|
+
birthplace: "Toulon",
|
|
129
|
+
family_name: "Valjean",
|
|
130
|
+
gender: "male",
|
|
131
|
+
given_name: "Jean Valjean",
|
|
132
|
+
sub: "fc-sub-jean",
|
|
133
|
+
user_id: 1,
|
|
134
|
+
},
|
|
135
|
+
]);
|
|
136
|
+
assert.equal((await readActivityLogs(db)).length, 1);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("❎ returns user_not_found for an unknown user and writes nothing", async () => {
|
|
140
|
+
const { db, runtime } = await setup();
|
|
141
|
+
|
|
142
|
+
const outcome = await runtime.connectFranceConnect({
|
|
143
|
+
user_id: 404,
|
|
144
|
+
user_info,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
assert.deepEqual(outcome, { kind: "user_not_found", user_id: 404 });
|
|
148
|
+
assert.deepEqual(await identities(db), []);
|
|
149
|
+
assert.deepEqual(await readActivityLogs(db), []);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("❎ rolls back the names and the identity when the journal insert fails", async () => {
|
|
153
|
+
const { db, runtime } = await setup();
|
|
154
|
+
await db.execute(sql`
|
|
155
|
+
ALTER TABLE activity_logs
|
|
156
|
+
ADD CONSTRAINT activity_logs_never_franceconnect_connected
|
|
157
|
+
CHECK (action <> 'franceconnect.connected')
|
|
158
|
+
`);
|
|
159
|
+
|
|
160
|
+
await assert.rejects(
|
|
161
|
+
runtime.connectFranceConnect({ user_id: 1, user_info }),
|
|
162
|
+
(error: Error) =>
|
|
163
|
+
error.cause instanceof Error &&
|
|
164
|
+
error.cause.message.includes(
|
|
165
|
+
"activity_logs_never_franceconnect_connected",
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
assert.deepEqual(await users(db), [
|
|
169
|
+
{ family_name: "Fauchelevent", given_name: "Jeannot", id: 1 },
|
|
170
|
+
{ family_name: "Fauchelevent", given_name: "Cosette", id: 2 },
|
|
171
|
+
]);
|
|
172
|
+
assert.deepEqual(await identities(db), []);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
function users(db: IdentiteDatabase) {
|
|
177
|
+
return db.query.users.findMany({
|
|
178
|
+
columns: { family_name: true, given_name: true, id: true },
|
|
179
|
+
orderBy: (table, { asc }) => asc(table.id),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function identities(db: IdentiteDatabase) {
|
|
184
|
+
return db.query.franceconnect_userinfo.findMany({
|
|
185
|
+
columns: {
|
|
186
|
+
birthcountry: true,
|
|
187
|
+
birthdate: true,
|
|
188
|
+
birthplace: true,
|
|
189
|
+
family_name: true,
|
|
190
|
+
gender: true,
|
|
191
|
+
given_name: true,
|
|
192
|
+
sub: true,
|
|
193
|
+
user_id: true,
|
|
194
|
+
},
|
|
195
|
+
orderBy: (table, { asc }) => asc(table.user_id),
|
|
196
|
+
});
|
|
197
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { ActivityContexts, RuntimeContext } from "#src/activity";
|
|
4
|
+
import { insertActivity } from "#src/journal";
|
|
5
|
+
import {
|
|
6
|
+
franceconnect_userinfo,
|
|
7
|
+
users,
|
|
8
|
+
} from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
9
|
+
import type { FranceConnectUserInfoResponse } from "@proconnect-gouv/proconnect.identite/types";
|
|
10
|
+
import { eq, sql } from "drizzle-orm";
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
|
|
14
|
+
type ConnectFranceConnectOutcome =
|
|
15
|
+
| {
|
|
16
|
+
kind: "franceconnect_connected";
|
|
17
|
+
activity_id: number;
|
|
18
|
+
connected_at: string;
|
|
19
|
+
user: {
|
|
20
|
+
family_name: string;
|
|
21
|
+
given_name: string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
| { kind: "user_not_found"; user_id: number };
|
|
25
|
+
|
|
26
|
+
export function connectFranceConnectFactory({
|
|
27
|
+
actor,
|
|
28
|
+
app,
|
|
29
|
+
db,
|
|
30
|
+
}: RuntimeContext) {
|
|
31
|
+
return function connectFranceConnect({
|
|
32
|
+
user_id,
|
|
33
|
+
user_info,
|
|
34
|
+
}: {
|
|
35
|
+
user_id: number;
|
|
36
|
+
user_info: FranceConnectUserInfoResponse;
|
|
37
|
+
}): Promise<ConnectFranceConnectOutcome> {
|
|
38
|
+
return db.transaction(async (tx) => {
|
|
39
|
+
const user = await tx.query.users.findFirst({
|
|
40
|
+
columns: { id: true },
|
|
41
|
+
where: (table, { eq }) => eq(table.id, user_id),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (!user) {
|
|
45
|
+
return { kind: "user_not_found", user_id };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const given_name = user_info.given_name.split(" ")[0];
|
|
49
|
+
const family_name = user_info.preferred_username || user_info.family_name;
|
|
50
|
+
|
|
51
|
+
await tx
|
|
52
|
+
.update(users)
|
|
53
|
+
.set({ family_name, given_name, updated_at: sql`now()` })
|
|
54
|
+
.where(eq(users.id, user_id));
|
|
55
|
+
|
|
56
|
+
const identity = {
|
|
57
|
+
birthcountry: user_info.birthcountry,
|
|
58
|
+
birthdate: user_info.birthdate.toISOString(),
|
|
59
|
+
birthplace: user_info.birthplace,
|
|
60
|
+
family_name: user_info.family_name,
|
|
61
|
+
gender: user_info.gender,
|
|
62
|
+
given_name: user_info.given_name,
|
|
63
|
+
preferred_username: user_info.preferred_username,
|
|
64
|
+
sub: user_info.sub,
|
|
65
|
+
updated_at: sql`now()`,
|
|
66
|
+
user_id,
|
|
67
|
+
};
|
|
68
|
+
const [franceconnect_identity] = await tx
|
|
69
|
+
.insert(franceconnect_userinfo)
|
|
70
|
+
.values(identity)
|
|
71
|
+
.onConflictDoUpdate({
|
|
72
|
+
target: franceconnect_userinfo.user_id,
|
|
73
|
+
set: identity,
|
|
74
|
+
})
|
|
75
|
+
.returning({ created_at: franceconnect_userinfo.created_at });
|
|
76
|
+
|
|
77
|
+
const { activity_id } = await insertActivity<ActivityContexts>(tx, {
|
|
78
|
+
action: "franceconnect.connected",
|
|
79
|
+
actor,
|
|
80
|
+
app,
|
|
81
|
+
context: {
|
|
82
|
+
inputs: {},
|
|
83
|
+
outcome: { connected_at: franceconnect_identity.created_at },
|
|
84
|
+
},
|
|
85
|
+
user_id,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
kind: "franceconnect_connected",
|
|
90
|
+
activity_id,
|
|
91
|
+
connected_at: franceconnect_identity.created_at,
|
|
92
|
+
user: { family_name, given_name },
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { IdentiteDatabase } from "#src/database";
|
|
4
|
+
import { createIdentiteRuntime } from "#src/runtime";
|
|
5
|
+
import { createTestDatabase, readActivityLogs } from "#testing";
|
|
6
|
+
import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
|
|
10
|
+
//
|
|
11
|
+
|
|
12
|
+
async function setup() {
|
|
13
|
+
const db = await createTestDatabase();
|
|
14
|
+
|
|
15
|
+
await db.insert(schema.users).values([
|
|
16
|
+
{
|
|
17
|
+
created_at: "4444-04-04",
|
|
18
|
+
email: "jean.valjean@republic.fr",
|
|
19
|
+
id: 1,
|
|
20
|
+
updated_at: "4444-04-04",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
created_at: "4444-04-04",
|
|
24
|
+
email: "cosette@republic.fr",
|
|
25
|
+
id: 2,
|
|
26
|
+
updated_at: "4444-04-04",
|
|
27
|
+
},
|
|
28
|
+
]);
|
|
29
|
+
await db.insert(schema.franceconnect_userinfo).values({
|
|
30
|
+
created_at: "4444-04-04",
|
|
31
|
+
sub: "fc-sub-jean",
|
|
32
|
+
updated_at: "4444-04-04",
|
|
33
|
+
user_id: 1,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
db,
|
|
38
|
+
runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
|
|
39
|
+
type: "user",
|
|
40
|
+
user_id: 1,
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("disconnectFranceConnect", () => {
|
|
46
|
+
it("✅ removes the FranceConnect identity and journals it under the user", async () => {
|
|
47
|
+
const { db, runtime } = await setup();
|
|
48
|
+
|
|
49
|
+
const outcome = await runtime.disconnectFranceConnect({ user_id: 1 });
|
|
50
|
+
|
|
51
|
+
assert.equal(outcome.kind, "franceconnect_disconnected");
|
|
52
|
+
if (outcome.kind !== "franceconnect_disconnected") return;
|
|
53
|
+
assert.match(outcome.connected_at, /^4444-04-04/);
|
|
54
|
+
assert.deepEqual(await identities(db), []);
|
|
55
|
+
assert.deepEqual(await readActivityLogs(db), [
|
|
56
|
+
{
|
|
57
|
+
action: "franceconnect.disconnected",
|
|
58
|
+
actor_ref: "1",
|
|
59
|
+
actor_type: "user",
|
|
60
|
+
app: "proconnect-identite",
|
|
61
|
+
cause_activity_id: null,
|
|
62
|
+
context: {
|
|
63
|
+
inputs: {},
|
|
64
|
+
outcome: { connected_at: outcome.connected_at },
|
|
65
|
+
},
|
|
66
|
+
interaction_cid: null,
|
|
67
|
+
moderation_id: null,
|
|
68
|
+
organization_id: null,
|
|
69
|
+
user_id: 1,
|
|
70
|
+
},
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("❎ returns franceconnect_identity_not_found and journals nothing for a missing identity", async () => {
|
|
75
|
+
const { db, runtime } = await setup();
|
|
76
|
+
const before = await identities(db);
|
|
77
|
+
|
|
78
|
+
const outcome = await runtime.disconnectFranceConnect({ user_id: 2 });
|
|
79
|
+
|
|
80
|
+
assert.deepEqual(outcome, {
|
|
81
|
+
kind: "franceconnect_identity_not_found",
|
|
82
|
+
user_id: 2,
|
|
83
|
+
});
|
|
84
|
+
assert.deepEqual(await identities(db), before);
|
|
85
|
+
assert.deepEqual(await readActivityLogs(db), []);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
function identities(db: IdentiteDatabase) {
|
|
90
|
+
return db.query.franceconnect_userinfo.findMany({
|
|
91
|
+
columns: { sub: true, user_id: true },
|
|
92
|
+
orderBy: (table, { asc }) => asc(table.user_id),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { ActivityContexts, RuntimeContext } from "#src/activity";
|
|
4
|
+
import { insertActivity } from "#src/journal";
|
|
5
|
+
import { franceconnect_userinfo } from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
6
|
+
import { eq } from "drizzle-orm";
|
|
7
|
+
|
|
8
|
+
//
|
|
9
|
+
|
|
10
|
+
type DisconnectFranceConnectOutcome =
|
|
11
|
+
| {
|
|
12
|
+
kind: "franceconnect_disconnected";
|
|
13
|
+
activity_id: number;
|
|
14
|
+
connected_at: string;
|
|
15
|
+
}
|
|
16
|
+
| { kind: "franceconnect_identity_not_found"; user_id: number };
|
|
17
|
+
|
|
18
|
+
export function disconnectFranceConnectFactory({
|
|
19
|
+
actor,
|
|
20
|
+
app,
|
|
21
|
+
db,
|
|
22
|
+
}: RuntimeContext) {
|
|
23
|
+
return function disconnectFranceConnect({
|
|
24
|
+
user_id,
|
|
25
|
+
}: {
|
|
26
|
+
user_id: number;
|
|
27
|
+
}): Promise<DisconnectFranceConnectOutcome> {
|
|
28
|
+
return db.transaction(async (tx) => {
|
|
29
|
+
const [previous] = await tx
|
|
30
|
+
.delete(franceconnect_userinfo)
|
|
31
|
+
.where(eq(franceconnect_userinfo.user_id, user_id))
|
|
32
|
+
.returning({ created_at: franceconnect_userinfo.created_at });
|
|
33
|
+
|
|
34
|
+
if (!previous) {
|
|
35
|
+
return { kind: "franceconnect_identity_not_found", user_id };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { activity_id } = await insertActivity<ActivityContexts>(tx, {
|
|
39
|
+
action: "franceconnect.disconnected",
|
|
40
|
+
actor,
|
|
41
|
+
app,
|
|
42
|
+
context: { inputs: {}, outcome: { connected_at: previous.created_at } },
|
|
43
|
+
user_id,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
kind: "franceconnect_disconnected",
|
|
48
|
+
activity_id,
|
|
49
|
+
connected_at: previous.created_at,
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -35,6 +35,12 @@ function toActorColumns({ actor, app }: Pick<RuntimeContext, "actor" | "app">) {
|
|
|
35
35
|
return { actor_ref: actor.job, actor_type: "app", app } as const;
|
|
36
36
|
case "moderator":
|
|
37
37
|
return { actor_ref: actor.sub, actor_type: "moderator", app } as const;
|
|
38
|
+
case "user":
|
|
39
|
+
return {
|
|
40
|
+
actor_ref: String(actor.user_id),
|
|
41
|
+
actor_type: "user",
|
|
42
|
+
app,
|
|
43
|
+
} as const;
|
|
38
44
|
default: {
|
|
39
45
|
const unhandled_actor: never = actor;
|
|
40
46
|
throw new Error(`Unhandled actor: ${JSON.stringify(unhandled_actor)}`);
|
|
@@ -6,6 +6,13 @@ import * as z from "zod";
|
|
|
6
6
|
//
|
|
7
7
|
|
|
8
8
|
export const membershipActivities = {
|
|
9
|
+
"member.joined": z.object({
|
|
10
|
+
inputs: z.object({
|
|
11
|
+
is_external: z.boolean(),
|
|
12
|
+
verification_type: z.string(),
|
|
13
|
+
}),
|
|
14
|
+
outcome: z.object({}),
|
|
15
|
+
}),
|
|
9
16
|
"member.promoted": z.object({
|
|
10
17
|
inputs: z.object({
|
|
11
18
|
previous_verification_type: z.string(),
|
|
@@ -14,4 +21,24 @@ export const membershipActivities = {
|
|
|
14
21
|
verification_type: LinkEnum,
|
|
15
22
|
}),
|
|
16
23
|
}),
|
|
24
|
+
"member.removed": z.object({
|
|
25
|
+
inputs: z.object({}),
|
|
26
|
+
outcome: z.object({
|
|
27
|
+
is_external: z.boolean(),
|
|
28
|
+
verification_type: z.string(),
|
|
29
|
+
}),
|
|
30
|
+
}),
|
|
31
|
+
"member.updated": z.object({
|
|
32
|
+
inputs: z.object({
|
|
33
|
+
is_external: z.boolean().optional(),
|
|
34
|
+
renew_verification: z.boolean().optional(),
|
|
35
|
+
verification_type: z.string().optional(),
|
|
36
|
+
}),
|
|
37
|
+
outcome: z.object({
|
|
38
|
+
previous: z.object({
|
|
39
|
+
is_external: z.boolean(),
|
|
40
|
+
verification_type: z.string(),
|
|
41
|
+
}),
|
|
42
|
+
}),
|
|
43
|
+
}),
|
|
17
44
|
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { IdentiteDatabase } from "#src/database";
|
|
4
|
+
import { createIdentiteRuntime } from "#src/runtime";
|
|
5
|
+
import { createTestDatabase, readActivityLogs } from "#testing";
|
|
6
|
+
import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
|
|
10
|
+
//
|
|
11
|
+
|
|
12
|
+
async function setup() {
|
|
13
|
+
const db = await createTestDatabase();
|
|
14
|
+
await db.insert(schema.organizations).values({ id: 1, siret: "🦁" });
|
|
15
|
+
await db.insert(schema.users).values([
|
|
16
|
+
{
|
|
17
|
+
created_at: "4444-04-04",
|
|
18
|
+
email: "jean.valjean@republic.fr",
|
|
19
|
+
id: 1,
|
|
20
|
+
updated_at: "4444-04-04",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
created_at: "4444-04-04",
|
|
24
|
+
email: "cosette@republic.fr",
|
|
25
|
+
id: 2,
|
|
26
|
+
updated_at: "4444-04-04",
|
|
27
|
+
},
|
|
28
|
+
]);
|
|
29
|
+
await db.insert(schema.users_organizations).values({
|
|
30
|
+
organization_id: 1,
|
|
31
|
+
user_id: 2,
|
|
32
|
+
verification_type: "domain",
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
db,
|
|
37
|
+
runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
|
|
38
|
+
type: "user",
|
|
39
|
+
user_id: 1,
|
|
40
|
+
}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe("addMembership", () => {
|
|
45
|
+
it("✅ adds the membership and journals it under the user", async () => {
|
|
46
|
+
const { db, runtime } = await setup();
|
|
47
|
+
|
|
48
|
+
const outcome = await runtime.addMembership({
|
|
49
|
+
is_external: true,
|
|
50
|
+
organization_id: 1,
|
|
51
|
+
user_id: 1,
|
|
52
|
+
verification_type: "official_contact_email",
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
assert.deepEqual(outcome, {
|
|
56
|
+
kind: "member_joined",
|
|
57
|
+
activity_id: 1,
|
|
58
|
+
membership: { organization_id: 1, user_id: 1 },
|
|
59
|
+
});
|
|
60
|
+
assert.deepEqual(await memberships(db), [
|
|
61
|
+
{
|
|
62
|
+
is_external: true,
|
|
63
|
+
organization_id: 1,
|
|
64
|
+
user_id: 1,
|
|
65
|
+
verification_type: "official_contact_email",
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
is_external: false,
|
|
69
|
+
organization_id: 1,
|
|
70
|
+
user_id: 2,
|
|
71
|
+
verification_type: "domain",
|
|
72
|
+
},
|
|
73
|
+
]);
|
|
74
|
+
assert.deepEqual(await readActivityLogs(db), [
|
|
75
|
+
{
|
|
76
|
+
action: "member.joined",
|
|
77
|
+
actor_ref: "1",
|
|
78
|
+
actor_type: "user",
|
|
79
|
+
app: "proconnect-identite",
|
|
80
|
+
cause_activity_id: null,
|
|
81
|
+
context: {
|
|
82
|
+
inputs: {
|
|
83
|
+
is_external: true,
|
|
84
|
+
verification_type: "official_contact_email",
|
|
85
|
+
},
|
|
86
|
+
outcome: {},
|
|
87
|
+
},
|
|
88
|
+
interaction_cid: null,
|
|
89
|
+
moderation_id: null,
|
|
90
|
+
organization_id: 1,
|
|
91
|
+
user_id: 1,
|
|
92
|
+
},
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("❎ returns member_already_exists and writes nothing for an existing link", async () => {
|
|
97
|
+
const { db, runtime } = await setup();
|
|
98
|
+
const before = await memberships(db);
|
|
99
|
+
|
|
100
|
+
const outcome = await runtime.addMembership({
|
|
101
|
+
organization_id: 1,
|
|
102
|
+
user_id: 2,
|
|
103
|
+
verification_type: "official_contact_email",
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
assert.deepEqual(outcome, {
|
|
107
|
+
kind: "member_already_exists",
|
|
108
|
+
organization_id: 1,
|
|
109
|
+
user_id: 2,
|
|
110
|
+
});
|
|
111
|
+
assert.deepEqual(await memberships(db), before);
|
|
112
|
+
assert.deepEqual(await readActivityLogs(db), []);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
function memberships(db: IdentiteDatabase) {
|
|
117
|
+
return db.query.users_organizations.findMany({
|
|
118
|
+
columns: {
|
|
119
|
+
is_external: true,
|
|
120
|
+
organization_id: true,
|
|
121
|
+
user_id: true,
|
|
122
|
+
verification_type: true,
|
|
123
|
+
},
|
|
124
|
+
orderBy: (table, { asc }) => [
|
|
125
|
+
asc(table.organization_id),
|
|
126
|
+
asc(table.user_id),
|
|
127
|
+
],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//
|
|
2
|
+
|
|
3
|
+
import type { ActivityContexts, RuntimeContext } from "#src/activity";
|
|
4
|
+
import { insertActivity } from "#src/journal";
|
|
5
|
+
import { users_organizations } from "@proconnect-gouv/proconnect.identite.database/drizzle";
|
|
6
|
+
import type { LinkType } from "@proconnect-gouv/proconnect.identite/types";
|
|
7
|
+
import { sql } from "drizzle-orm";
|
|
8
|
+
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
type AddMembershipOutcome =
|
|
12
|
+
| {
|
|
13
|
+
kind: "member_joined";
|
|
14
|
+
activity_id: number;
|
|
15
|
+
membership: {
|
|
16
|
+
organization_id: number;
|
|
17
|
+
user_id: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
| {
|
|
21
|
+
kind: "member_already_exists";
|
|
22
|
+
organization_id: number;
|
|
23
|
+
user_id: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function addMembershipFactory({ actor, app, db }: RuntimeContext) {
|
|
27
|
+
return function addMembership({
|
|
28
|
+
is_external = false,
|
|
29
|
+
organization_id,
|
|
30
|
+
user_id,
|
|
31
|
+
verification_type,
|
|
32
|
+
}: {
|
|
33
|
+
is_external?: boolean;
|
|
34
|
+
organization_id: number;
|
|
35
|
+
user_id: number;
|
|
36
|
+
verification_type: LinkType;
|
|
37
|
+
}): Promise<AddMembershipOutcome> {
|
|
38
|
+
return db.transaction(async (tx) => {
|
|
39
|
+
const [membership] = await tx
|
|
40
|
+
.insert(users_organizations)
|
|
41
|
+
.values({
|
|
42
|
+
created_at: sql`now()`,
|
|
43
|
+
is_external,
|
|
44
|
+
organization_id,
|
|
45
|
+
updated_at: sql`now()`,
|
|
46
|
+
user_id,
|
|
47
|
+
verification_type,
|
|
48
|
+
verified_at: sql`now()`,
|
|
49
|
+
})
|
|
50
|
+
.onConflictDoNothing({
|
|
51
|
+
target: [
|
|
52
|
+
users_organizations.user_id,
|
|
53
|
+
users_organizations.organization_id,
|
|
54
|
+
],
|
|
55
|
+
})
|
|
56
|
+
.returning({
|
|
57
|
+
organization_id: users_organizations.organization_id,
|
|
58
|
+
user_id: users_organizations.user_id,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (!membership) {
|
|
62
|
+
return { kind: "member_already_exists", organization_id, user_id };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const { activity_id } = await insertActivity<ActivityContexts>(tx, {
|
|
66
|
+
action: "member.joined",
|
|
67
|
+
actor,
|
|
68
|
+
app,
|
|
69
|
+
context: {
|
|
70
|
+
inputs: { is_external, verification_type },
|
|
71
|
+
outcome: {},
|
|
72
|
+
},
|
|
73
|
+
organization_id,
|
|
74
|
+
user_id,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return { kind: "member_joined", activity_id, membership };
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
}
|