Skip to content

Commit a84a4d8

Browse files
committed
fix(api-security-cognito): skip validation if email is not updated
1 parent a8fd53c commit a84a4d8

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

packages/api-security-cognito/__tests__/users.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,53 @@ describe("Security User CRUD Test", () => {
257257
});
258258
});
259259

260+
test("should not allow to update user with an existing email", async () => {
261+
// Let's create a user.
262+
const [createUserAResponse] = await adminUsers.create({
263+
data: {
264+
...mocks.userA,
265+
password: "12345678",
266+
groups: []
267+
}
268+
});
269+
270+
const userA = createUserAResponse.data.adminUsers.createUser.data;
271+
272+
const [createUserBResponse] = await adminUsers.create({
273+
data: {
274+
...mocks.userB,
275+
password: "12345678",
276+
groups: []
277+
}
278+
});
279+
280+
const userB = createUserBResponse.data.adminUsers.createUser.data;
281+
282+
// Let's update the "userB" name
283+
const [updateUserResponse] = await adminUsers.update({
284+
id: userB.id,
285+
data: {
286+
// Use an existing email of another user.
287+
email: userA.email
288+
}
289+
});
290+
291+
expect(updateUserResponse).toEqual({
292+
data: {
293+
adminUsers: {
294+
updateUser: {
295+
data: null,
296+
error: {
297+
code: "UPDATE_USER_ERROR",
298+
message: "Email is already taken!",
299+
data: null
300+
}
301+
}
302+
}
303+
}
304+
});
305+
});
306+
260307
test("should return current user based on identity", async () => {
261308
const [groupResponseA] = await securityGroups.get({ slug: "full-access" });
262309
const fullAccessGroup = groupResponseA.data.security.getGroup.data;

packages/api-security-cognito/src/createAdminUsersHooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const createAdminUsersHooks = () => {
7676
adminUsers.onUserBeforeUpdate.subscribe(async ({ user, updateData }) => {
7777
const tenant = getTenant();
7878

79-
if (!tenant) {
79+
if (!tenant || !updateData.email) {
8080
return;
8181
}
8282

packages/api-security-cognito/src/graphql/user.gql.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ export default (params: CreateUserGraphQlPluginsParams) => {
100100

101101
return new Response(user);
102102
} catch (e) {
103-
return new ErrorResponse(e);
103+
return new ErrorResponse({
104+
message: e.message,
105+
code: e.code
106+
});
104107
}
105108
},
106109
deleteUser: async (_, { id }: any, context) => {

0 commit comments

Comments
 (0)