Skip to content

Commit e6e5fe4

Browse files
PD-8720 mount the interstitial outlet before attaching it (#2920)
The already authorized OAuth path skips the authorization component, so none of the flags gating the card were set when the deferred showInterstitial() ran. The outlet lives inside that card, so the static:false ViewChild was never assigned and attachComponentPortal() threw, leaving the user with a blank page and no redirect to redirect_uri. Latent since a519d01 (PD-2371) gave the card an *ngIf; the setTimeout added back when the card was unconditional was no longer waiting for anything. showInterstitial() now mounts the card and runs a change detection pass before attaching, both in one synchronous block so the card is never painted empty, and falls back to the redirect if the outlet still cannot be resolved. The specs could not have caught this: the TestBed omitted PortalModule and every test either stubbed showInterstitial() out or hand-assigned an outlet. Co-authored-by: Angel Montenegro <a.montenegro@orcid.org>
1 parent ef47f54 commit e6e5fe4

3 files changed

Lines changed: 141 additions & 35 deletions

File tree

‎src/app/authorize/pages/authorize/authorize.component.html‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
<div class="container">
33
<div class="row space-around">
44
<div class="col m6 s4 l6">
5-
<!-- showInterstital keeps this card mounted after showInterstitial()
6-
clears showAuthorizationComponent. The portal outlet below lives
7-
inside the card, so without it the interstitial is attached and
8-
then destroyed in the same tick, leaving a blank page. -->
5+
<!-- The portal outlet below lives inside this card, so showInterstitial()
6+
sets showInterstital to mount it before attaching, and this keeps the
7+
card mounted afterwards when showAuthorizationComponent is cleared.
8+
Without it the interstitial has nowhere to attach on the already
9+
authorized path, and is destroyed in the same tick on the other one —
10+
both leaving a blank page. -->
911
<mat-card
1012
*ngIf="
1113
showAuthorizationComponent ||

‎src/app/authorize/pages/authorize/authorize.component.spec.ts‎

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
import { HttpClientTestingModule } from '@angular/common/http/testing'
88
import { RouterTestingModule } from '@angular/router/testing'
99
import { CUSTOM_ELEMENTS_SCHEMA, Component } from '@angular/core'
10+
import { PortalModule } from '@angular/cdk/portal'
11+
import { MatCardModule } from '@angular/material/card'
1012
import { of, Subject, NEVER, throwError } from 'rxjs'
1113

1214
import { WINDOW } from '../../../cdk/window'
@@ -22,10 +24,21 @@ import { InterstitialObservabilityService } from 'src/app/core/login-interstitia
2224

2325
import { AuthorizeComponent } from './authorize.component'
2426

25-
// Dummy interstitial component used for typing purposes in tests
26-
@Component({ template: '', standalone: true })
27+
// Dummy interstitial component used for typing purposes in tests. It renders a
28+
// marker so tests can tell "attached to the outlet" from "the flag was set".
29+
@Component({
30+
template: '<p class="interstitial-body">interstitial</p>',
31+
standalone: true,
32+
})
2733
class DummyInterstitialComponent {
34+
// Set on construction so tests that attach through a real outlet can reach
35+
// the instance the portal built, rather than a hand-made stand-in.
36+
static lastInstance: DummyInterstitialComponent | null = null
2837
finish = new Subject<void>()
38+
39+
constructor() {
40+
DummyInterstitialComponent.lastInstance = this
41+
}
2942
}
3043

3144
describe('AuthorizeComponent', () => {
@@ -84,6 +97,12 @@ describe('AuthorizeComponent', () => {
8497
imports: [
8598
HttpClientTestingModule,
8699
RouterTestingModule,
100+
// The real AuthorizeModule imports both. Without them cdkPortalOutlet is
101+
// an unknown attribute here, the outlet ViewChild can never resolve, and
102+
// no test can tell a mounted outlet from a missing one — which is how
103+
// PD-8720 shipped green.
104+
PortalModule,
105+
MatCardModule,
87106
DummyInterstitialComponent,
88107
],
89108
declarations: [AuthorizeComponent],
@@ -112,11 +131,24 @@ describe('AuthorizeComponent', () => {
112131
})
113132

114133
function createComponent() {
134+
DummyInterstitialComponent.lastInstance = null
115135
fixture = TestBed.createComponent(AuthorizeComponent)
116136
component = fixture.componentInstance
117137
fixture.detectChanges()
118138
}
119139

140+
/**
141+
* Renders the authorization card so the `#interstitialOutlet` ViewChild
142+
* resolves against a real CdkPortalOutlet. Hand-assigning `component.outlet`
143+
* instead is what hid PD-8720: it guarantees the one thing that was actually
144+
* undefined in production.
145+
*/
146+
function mountAuthorizationCard() {
147+
component.loading = false
148+
component.showAuthorizationComponent = true
149+
fixture.detectChanges()
150+
}
151+
120152
it('should create', () => {
121153
createComponent()
122154
expect(component).toBeTruthy()
@@ -324,13 +356,7 @@ describe('AuthorizeComponent', () => {
324356
;(component as any).interstitialComponent =
325357
DummyInterstitialComponent as any
326358

327-
const finish$ = new Subject<void>()
328-
;(component as any).outlet = {
329-
attachComponentPortal: () => ({
330-
instance: { finish: finish$.asObservable() },
331-
changeDetectorRef: { detectChanges: () => {} },
332-
}),
333-
}
359+
mountAuthorizationCard()
334360

335361
const finishSpy = spyOn<any>(
336362
component as any,
@@ -340,7 +366,7 @@ describe('AuthorizeComponent', () => {
340366
component.handleRedirect('/x')
341367
expect(component.showInterstital).toBeTrue()
342368

343-
finish$.next()
369+
DummyInterstitialComponent.lastInstance.finish.next()
344370

345371
expect(finishSpy).toHaveBeenCalled()
346372
// The dialog path closes on afterClosed(); here `finish` is the only signal
@@ -354,41 +380,28 @@ describe('AuthorizeComponent', () => {
354380
// card's *ngIf does not also test showInterstital the interstitial is
355381
// destroyed in the same tick and the user is left on a blank page.
356382
createComponent()
357-
component.loading = false
358-
component.showAuthorizationComponent = true
359-
fixture.detectChanges()
383+
mountAuthorizationCard()
360384
expect(fixture.nativeElement.querySelector('mat-card')).toBeTruthy()
361385
;(component as any).interstitialComponent =
362386
DummyInterstitialComponent as any
363-
;(component as any).outlet = {
364-
attachComponentPortal: () => ({
365-
instance: { finish: new Subject<void>().asObservable() },
366-
changeDetectorRef: { detectChanges: () => {} },
367-
}),
368-
}
369387

370388
component.handleRedirect('/x')
371389
fixture.detectChanges()
372390

373391
expect(component.showAuthorizationComponent).toBeFalse()
374392
expect(component.showInterstital).toBeTrue()
375393
expect(fixture.nativeElement.querySelector('mat-card')).toBeTruthy()
394+
expect(
395+
fixture.nativeElement.querySelector('.interstitial-body')
396+
).toBeTruthy()
376397
})
377398

378399
it('handleRedirect: with interstitial -> shows interstitial instead of redirect', () => {
379400
createComponent()
401+
mountAuthorizationCard()
380402
;(component as any).interstitialComponent =
381403
DummyInterstitialComponent as any
382404

383-
// mock outlet to avoid CDK dependency
384-
const finish$ = new Subject<void>()
385-
;(component as any).outlet = {
386-
attachComponentPortal: () => ({
387-
instance: { finish: finish$.asObservable() },
388-
changeDetectorRef: { detectChanges: () => {} },
389-
}),
390-
}
391-
392405
spyOn<any>(component as any, 'finishRedirect').and.returnValue(NEVER)
393406

394407
component.handleRedirect('/x')
@@ -397,6 +410,66 @@ describe('AuthorizeComponent', () => {
397410
expect(component.showAuthorizationComponent).toBeFalse()
398411
})
399412

413+
it('ngOnInit: already authorized WITH interstitial -> renders it instead of a blank page', fakeAsync(() => {
414+
// PD-8720. This path skips the authorization component, so nothing mounts
415+
// the card that holds the outlet. showInterstitial() has to mount it
416+
// itself; before it did, the ViewChild was undefined and attaching the
417+
// portal threw, leaving no interstitial and no redirect.
418+
userServiceSpy.getUserSession.and.returnValue(
419+
of({
420+
loggedIn: true,
421+
oauthSession: { redirectUrl: '/here?code=1', responseType: 'code' },
422+
} as any)
423+
)
424+
recordServiceSpy.getRecord.and.returnValue(of({} as any))
425+
loginInterstitialsSpy.isUserFullyLoaded.and.returnValue(true)
426+
loginInterstitialsSpy.checkLoginInterstitials.and.returnValue(
427+
of(DummyInterstitialComponent as any)
428+
)
429+
430+
createComponent()
431+
432+
// Nothing has mounted the card at this point, which is the state the bug
433+
// was reported in.
434+
expect(component.showAuthorizationComponent).toBeFalse()
435+
expect(fixture.nativeElement.querySelector('mat-card')).toBeFalsy()
436+
437+
tick()
438+
fixture.detectChanges()
439+
440+
expect(component.outlet).toBeDefined()
441+
expect(component.showInterstital).toBeTrue()
442+
expect(fixture.nativeElement.querySelector('mat-card')).toBeTruthy()
443+
expect(
444+
fixture.nativeElement.querySelector('.interstitial-body')
445+
).toBeTruthy()
446+
// The flow waits on the interstitial rather than redirecting past it
447+
expect(windowMock.outOfRouterNavigation).not.toHaveBeenCalled()
448+
}))
449+
450+
it('showInterstitial: outlet that cannot be mounted redirects instead of blanking', () => {
451+
createComponent()
452+
// `loading` keeps <main> and the card it holds out of the DOM, so the
453+
// outlet cannot resolve. The user is still owed the redirect_uri.
454+
component.loading = true
455+
fixture.detectChanges()
456+
;(component as any).interstitialComponent =
457+
DummyInterstitialComponent as any
458+
459+
const finishSpy = spyOn<any>(
460+
component as any,
461+
'finishRedirect'
462+
).and.returnValue(of(true))
463+
464+
expect(() => component.handleRedirect('/x')).not.toThrow()
465+
466+
expect(component.outlet).toBeUndefined()
467+
expect(finishSpy).toHaveBeenCalled()
468+
expect(component.showInterstital).toBeFalse()
469+
// The journey opened by shown() has to close, or it stays open forever
470+
expect(interstitialObservabilitySpy.closed).toHaveBeenCalled()
471+
})
472+
400473
it('handleRedirect: without interstitial -> calls finishRedirect', () => {
401474
createComponent()
402475

‎src/app/authorize/pages/authorize/authorize.component.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ComponentType } from '@angular/cdk/overlay'
2-
import { Component, Inject, inject, ViewChild } from '@angular/core'
2+
import {
3+
ChangeDetectorRef,
4+
Component,
5+
Inject,
6+
inject,
7+
ViewChild,
8+
} from '@angular/core'
39
import { Observable, forkJoin, of } from 'rxjs'
410
import {
511
filter,
@@ -62,7 +68,8 @@ export class AuthorizeComponent {
6268
private toglzService: TogglzService,
6369
private oauthUrlSessionManger: OauthURLSessionManagerService,
6470
private readonly featureLogger: FeatureLoggerService,
65-
private readonly _observability: RumJourneyEventService
71+
private readonly _observability: RumJourneyEventService,
72+
private readonly changeDetectorRef: ChangeDetectorRef
6673
) {
6774
this.log = this.featureLogger.scoped('Auth Component')
6875
}
@@ -179,8 +186,30 @@ export class AuthorizeComponent {
179186

180187
/**
181188
* Displays the interstitial
189+
*
190+
* The outlet lives inside the card, and the card only renders once one of the
191+
* flags in its *ngIf is set. A `static: false` view query is refreshed by
192+
* change detection, so mounting the card and running a pass are both
193+
* preconditions for `outlet` to exist — the already authorized path reaches
194+
* here with none of those flags set, which is what left `outlet` undefined.
195+
* Doing both here keeps mount and attach in the same synchronous block, so
196+
* the card is never painted empty (PD-2371).
182197
*/
183198
private showInterstitial(): void {
199+
this.showInterstital = true
200+
this.changeDetectorRef.detectChanges()
201+
202+
if (!this.outlet) {
203+
// Never strand the user on a blank page: the OAuth flow still owes them a
204+
// redirect to redirect_uri, and the journey opened by `shown()` has to be
205+
// closed or it stays open forever.
206+
this.log.error('Interstitial outlet unavailable, redirecting instead')
207+
this.showInterstital = false
208+
this.interstitialObservability.closed()
209+
this.finishRedirect().subscribe()
210+
return
211+
}
212+
184213
const portal = new ComponentPortal(this.interstitialComponent)
185214

186215
const componentRef = this.outlet.attachComponentPortal(portal)
@@ -197,7 +226,6 @@ export class AuthorizeComponent {
197226
componentRef.changeDetectorRef.detectChanges()
198227

199228
this.showAuthorizationComponent = false
200-
this.showInterstital = true
201229
}
202230

203231
/**
@@ -240,6 +268,9 @@ export class AuthorizeComponent {
240268
this.loading = false
241269
this.redirectUrl = this.oauthSession.redirectUrl
242270
trace.push('already authorized with interstitial → show interstitial')
271+
// Deferred because `finalize` runs synchronously inside change
272+
// detection when the source observables are, and `showInterstitial`
273+
// runs a pass of its own.
243274
setTimeout(() => this.showInterstitial())
244275
} else {
245276
trace.push('already authorized without interstitial → redirect now')

0 commit comments

Comments
 (0)