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

Enable user status update #2591

Merged
merged 2 commits into from
May 6, 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
5 changes: 2 additions & 3 deletions assets/js/pages/Users/UserForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ function UserForm({
<Select
optionsName="status"
options={['Enabled', 'Disabled']}
value={status}
disabled
onChange={({ target: { value } }) => {
value={statusState}
onChange={(value) => {
setStatus(value);
}}
/>
Expand Down
9 changes: 9 additions & 0 deletions assets/js/pages/Users/UserForm.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ export default {
type: 'text',
},
},
status: {
control: { type: 'radio' },
options: ['Enabled', 'Disabled'],
description: 'Status',
table: {
type: { summary: 'string' },
defaultValue: { summary: 'Enabled' },
},
},
createdAt: {
description: 'User creation timestamp',
control: {
Expand Down
43 changes: 42 additions & 1 deletion assets/js/pages/Users/UserForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('UserForm', () => {
expect(screen.getAllByText('Required field').length).toBe(3);
});

it('saves the user', async () => {
it('should save the user', async () => {
const user = userEvent.setup();
const { fullname, email, username } = userFactory.build();
const password = faker.internet.password();
Expand All @@ -158,4 +158,45 @@ describe('UserForm', () => {
enabled: true,
});
});

it.each([
{ option: 'Enabled', result: true },
{ option: 'Disabled', result: false },
])('should set the user status correctly', async ({ option, result }) => {
const user = userEvent.setup();
const mockOnSave = jest.fn();

const {
fullname,
email,
username,
created_at: createdAt,
updated_at: updatedAt,
} = userFactory.build();

await act(async () => {
render(
<UserForm
fullName={fullname}
emailAddress={email}
username={username}
createdAt={createdAt}
updatedAt={updatedAt}
editing
onSave={mockOnSave}
/>
);
});

await user.click(screen.getByText('Enabled'));
await user.click(screen.getAllByText(option)[0]);

await user.click(screen.getByRole('button', { name: 'Create' }));

expect(mockOnSave).toHaveBeenNthCalledWith(1, {
fullname,
email,
enabled: result,
});
});
});
8 changes: 7 additions & 1 deletion lib/trento/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Trento.Users do

def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> User.changeset(set_locked_at(attrs))
|> Repo.insert()
end

Expand All @@ -72,6 +72,12 @@ defmodule Trento.Users do
|> Repo.update()
end

defp set_locked_at(%{enabled: false} = attrs) do
Map.put(attrs, :locked_at, DateTime.utc_now())
end

defp set_locked_at(attrs), do: attrs

defp do_update(user, attrs) do
user
|> User.update_changeset(attrs)
Expand Down
1 change: 1 addition & 0 deletions lib/trento/users/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule Trento.Users.User do
|> pow_extension_changeset(attrs)
|> validate_password()
|> custom_fields_changeset(attrs)
|> lock_changeset(attrs)
end

def update_changeset(user, attrs) do
Expand Down
17 changes: 17 additions & 0 deletions test/trento/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ defmodule Trento.UsersTest do
assert user.fullname == "some fullname"
assert user.email == "test@trento.com"
assert user.username == "username"
assert user.locked_at == nil
end

test "create_user creates a disabled user" do
%{username: username, email: email, fullname: fullname} = build(:user)

assert {:ok, %User{} = user} =
Users.create_user(%{
username: username,
password: "some password",
email: email,
fullname: fullname,
confirm_password: "some password",
enabled: false
})

refute user.locked_at == nil
end

test "create_user should return an error if the email has already been taken" do
Expand Down
Loading