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

[READY] - 33 Atribute pages permision #56

Merged
merged 61 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
d43d4a0
[feat:33] add checkPermission on getUser
Diogo-Barboza Aug 30, 2024
86d219a
[Feat 33] implementing pagelock based on roles modules and actions
Gam13 Aug 31, 2024
92e4848
[Feat 33] implementing pagelock based on roles modules and actions
Gam13 Aug 31, 2024
644fba6
[Feat 33] setting route range for each page
Gam13 Aug 31, 2024
56e0f59
[Test 33] unauthorized page
Gam13 Sep 1, 2024
4d98fbb
Merge branch 'devel' into 33-attribute
Gam13 Sep 1, 2024
91de9ba
[test 33] unauthorized page test
Gam13 Sep 1, 2024
ed460e7
[fix 33] fixing lint and format
Gam13 Sep 1, 2024
56ba7dd
Merge remote-tracking branch 'origin/devel' into 33-attribute
Gam13 Sep 2, 2024
c6d17ac
Merge remote-tracking branch 'origin/devel' into 33-attribute
Gam13 Sep 2, 2024
e9acb0b
[fix 33] making create user don't appear for those who don't have per…
Gam13 Sep 2, 2024
aa05ff8
[fix 33] making some permissions updates
Gam13 Sep 2, 2024
9c130dd
[fix 33] making create role don't appear for those who don't have per…
Gam13 Sep 2, 2024
d82acf5
[fix 33] making update and delete button don't appear for those who …
Gam13 Sep 2, 2024
70acd6e
[format] fixing format
Gam13 Sep 2, 2024
6a2efbb
[fix 33] making update and delete button don't appear for those who d…
Gam13 Sep 2, 2024
24c7c19
[Feat 33] only showing on userHub what the user can acess
Gam13 Sep 2, 2024
2e55dd1
[Fix 33] refactoring for easer use of the permission module
Gam13 Sep 2, 2024
eb33010
[Fix 33] setting new use for permission module on role page
Gam13 Sep 2, 2024
7b6c477
format
Gam13 Sep 2, 2024
632bdf0
[fix:33] fixing params on services
Diogo-Barboza Sep 2, 2024
b9e3849
[Test 33] testing new features on permission module
Gam13 Sep 2, 2024
a7a0cf1
[fix:33] fix test on role update and create
Diogo-Barboza Sep 2, 2024
b230cc1
Merge branch '33-attribute' of https://github.com/fga-eps-mds/2024.1-…
Diogo-Barboza Sep 2, 2024
2665aff
[fix:33] ajust css on view user page
Diogo-Barboza Sep 3, 2024
a2f0ed0
[feat 33] implementing permissions on organ sector
Gam13 Sep 3, 2024
f5ad00f
[feat 33] implementing permissions on benefits sector
Gam13 Sep 3, 2024
b67b2bc
format
Gam13 Sep 3, 2024
8c7d39a
[feat 33] finish implementing permissions on benefits sector
Gam13 Sep 3, 2024
75554e1
lint
Gam13 Sep 3, 2024
5e0e13e
[wip 33] most of financial flow aranged
Gam13 Sep 3, 2024
0e59ddb
[test:33] Fix test Financial and Supllier update page
Diogo-Barboza Sep 3, 2024
8500955
merge - resolving conflicts
Diogo-Barboza Sep 3, 2024
8398730
[test:33] add test to organ service
Diogo-Barboza Sep 3, 2024
c54212b
[test:33] testing catch error
Diogo-Barboza Sep 3, 2024
efcd8fb
[test:33] add test to benefits service
Diogo-Barboza Sep 3, 2024
3aa77f2
[test:33] increasing coverage on benefits page
Diogo-Barboza Sep 4, 2024
b44befa
[test:33] add test to catch error on Benefits Service
Diogo-Barboza Sep 4, 2024
0e3dca6
[test:33] add beforaAll on Benefits service
Diogo-Barboza Sep 4, 2024
e27ce6b
[test:33] add test catch erros Benefits Service
Diogo-Barboza Sep 4, 2024
0890c42
[test:33] add test erros on RoleService test
Diogo-Barboza Sep 4, 2024
a267294
[test:33] add test to userService
Diogo-Barboza Sep 4, 2024
3d7ea3f
[test:33] add test to uncovered functions on user Service
Diogo-Barboza Sep 4, 2024
c58d6e8
[test:33] add test button cadastrar on benefits list
Diogo-Barboza Sep 4, 2024
ee8f52d
[feat 33] fixing sidebar based on roles
Gam13 Sep 5, 2024
ced1a29
Merge branch 'devel' of https://github.com/fga-eps-mds/2024.1-SENTINE…
Diogo-Barboza Sep 6, 2024
037df8e
[fix 33] quick fix route
Gam13 Sep 6, 2024
143b6b4
[fix:33] fix update and create profile
Diogo-Barboza Sep 6, 2024
8980549
[fix:33] fixing update profile page
Diogo-Barboza Sep 6, 2024
204e141
[feat:33] diferent profile update page based on role
Diogo-Barboza Sep 6, 2024
3a42aa6
Merge remote-tracking branch 'origin/devel' into 33-attribute
Gam13 Sep 6, 2024
02c2504
[feat 33] fluxos funcionando e permissions sendo pegas corretamente
Gam13 Sep 6, 2024
28805d5
[fix 33] checkAction fixed
Gam13 Sep 7, 2024
a68b889
[test 33] fixing permissions test
Gam13 Sep 7, 2024
adacbb8
[fix 33] button display
Gam13 Sep 7, 2024
61a2ef0
[fix 33] finance create button display
Gam13 Sep 7, 2024
1947a1f
[fix 33] routes fixed to receive more then one action
Gam13 Sep 7, 2024
169662b
[fix 33] page acess
Gam13 Sep 7, 2024
d207307
[fix 33] fix routes permission
Gam13 Sep 7, 2024
3ee40de
[test 33] fixing permissions test
Gam13 Sep 7, 2024
b901403
[test 33] tests from updateSupplier after flow rearange
Gam13 Sep 8, 2024
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
11 changes: 0 additions & 11 deletions 2024.1-SENTINELA-FRONT.code-workspace

