Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { User } from "shared/types";
import dayjs from "dayjs";
import { useAdmin } from "shared/hooks";
import styles from "./user-form.module.scss";
import { RestrictionCode } from "shared/enums";

type Props = {
user: User;
Expand Down Expand Up @@ -41,6 +42,8 @@ export const UserFormComponent: React.FC<Props> = ({ user, setUser }) => {
createdAt: dayjs(createdAt).valueOf(),
admin: user.admin,
languages: user.languages,
restrictions: user.restrictions,
blocked: user.blocked,
};

await updateUser($user);
Expand All @@ -66,6 +69,25 @@ export const UserFormComponent: React.FC<Props> = ({ user, setUser }) => {
adminOptions.find(({ key }) => (user?.admin ? "true" : "false" === key)),
[user, adminOptions],
);
const restrictionOptions = useMemo(
() =>
Object.keys(RestrictionCode).map((value) => ({
value,
key: RestrictionCode[value],
})),
[],
);

const selectedRestrictionOption = useMemo(
() =>
restrictionOptions.find(({ key }) =>
user?.restrictions?.[0] ? user.restrictions[0][1] === key : null,
),
[user, restrictionOptions],
);

const isBlocked = useMemo(() => user?.blocked, [user]);

const $onRemoveAccount = useCallback(async () => {
await deleteUser(user);
fetchUsers();
Expand Down Expand Up @@ -131,6 +153,34 @@ export const UserFormComponent: React.FC<Props> = ({ user, setUser }) => {
}
/>
</div>
<hr />
<div className={styles.formRow}>
<SelectorComponent
name="restrictions"
placeholder="restrictions"
options={restrictionOptions}
defaultOption={selectedRestrictionOption}
onChange={(option) =>
setUser({
...user,
restrictions: option
? [Date.now(), option.key as RestrictionCode]
: undefined,
})
}
/>
<ButtonComponent
color={isBlocked ? "yellow" : "grey"}
onClick={() =>
setUser({
...user,
blocked: !user.blocked,
})
}
>
{isBlocked ? "UnBlock" : "Block"}
</ButtonComponent>
</div>
<div className={styles.actions}>
{user.admin ? null : (
<ButtonComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const AdminUsersComponent = () => {
const title = $row[$column.key];
let value = title;

if ($column.key === "restrictions")
value = title[1] ? "🚫" : title[0] ? "⚠️" : null;
if ($column.key === "email") value = getCensoredEmail(title);
if ($column.key === "accountId")
value = value.substring(0, 12) + "...";
Expand All @@ -62,6 +64,7 @@ export const AdminUsersComponent = () => {

return {
...user,
restrictions: [user.restrictions, user.blocked],
otp: user.otp ? "✅" : "❌",
verified: user.verified
? "✅"
Expand All @@ -71,6 +74,10 @@ export const AdminUsersComponent = () => {
};
})}
columns={[
{
key: "restrictions",
label: "-",
},
{
key: "accountId",
label: "Account Id",
Expand Down
1 change: 1 addition & 0 deletions app/client/src/shared/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./request.enums";
export * from "./hotels.enums";
export * from "./restriction.enums";
5 changes: 5 additions & 0 deletions app/client/src/shared/enums/restriction.enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum RestrictionCode {
GENERIC = "0x1000",
HATE_SPEECH = "0x6001",
RACISM = "0x6002",
}
6 changes: 6 additions & 0 deletions app/client/src/shared/types/user.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { RestrictionCode } from "shared/enums";

export type User = {
createdAt: number;
accountId: string;
Expand All @@ -9,4 +11,8 @@ export type User = {
otp?: boolean;
verified?: boolean;
githubLogin?: string;

// [from, kind]
restrictions?: [number, RestrictionCode];
blocked?: boolean;
};
30 changes: 11 additions & 19 deletions app/server/__tests__/tests/A-admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ describe("10. admin", () => {
assertEquals(user1.otp, false);
assertEquals(user1.username, USER_2.username);
assertEquals(user1.verified, true);
assertEquals(user1.restrictions, undefined);
assertEquals(user1.blocked, undefined);
assertExists(user1.createdAt);
assertExists(user1.updatedAt);

Expand All @@ -114,6 +116,8 @@ describe("10. admin", () => {
assertEquals(user2.otp, false);
assertEquals(user2.username, USER_1.username);
assertEquals(user2.verified, true);
assertEquals(user1.restrictions, undefined);
assertEquals(user2.blocked, undefined);
assertExists(user2.createdAt);
assertExists(user2.updatedAt);
});
Expand Down Expand Up @@ -152,29 +156,17 @@ describe("10. admin", () => {
assertEquals(user.verified, true);
assertEquals(user.createdAt, new Date(1994, 3, 19).getTime());
assertExists(user.updatedAt);
});
it("delete user", async () => {
const { accountId } = STATE.getUser(USER_2.email);
const { status } = await fetcher(`/admin/user`, {
method: "DELETE",

const { status: status2 } = await fetcher(`/admin/user`, {
method: "PATCH",
headers: STATE.getSessionHeaders(USER_1.email),
body: JSON.stringify({
accountId,
username: "test",
email: USER_2.email,
createdAt: new Date(1994, 3, 19).getTime(),
}),
});
assertEquals(status, 200);

const response = await fetcher(`/admin/users`, {
method: "GET",
headers: STATE.getSessionHeaders(USER_1.email),
});
assertEquals(response.status, 200);
assertEquals(response.data.users.length, 1);

const user = response.data.users.find(
(user: any) => user.accountId === accountId,
);

assertEquals(user, undefined);
assertEquals(status2, 200);
});
});
111 changes: 111 additions & 0 deletions app/server/__tests__/tests/C-login-blocked-account.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { describe, it } from "jsr:@std/testing/bdd";
import { assertEquals, assertExists } from "jsr:@std/assert";
import { STATE } from "../state.ts";
import { USER_1, USER_2 } from "../consts.ts";
import { fetcher } from "../utils.ts";

describe("12. login with a blocked account", () => {
it("login with a non blocked account", async () => {
const { status, data } = await fetcher("/account/login", {
method: "POST",
headers: STATE.getSessionHeaders(USER_2.email),
body: JSON.stringify({
email: USER_2.email,
password: USER_2.password,
}),
});
assertEquals(status, 200);
assertExists(data.accountId);
assertExists(data.durations);
assertExists(data.refreshToken);
assertExists(data.token);
});
it("blocks user", async () => {
const { accountId } = STATE.getUser(USER_2.email);
const { status } = await fetcher(`/admin/user`, {
method: "PATCH",
headers: STATE.getSessionHeaders(USER_1.email),
body: JSON.stringify({
accountId,
username: "test",
email: "test@test.com",
createdAt: new Date(1994, 3, 19).getTime(),
blocked: true,
}),
});
assertEquals(status, 200);

const response = await fetcher(`/admin/users`, {
method: "GET",
headers: STATE.getSessionHeaders(USER_1.email),
});
assertEquals(response.status, 200);
assertEquals(response.data.users.length, 2);

const user = response.data.users.find(
(user: any) => user.accountId === accountId,
);

assertEquals(Object.keys(user).length, 10);
assertEquals(user.accountId, STATE.getUser(USER_2.email).accountId);
assertEquals(user.blocked, true);
assertExists(user.updatedAt);
});
it("try to login with a blocked account", async () => {
const { status } = await fetcher("/account/login", {
method: "POST",
headers: STATE.getSessionHeaders(USER_2.email),
body: JSON.stringify({
email: USER_2.email,
password: USER_2.password,
}),
});
assertEquals(status, 400);
});
it("unblock user", async () => {
const { accountId } = STATE.getUser(USER_2.email);
const { status } = await fetcher(`/admin/user`, {
method: "PATCH",
headers: STATE.getSessionHeaders(USER_1.email),
body: JSON.stringify({
accountId,
username: "test",
email: USER_2.email,
createdAt: new Date(1994, 3, 19).getTime(),
blocked: false,
}),
});
assertEquals(status, 200);

const response = await fetcher(`/admin/users`, {
method: "GET",
headers: STATE.getSessionHeaders(USER_1.email),
});
assertEquals(response.status, 200);
assertEquals(response.data.users.length, 2);

const user = response.data.users.find(
(user: any) => user.accountId === accountId,
);

assertEquals(Object.keys(user).length, 10);
assertEquals(user.accountId, STATE.getUser(USER_2.email).accountId);
assertEquals(user.blocked, false);
assertExists(user.updatedAt);
});
it("login with a non blocked account", async () => {
const { status, data } = await fetcher("/account/login", {
method: "POST",
headers: STATE.getSessionHeaders(USER_2.email),
body: JSON.stringify({
email: USER_2.email,
password: USER_2.password,
}),
});
assertEquals(status, 200);
assertExists(data.accountId);
assertExists(data.durations);
assertExists(data.refreshToken);
assertExists(data.token);
});
});
Loading