-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Github redirect encapsulated in a service to allow mocking
- Loading branch information
1 parent
4456068
commit 731535b
Showing
7 changed files
with
120 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,125 @@ | ||
import { ComponentFixture, TestBed, fakeAsync, tick } from "@angular/core/testing"; | ||
import { ComponentFixture, TestBed, fakeAsync, flush, tick } from "@angular/core/testing"; | ||
import { Location } from "@angular/common"; | ||
import { Router } from "@angular/router"; | ||
import { RouterTestingModule } from "@angular/router/testing"; | ||
import { routes } from "./app-routing.module"; | ||
import { AppComponent } from "./app.component"; | ||
import ProjectLinks from "./project-links"; | ||
import { promiseWithCatch } from "./common/app.helpers"; | ||
import { ApolloTestingModule } from "apollo-angular/testing"; | ||
import { NO_ERRORS_SCHEMA } from "@angular/core"; | ||
import { LoggedUserService } from "./auth/logged-user.service"; | ||
import { AppConfigService } from "./app-config.service"; | ||
import { LoginService } from "./auth/login/login.service"; | ||
import { PageNotFoundComponent } from "./components/page-not-found/page-not-found.component"; | ||
|
||
describe("Router: App", () => { | ||
describe("Router", () => { | ||
let router: Router; | ||
let fixture: ComponentFixture<AppComponent>; | ||
let fixture: ComponentFixture<PageNotFoundComponent>; // any component is fine, we are testing router | ||
let location: Location; | ||
let loggedUserService: LoggedUserService; | ||
let appConfigService: AppConfigService; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [RouterTestingModule.withRoutes(routes), ApolloTestingModule], | ||
declarations: [AppComponent], | ||
declarations: [PageNotFoundComponent], | ||
schemas: [NO_ERRORS_SCHEMA], | ||
}).compileComponents(); | ||
|
||
router = TestBed.inject(Router); | ||
location = TestBed.inject(Location); | ||
loggedUserService = TestBed.inject(LoggedUserService); | ||
appConfigService = TestBed.inject(AppConfigService); | ||
|
||
fixture = TestBed.createComponent(AppComponent); | ||
fixture = TestBed.createComponent(PageNotFoundComponent); | ||
router.initialNavigation(); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('navigate to "" redirects you to search', fakeAsync(() => { | ||
it("navigate to home redirects you to default URL", fakeAsync(() => { | ||
promiseWithCatch(router.navigate([""])); | ||
tick(); | ||
expect(location.path()).toBe("/" + ProjectLinks.URL_SEARCH); | ||
|
||
expect(location.path()).toBe("/" + ProjectLinks.DEFAULT_URL); | ||
flush(); | ||
})); | ||
|
||
[ | ||
ProjectLinks.URL_GITHUB_CALLBACK, | ||
ProjectLinks.URL_SEARCH, | ||
`myaccount`, | ||
`myaccount/mydataset`, | ||
`myaccount/mydataset/${ProjectLinks.URL_BLOCK}/:someHash`, | ||
ProjectLinks.URL_PAGE_NOT_FOUND, | ||
"dummy", | ||
].forEach((url: string) => { | ||
it(`Route to ${url} lands on the component without Login`, fakeAsync(() => { | ||
promiseWithCatch(router.navigate([url])); | ||
tick(); | ||
|
||
expect(location.path()).toBe("/" + url); | ||
flush(); | ||
})); | ||
}); | ||
|
||
[ | ||
ProjectLinks.URL_DATASET_CREATE, | ||
ProjectLinks.URL_SETTINGS, | ||
`myaccount/mydataset/${ProjectLinks.URL_PARAM_ADD_POLLING_SOURCE}`, | ||
`myaccount/mydataset/${ProjectLinks.URL_PARAM_SET_TRANSFORM}`, | ||
].forEach((url: string) => { | ||
it(`Route to ${url} fails without a login and moves to Home`, fakeAsync(() => { | ||
promiseWithCatch(router.navigate([url])); | ||
tick(); | ||
|
||
expect(location.path()).toBe("/" + ProjectLinks.DEFAULT_URL); | ||
flush(); | ||
})); | ||
|
||
it(`Route to ${url} lands on the component with active login`, fakeAsync(() => { | ||
spyOnProperty(loggedUserService, "isAuthenticated", "get").and.returnValue(true); | ||
|
||
promiseWithCatch(router.navigate([url])); | ||
tick(); | ||
|
||
expect(location.path()).toBe("/" + url); | ||
flush(); | ||
})); | ||
}); | ||
|
||
describe("#login routes", () => { | ||
it("login redirects to default page when not allowed in configuration", fakeAsync(() => { | ||
spyOnProperty(appConfigService, "featureFlags", "get").and.returnValue({ | ||
enableLogin: false, | ||
enableLogout: true, | ||
}); | ||
|
||
promiseWithCatch(router.navigate([ProjectLinks.URL_LOGIN])); | ||
tick(); | ||
|
||
expect(location.path()).toBe("/" + ProjectLinks.URL_SEARCH); | ||
flush(); | ||
})); | ||
|
||
it("login redirects to default page when user is already logged", fakeAsync(() => { | ||
spyOnProperty(loggedUserService, "isAuthenticated", "get").and.returnValue(true); | ||
|
||
promiseWithCatch(router.navigate([ProjectLinks.URL_LOGIN])); | ||
tick(); | ||
|
||
expect(location.path()).toBe("/" + ProjectLinks.URL_SEARCH); | ||
flush(); | ||
})); | ||
|
||
it("login initiates GitHub redirect when allowed and not logged in", fakeAsync(() => { | ||
spyOnProperty(loggedUserService, "isAuthenticated", "get").and.returnValue(false); | ||
const gotoGithubSpy = spyOn(LoginService, "gotoGithub"); | ||
|
||
promiseWithCatch(router.navigate([ProjectLinks.URL_LOGIN])); | ||
tick(); | ||
|
||
expect(gotoGithubSpy).toHaveBeenCalledWith(); | ||
flush(); | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<p>Please login with your <strong>GitHub account</strong>.</p> | ||
<a href="{{ githubUrl }}" class="login__btn login__btn--full" md-raised-button="primary"> Login </a> | ||
<a href="{{ GITHUB_URL }}" class="login__btn login__btn--full" md-raised-button="primary"> Login </a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable } from "@angular/core"; | ||
import ProjectLinks from "src/app/project-links"; | ||
|
||
@Injectable({ | ||
providedIn: "root", | ||
}) | ||
export class LoginService { | ||
public static gotoGithub(): void { | ||
window.location.href = ProjectLinks.GITHUB_URL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters