SHOW:
|
|
- or go back to the newest paste.
1 | - | ######## |
1 | + | ############ |
2 | login-fca.component.ts | |
3 | - | ######## |
3 | + | ############ |
4 | ||
5 | import { Component, OnInit } from '@angular/core'; | |
6 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; | |
7 | import { JhiEventManager } from 'ng-jhipster'; | |
8 | import { Router } from '@angular/router'; | |
9 | ||
10 | import { Account, LoginModalService, Principal } from '../shared'; | |
11 | import {LoginService} from '../shared/login/login.service'; | |
12 | import {StateStorageService} from '../shared/auth/state-storage.service'; | |
13 | ||
14 | @Component({ | |
15 | selector: 'jhi-home', | |
16 | templateUrl: './login-fca.component.html', | |
17 | styleUrls: [ | |
18 | 'login-fca.css' | |
19 | ] | |
20 | ||
21 | }) | |
22 | export class LoginFcaComponent implements OnInit { | |
23 | account: Account; | |
24 | modalRef: NgbModalRef; | |
25 | ||
26 | authenticationError: boolean; | |
27 | password: string; | |
28 | rememberMe: boolean; | |
29 | username: string; | |
30 | credentials: any; | |
31 | ||
32 | constructor( | |
33 | private principal: Principal, | |
34 | private loginService: LoginService, | |
35 | private eventManager: JhiEventManager, | |
36 | private router: Router, | |
37 | private stateStorageService: StateStorageService | |
38 | ) { | |
39 | this.credentials = {}; | |
40 | } | |
41 | ||
42 | ngOnInit() { | |
43 | this.principal.identity().then((account) => { | |
44 | this.account = account; | |
45 | }); | |
46 | this.registerAuthenticationSuccess(); | |
47 | } | |
48 | ||
49 | registerAuthenticationSuccess() { | |
50 | this.eventManager.subscribe('authenticationSuccess', (message) => { | |
51 | this.principal.identity().then((account) => { | |
52 | this.account = account; | |
53 | }); | |
54 | }); | |
55 | } | |
56 | ||
57 | isAuthenticated() { | |
58 | return this.principal.isAuthenticated(); | |
59 | } | |
60 | ||
61 | login() { | |
62 | this.loginService.login({ | |
63 | username: this.username, | |
64 | password: this.password, | |
65 | rememberMe: this.rememberMe | |
66 | }).then(() => { | |
67 | this.authenticationError = false; | |
68 | if (this.router.url === '/register' || (/^\/activate\//.test(this.router.url)) || | |
69 | (/^\/reset\//.test(this.router.url))) { | |
70 | this.router.navigate(['']); | |
71 | } | |
72 | ||
73 | this.eventManager.broadcast({ | |
74 | - | ######## |
74 | + | |
75 | - | login-fca.component.spec.ts |
75 | + | |
76 | - | ######## |
76 | + | |
77 | ||
78 | // // previousState was set in the authExpiredInterceptor before being redirected to login modal. | |
79 | // // since login is succesful, go to stored previousState and clear previousState | |
80 | const redirect = this.stateStorageService.getUrl(); | |
81 | if (redirect) { | |
82 | this.stateStorageService.storeUrl(null); | |
83 | this.router.navigate([redirect]); | |
84 | } | |
85 | this.router.navigate(['dashboard']); | |
86 | }).catch(() => { | |
87 | this.authenticationError = true; | |
88 | }); | |
89 | } | |
90 | } | |
91 | ||
92 | ||
93 | ||
94 | ||
95 | ||
96 | ############ | |
97 | login.component.spec.ts | |
98 | ############ | |
99 | ||
100 | import {ComponentFixture, TestBed, async, inject, tick} from '@angular/core/testing'; | |
101 | import { FcaSystemIntegrationToolTestModule } from '../../../test.module'; | |
102 | import { Principal, AccountService } from '../../../../../../main/webapp/app/shared'; | |
103 | import { MockAccountService } from '../../../helpers/mock-account.service'; | |
104 | import { MockPrincipal } from '../../../helpers/mock-principal.service'; | |
105 | import {LoginFcaComponent} from '../../../../../../main/webapp/app/loginFca/login-fca.component'; | |
106 | import {LoginService} from '../../../../../../main/webapp/app/shared/login/login.service'; | |
107 | - | JhiEventManager |
107 | + | import {JhiEventManager} from 'ng-jhipster'; |
108 | import {Router} from '@angular/router'; | |
109 | import {MockActivatedRoute} from '../../../helpers/mock-route.service'; | |
110 | import {MockLoginService} from '../../../helpers/mock-login.service'; | |
111 | import {fakeAsync} from '@angular/core/testing'; | |
112 | import {Observable} from 'rxjs/Observable'; | |
113 | import {StateStorageService} from '../../../../../../main/webapp/app/shared/auth/state-storage.service'; | |
114 | ||
115 | describe('Login Tests', () => { | |
116 | ||
117 | describe('LoginFcaComponent', () => { | |
118 | ||
119 | let comp: LoginFcaComponent; | |
120 | - | it('should ensure the two passwords entered match', () => { |
120 | + | |
121 | - | comp.login(); |
121 | + | |
122 | - | expect(true).toEqual(true); |
122 | + | |
123 | let mockLoginService: any; | |
124 | ||
125 | beforeEach(async(() => { | |
126 | TestBed.configureTestingModule({ | |
127 | imports: [FcaSystemIntegrationToolTestModule], | |
128 | declarations: [LoginFcaComponent], | |
129 | providers: [ | |
130 | { | |
131 | provider: Principal, | |
132 | useClass: MockPrincipal | |
133 | }, | |
134 | { | |
135 | provider: AccountService, | |
136 | useClass: MockAccountService | |
137 | }, | |
138 | { | |
139 | provider: LoginService, | |
140 | useClass: MockLoginService | |
141 | }, | |
142 | { | |
143 | provider: Router, | |
144 | useClass: MockActivatedRoute | |
145 | }, | |
146 | { | |
147 | provider: StateStorageService, | |
148 | useClass: null | |
149 | }, | |
150 | JhiEventManager, | |
151 | ] | |
152 | }).overrideTemplate(LoginFcaComponent, '') | |
153 | .compileComponents(); | |
154 | })); | |
155 | ||
156 | beforeEach(() => { | |
157 | fixture = TestBed.createComponent(LoginFcaComponent); | |
158 | comp = fixture.componentInstance; | |
159 | mockAuth = fixture.debugElement.injector.get(AccountService); | |
160 | mockPrincipal = fixture.debugElement.injector.get(Principal); | |
161 | mockLoginService = fixture.debugElement.injector.get(LoginService); | |
162 | }); | |
163 | ||
164 | it('should set set success to OK upon successful activation', | |
165 | inject([LoginService], | |
166 | fakeAsync((service: LoginService) => { | |
167 | spyOn(service, 'login').and.returnValue(Observable.of({})); | |
168 | ||
169 | mockLoginService.setResponse({username: 'rildomar', password: 'rildomar'}); | |
170 | ||
171 | comp.login(); | |
172 | tick(); | |
173 | ||
174 | expect(true).toEqual(false); | |
175 | }) | |
176 | ) | |
177 | ); | |
178 | }); | |
179 | }); |