test: update mock HTTP response in login component tests
testing/ogGui-multibranch/pipeline/head This commit looks good
Details
testing/ogGui-multibranch/pipeline/head This commit looks good
Details
parent
20452c83eb
commit
7d2c0e4c29
|
@ -56,7 +56,7 @@ describe('LoginComponent', () => {
|
|||
|
||||
it('should call onLogin and navigate on successful login', () => {
|
||||
const mockRouter = spyOn(component['router'], 'navigateByUrl');
|
||||
const mockHttp = spyOn(component['http'], 'post').and.returnValue(of({ token: '123', refreshToken: '456' }));
|
||||
const mockHttp = spyOn(component['http'], 'post').and.returnValue(of({ token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', refreshToken: '456' }));
|
||||
|
||||
component.loginObj.username = 'testUser';
|
||||
component.loginObj.password = 'testPass';
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { HttpClient } from '@angular/common/http';
|
||||
import {Component, signal} from '@angular/core';
|
||||
import { Component, signal } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import {ToastrService} from "ngx-toastr";
|
||||
import {jwtDecode} from "jwt-decode";
|
||||
import { ToastrService } from "ngx-toastr";
|
||||
import { jwtDecode } from "jwt-decode";
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
|
@ -11,69 +11,69 @@ import {jwtDecode} from "jwt-decode";
|
|||
styleUrls: ['./login.component.css'],
|
||||
})
|
||||
export class LoginComponent {
|
||||
loginObj: any = {
|
||||
"username": "",
|
||||
"password": ""
|
||||
};
|
||||
errorMessage: string = '';
|
||||
isLoading: boolean = false;
|
||||
decodedToken: any;
|
||||
loginObj: any = {
|
||||
"username": "",
|
||||
"password": ""
|
||||
};
|
||||
errorMessage: string = '';
|
||||
isLoading: boolean = false;
|
||||
decodedToken: any;
|
||||
|
||||
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
|
||||
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
|
||||
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
private router: Router,
|
||||
private toastService: ToastrService,
|
||||
private translateService: TranslateService
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
private router: Router,
|
||||
private toastService: ToastrService,
|
||||
private translateService: TranslateService
|
||||
) {
|
||||
const savedLanguage = localStorage.getItem('language') || 'es';
|
||||
this.translateService.use(savedLanguage);
|
||||
}
|
||||
|
||||
onLogin() {
|
||||
this.errorMessage = '';
|
||||
this.isLoading = true;
|
||||
onLogin() {
|
||||
this.errorMessage = '';
|
||||
this.isLoading = true;
|
||||
|
||||
if (!this.loginObj.username || !this.loginObj.password) {
|
||||
if (!this.loginObj.username) {
|
||||
document.getElementById('username')?.classList.add('invalid');
|
||||
} else {
|
||||
document.getElementById('username')?.classList.remove('invalid');
|
||||
}
|
||||
if (!this.loginObj.username || !this.loginObj.password) {
|
||||
if (!this.loginObj.username) {
|
||||
document.getElementById('username')?.classList.add('invalid');
|
||||
} else {
|
||||
document.getElementById('username')?.classList.remove('invalid');
|
||||
}
|
||||
|
||||
if (!this.loginObj.password) {
|
||||
document.getElementById('password')?.classList.add('invalid');
|
||||
} else {
|
||||
document.getElementById('password')?.classList.remove('invalid');
|
||||
}
|
||||
if (!this.loginObj.password) {
|
||||
document.getElementById('password')?.classList.add('invalid');
|
||||
} else {
|
||||
document.getElementById('password')?.classList.remove('invalid');
|
||||
}
|
||||
|
||||
this.isLoading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.http.post(`${this.baseUrl}/auth/login`, this.loginObj).subscribe({
|
||||
next: (res: any) => {
|
||||
if (res.token) {
|
||||
localStorage.setItem('loginToken', res.token);
|
||||
localStorage.setItem('refreshToken', res.refreshToken);
|
||||
localStorage.setItem('username', this.loginObj.username);
|
||||
|
||||
this.decodedToken = jwtDecode(res.token);
|
||||
localStorage.setItem('groupsView', this.decodedToken.groupsView);
|
||||
|
||||
this.openSnackBar(false, 'Bienvenido ' + this.loginObj.username);
|
||||
this.router.navigateByUrl('/groups');
|
||||
}
|
||||
this.isLoading = false;
|
||||
},
|
||||
error: (err) => {
|
||||
this.isLoading = false;
|
||||
this.openSnackBar(true, 'Error al iniciar sesión: ' + err.error.message);
|
||||
}
|
||||
});
|
||||
this.isLoading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.http.post(`${this.baseUrl}/auth/login`, this.loginObj).subscribe({
|
||||
next: (res: any) => {
|
||||
if (res.token) {
|
||||
localStorage.setItem('loginToken', res.token);
|
||||
localStorage.setItem('refreshToken', res.refreshToken);
|
||||
localStorage.setItem('username', this.loginObj.username);
|
||||
|
||||
this.decodedToken = jwtDecode(res.token);
|
||||
localStorage.setItem('groupsView', this.decodedToken.groupsView);
|
||||
|
||||
this.openSnackBar(false, 'Bienvenido ' + this.loginObj.username);
|
||||
this.router.navigateByUrl('/groups');
|
||||
}
|
||||
this.isLoading = false;
|
||||
},
|
||||
error: (err) => {
|
||||
this.isLoading = false;
|
||||
this.openSnackBar(true, 'Error al iniciar sesión: ' + err.error.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
hide = signal(true);
|
||||
clickEvent(event: Event) {
|
||||
event.stopPropagation();
|
||||
|
|
Loading…
Reference in New Issue