Skip to content

Commit

Permalink
Move sso auth logic to auth folder
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Aug 6, 2024
1 parent 1bf160e commit 42445db
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 16 deletions.
5 changes: 5 additions & 0 deletions assets/js/lib/auth/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getFromConfig } from '@lib/config';

const OIDC_ENABLED = getFromConfig('oidcEnabled') || false;

export const isSingleSignOnEnabled = () => OIDC_ENABLED;
7 changes: 7 additions & 0 deletions assets/js/lib/auth/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { isSingleSignOnEnabled } from './config';

describe('auth config', () => {
it('should check if single sign on is enabled', () => {
expect(isSingleSignOnEnabled()).toBe(false);
});
});
2 changes: 0 additions & 2 deletions assets/js/lib/model/users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { getFromConfig } from '@lib/config';

const TRENTO_ADMIN_USERNAME = getFromConfig('adminUsername') || 'admin';
const OIDC_ENABLED = getFromConfig('oidcEnabled') || false;

export const isAdmin = (user) => user.username === TRENTO_ADMIN_USERNAME;
export const isSingleSignOnEnabled = () => OIDC_ENABLED;
6 changes: 1 addition & 5 deletions assets/js/lib/model/users.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { adminUser, userFactory } from '@lib/test-utils/factories/users';

import { isAdmin, isSingleSignOnEnabled } from './users';
import { isAdmin } from './users';

describe('users', () => {
it('should check if a user is admin', () => {
Expand All @@ -10,8 +10,4 @@ describe('users', () => {
const user = userFactory.build({ username: 'other' });
expect(isAdmin(user)).toBe(false);
});

it('should check if single sign on is enabled', () => {
expect(isSingleSignOnEnabled()).toBe(false);
});
});
3 changes: 2 additions & 1 deletion assets/js/pages/Profile/ProfilePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { useEffect, useState } from 'react';
import { toast } from 'react-hot-toast';
import { useDispatch } from 'react-redux';
import PageHeader from '@common/PageHeader';
import { isAdmin, isSingleSignOnEnabled } from '@lib/model/users';
import { isAdmin } from '@lib/model/users';
import { isSingleSignOnEnabled } from '@lib/auth/config';
import ProfileForm from '@pages/Profile/ProfileForm';
import {
getUserProfile,
Expand Down
2 changes: 1 addition & 1 deletion assets/js/pages/Users/CreateUserPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import BackButton from '@common/BackButton';
import PageHeader from '@common/PageHeader';
import NotFound from '@pages/NotFound';

import { isSingleSignOnEnabled } from '@lib/model/users';
import { isSingleSignOnEnabled } from '@lib/auth/config';
import { listAbilities } from '@lib/api/abilities';
import { createUser } from '@lib/api/users';

Expand Down
4 changes: 2 additions & 2 deletions assets/js/pages/Users/CreateUserPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { faker } from '@faker-js/faker';
import * as router from 'react-router';

import { networkClient } from '@lib/network';
import * as usersModel from '@lib/model/users';
import * as authConfig from '@lib/auth/config';
import { abilityFactory, userFactory } from '@lib/test-utils/factories/users';

import CreateUserPage from './CreateUserPage';
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('CreateUserPage', () => {

describe('Single sign on', () => {
it('should redirect to not found page', async () => {
jest.spyOn(usersModel, 'isSingleSignOnEnabled').mockReturnValue(true);
jest.spyOn(authConfig, 'isSingleSignOnEnabled').mockReturnValue(true);

render(<CreateUserPage />);

Expand Down
4 changes: 3 additions & 1 deletion assets/js/pages/Users/EditUserPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import BackButton from '@common/BackButton';
import Banner from '@common/Banners/Banner';
import PageHeader from '@common/PageHeader';

import { isAdmin, isSingleSignOnEnabled } from '@lib/model/users';
import { isAdmin } from '@lib/model/users';
import { isSingleSignOnEnabled } from '@lib/auth/config';

import { editUser, getUser } from '@lib/api/users';

import { fetchAbilities } from './CreateUserPage';
Expand Down
2 changes: 1 addition & 1 deletion assets/js/pages/Users/UsersPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom';
import { toast } from 'react-hot-toast';

import { listUsers, deleteUser } from '@lib/api/users';
import { isSingleSignOnEnabled } from '@lib/model/users';
import { isSingleSignOnEnabled } from '@lib/auth/config';

import Users from './Users';

Expand Down
2 changes: 1 addition & 1 deletion assets/js/state/sagas/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
clearCredentialsFromStore,
} from '@lib/auth';
import { networkClient } from '@lib/network';
import { isSingleSignOnEnabled } from '@lib/model/users';
import { isSingleSignOnEnabled } from '@lib/auth/config';

export function* performOIDCEnrollment({ payload: { code, state } }) {
yield put(setAuthInProgress());
Expand Down
4 changes: 2 additions & 2 deletions assets/js/state/sagas/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { customNotify } from '@state/notifications';
import { networkClient } from '@lib/network';
import { profileFactory } from '@lib/test-utils/factories/users';
import * as usersModel from '@lib/model/users';
import * as authConfig from '@lib/auth/config';
import {
performLogin,
clearUserAndLogout,
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('user login saga', () => {

describe('Single sign on', () => {
it('should not dispatch notification if single sign on is enabled', async () => {
jest.spyOn(usersModel, 'isSingleSignOnEnabled').mockReturnValue(true);
jest.spyOn(authConfig, 'isSingleSignOnEnabled').mockReturnValue(true);

const dispatched = await recordSaga(
checkUserPasswordChangeRequested,
Expand Down

0 comments on commit 42445db

Please sign in to comment.