Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): should not throw when not adding any new roles to a user #6387

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/core/src/queries/users-roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@
${conditionalSql(limit, (value) => sql`limit ${value}`)}
`);

const insertUsersRoles = async (usersRoles: CreateUsersRole[]) =>
pool.query(sql`
const insertUsersRoles = async (usersRoles: CreateUsersRole[]) => {
if (usersRoles.length === 0) {
return;
}

return pool.query(sql`

Check warning on line 50 in packages/core/src/queries/users-roles.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/queries/users-roles.ts#L46-L50

Added lines #L46 - L50 were not covered by tests
insert into ${table} (${fields.id}, ${fields.userId}, ${fields.roleId}) values
${sql.join(
usersRoles.map(({ id, userId, roleId }) => sql`(${id}, ${userId}, ${roleId})`),
sql`, `
)}
`);
};

const deleteUsersRolesByUserIdAndRoleId = async (userId: string, roleId: string) => {
const { rowCount } = await pool.query(sql`
Expand Down
3 changes: 3 additions & 0 deletions packages/integration-tests/src/api/admin-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export const deleteUserIdentity = async (userId: string, connectorTarget: string
export const assignRolesToUser = async (userId: string, roleIds: string[]) =>
authedAdminApi.post(`users/${userId}/roles`, { json: { roleIds } });

export const putRolesToUser = async (userId: string, roleIds: string[]) =>
authedAdminApi.put(`users/${userId}/roles`, { json: { roleIds } });

/**
* Get roles assigned to the user.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Prompt } from '@logto/node';
import { InteractionEvent, demoAppApplicationId } from '@logto/schemas';

import { assignRolesToUser, putInteraction } from '#src/api/index.js';
import { assignRolesToUser, putRolesToUser, putInteraction } from '#src/api/index.js';
import { createRole } from '#src/api/role.js';
import MockClient from '#src/client/index.js';
import { demoAppRedirectUri } from '#src/constants.js';
Expand Down Expand Up @@ -56,6 +56,8 @@ describe('OpenID Connect ID token', () => {
it('should be issued with correct `username` and `roles` claims', async () => {
const role = await createRole({});
await assignRolesToUser(userId, [role.id]);
// Should not throw when not adding any new role(s) to a user.
await expect(putRolesToUser(userId, [role.id])).resolves.not.toThrow();
await fetchIdToken(['username', 'roles'], {
username,
roles: [role.name],
Expand Down
Loading