Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############
- login-fca.component.ts
- ############
- import { Component, OnInit } from '@angular/core';
- import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
- import { JhiEventManager } from 'ng-jhipster';
- import { Router } from '@angular/router';
- import { Account, LoginModalService, Principal } from '../shared';
- import {LoginService} from '../shared/login/login.service';
- import {StateStorageService} from '../shared/auth/state-storage.service';
- @Component({
- selector: 'jhi-home',
- templateUrl: './login-fca.component.html',
- styleUrls: [
- 'login-fca.css'
- ]
- })
- export class LoginFcaComponent implements OnInit {
- account: Account;
- modalRef: NgbModalRef;
- authenticationError: boolean;
- password: string;
- rememberMe: boolean;
- username: string;
- credentials: any;
- constructor(
- private principal: Principal,
- private loginService: LoginService,
- private eventManager: JhiEventManager,
- private router: Router,
- private stateStorageService: StateStorageService
- ) {
- this.credentials = {};
- }
- ngOnInit() {
- this.principal.identity().then((account) => {
- this.account = account;
- });
- this.registerAuthenticationSuccess();
- }
- registerAuthenticationSuccess() {
- this.eventManager.subscribe('authenticationSuccess', (message) => {
- this.principal.identity().then((account) => {
- this.account = account;
- });
- });
- }
- isAuthenticated() {
- return this.principal.isAuthenticated();
- }
- login() {
- this.loginService.login({
- username: this.username,
- password: this.password,
- rememberMe: this.rememberMe
- }).then(() => {
- this.authenticationError = false;
- if (this.router.url === '/register' || (/^\/activate\//.test(this.router.url)) ||
- (/^\/reset\//.test(this.router.url))) {
- this.router.navigate(['']);
- }
- this.eventManager.broadcast({
- name: 'authenticationSuccess',
- content: 'Sending Authentication Success'
- });
- // // previousState was set in the authExpiredInterceptor before being redirected to login modal.
- // // since login is succesful, go to stored previousState and clear previousState
- const redirect = this.stateStorageService.getUrl();
- if (redirect) {
- this.stateStorageService.storeUrl(null);
- this.router.navigate([redirect]);
- }
- this.router.navigate(['dashboard']);
- }).catch(() => {
- this.authenticationError = true;
- });
- }
- }
- ############
- login.component.spec.ts
- ############
- import {ComponentFixture, TestBed, async, inject, tick} from '@angular/core/testing';
- import { FcaSystemIntegrationToolTestModule } from '../../../test.module';
- import { Principal, AccountService } from '../../../../../../main/webapp/app/shared';
- import { MockAccountService } from '../../../helpers/mock-account.service';
- import { MockPrincipal } from '../../../helpers/mock-principal.service';
- import {LoginFcaComponent} from '../../../../../../main/webapp/app/loginFca/login-fca.component';
- import {LoginService} from '../../../../../../main/webapp/app/shared/login/login.service';
- import {JhiEventManager} from 'ng-jhipster';
- import {Router} from '@angular/router';
- import {MockActivatedRoute} from '../../../helpers/mock-route.service';
- import {MockLoginService} from '../../../helpers/mock-login.service';
- import {fakeAsync} from '@angular/core/testing';
- import {Observable} from 'rxjs/Observable';
- import {StateStorageService} from '../../../../../../main/webapp/app/shared/auth/state-storage.service';
- describe('Login Tests', () => {
- describe('LoginFcaComponent', () => {
- let comp: LoginFcaComponent;
- let fixture: ComponentFixture<LoginFcaComponent>;
- let mockAuth: any;
- let mockPrincipal: any;
- let mockLoginService: any;
- beforeEach(async(() => {
- TestBed.configureTestingModule({
- imports: [FcaSystemIntegrationToolTestModule],
- declarations: [LoginFcaComponent],
- providers: [
- {
- provider: Principal,
- useClass: MockPrincipal
- },
- {
- provider: AccountService,
- useClass: MockAccountService
- },
- {
- provider: LoginService,
- useClass: MockLoginService
- },
- {
- provider: Router,
- useClass: MockActivatedRoute
- },
- {
- provider: StateStorageService,
- useClass: null
- },
- JhiEventManager,
- ]
- }).overrideTemplate(LoginFcaComponent, '')
- .compileComponents();
- }));
- beforeEach(() => {
- fixture = TestBed.createComponent(LoginFcaComponent);
- comp = fixture.componentInstance;
- mockAuth = fixture.debugElement.injector.get(AccountService);
- mockPrincipal = fixture.debugElement.injector.get(Principal);
- mockLoginService = fixture.debugElement.injector.get(LoginService);
- });
- it('should set set success to OK upon successful activation',
- inject([LoginService],
- fakeAsync((service: LoginService) => {
- spyOn(service, 'login').and.returnValue(Observable.of({}));
- mockLoginService.setResponse({username: 'rildomar', password: 'rildomar'});
- comp.login();
- tick();
- expect(true).toEqual(false);
- })
- )
- );
- });
- });
Add Comment
Please, Sign In to add comment