-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Defect/365 istrusted logindotgov code error #388
Merged
mjtravers
merged 6 commits into
develop
from
defect/365-istrusted-logindotgov-code-error
Jul 13, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0e8c5a
restore 109 branch changes
dheitzer 05534e3
Merge branch 'develop' into defect/365-istrusted-logindotgov-code-error
dheitzer 031c6bf
testing 365 on dev
dheitzer a89fd12
fix imports
dheitzer 55406d5
update to develop branch in tasks.py
dheitzer 20ea89c
lint fix
dheitzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,19 +1,47 @@ | ||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { Store } from '@ngrx/store'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
import { UserLoginData } from 'app/shared/models/user.model'; | ||
import { userLoggedInAction } from 'app/store/login.actions'; | ||
import { selectUserLoginData } from 'app/store/login.selectors'; | ||
import { environment } from 'environments/environment'; | ||
import { CookieService } from 'ngx-cookie-service'; | ||
import { PanelModule } from 'primeng/panel'; | ||
import { DashboardComponent } from './dashboard.component'; | ||
|
||
xdescribe('DashboardComponent', () => { | ||
describe('DashboardComponent', () => { | ||
let component: DashboardComponent; | ||
let fixture: ComponentFixture<DashboardComponent>; | ||
let cookieService: CookieService; | ||
let store: Store; | ||
|
||
beforeEach( | ||
waitForAsync(() => { | ||
const userLoginData: UserLoginData = { | ||
committee_id: 'C00000000', | ||
email: '[email protected]', | ||
is_allowed: true, | ||
token: 'jwttokenstring', | ||
}; | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule, RouterTestingModule], | ||
imports: [ | ||
HttpClientTestingModule, | ||
RouterTestingModule, | ||
PanelModule, | ||
BrowserAnimationsModule, | ||
], | ||
declarations: [DashboardComponent], | ||
providers: [CookieService], | ||
providers: [ | ||
CookieService, | ||
provideMockStore({ | ||
initialState: { fecfile_online_userLoginData: userLoginData }, | ||
selectors: [{ selector: selectUserLoginData, value: userLoginData }], | ||
}), | ||
], | ||
}).compileComponents(); | ||
}) | ||
); | ||
|
@@ -22,9 +50,41 @@ xdescribe('DashboardComponent', () => { | |
fixture = TestBed.createComponent(DashboardComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
cookieService = TestBed.inject(CookieService); | ||
store = TestBed.inject(Store); | ||
}); | ||
|
||
xit('should create', () => { | ||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('#dispatchUserLoggedInFromCookies happy path', () => { | ||
const testCommitteeId = "testCommitteeId"; | ||
const testEmail = "testEmail"; | ||
const testIsAllowed = true; | ||
const testToken = null; | ||
|
||
const expectedUserLoginData: UserLoginData = { | ||
committee_id: testCommitteeId, | ||
email: testEmail, | ||
is_allowed: testIsAllowed, | ||
token: testToken | ||
} | ||
spyOn(cookieService, 'check').and.returnValue(true); | ||
spyOn(cookieService, 'get').and.callFake((name: string) => { | ||
if (name === environment.ffapiCommitteeIdCookieName) { | ||
return testCommitteeId; | ||
} | ||
if (name === environment.ffapiEmailCookieName) { | ||
return testEmail; | ||
} | ||
throw Error("fail!"); | ||
}); | ||
spyOn(store, 'dispatch'); | ||
|
||
component.dispatchUserLoggedInFromCookies(); | ||
expect(store.dispatch).toHaveBeenCalledWith( | ||
userLoggedInAction({ payload: expectedUserLoginData })); | ||
}); | ||
|
||
}); |
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,8 +1,38 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import { UserLoginData } from 'app/shared/models/user.model'; | ||
import { userLoggedInAction } from 'app/store/login.actions'; | ||
import { environment } from 'environments/environment'; | ||
import { CookieService } from 'ngx-cookie-service'; | ||
|
||
@Component({ | ||
selector: 'app-dashboard', | ||
templateUrl: './dashboard.component.html', | ||
// styleUrls: ['./dashboard.component.scss'], | ||
}) | ||
export class DashboardComponent {} | ||
export class DashboardComponent implements OnInit { | ||
constructor( | ||
private store: Store, | ||
private cookieService: CookieService | ||
) { } | ||
|
||
ngOnInit(): void { | ||
this.dispatchUserLoggedInFromCookies(); | ||
} | ||
|
||
dispatchUserLoggedInFromCookies() { | ||
if (this.cookieService.check(environment.ffapiCommitteeIdCookieName)) { | ||
const userLoginData: UserLoginData = { | ||
committee_id: this.cookieService.get( | ||
environment.ffapiCommitteeIdCookieName), | ||
email: this.cookieService.get( | ||
environment.ffapiEmailCookieName), | ||
is_allowed: true, | ||
token: null | ||
} | ||
this.cookieService.delete(environment.ffapiCommitteeIdCookieName) | ||
this.cookieService.delete(environment.ffapiEmailCookieName) | ||
this.store.dispatch(userLoggedInAction({ payload: userLoginData })); | ||
} | ||
} | ||
} | ||
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
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,13 +1,42 @@ | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ReactiveFormsModule } from '@angular/forms'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
import { UserLoginData } from 'app/shared/models/user.model'; | ||
import { selectUserLoginData } from 'app/store/login.selectors'; | ||
import { environment } from 'environments/environment'; | ||
import { LoginComponent } from './login.component'; | ||
|
||
|
||
|
||
describe('LoginComponent', () => { | ||
let component: LoginComponent; | ||
let fixture: ComponentFixture<LoginComponent>; | ||
|
||
const userLoginData: UserLoginData = { | ||
committee_id: 'C00000000', | ||
email: '[email protected]', | ||
is_allowed: true, | ||
token: 'jwttokenstring', | ||
}; | ||
|
||
|
||
beforeEach(async () => { | ||
window.onbeforeunload = jasmine.createSpy(); | ||
await TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientTestingModule, | ||
RouterTestingModule, | ||
ReactiveFormsModule, | ||
], | ||
providers: [ | ||
{ provide: Window, useValue: window }, | ||
provideMockStore({ | ||
initialState: { fecfile_online_userLoginData: userLoginData }, | ||
selectors: [{ selector: selectUserLoginData, value: userLoginData }], | ||
}), | ||
], | ||
declarations: [LoginComponent], | ||
}).compileComponents(); | ||
}); | ||
|
@@ -18,7 +47,13 @@ describe('LoginComponent', () => { | |
fixture.detectChanges(); | ||
}); | ||
|
||
xit('should create', () => { | ||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
xit('navigateToLoginDotGov should href environment location', () => { | ||
component.navigateToLoginDotGov(); | ||
expect(window.location.href).toEqual(environment.loginDotGovAuthUrl); | ||
}); | ||
|
||
}); |
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,60 @@ | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Router } from '@angular/router'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
import { UserLoginData } from 'app/shared/models/user.model'; | ||
import { ApiService } from 'app/shared/services/api.service'; | ||
import { selectUserLoginData } from 'app/store/login.selectors'; | ||
import { LoginGuard } from './login-page.guard'; | ||
|
||
|
||
describe('LoginGuard', () => { | ||
let guard: LoginGuard; | ||
let apiService: ApiService; | ||
let router: Router; | ||
|
||
const userLoginData: UserLoginData = { | ||
committee_id: 'C00000000', | ||
email: '[email protected]', | ||
is_allowed: true, | ||
token: 'jwttokenstring', | ||
}; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule, RouterTestingModule], | ||
providers: [ | ||
ApiService, | ||
provideMockStore({ | ||
initialState: { fecfile_online_userLoginData: userLoginData }, | ||
selectors: [{ selector: selectUserLoginData, value: userLoginData }], | ||
}), | ||
], | ||
}); | ||
guard = TestBed.inject(LoginGuard); | ||
apiService = TestBed.inject(ApiService); | ||
router = TestBed.inject(Router); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(guard).toBeTruthy(); | ||
}); | ||
|
||
it('should reroute to dashboard if logged in', () => { | ||
spyOn(apiService, 'isAuthenticated').and.returnValue(true); | ||
const navigateSpy = spyOn(router, 'navigate'); | ||
const retval = guard.canActivate() | ||
expect(navigateSpy).toHaveBeenCalledWith(['dashboard']); | ||
expect(retval).toEqual(false); | ||
}); | ||
|
||
it('should allow activate if not logged in', () => { | ||
spyOn(apiService, 'isAuthenticated').and.returnValue(false); | ||
const navigateSpy = spyOn(router, 'navigate'); | ||
const retval = guard.canActivate() | ||
expect(navigateSpy).not.toHaveBeenCalled(); | ||
expect(retval).toEqual(true); | ||
}); | ||
|
||
}); |
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,20 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, Router, UrlTree } from '@angular/router'; | ||
import { ApiService } from 'app/shared/services/api.service'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class LoginGuard implements CanActivate { | ||
constructor(private apiService: ApiService, private router: Router) { } | ||
canActivate(): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { | ||
if (this.apiService.isAuthenticated()) { | ||
this.router.navigate(['dashboard']); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we drop the use of cookies and use the ngrx store 100% or is the use of cookies required?
[googling around...]
I see that the CORS_ALLOW_CRENDENTIALS setting in Django may require the use of cookies. Is that what you found, David?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mjtravers well, the problem was that the server needs to send this data down to the client alongside a redirect. Because I couldn't use a JSON body in the redirect, I felt my only option was to pass them using cookies. This code then deletes the cookies once they are received following login.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually does ultimately store the cookie data in the store UserLoginData object, but only receives it from the server via cookies. I'm open for any suggestions on how to send these from the server though if we want to use something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works for me. Thanks for the clarification