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,27 @@ 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+ {
50+ provide : AccountTrustedOrganizationsService ,
51+ useValue : trustedOrgsSpy ,
52+ } ,
4253 ] ,
4354 } )
4455 service = TestBed . inject ( PermissionNotificationsService )
@@ -96,6 +107,70 @@ describe('PermissionNotificationsService', () => {
96107 expect ( result [ 0 ] . source . sourceClientId . path ) . toBe ( 'client-a' )
97108 expect ( result [ 1 ] . source . sourceClientId . path ) . toBe ( 'client-b' )
98109 expect ( inboxSpy . fetchNotificationsIncremental ) . toHaveBeenCalledWith ( false )
110+ expect ( trustedOrgsSpy . get ) . toHaveBeenCalled ( )
111+ done ( )
112+ } )
113+ } )
114+
115+ it ( 'should filter out notifications from already trusted clientIds' , ( done ) => {
116+ const perm1 = createPermissionNotification ( {
117+ putCode : 1 ,
118+ sentDate : 2000 ,
119+ source : {
120+ sourceClientId : { path : 'client-a' } as any ,
121+ sourceName : { content : 'Org A' } ,
122+ } ,
123+ } )
124+ const perm2 = createPermissionNotification ( {
125+ putCode : 2 ,
126+ sentDate : 1000 ,
127+ source : {
128+ sourceClientId : { path : 'client-b' } as any ,
129+ sourceName : { content : 'Org B' } ,
130+ } ,
131+ } )
132+
133+ trustedOrgsSpy . get . and . returnValue ( of ( [ { clientId : 'client-a' } as any ] ) )
134+ inboxSpy . getUnreadCount . and . returnValue ( of ( 5 ) )
135+ inboxSpy . fetchNotificationsIncremental . and . returnValue (
136+ of ( { total : 5 , notifications : [ perm1 , perm2 ] , done : true } )
137+ )
138+
139+ service . loadUnreadPermissionNotifications ( 3 ) . subscribe ( ( result ) => {
140+ expect ( result . length ) . toBe ( 1 )
141+ expect ( result [ 0 ] . source . sourceClientId . path ) . toBe ( 'client-b' )
142+ done ( )
143+ } )
144+ } )
145+
146+ it ( 'should not fail if trusted-orgs lookup fails (no filtering applied)' , ( done ) => {
147+ const perm1 = createPermissionNotification ( {
148+ putCode : 1 ,
149+ sentDate : 2000 ,
150+ source : {
151+ sourceClientId : { path : 'client-a' } as any ,
152+ sourceName : { content : 'Org A' } ,
153+ } ,
154+ } )
155+ const perm2 = createPermissionNotification ( {
156+ putCode : 2 ,
157+ sentDate : 1000 ,
158+ source : {
159+ sourceClientId : { path : 'client-b' } as any ,
160+ sourceName : { content : 'Org B' } ,
161+ } ,
162+ } )
163+
164+ trustedOrgsSpy . get . and . returnValue ( throwError ( ( ) => new Error ( 'fail' ) ) )
165+ inboxSpy . getUnreadCount . and . returnValue ( of ( 5 ) )
166+ inboxSpy . fetchNotificationsIncremental . and . returnValue (
167+ of ( { total : 5 , notifications : [ perm1 , perm2 ] , done : true } )
168+ )
169+
170+ service . loadUnreadPermissionNotifications ( 3 ) . subscribe ( ( result ) => {
171+ expect ( result . length ) . toBe ( 2 )
172+ expect ( result [ 0 ] . source . sourceClientId . path ) . toBe ( 'client-a' )
173+ expect ( result [ 1 ] . source . sourceClientId . path ) . toBe ( 'client-b' )
99174 done ( )
100175 } )
101176 } )
0 commit comments