Advertisement
Guest User

LoginSpec

a guest
Nov 24th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ########
  2. login-fca.component.ts
  3. ########
  4. export class LoginFcaComponent implements OnInit {
  5.     account: Account;
  6.     modalRef: NgbModalRef;
  7.  
  8.     authenticationError: boolean;
  9.     password: string;
  10.     rememberMe: boolean;
  11.     username: string;
  12.     credentials: any;
  13.  
  14.     constructor(
  15.         private principal: Principal,
  16.         private loginService: LoginService,
  17.         private eventManager: JhiEventManager,
  18.         private router: Router,
  19.         private stateStorageService: StateStorageService
  20.     ) {
  21.         this.credentials = {};
  22.     }
  23.  
  24.     ngOnInit() {
  25.         this.principal.identity().then((account) => {
  26.             this.account = account;
  27.         });
  28.         this.registerAuthenticationSuccess();
  29.     }
  30.  
  31.     registerAuthenticationSuccess() {
  32.         this.eventManager.subscribe('authenticationSuccess', (message) => {
  33.             this.principal.identity().then((account) => {
  34.                 this.account = account;
  35.             });
  36.         });
  37.     }
  38.  
  39.     isAuthenticated() {
  40.         return this.principal.isAuthenticated();
  41.     }
  42.  
  43.     login() {
  44.             this.loginService.login({
  45.                 username: this.username,
  46.                 password: this.password,
  47.                 rememberMe: this.rememberMe
  48.             }).then(() => {
  49.                 this.authenticationError = false;
  50.                 if (this.router.url === '/register' || (/^\/activate\//.test(this.router.url)) ||
  51.                     (/^\/reset\//.test(this.router.url))) {
  52.                     this.router.navigate(['']);
  53.                 }
  54.  
  55.                 this.eventManager.broadcast({
  56.                     name: 'authenticationSuccess',
  57.                     content: 'Sending Authentication Success'
  58.                 });
  59.  
  60.                 // // previousState was set in the authExpiredInterceptor before being redirected to login modal.
  61.                 // // since login is succesful, go to stored previousState and clear previousState
  62.                 const redirect = this.stateStorageService.getUrl();
  63.                 if (redirect) {
  64.                     this.stateStorageService.storeUrl(null);
  65.                     this.router.navigate([redirect]);
  66.                 }
  67.                 this.router.navigate(['dashboard']);
  68.             }).catch(() => {
  69.                 this.authenticationError = true;
  70.             });
  71.     }
  72. }
  73.  
  74. ########
  75. login-fca.component.spec.ts
  76. ########
  77. describe('Login Tests', () => {
  78.  
  79.     describe('LoginFcaComponent', () => {
  80.  
  81.         let comp: LoginFcaComponent;
  82.         let fixture: ComponentFixture<LoginFcaComponent>;
  83.         let mockAuth: any;
  84.         let mockPrincipal: any;
  85.  
  86.         beforeEach(async(() => {
  87.             TestBed.configureTestingModule({
  88.                 imports: [FcaSystemIntegrationToolTestModule],
  89.                 declarations: [LoginFcaComponent],
  90.                 providers: [
  91.                     {
  92.                         provider: Principal,
  93.                         useClass: MockPrincipal
  94.                     },
  95.                     {
  96.                         provider: AccountService,
  97.                         useClass: MockAccountService
  98.                     },
  99.                     {
  100.                         provider: LoginService,
  101.                         useClass: null
  102.                     },
  103.                     {
  104.                         provider: Router,
  105.                         useClass: MockActivatedRoute
  106.                     },
  107.                     JhiEventManager
  108.                 ]
  109.             }).overrideTemplate(LoginFcaComponent, '')
  110.             .compileComponents();
  111.         }));
  112.  
  113.         beforeEach(() => {
  114.             fixture = TestBed.createComponent(LoginFcaComponent);
  115.             comp = fixture.componentInstance;
  116.             mockAuth = fixture.debugElement.injector.get(AccountService);
  117.             mockPrincipal = fixture.debugElement.injector.get(Principal);
  118.         });
  119.  
  120.         it('should ensure the two passwords entered match', () => {
  121.             comp.login();
  122.             expect(true).toEqual(true);
  123.         });
  124.     });
  125. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement