mirror of
https://github.com/navidrome/navidrome.git
synced 2026-05-03 06:51:16 +00:00
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:
parent
08b269071f
commit
ef3dddd164
@ -267,7 +267,6 @@ func (m *MockLibraryRepo) GetUserLibraries(ctx context.Context, userID string) (
|
||||
if userID == "non-existent" {
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
// If per-user tracking is set, return only the assigned libraries
|
||||
if m.UserLibraries != nil {
|
||||
ids, ok := m.UserLibraries[userID]
|
||||
if !ok {
|
||||
@ -284,15 +283,7 @@ func (m *MockLibraryRepo) GetUserLibraries(ctx context.Context, userID string) (
|
||||
})
|
||||
return libraries, nil
|
||||
}
|
||||
// Fallback: return all libraries
|
||||
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
|
||||
return m.GetAll()
|
||||
}
|
||||
|
||||
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" {
|
||||
return fmt.Errorf("%w: cannot manually assign libraries to admin users", model.ErrValidation)
|
||||
}
|
||||
// Validate all library IDs exist
|
||||
for _, id := range libraryIDs {
|
||||
if _, exists := m.Data[id]; !exists {
|
||||
return fmt.Errorf("%w: library ID %d does not exist", model.ErrValidation, id)
|
||||
}
|
||||
}
|
||||
// Store per-user assignments
|
||||
if m.UserLibraries == nil {
|
||||
m.UserLibraries = make(map[string][]int)
|
||||
}
|
||||
|
||||
@ -51,17 +51,9 @@ const UserCreate = (props) => {
|
||||
[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 (
|
||||
<Create title={<Title subTitle={title} />} {...props}>
|
||||
<SimpleForm save={save} validate={validateUserForm} variant={'outlined'}>
|
||||
<SimpleForm save={save} variant={'outlined'}>
|
||||
<TextInput
|
||||
spellCheck={false}
|
||||
source="userName"
|
||||
|
||||
@ -24,7 +24,6 @@ import { Typography } from '@material-ui/core'
|
||||
import { Title } from '../common'
|
||||
import DeleteUserButton from './DeleteUserButton'
|
||||
import { LibrarySelectionField } from './LibrarySelectionField.jsx'
|
||||
import { validateUserForm } from './userValidation'
|
||||
|
||||
const useStyles = makeStyles({
|
||||
toolbar: {
|
||||
@ -104,18 +103,12 @@ const UserEdit = (props) => {
|
||||
[mutate, notify, permissions, redirect, refresh],
|
||||
)
|
||||
|
||||
// Custom validation function
|
||||
const validateForm = (values) => {
|
||||
return validateUserForm(values, translate)
|
||||
}
|
||||
|
||||
return (
|
||||
<Edit title={<UserTitle />} undoable={false} {...props}>
|
||||
<SimpleForm
|
||||
variant={'outlined'}
|
||||
toolbar={<UserToolbar showDelete={canDelete} />}
|
||||
save={save}
|
||||
validate={validateForm}
|
||||
>
|
||||
{permissions === 'admin' && (
|
||||
<TextInput
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
// User form validation utilities
|
||||
export const validateUserForm = (_values, _translate) => {
|
||||
const errors = {}
|
||||
return errors
|
||||
}
|
||||
@ -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({})
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user