Skip to content

Commit

Permalink
[create docker image]
Browse files Browse the repository at this point in the history
fix
fix e2e
[MNT-21636] Use URLTree for redirect (#6691)

* use URLTree for redirect

* use always urltree

* fix e2e

* fix
  • Loading branch information
eromano committed Feb 23, 2021
1 parent c403068 commit e27f581
Show file tree
Hide file tree
Showing 11 changed files with 923 additions and 1,085 deletions.
2 changes: 2 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"superagent",
"event-emitter",
"brace-expansion",
"zen-observable",
"subscriptions-transport-ws",
"d",
"chart.js"
],
Expand Down
8 changes: 5 additions & 3 deletions lib/core/services/auth-guard-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ export abstract class AuthGuardBase implements CanActivate, CanActivateChild {
const redirectFragment = this.storageService.getItem('loginFragment');

if (redirectFragment && this.getLoginRoute() !== redirectFragment) {
await this.navigate(redirectFragment);
this.storageService.removeItem('loginFragment');
return this.router.parseUrl(redirectFragment);
return false;
}

return true;
Expand Down Expand Up @@ -110,9 +111,10 @@ export abstract class AuthGuardBase implements CanActivate, CanActivateChild {
return false;
}

protected navigate(url: string): UrlTree {
protected async navigate(url: string): Promise<boolean> {
this.dialog.closeAll();
return this.router.parseUrl(url);
await this.router.navigateByUrl(this.router.parseUrl(url));
return false;
}

protected getOauthConfig(): OauthConfigModel {
Expand Down
24 changes: 16 additions & 8 deletions lib/core/services/auth-guard-bpm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('AuthGuardService BPM', () => {
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login?redirectUrl=some-url'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
}));

it('if the alfresco js api is NOT logged in should trigger a redirect event', async(async () => {
Expand All @@ -105,7 +105,8 @@ describe('AuthGuardService BPM', () => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login?redirectUrl=some-url'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url'));
}));

it('should redirect url if the alfresco js api is NOT logged in and isOAuthWithoutSilentLogin', async(async () => {
Expand All @@ -115,20 +116,22 @@ describe('AuthGuardService BPM', () => {
appConfigService.config.oauth2.silentLogin = false;
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
}));

it('should redirect to login url if NOT you are not logged in and silentLogin is false', async(async () => {
it('should redirect url if NOT logged in and isOAuth but no silentLogin configured', async(async () => {
spyOn(router, 'navigateByUrl').and.stub();
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
spyOn(authService, 'isOauth').and.returnValue(true);
appConfigService.config.oauth2.silentLogin = undefined;
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
}));

it('should set redirect url', async(async () => {
it('should set redirect url', async(() => {
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
Expand Down Expand Up @@ -167,13 +170,18 @@ describe('AuthGuardService BPM', () => {
expect(authService.getRedirect()).toEqual('/');
}));

it('should get redirect url from config if there is one configured', async(async () => {
it('should get redirect url from config if there is one configured', async(() => {
appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
authGuard.canActivate(null, route);

expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'BPM', url: 'some-url'
});
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
}));

it('should to close the material dialog if is redirect to the login', () => {
Expand Down
46 changes: 27 additions & 19 deletions lib/core/services/auth-guard-ecm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,53 @@ describe('AuthGuardService ECM', () => {
appConfigService.config.oauth2 = {};
});

it('if the alfresco js api is logged in should canActivate be true', async(async () => {
it('if the alfresco js api is logged in should canActivate be true', async(async() => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(true);
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};

expect(await authGuard.canActivate(null, route)).toBeTruthy();
}));

it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(async () => {
it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(async() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(true);
appConfigService.config.auth.withCredentials = true;

const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};

expect(await authGuard.canActivate(null, route)).toBeTruthy();
}));

it('if the alfresco js api is NOT logged in should canActivate be false', async(async () => {
it('if the alfresco js api is NOT logged in should canActivate be false', async(async() => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login?redirectUrl=some-url'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
}));

it('if the alfresco js api is NOT logged in should trigger a redirect event', async(async () => {
it('if the alfresco js api is NOT logged in should trigger a redirect event', async(async() => {
appConfigService.config.loginRoute = 'login';

spyOn(router, 'navigateByUrl');
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login?redirectUrl=some-url'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url'));
}));

it('should redirect url if the alfresco js api is NOT logged in and isOAuthWithoutSilentLogin', async(async () => {
it('should redirect url if the alfresco js api is NOT logged in and isOAuthWithoutSilentLogin', async(async() => {
spyOn(router, 'navigateByUrl').and.stub();
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
spyOn(authService, 'isOauth').and.returnValue(true);
appConfigService.config.oauth2.silentLogin = false;
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
}));

it('should redirect url if the alfresco js api is NOT logged in and isOAuth with silentLogin', async(async () => {
it('should redirect url if the alfresco js api is NOT logged in and isOAuth with silentLogin', async(async() => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
spyOn(authService, 'isOauth').and.returnValue(true);
spyOn(authService, 'isPublicUrl').and.returnValue(false);
Expand All @@ -110,20 +112,21 @@ describe('AuthGuardService ECM', () => {
scope: 'openid'
};

const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'abc' };
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'abc'};

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(authService.ssoImplicitLogin).toHaveBeenCalledTimes(1);
}));

it('should not redirect url if NOT logged in and isOAuth but no silentLogin configured', async(async () => {
it('should not redirect url if NOT logged in and isOAuth but no silentLogin configured', async(async() => {
spyOn(router, 'navigateByUrl').and.stub();
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
spyOn(authService, 'isOauth').and.returnValue(true);
appConfigService.config.oauth2.silentLogin = undefined;
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/login'));
expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
}));

it('should set redirect navigation commands', async(() => {
Expand Down Expand Up @@ -165,13 +168,18 @@ describe('AuthGuardService ECM', () => {
expect(authService.getRedirect()).toEqual('/');
}));

it('should get redirect url from config if there is one configured', async(async () => {
it('should get redirect url from config if there is one configured', async(() => {
appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };

expect(await authGuard.canActivate(null, route)).toEqual(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
authGuard.canActivate(null, route);

expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM', url: 'some-url'
});
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
}));

it('should to close the material dialog if is redirect to the login', () => {
Expand Down
30 changes: 24 additions & 6 deletions lib/core/services/auth-guard.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ describe('AuthGuardService', () => {
spyOn(router, 'navigateByUrl');
spyOn(authService, 'isLoggedIn').and.returnValue(false);

expect(await authGuard.canActivate(null, state)).toEqual(router.parseUrl('/login?redirectUrl=some-url'));
expect(await authGuard.canActivate(null, state)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
}));

it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(async () => {
Expand Down Expand Up @@ -96,7 +97,8 @@ describe('AuthGuardService', () => {
spyOn(authService, 'isOauth').and.returnValue(true);
appConfigService.config.oauth2.silentLogin = false;

expect(await authGuard.canActivate(null, state)).toEqual(router.parseUrl('/login'));
expect(await authGuard.canActivate(null, state)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
}));

it('should redirect url if the User is NOT logged in and isOAuth but no silentLogin configured', async(async () => {
Expand All @@ -105,7 +107,8 @@ describe('AuthGuardService', () => {
spyOn(authService, 'isOauth').and.returnValue(true);
appConfigService.config.oauth2.silentLogin = undefined;

expect(await authGuard.canActivate(null, state)).toEqual(router.parseUrl('/login'));
expect(await authGuard.canActivate(null, state)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
}));

it('should NOT redirect url if the User is NOT logged in and isOAuth but with silentLogin configured', async(async () => {
Expand All @@ -125,7 +128,12 @@ describe('AuthGuardService', () => {
spyOn(router, 'navigateByUrl');
spyOn(authService, 'setRedirect');

expect(await authGuard.canActivate(null, state)).toEqual(router.parseUrl('/login?redirectUrl=some-url'));
await authGuard.canActivate(null, state);

expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', url: 'some-url'
});
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url'));
}));

it('should set redirect url with query params', async(async () => {
Expand All @@ -136,7 +144,12 @@ describe('AuthGuardService', () => {
spyOn(router, 'navigateByUrl');
spyOn(authService, 'setRedirect');

expect(await authGuard.canActivate(null, state)).toEqual(router.parseUrl('/login?redirectUrl=some-url;q=query'));
await authGuard.canActivate(null, state);

expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', url: 'some-url;q=query'
});
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url;q=query'));
}));

it('should get redirect url from config if there is one configured', async(async () => {
Expand All @@ -146,7 +159,12 @@ describe('AuthGuardService', () => {
spyOn(router, 'navigateByUrl');
spyOn(authService, 'setRedirect');

expect(await authGuard.canActivate(null, state)).toEqual(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
await authGuard.canActivate(null, state);

expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', url: 'some-url'
});
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
}));

it('should pass actual redirect when no state segments exists', async(async () => {
Expand Down
1 change: 0 additions & 1 deletion lib/core/services/auth-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export class AuthGuard extends AuthGuardBase {

async checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): Promise<boolean | UrlTree> {
if (this.authenticationService.isLoggedIn() || this.withCredentials) {

return true;
}
return this.redirectToUrl(redirectUrl);
Expand Down
35 changes: 27 additions & 8 deletions lib/testing/src/lib/core/test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { BrowserActions } from './utils/browser-actions';
import { BrowserVisibility } from './utils/browser-visibility';

export class TestElement {
constructor(public elementFinder: ElementFinder) {}
constructor(public elementFinder: ElementFinder) {
}

static byId(id: string): TestElement {
return new TestElement(element(by.id(id)));
Expand All @@ -42,23 +43,41 @@ export class TestElement {
return BrowserActions.click(this.elementFinder);
}

async waitVisible(waitTimeout?: number) {
async isVisible(waitTimeout?: number): Promise<boolean> {
try {
await BrowserVisibility.waitUntilElementIsVisible(this.elementFinder, waitTimeout);
return true;
} catch {
return false;
}
}

async waitVisible(waitTimeout?: number): Promise<any> {
return BrowserVisibility.waitUntilElementIsVisible(this.elementFinder, waitTimeout);
}

async waitNotVisible(waitTimeout?: number) {
async waitNotVisible(waitTimeout?: number): Promise<any> {
return BrowserVisibility.waitUntilElementIsNotVisible(this.elementFinder, waitTimeout);
}

async waitPresent(waitTimeout?: number) {
async isPresent(waitTimeout?: number): Promise<boolean> {
try {
await BrowserVisibility.waitUntilElementIsPresent(this.elementFinder, waitTimeout);
return true;
} catch {
return false;
}
}

async waitPresent(waitTimeout?: number): Promise<any> {
return BrowserVisibility.waitUntilElementIsPresent(this.elementFinder, waitTimeout);
}

async waitNotPresent(waitTimeout?: number) {
async waitNotPresent(waitTimeout?: number): Promise<any> {
return BrowserVisibility.waitUntilElementIsNotPresent(this.elementFinder, waitTimeout);
}

async waitHasValue(value: string) {
async waitHasValue(value: string): Promise<any> {
return BrowserVisibility.waitUntilElementHasValue(this.elementFinder, value);
}

Expand All @@ -79,8 +98,8 @@ export class TestElement {
return BrowserActions.getText(this.elementFinder);
}

async typeText(text: string) {
return BrowserActions.clearSendKeys(this.elementFinder, text);
async typeText(text: string): Promise<void> {
await BrowserActions.clearSendKeys(this.elementFinder, text);
}

async clearInput() {
Expand Down
Loading

0 comments on commit e27f581

Please sign in to comment.