mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-18 00:46:25 +00:00
Support default permissions for object creation via frontend (#4233)
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import { SettingsService } from './settings.service'
|
||||
import {
|
||||
HttpClientTestingModule,
|
||||
HttpTestingController,
|
||||
HttpClientTestingModule,
|
||||
} from '@angular/common/http/testing'
|
||||
import { RouterTestingModule } from '@angular/router/testing'
|
||||
import { environment } from 'src/environments/environment'
|
||||
import { Subscription } from 'rxjs'
|
||||
import { PaperlessUiSettings } from '../data/paperless-uisettings'
|
||||
import { SETTINGS_KEYS } from '../data/paperless-uisettings'
|
||||
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
|
||||
import { RouterTestingModule } from '@angular/router/testing'
|
||||
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'
|
||||
import { CookieService } from 'ngx-cookie-service'
|
||||
import { Subscription } from 'rxjs'
|
||||
import { environment } from 'src/environments/environment'
|
||||
import { AppModule } from '../app.module'
|
||||
import {
|
||||
PaperlessUiSettings,
|
||||
SETTINGS_KEYS,
|
||||
} from '../data/paperless-uisettings'
|
||||
import { SettingsService } from './settings.service'
|
||||
|
||||
describe('SettingsService', () => {
|
||||
let httpTestingController: HttpTestingController
|
||||
let settingsService: SettingsService
|
||||
let cookieService: CookieService
|
||||
let subscription: Subscription
|
||||
|
||||
const ui_settings: PaperlessUiSettings = {
|
||||
@@ -46,6 +50,13 @@ describe('SettingsService', () => {
|
||||
saved_views: { warn_on_unsaved_change: true },
|
||||
notes_enabled: true,
|
||||
tour_complete: false,
|
||||
permissions: {
|
||||
default_owner: null,
|
||||
default_view_users: [1],
|
||||
default_view_groups: [2],
|
||||
default_edit_users: [3],
|
||||
default_edit_groups: [4],
|
||||
},
|
||||
},
|
||||
permissions: [],
|
||||
}
|
||||
@@ -53,7 +64,7 @@ describe('SettingsService', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [],
|
||||
providers: [SettingsService],
|
||||
providers: [SettingsService, CookieService],
|
||||
imports: [
|
||||
HttpClientTestingModule,
|
||||
RouterTestingModule,
|
||||
@@ -65,6 +76,7 @@ describe('SettingsService', () => {
|
||||
})
|
||||
|
||||
httpTestingController = TestBed.inject(HttpTestingController)
|
||||
cookieService = TestBed.inject(CookieService)
|
||||
settingsService = TestBed.inject(SettingsService)
|
||||
})
|
||||
|
||||
@@ -136,7 +148,52 @@ describe('SettingsService', () => {
|
||||
expect(settingsService.get(SETTINGS_KEYS.THEME_COLOR)).toEqual('#000000')
|
||||
})
|
||||
|
||||
it('updates appearnce settings', () => {
|
||||
it('sets django cookie for languages', () => {
|
||||
httpTestingController
|
||||
.expectOne(`${environment.apiBaseUrl}ui_settings/`)
|
||||
.flush(ui_settings)
|
||||
const cookieSetSpy = jest.spyOn(cookieService, 'set')
|
||||
settingsService.initializeSettings().subscribe(() => {})
|
||||
const req = httpTestingController.expectOne(
|
||||
`${environment.apiBaseUrl}ui_settings/`
|
||||
)
|
||||
ui_settings.settings['language'] = 'foobar'
|
||||
req.flush(ui_settings)
|
||||
expect(cookieSetSpy).toHaveBeenCalledWith('django_language', 'foobar')
|
||||
const cookieDeleteSpy = jest.spyOn(cookieService, 'delete')
|
||||
settingsService.setLanguage('')
|
||||
expect(cookieDeleteSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should support null values for settings if set, undefined if not', () => {
|
||||
httpTestingController
|
||||
.expectOne(`${environment.apiBaseUrl}ui_settings/`)
|
||||
.flush(ui_settings)
|
||||
expect(settingsService.get('foo')).toEqual(undefined)
|
||||
expect(settingsService.get(SETTINGS_KEYS.DEFAULT_PERMS_OWNER)).toEqual(null)
|
||||
})
|
||||
|
||||
it('should support array values', () => {
|
||||
httpTestingController
|
||||
.expectOne(`${environment.apiBaseUrl}ui_settings/`)
|
||||
.flush(ui_settings)
|
||||
expect(settingsService.get(SETTINGS_KEYS.DEFAULT_PERMS_VIEW_USERS)).toEqual(
|
||||
[1]
|
||||
)
|
||||
})
|
||||
|
||||
it('should support default permissions values', () => {
|
||||
delete ui_settings.settings['permissions']
|
||||
httpTestingController
|
||||
.expectOne(`${environment.apiBaseUrl}ui_settings/`)
|
||||
.flush(ui_settings)
|
||||
expect(settingsService.get(SETTINGS_KEYS.DEFAULT_PERMS_OWNER)).toEqual(1)
|
||||
expect(settingsService.get(SETTINGS_KEYS.DEFAULT_PERMS_VIEW_USERS)).toEqual(
|
||||
[]
|
||||
)
|
||||
})
|
||||
|
||||
it('updates appearance settings', () => {
|
||||
const req = httpTestingController.expectOne(
|
||||
`${environment.apiBaseUrl}ui_settings/`
|
||||
)
|
||||
|
@@ -372,7 +372,7 @@ export class SettingsService {
|
||||
}
|
||||
|
||||
private getSettingRawValue(key: string): any {
|
||||
let value = null
|
||||
let value = undefined
|
||||
// parse key:key:key into nested object
|
||||
const keys = key.replace('general-settings:', '').split(':')
|
||||
let settingObj = this.settings
|
||||
@@ -389,12 +389,20 @@ export class SettingsService {
|
||||
let setting = SETTINGS.find((s) => s.key == key)
|
||||
|
||||
if (!setting) {
|
||||
return null
|
||||
return undefined
|
||||
}
|
||||
|
||||
let value = this.getSettingRawValue(key)
|
||||
|
||||
if (value != null) {
|
||||
// special case to fallback
|
||||
if (key === SETTINGS_KEYS.DEFAULT_PERMS_OWNER && value === undefined) {
|
||||
return this.currentUser.id
|
||||
}
|
||||
|
||||
if (value !== undefined) {
|
||||
if (value === null) {
|
||||
return null
|
||||
}
|
||||
switch (setting.type) {
|
||||
case 'boolean':
|
||||
return JSON.parse(value)
|
||||
@@ -424,7 +432,7 @@ export class SettingsService {
|
||||
|
||||
private settingIsSet(key: string): boolean {
|
||||
let value = this.getSettingRawValue(key)
|
||||
return value != null
|
||||
return value != undefined
|
||||
}
|
||||
|
||||
storeSettings(): Observable<any> {
|
||||
|
Reference in New Issue
Block a user