11import { TestBed } from '@angular/core/testing'
2- import { of } from 'rxjs'
2+ import { of , throwError } from 'rxjs'
33import { PermissionNotificationsService } from './permission-notifications.service'
44import { InboxService } from './inbox.service'
55import { InboxNotificationPermission } from '../../types/notifications.endpoint'
6+ import { AccountTrustedOrganizationsService } from '../account-trusted-organizations/account-trusted-organizations.service'
67
78function createPermissionNotification (
89 overrides : Partial < InboxNotificationPermission > = { }
@@ -28,17 +29,24 @@ function createPermissionNotification(
2829describe ( 'PermissionNotificationsService' , ( ) => {
2930 let service : PermissionNotificationsService
3031 let inboxSpy : jasmine . SpyObj < InboxService >
32+ let trustedOrgsSpy : jasmine . SpyObj < AccountTrustedOrganizationsService >
3133
3234 beforeEach ( ( ) => {
3335 inboxSpy = jasmine . createSpyObj < InboxService > ( 'InboxService' , [
3436 'getUnreadCount' ,
3537 'fetchNotificationsIncremental' ,
3638 ] )
39+ trustedOrgsSpy = jasmine . createSpyObj < AccountTrustedOrganizationsService > (
40+ 'AccountTrustedOrganizationsService' ,
41+ [ 'get' ]
42+ )
43+ trustedOrgsSpy . get . and . returnValue ( of ( [ ] ) )
3744
3845 TestBed . configureTestingModule ( {
3946 providers : [
4047 PermissionNotificationsService ,
4148 { provide : InboxService , useValue : inboxSpy } ,
49+ { provide : AccountTrustedOrganizationsService , useValue : trustedOrgsSpy } ,
4250 ] ,
4351 } )
4452 service = TestBed . inject ( PermissionNotificationsService )
@@ -96,6 +104,70 @@ describe('PermissionNotificationsService', () => {
96104 expect ( result [ 0 ] . source . sourceClientId . path ) . toBe ( 'client-a' )
97105 expect ( result [ 1 ] . source . sourceClientId . path ) . toBe ( 'client-b' )
98106 expect ( inboxSpy . fetchNotificationsIncremental ) . toHaveBeenCalledWith ( false )
107+ expect ( trustedOrgsSpy . get ) . toHaveBeenCalled ( )
108+ done ( )
109+ } )
110+ } )
111+
112+ it ( 'should filter out notifications from already trusted clientIds' , ( done ) => {
113+ const perm1 = createPermissionNotification ( {
114+ putCode : 1 ,
115+ sentDate : 2000 ,
116+ source : {
117+ sourceClientId : { path : 'client-a' } as any ,
118+ sourceName : { content : 'Org A' } ,
119+ } ,
120+ } )
121+ const perm2 = createPermissionNotification ( {
122+ putCode : 2 ,
123+ sentDate : 1000 ,
124+ source : {
125+ sourceClientId : { path : 'client-b' } as any ,
126+ sourceName : { content : 'Org B' } ,
127+ } ,
128+ } )
129+
130+ trustedOrgsSpy . get . and . returnValue ( of ( [ { clientId : 'client-a' } as any ] ) )
131+ inboxSpy . getUnreadCount . and . returnValue ( of ( 5 ) )
132+ inboxSpy . fetchNotificationsIncremental . and . returnValue (
133+ of ( { total : 5 , notifications : [ perm1 , perm2 ] , done : true } )
134+ )
135+
136+ service . loadUnreadPermissionNotifications ( 3 ) . subscribe ( ( result ) => {
137+ expect ( result . length ) . toBe ( 1 )
138+ expect ( result [ 0 ] . source . sourceClientId . path ) . toBe ( 'client-b' )
139+ done ( )
140+ } )
141+ } )
142+
143+ it ( 'should not fail if trusted-orgs lookup fails (no filtering applied)' , ( done ) => {
144+ const perm1 = createPermissionNotification ( {
145+ putCode : 1 ,
146+ sentDate : 2000 ,
147+ source : {
148+ sourceClientId : { path : 'client-a' } as any ,
149+ sourceName : { content : 'Org A' } ,
150+ } ,
151+ } )
152+ const perm2 = createPermissionNotification ( {
153+ putCode : 2 ,
154+ sentDate : 1000 ,
155+ source : {
156+ sourceClientId : { path : 'client-b' } as any ,
157+ sourceName : { content : 'Org B' } ,
158+ } ,
159+ } )
160+
161+ trustedOrgsSpy . get . and . returnValue ( throwError ( ( ) => new Error ( 'fail' ) ) )
162+ inboxSpy . getUnreadCount . and . returnValue ( of ( 5 ) )
163+ inboxSpy . fetchNotificationsIncremental . and . returnValue (
164+ of ( { total : 5 , notifications : [ perm1 , perm2 ] , done : true } )
165+ )
166+
167+ service . loadUnreadPermissionNotifications ( 3 ) . subscribe ( ( result ) => {
168+ expect ( result . length ) . toBe ( 2 )
169+ expect ( result [ 0 ] . source . sourceClientId . path ) . toBe ( 'client-a' )
170+ expect ( result [ 1 ] . source . sourceClientId . path ) . toBe ( 'client-b' )
99171 done ( )
100172 } )
101173 } )
0 commit comments