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

feat: remiew types for auth #899

Merged
merged 1 commit into from
Aug 26, 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
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