@@ -1834,6 +1834,88 @@ describe('ParseLiveQueryServer', function () {
18341834 expect ( parseLiveQueryServer . authCache . get ( 'invalid' ) ) . not . toBe ( undefined ) ;
18351835 } ) ;
18361836
1837+ describe ( 'role cache invalidation' , ( ) => {
1838+ const clearCacheChannel = ( ) => `${ Parse . applicationId } clearCache` ;
1839+
1840+ // The subscriber is mocked, so the handler registered for the clearCache
1841+ // channel is recovered from the spy rather than by publishing for real.
1842+ const clearCacheHandler = server => {
1843+ const call = server . subscriber . subscribe . calls
1844+ . all ( )
1845+ . find ( ( { args } ) => args [ 0 ] === clearCacheChannel ( ) ) ;
1846+ return call . args [ 1 ] ;
1847+ } ;
1848+
1849+ it ( 'publishes a full clear when a role changes without an acting user' , ( ) => {
1850+ const controller = new LiveQueryController ( { classNames : [ 'Yolo' ] } ) ;
1851+ const publish = controller . liveQueryPublisher . parsePublisher . publish ;
1852+
1853+ controller . clearCachedRoles ( undefined ) ;
1854+
1855+ expect ( publish ) . toHaveBeenCalledTimes ( 1 ) ;
1856+ const [ channel , payload ] = publish . calls . mostRecent ( ) . args ;
1857+ expect ( channel ) . toBe ( clearCacheChannel ( ) ) ;
1858+ expect ( JSON . parse ( payload ) ) . toEqual ( { clearAll : true } ) ;
1859+ } ) ;
1860+
1861+ it ( 'keeps publishing the user id for older LiveQuery servers' , ( ) => {
1862+ const controller = new LiveQueryController ( { classNames : [ 'Yolo' ] } ) ;
1863+ const publish = controller . liveQueryPublisher . parsePublisher . publish ;
1864+
1865+ controller . clearCachedRoles ( { id : testUserId } ) ;
1866+
1867+ const payload = JSON . parse ( publish . calls . mostRecent ( ) . args [ 1 ] ) ;
1868+ expect ( payload ) . toEqual ( { userId : testUserId , clearAll : true } ) ;
1869+ } ) ;
1870+
1871+ it ( 'clears every cached auth on a full clear message' , async ( ) => {
1872+ const parseLiveQueryServer = new ParseLiveQueryServer ( { } ) ;
1873+ const clearAll = spyOn ( parseLiveQueryServer , '_clearAllCachedRoles' ) . and . resolveTo ( ) ;
1874+ const clearOne = spyOn ( parseLiveQueryServer , '_clearCachedRoles' ) . and . resolveTo ( ) ;
1875+
1876+ clearCacheHandler ( parseLiveQueryServer ) ( JSON . stringify ( { clearAll : true } ) ) ;
1877+
1878+ expect ( clearAll ) . toHaveBeenCalledTimes ( 1 ) ;
1879+ expect ( clearOne ) . not . toHaveBeenCalled ( ) ;
1880+ } ) ;
1881+
1882+ it ( 'falls back to the targeted clear when the message has no clearAll' , async ( ) => {
1883+ const parseLiveQueryServer = new ParseLiveQueryServer ( { } ) ;
1884+ const clearAll = spyOn ( parseLiveQueryServer , '_clearAllCachedRoles' ) . and . resolveTo ( ) ;
1885+ const clearOne = spyOn ( parseLiveQueryServer , '_clearCachedRoles' ) . and . resolveTo ( ) ;
1886+
1887+ clearCacheHandler ( parseLiveQueryServer ) ( JSON . stringify ( { userId : testUserId } ) ) ;
1888+
1889+ expect ( clearOne ) . toHaveBeenCalledWith ( testUserId ) ;
1890+ expect ( clearAll ) . not . toHaveBeenCalled ( ) ;
1891+ } ) ;
1892+
1893+ it ( 'drops the auth cache and the role cache on a full clear' , async ( ) => {
1894+ const parseLiveQueryServer = new ParseLiveQueryServer ( { } ) ;
1895+ const roleClear = jasmine . createSpy ( 'clear' ) . and . resolveTo ( ) ;
1896+ parseLiveQueryServer . cacheController = { role : { clear : roleClear } } ;
1897+ parseLiveQueryServer . authCache . set ( 'someToken' , Promise . resolve ( { } ) ) ;
1898+ expect ( parseLiveQueryServer . authCache . size ) . toBe ( 1 ) ;
1899+
1900+ await parseLiveQueryServer . _clearAllCachedRoles ( ) ;
1901+
1902+ expect ( parseLiveQueryServer . authCache . size ) . toBe ( 0 ) ;
1903+ expect ( roleClear ) . toHaveBeenCalledTimes ( 1 ) ;
1904+ } ) ;
1905+
1906+ it ( 'survives a role cache that rejects on a full clear' , async ( ) => {
1907+ const parseLiveQueryServer = new ParseLiveQueryServer ( { } ) ;
1908+ parseLiveQueryServer . cacheController = {
1909+ role : { clear : ( ) => Promise . reject ( new Error ( 'cache down' ) ) } ,
1910+ } ;
1911+ parseLiveQueryServer . authCache . set ( 'someToken' , Promise . resolve ( { } ) ) ;
1912+
1913+ await expectAsync ( parseLiveQueryServer . _clearAllCachedRoles ( ) ) . toBeResolved ( ) ;
1914+
1915+ expect ( parseLiveQueryServer . authCache . size ) . toBe ( 0 ) ;
1916+ } ) ;
1917+ } ) ;
1918+
18371919 afterEach ( function ( ) {
18381920 jasmine . restoreLibrary ( '../lib/LiveQuery/ParseWebSocketServer' , 'ParseWebSocketServer' ) ;
18391921 jasmine . restoreLibrary ( '../lib/LiveQuery/Client' , 'Client' ) ;
@@ -2045,4 +2127,5 @@ describe('LiveQueryController', () => {
20452127 original : undefined ,
20462128 } ) ;
20472129 } ) ;
2130+
20482131} ) ;
0 commit comments