fix: remove OAuthService from ApartmentService - cookies handle auth in BFF pattern
This commit is contained in:
1 parent
bbe8916e82
commit
be80422c99
2 files changed
+12
-27
No files matched your search
@@ -1,6 +1,5 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { OAuthService } from 'angular-oauth2-oidc';
|
||||
import { ApartmentService, Apartment } from './apartment.service';
|
||||
import { of } from 'rxjs';
|
||||
import { vi } from 'vitest';
|
||||
@@ -8,7 +7,6 @@ import { vi } from 'vitest';
|
||||
describe('ApartmentService', () => {
|
||||
let service: ApartmentService;
|
||||
let mockHttpClient: any;
|
||||
let mockOAuthService: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockHttpClient = {
|
||||
@@ -18,15 +16,10 @@ describe('ApartmentService', () => {
|
||||
delete: vi.fn().mockReturnValue(of({}))
|
||||
};
|
||||
|
||||
mockOAuthService = {
|
||||
getAccessToken: () => 'mock-token'
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
ApartmentService,
|
||||
{ provide: HttpClient, useValue: mockHttpClient },
|
||||
{ provide: OAuthService, useValue: mockOAuthService }
|
||||
{ provide: HttpClient, useValue: mockHttpClient }
|
||||
]
|
||||
});
|
||||
|
||||
@@ -46,7 +39,7 @@ describe('ApartmentService', () => {
|
||||
service.getApartments().subscribe(data => {
|
||||
expect(data).toEqual(mockApts);
|
||||
});
|
||||
expect(mockHttpClient.get).toHaveBeenCalledWith('/api/apartments', expect.any(Object));
|
||||
expect(mockHttpClient.get).toHaveBeenCalledWith('/api/apartments');
|
||||
});
|
||||
|
||||
it('should call GET /api/apartments/:id', () => {
|
||||
@@ -56,7 +49,7 @@ describe('ApartmentService', () => {
|
||||
service.getApartment('1').subscribe(data => {
|
||||
expect(data).toEqual(mockApt);
|
||||
});
|
||||
expect(mockHttpClient.get).toHaveBeenCalledWith('/api/apartments/1', expect.any(Object));
|
||||
expect(mockHttpClient.get).toHaveBeenCalledWith('/api/apartments/1');
|
||||
});
|
||||
|
||||
it('should call POST /api/apartments', () => {
|
||||
@@ -66,7 +59,7 @@ describe('ApartmentService', () => {
|
||||
service.createApartment(mockApt).subscribe(data => {
|
||||
expect(data.id).toBe('2');
|
||||
});
|
||||
expect(mockHttpClient.post).toHaveBeenCalledWith('/api/apartments', mockApt, expect.any(Object));
|
||||
expect(mockHttpClient.post).toHaveBeenCalledWith('/api/apartments', mockApt);
|
||||
});
|
||||
|
||||
it('should call PUT /api/apartments/:id', () => {
|
||||
@@ -76,11 +69,11 @@ describe('ApartmentService', () => {
|
||||
service.updateApartment('1', mockApt).subscribe(data => {
|
||||
expect(data.name).toBe('Updated Apt');
|
||||
});
|
||||
expect(mockHttpClient.put).toHaveBeenCalledWith('/api/apartments/1', mockApt, expect.any(Object));
|
||||
expect(mockHttpClient.put).toHaveBeenCalledWith('/api/apartments/1', mockApt);
|
||||
});
|
||||
|
||||
it('should call DELETE /api/apartments/:id', () => {
|
||||
service.deleteApartment('1').subscribe();
|
||||
expect(mockHttpClient.delete).toHaveBeenCalledWith('/api/apartments/1', expect.any(Object));
|
||||
expect(mockHttpClient.delete).toHaveBeenCalledWith('/api/apartments/1');
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { OAuthService } from 'angular-oauth2-oidc';
|
||||
|
||||
export interface Apartment {
|
||||
id?: string;
|
||||
@@ -17,31 +16,24 @@ export interface Apartment {
|
||||
})
|
||||
export class ApartmentService {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly oauthService = inject(OAuthService);
|
||||
|
||||
private getHeaders(): HttpHeaders {
|
||||
return new HttpHeaders({
|
||||
'Authorization': 'Bearer ' + this.oauthService.getAccessToken()
|
||||
});
|
||||
}
|
||||
|
||||
getApartments(): Observable<Apartment[]> {
|
||||
return this.http.get<Apartment[]>('/api/apartments', { headers: this.getHeaders() });
|
||||
return this.http.get<Apartment[]>('/api/apartments');
|
||||
}
|
||||
|
||||
getApartment(id: string): Observable<Apartment> {
|
||||
return this.http.get<Apartment>(`/api/apartments/${id}`, { headers: this.getHeaders() });
|
||||
return this.http.get<Apartment>(`/api/apartments/${id}`);
|
||||
}
|
||||
|
||||
createApartment(apartment: Apartment): Observable<Apartment> {
|
||||
return this.http.post<Apartment>('/api/apartments', apartment, { headers: this.getHeaders() });
|
||||
return this.http.post<Apartment>('/api/apartments', apartment);
|
||||
}
|
||||
|
||||
updateApartment(id: string, apartment: Apartment): Observable<Apartment> {
|
||||
return this.http.put<Apartment>(`/api/apartments/${id}`, apartment, { headers: this.getHeaders() });
|
||||
return this.http.put<Apartment>(`/api/apartments/${id}`, apartment);
|
||||
}
|
||||
|
||||
deleteApartment(id: string): Observable<void> {
|
||||
return this.http.delete<void>(`/api/apartments/${id}`, { headers: this.getHeaders() });
|
||||
return this.http.delete<void>(`/api/apartments/${id}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user