Skip to content

Commit

Permalink
fix: Don't show join default channels option for edit user form (Rock…
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-rajpal authored May 21, 2024
1 parent 760ab4b commit 07c4ca0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/real-bobcats-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/rest-typings': patch
'@rocket.chat/meteor': patch
---

Don't show Join default channels option on edit user form.
38 changes: 22 additions & 16 deletions apps/meteor/client/views/admin/users/AdminUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ const getInitialValue = ({
data,
defaultUserRoles,
isSmtpEnabled,
isEditingExistingUser,
}: {
data?: Serialized<IUser>;
defaultUserRoles?: IUser['roles'];
isSmtpEnabled?: boolean;
isEditingExistingUser?: boolean;
}) => ({
roles: data?.roles ?? defaultUserRoles,
name: data?.name ?? '',
Expand All @@ -69,7 +71,7 @@ const getInitialValue = ({
requirePasswordChange: data?.requirePasswordChange || false,
customFields: data?.customFields ?? {},
statusText: data?.statusText ?? '',
joinDefaultChannels: true,
...(!isEditingExistingUser && { joinDefaultChannels: true }),
sendWelcomeEmail: isSmtpEnabled,
avatar: '' as AvatarObject,
});
Expand Down Expand Up @@ -97,14 +99,16 @@ const UserForm = ({ userData, onReload, ...props }: AdminUserFormProps) => {

const goToUser = useCallback((id) => router.navigate(`/admin/users/info/${id}`), [router]);

const isEditingExistingUser = Boolean(userData?._id);

const {
control,
watch,
handleSubmit,
reset,
formState: { errors, isDirty },
} = useForm({
defaultValues: getInitialValue({ data: userData, defaultUserRoles, isSmtpEnabled }),
defaultValues: getInitialValue({ data: userData, defaultUserRoles, isSmtpEnabled, isEditingExistingUser }),
mode: 'onBlur',
});

Expand Down Expand Up @@ -166,7 +170,7 @@ const UserForm = ({ userData, onReload, ...props }: AdminUserFormProps) => {
<>
<ContextualbarScrollableContent {...props}>
<FieldGroup>
{userData?._id && (
{isEditingExistingUser && (
<Field>
<Controller
name='avatar'
Expand Down Expand Up @@ -346,7 +350,7 @@ const UserForm = ({ userData, onReload, ...props }: AdminUserFormProps) => {
<Controller
control={control}
name='password'
rules={{ required: !userData?._id && t('The_field_is_required', t('Password')) }}
rules={{ required: !isEditingExistingUser && t('The_field_is_required', t('Password')) }}
render={({ field }) => (
<PasswordInput
{...field}
Expand Down Expand Up @@ -434,18 +438,20 @@ const UserForm = ({ userData, onReload, ...props }: AdminUserFormProps) => {
</FieldRow>
{errors?.roles && <FieldError>{errors.roles.message}</FieldError>}
</Field>
<Field>
<FieldRow>
<FieldLabel htmlFor={joinDefaultChannelsId}>{t('Join_default_channels')}</FieldLabel>
<Controller
control={control}
name='joinDefaultChannels'
render={({ field: { ref, onChange, value } }) => (
<ToggleSwitch id={joinDefaultChannelsId} ref={ref} onChange={onChange} checked={value} />
)}
/>
</FieldRow>
</Field>
{!isEditingExistingUser && (
<Field>
<FieldRow>
<FieldLabel htmlFor={joinDefaultChannelsId}>{t('Join_default_channels')}</FieldLabel>
<Controller
control={control}
name='joinDefaultChannels'
render={({ field: { ref, onChange, value } }) => (
<ToggleSwitch id={joinDefaultChannelsId} ref={ref} onChange={onChange} checked={value} />
)}
/>
</FieldRow>
</Field>
)}
<Field>
<FieldRow>
<FieldLabel htmlFor={sendWelcomeEmailId}>{t('Send_welcome_email')}</FieldLabel>
Expand Down
20 changes: 20 additions & 0 deletions apps/meteor/tests/e2e/administration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ test.describe.parallel('administration', () => {
await poAdmin.tabs.users.setupSmtpLink.click();
await expect(page).toHaveURL('/admin/settings/Email');
});

test('expect to show join default channels option only when creating new users, not when editing users', async () => {
const username = faker.internet.userName();

await poAdmin.tabs.users.btnNewUser.click();
await poAdmin.tabs.users.inputName.type(faker.person.firstName());
await poAdmin.tabs.users.inputUserName.type(username);
await poAdmin.tabs.users.inputEmail.type(faker.internet.email());
await poAdmin.tabs.users.checkboxVerified.click();
await poAdmin.tabs.users.inputPassword.type('any_password');
await expect(poAdmin.tabs.users.userRole).toBeVisible();
await expect(poAdmin.tabs.users.joinDefaultChannels).toBeVisible();
await poAdmin.tabs.users.btnSave.click();

await poAdmin.inputSearchUsers.fill(username);
await poAdmin.getUserRow(username).click();
await poAdmin.btnEdit.click();
await expect(poAdmin.tabs.users.inputUserName).toHaveValue(username);
await expect(poAdmin.tabs.users.joinDefaultChannels).not.toBeVisible();
});
});

test.describe('Rooms', () => {
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/tests/e2e/page-objects/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ export class Admin {
return this.page.locator('[role="link"]', { hasText: name });
}

getUserRow(username?: string): Locator {
return this.page.locator('[role="link"]', { hasText: username });
}

get btnSave(): Locator {
return this.page.locator('button >> text="Save"');
}

get btnEdit(): Locator {
return this.page.locator('button >> text="Edit"');
}

get privateLabel(): Locator {
return this.page.locator(`label >> text=Private`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class AdminFlextabUsers {
return this.page.locator('//label[text()="Verified"]');
}

get joinDefaultChannels(): Locator {
return this.page.locator('//label[text()="Join default channels"]');
}

get userRole(): Locator {
return this.page.locator('button[role="option"]:has-text("user")');
}
Expand Down

0 comments on commit 07c4ca0

Please sign in to comment.