refactor: remove dead user validation code and simplify mock

Removed the now-empty userValidation.js and its test file since the
validation function was reduced to a no-op. Cleaned up the callers in
UserCreate and UserEdit that referenced it. Simplified the mock
GetUserLibraries fallback to delegate to GetAll instead of duplicating
its logic, and removed redundant comments.
This commit is contained in:
Deluan 2026-03-29 11:48:05 -04:00
parent 08b269071f
commit ef3dddd164
5 changed files with 2 additions and 76 deletions

View File

@ -267,7 +267,6 @@ func (m *MockLibraryRepo) GetUserLibraries(ctx context.Context, userID string) (
if userID == "non-existent" { if userID == "non-existent" {
return nil, model.ErrNotFound return nil, model.ErrNotFound
} }
// If per-user tracking is set, return only the assigned libraries
if m.UserLibraries != nil { if m.UserLibraries != nil {
ids, ok := m.UserLibraries[userID] ids, ok := m.UserLibraries[userID]
if !ok { if !ok {
@ -284,15 +283,7 @@ func (m *MockLibraryRepo) GetUserLibraries(ctx context.Context, userID string) (
}) })
return libraries, nil return libraries, nil
} }
// Fallback: return all libraries return m.GetAll()
var libraries model.Libraries
for _, lib := range m.Data {
libraries = append(libraries, lib)
}
slices.SortFunc(libraries, func(a, b model.Library) int {
return a.ID - b.ID
})
return libraries, nil
} }
func (m *MockLibraryRepo) SetUserLibraries(ctx context.Context, userID string, libraryIDs []int) error { func (m *MockLibraryRepo) SetUserLibraries(ctx context.Context, userID string, libraryIDs []int) error {
@ -305,13 +296,11 @@ func (m *MockLibraryRepo) SetUserLibraries(ctx context.Context, userID string, l
if userID == "admin-1" { if userID == "admin-1" {
return fmt.Errorf("%w: cannot manually assign libraries to admin users", model.ErrValidation) return fmt.Errorf("%w: cannot manually assign libraries to admin users", model.ErrValidation)
} }
// Validate all library IDs exist
for _, id := range libraryIDs { for _, id := range libraryIDs {
if _, exists := m.Data[id]; !exists { if _, exists := m.Data[id]; !exists {
return fmt.Errorf("%w: library ID %d does not exist", model.ErrValidation, id) return fmt.Errorf("%w: library ID %d does not exist", model.ErrValidation, id)
} }
} }
// Store per-user assignments
if m.UserLibraries == nil { if m.UserLibraries == nil {
m.UserLibraries = make(map[string][]int) m.UserLibraries = make(map[string][]int)
} }

View File

@ -51,17 +51,9 @@ const UserCreate = (props) => {
[mutate, notify, redirect], [mutate, notify, redirect],
) )
// Custom validation function
const validateUserForm = (values) => {
const errors = {}
// Library selection is optional for non-admin users since they will be auto-assigned to default libraries
// No validation required for library selection
return errors
}
return ( return (
<Create title={<Title subTitle={title} />} {...props}> <Create title={<Title subTitle={title} />} {...props}>
<SimpleForm save={save} validate={validateUserForm} variant={'outlined'}> <SimpleForm save={save} variant={'outlined'}>
<TextInput <TextInput
spellCheck={false} spellCheck={false}
source="userName" source="userName"

View File

@ -24,7 +24,6 @@ import { Typography } from '@material-ui/core'
import { Title } from '../common' import { Title } from '../common'
import DeleteUserButton from './DeleteUserButton' import DeleteUserButton from './DeleteUserButton'
import { LibrarySelectionField } from './LibrarySelectionField.jsx' import { LibrarySelectionField } from './LibrarySelectionField.jsx'
import { validateUserForm } from './userValidation'
const useStyles = makeStyles({ const useStyles = makeStyles({
toolbar: { toolbar: {
@ -104,18 +103,12 @@ const UserEdit = (props) => {
[mutate, notify, permissions, redirect, refresh], [mutate, notify, permissions, redirect, refresh],
) )
// Custom validation function
const validateForm = (values) => {
return validateUserForm(values, translate)
}
return ( return (
<Edit title={<UserTitle />} undoable={false} {...props}> <Edit title={<UserTitle />} undoable={false} {...props}>
<SimpleForm <SimpleForm
variant={'outlined'} variant={'outlined'}
toolbar={<UserToolbar showDelete={canDelete} />} toolbar={<UserToolbar showDelete={canDelete} />}
save={save} save={save}
validate={validateForm}
> >
{permissions === 'admin' && ( {permissions === 'admin' && (
<TextInput <TextInput

View File

@ -1,5 +0,0 @@
// User form validation utilities
export const validateUserForm = (_values, _translate) => {
const errors = {}
return errors
}

View File

@ -1,43 +0,0 @@
import { describe, it, expect, vi } from 'vitest'
import { validateUserForm } from './userValidation'
describe('User Validation Utilities', () => {
const mockTranslate = vi.fn((key) => key)
describe('validateUserForm', () => {
it('should not return errors for admin users', () => {
const values = {
isAdmin: true,
libraryIds: [],
}
const errors = validateUserForm(values, mockTranslate)
expect(errors).toEqual({})
})
it('should not return errors for non-admin users with libraries', () => {
const values = {
isAdmin: false,
libraryIds: [1, 2, 3],
}
const errors = validateUserForm(values, mockTranslate)
expect(errors).toEqual({})
})
it('should not return errors for non-admin users without libraries', () => {
const values = {
isAdmin: false,
libraryIds: [],
}
const errors = validateUserForm(values, mockTranslate)
expect(errors).toEqual({})
})
it('should not return errors for non-admin users with undefined libraryIds', () => {
const values = {
isAdmin: false,
}
const errors = validateUserForm(values, mockTranslate)
expect(errors).toEqual({})
})
})
})