Skip to content

Commit

Permalink
Merge pull request #899 from InseeFr/fix/review-typings
Browse files Browse the repository at this point in the history
feat: remiew types for auth
  • Loading branch information
PierreVasseur authored Aug 26, 2024
2 parents 1fa11a7 + 110a742 commit 4db9545
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AuthDumb, mapStateToProps } from './auth';
import { render } from '@testing-library/react';
import { LOADING } from '../../sdk/constants';
import { ReduxModel } from '../../redux/model';

describe('AuthDumb', () => {
it('should return the fallback if the user is not authorized', () => {
const { container } = render(
<AuthDumb
children={'children'}
fallback={'fallback'}
fallback="fallback"
userRoles={['roles']}
roles={['roles1']}
loadUserStamp={jest.fn()}
Expand All @@ -20,8 +20,7 @@ describe('AuthDumb', () => {
it('should return the children if the user is authorized', () => {
const { container } = render(
<AuthDumb
children={'children'}
fallback={'fallback'}
fallback="fallback"
userRoles={['roles']}
roles={['roles']}
loadUserStamp={jest.fn()}
Expand All @@ -35,11 +34,11 @@ describe('AuthDumb', () => {
it('should return the children if the user is authorized via a complementary check', () => {
const { container } = render(
<AuthDumb
children={'children'}
fallback={'fallback'}
fallback="fallback"
userRoles={['roles']}
roles={[['roles', () => true]]}
loadUserStamp={jest.fn()}
userStamp="stamp"
>
Children
</AuthDumb>
Expand All @@ -50,8 +49,7 @@ describe('AuthDumb', () => {
it('should return the fallback if the user is not authorized via a complementary check', () => {
const { container } = render(
<AuthDumb
children={'children'}
fallback={'fallback'}
fallback="fallback"
userRoles={['roles']}
roles={[['roles', () => false]]}
loadUserStamp={jest.fn()}
Expand All @@ -61,15 +59,31 @@ describe('AuthDumb', () => {
);
expect(container.innerHTML).toEqual('fallback');
});

it('should return the fallback if the user does not have a stamp via a complementary check', () => {
const { container } = render(
<AuthDumb
fallback="fallback"
userRoles={['roles']}
roles={[['roles', () => true]]}
loadUserStamp={jest.fn()}
>
Children
</AuthDumb>
);
expect(container.innerHTML).toEqual('fallback');
});
});

describe('mapStateToProps', () => {
it('should return the user object', () => {
const state = {
app: {
auth: {
type: 'type',
user: {
roles: 'roles',
roles: ['roles'],
stamp: 'stamp',
},
},
},
Expand All @@ -79,9 +93,9 @@ describe('mapStateToProps', () => {
stamp: 'stamp',
},
},
};
} as unknown as ReduxModel;
expect(mapStateToProps(state)).toEqual({
userRoles: 'roles',
userRoles: ['roles'],
userStamp: 'stamp',
isLoading: true,
});
Expand Down
7 changes: 5 additions & 2 deletions src/packages/auth/components/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ export function AuthDumb({
}, [userStamp, isLoading, loadUserStamp]);
const isAuthorized = !!roles.find((role) => {
if (Array.isArray(role)) {
const [r, check] = role;
return userRoles?.includes(r) && check(userStamp!);
if (!!userStamp) {
const [r, check] = role;
return userRoles?.includes(r) && check(userStamp);
}
return false;
}
return userRoles?.includes(role);
});
Expand Down

0 comments on commit 4db9545

Please sign in to comment.