This file was deleted.

34 changes: 24 additions & 10 deletions src/Components/SideBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,42 @@ import { AiOutlineMenu } from "react-icons/ai";
import sindpol_logo from "../../assets/sindpol-logo.png";
import sentinela_logo from "../../assets/sentinela-logo.png";
import { ButtonGroup } from "@mui/material";
import { useContext, useState } from "react";
import { useEffect, useContext, useState } from "react";
import SideButton from "../SideButton";
import { AiOutlineUser } from "react-icons/ai";
import { RiLogoutCircleRLine } from "react-icons/ri";
import { useNavigate } from "react-router-dom";
import AuthContext, { useAuth } from "../../Context/auth";
import { usePermissions, checkModule } from "../../Utils/permission";
import { getRoleById } from "../../Services/RoleService/roleService";

export default function SideBar({ fullHeight = true }) {
const [isSideBarOpen, setIsSideBarOpen] = useState(false);
const handleSideBar = () => setIsSideBarOpen(!isSideBarOpen);
const navigate = useNavigate();
const context = useContext(AuthContext);
const { user } = useAuth();
const permissions = usePermissions();
const [role, setRole] = useState("");

const handleItemClick = (user) => {
if (user?.role?.name == "sindicalizado") {
navigate(`/filiados/${user.name}`, {
state: { membershipId: user._id },
});
} else {
useEffect(() => {}, [navigate]);

const handleItemClick = async (user) => {
if (user) {
try {
const result = await getRoleById(user.role);
setRole(result.name);
} catch (error) {
console.error(error);
}
}

if (role == "administrador") {
navigate(`/usuarios/editar/${user.name}`, {
state: { userId: user._id },
});
} else {
navigate("/perfil");
}
};

Expand All @@ -40,23 +53,23 @@ export default function SideBar({ fullHeight = true }) {
}}
/>,
<SideButton
hidden={user ? "flex" : "none"}
hidden={checkModule(permissions, "users") ? "flex" : "none"}
key="filiacao"
text="CADASTROS"
onClick={() => {
navigate("/usuarios/hub");
}}
/>,
<SideButton
hidden={user ? "flex" : "none"}
hidden={checkModule(permissions, "finance") ? "flex" : "none"}
key="financeiro"
text="FINANCEIRO"
onClick={() => {
navigate("/finance/hub");
}}
/>,
<SideButton
hidden={user ? "flex" : "none"}
hidden={checkModule(permissions, "benefits") ? "flex" : "none"}
key="beneficios"
text="BENEFÍCIOS"
onClick={() => {
Expand Down Expand Up @@ -144,6 +157,7 @@ export default function SideBar({ fullHeight = true }) {
onClick={() => {
context.Logout();
navigate("/");
window.location.reload();
}}
>
LOGOUT <RiLogoutCircleRLine className="logout-icon" />
Expand Down
6 changes: 4 additions & 2 deletions src/Context/auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ const AuthContext = createContext({});

export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);

const [flag, setFlag] = useState(true);
useEffect(() => {
const storagedUser = localStorage.getItem("@App:user");
const storagedToken = localStorage.getItem("@App:token");

if (storagedToken && storagedUser) {
setUser(JSON.parse(storagedUser));
}
}, []);
}, [flag]);

const Login = async (email, password) => {
try {
const response = await userLogin(email, password);
setUser(response.data);
localStorage.setItem("@App:user", JSON.stringify(response.data.user));
localStorage.setItem("@App:token", JSON.stringify(response.data.token));
setFlag(!flag);
return false;
} catch (err) {
return true;
Expand All @@ -33,6 +34,7 @@ export const AuthProvider = ({ children }) => {

localStorage.removeItem("@App:user");
localStorage.removeItem("App:token");
setFlag(!flag);
};

// ### Auth return
Expand Down
13 changes: 9 additions & 4 deletions src/Pages/Protected/Benefit/Benefits/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import SecondaryButton from "../../../../Components/SecondaryButton";
import sindpol_logo from "../../../../assets/sindpol-logo.png";
import sentinela_logo from "../../../../assets/sentinela-logo.png";
import "./index.css";
import { checkAction, usePermissions } from "../../../../Utils/permission";

const Benefits = () => {
const navigate = useNavigate();
const { user } = useAuth();
const permissions = usePermissions();
const canCreate = checkAction(permissions, "benefits", "create");

const handleBenefitsList = () => {
navigate("/beneficios/lista");
Expand All @@ -29,10 +32,12 @@ const Benefits = () => {
alt="Sentinela Logo"
/>
<div className="hub-btn">
<SecondaryButton
text="CADASTRO DE BENEFÍCIOS"
onClick={handleBenefitsCreate}
/>
{canCreate && (
<SecondaryButton
text="CADASTRO DE BENEFÍCIOS"
onClick={handleBenefitsCreate}
/>
)}
<SecondaryButton
text="LISTA DE BENEFÍCIOS"
onClick={handleBenefitsList}
Expand Down
54 changes: 53 additions & 1 deletion src/Pages/Protected/Benefit/Benefits/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import { BrowserRouter as Router } from "react-router-dom";
import Benefits from "./index";
import "@testing-library/jest-dom";

let canCreatePermission = true;

vi.mock("../../../../Utils/permission", () => ({
usePermissions: () => ({
somePermission: true,
}),
checkAction: () => canCreatePermission,
}));

vi.mock("../../../../Context/auth", () => ({
useAuth: () => ({
user: { name: "Test User" }, // Mocking a user
}),
}));

describe("Benefits", () => {
it("renders buttons correctly", () => {
render(
Expand Down Expand Up @@ -49,4 +64,41 @@ describe("Benefits", () => {
expect(window.location.pathname).toBe("/beneficios/lista");
});
});

it("does not render the 'CADASTRO DE BENEFÍCIOS' button when canCreate is false", async () => {
// Ajustar a variável global para simular ausência de permissão de criação
canCreatePermission = false;

render(
<Router>
<Benefits />
</Router>
);

await waitFor(() => {
// Verificar que o botão CADASTRO DE BENEFÍCIOS não está presente
expect(
screen.queryByText("CADASTRO DE BENEFÍCIOS")
).not.toBeInTheDocument();

// O botão LISTA DE BENEFÍCIOS ainda deve estar presente
expect(screen.getByText("LISTA DE BENEFÍCIOS")).toBeInTheDocument();
});
});

it("renders buttons correctly when user has create permission", async () => {
// Ajustar a variável global para simular permissão de criação
canCreatePermission = true;

render(
<Router>
<Benefits />
</Router>
);

await waitFor(() => {
// Verificar se o botão de CADASTRO DE BENEFÍCIOS está presente
expect(screen.getByText("CADASTRO DE BENEFÍCIOS")).toBeInTheDocument();
});
});
});
7 changes: 6 additions & 1 deletion src/Pages/Protected/Benefit/BenefitsList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import ListItemText from "@mui/material/ListItemText";
import Divider from "@mui/material/Divider";
import "./index.css";
import { getBenefitsForm } from "../../../../Services/benefitsService";
import { checkAction, usePermissions } from "../../../../Utils/permission";

export default function BenefitsList() {
const [search, setSearch] = useState("");
const [benefits, setBenefits] = useState([]);
const navigate = useNavigate();
const permissions = usePermissions();
const canCreate = checkAction(permissions, "benefits", "create");

const handleSubmit = () => {
navigate("/beneficios/criar");
Expand Down Expand Up @@ -43,7 +46,9 @@ export default function BenefitsList() {
<div className="forms-container-list-benefits">
<div className="double-box-list-benefits">
<h1> Lista de benefícios</h1>
<PrimaryButton text="Cadastrar benefício" onClick={handleSubmit} />
{canCreate && (
<PrimaryButton text="Cadastrar benefício" onClick={handleSubmit} />
)}
</div>
<div className="search-box-benefits">
<FieldText
Expand Down
6 changes: 6 additions & 0 deletions src/Pages/Protected/Benefit/BenefitsList/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { getBenefitsForm } from "../../../../Services/benefitsService";
import "@testing-library/jest-dom";

vi.mock("../../../../Services/benefitsService");
vi.mock("../../../../Utils/permission", () => ({
usePermissions: () => ({
somePermission: true,
}),
checkAction: () => true,
}));

describe("BenefitsList", () => {
beforeEach(() => {
Expand Down
20 changes: 14 additions & 6 deletions src/Pages/Protected/Benefit/BenefitsUpdate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "../../../../Utils/validators";
import { Snackbar } from "@mui/material";
import Alert from "@mui/material/Alert";
import { checkAction, usePermissions } from "../../../../Utils/permission";

export default function BenefitsUpdate() {
const navigate = useNavigate();
Expand Down Expand Up @@ -53,6 +54,9 @@ export default function BenefitsUpdate() {
const [showDeleteModal, setShowDeleteModal] = useState(false);
const [showDeletedModal, setShowDeletedModal] = useState(false);
const [openError, setOpenError] = useState(false);
const permissions = usePermissions();
const canUpdate = checkAction(permissions, "benefits", "update");
const canDelete = checkAction(permissions, "benefits", "delete");

const tipoPessoaList = ["Jurídica", "Física"];
const categoriaList = [
Expand Down Expand Up @@ -392,12 +396,16 @@ export default function BenefitsUpdate() {
/>

<div className="double-buttons">
<SecondaryButton text="Deletar" onClick={handleDeleteModal} />

<PrimaryButton
text="Salvar"
onClick={() => handleUpdateBenefitsButton()}
/>
{canDelete && (
<SecondaryButton text="Deletar" onClick={handleDeleteModal} />
)}

{canUpdate && (
<PrimaryButton
text="Salvar"
onClick={() => handleUpdateBenefitsButton()}
/>
)}
</div>

<Snackbar
Expand Down
7 changes: 7 additions & 0 deletions src/Pages/Protected/Benefit/BenefitsUpdate/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import {
updateBenefitsFormById,
} from "../../../../Services/benefitsService";

vi.mock("../../../../Utils/permission", () => ({
usePermissions: () => ({
somePermission: true,
}),
checkAction: () => true,
}));

function mockValidators() {
// mocka funções de validação do service
vi.mock("../../../../Utils/validators", () => {
Expand Down
13 changes: 9 additions & 4 deletions src/Pages/Protected/Finance/BankAccountList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import ListItemButton from "@mui/material/ListItemButton";
import ListItemText from "@mui/material/ListItemText";
import FieldText from "../../../../Components/FieldText";
import { getAll } from "../../../../../src/Services/bankAccountService";
import { checkAction, usePermissions } from "../../../../Utils/permission";

export default function ListBankAccount() {
const [busca, setBusca] = useState("");
const navigate = useNavigate();
const { user } = useAuth();
const [bankAccounts, setBankAccounts] = useState([]);
const permissions = usePermissions();
const canCreate = checkAction(permissions, "finance", "create");

useEffect(() => {
const fetchBankAccounts = async () => {
Expand Down Expand Up @@ -48,10 +51,12 @@ export default function ListBankAccount() {
<div className="forms-container-list-bankAccount">
<div className="double-box-list-bankAccount">
<h1>Lista de Contas Bancárias</h1>
<PrimaryButtom
text="Cadastrar contas bancárias"
onClick={handleRegisterClick}
/>
{canCreate && (
<PrimaryButtom
text="Cadastrar contas bancárias"
onClick={handleRegisterClick}
/>
)}
</div>
<div className="search-box-bankAccount">
<FieldText
Expand Down
8 changes: 7 additions & 1 deletion src/Pages/Protected/Finance/BankAccountList/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { MemoryRouter, Route, Routes } from "react-router-dom";
import ListBankAccount from "./index";
import { getAll } from "../../../../../src/Services/bankAccountService";
import "@testing-library/jest-dom";

// Mockar o serviço getAll
vi.mock("../../../../../src/Services/bankAccountService", () => ({
getAll: vi.fn(),
}));

vi.mock("../../../../Utils/permission", () => ({
usePermissions: () => ({
somePermission: true,
}),
checkAction: () => true,
}));

// Mockar o contexto de autenticação
vi.mock("../../../../Context/auth", () => ({
useAuth: () => ({
Expand Down
Loading
Loading