Skip to content

Commit 1f775f6

Browse files
authored
feat(auth): support in-memory backend delegate (#3181)
1 parent 1fa2825 commit 1f775f6

11 files changed

+101
-40
lines changed

libs/auth/driver/in-memory/src/backend/auth.service.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
STATUS,
66
} from 'angular-in-memory-web-api';
77

8+
import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';
9+
10+
import { DAFF_AUTH_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';
811
import { DaffInMemoryDbCustomer } from '../models/db-customer.type';
912

1013
/**
@@ -13,7 +16,9 @@ import { DaffInMemoryDbCustomer } from '../models/db-customer.type';
1316
@Injectable({
1417
providedIn: 'root',
1518
})
16-
export class DaffInMemoryBackendAuthService implements InMemoryDbService {
19+
export class DaffInMemoryBackendAuthService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
20+
readonly collectionName = DAFF_AUTH_IN_MEMORY_COLLECTION_NAME;
21+
1722
customers: Record<DaffInMemoryDbCustomer['email'], DaffInMemoryDbCustomer> = {};
1823

1924
private generateToken(): string {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DAFF_AUTH_IN_MEMORY_COLLECTION_NAME = 'auth';

libs/auth/driver/in-memory/src/drivers/auth-driver.module.ts

+5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import {
1010
DaffAuthDriver,
1111
DaffResetPasswordDriver,
1212
} from '@daffodil/auth/driver';
13+
import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory';
1314

1415
import { DaffInMemoryAuthService } from './auth/auth.service';
1516
import { DaffInMemoryLoginService } from './login/login.service';
1617
import { DaffInMemoryRegisterService } from './register/register.service';
1718
import { DaffInMemoryResetPasswordService } from './reset-password/service';
19+
import { DaffInMemoryBackendAuthService } from '../backend/auth.service';
1820

1921
@NgModule({
2022
imports: [
@@ -42,6 +44,9 @@ export class DaffAuthInMemoryDriverModule {
4244
provide: DaffResetPasswordDriver,
4345
useExisting: DaffInMemoryResetPasswordService,
4446
},
47+
provideDaffInMemoryBackends(
48+
DaffInMemoryBackendAuthService,
49+
),
4550
],
4651
};
4752
}

libs/auth/driver/in-memory/src/drivers/auth/auth.service.spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
provideHttpClientTesting,
88
} from '@angular/common/http/testing';
99
import { TestBed } from '@angular/core/testing';
10+
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
1011

1112
import { DaffAuthToken } from '@daffodil/auth';
1213
import { DaffAuthTokenFactory } from '@daffodil/auth/testing';
@@ -26,6 +27,12 @@ describe('@daffodil/auth/driver/in-memory | AuthService', () => {
2627
imports: [],
2728
providers: [
2829
DaffInMemoryAuthService,
30+
{
31+
provide: InMemoryBackendConfig,
32+
useValue: {
33+
apiBase: 'api',
34+
},
35+
},
2936
provideHttpClient(withInterceptorsFromDi()),
3037
provideHttpClientTesting(),
3138
],
@@ -52,7 +59,7 @@ describe('@daffodil/auth/driver/in-memory | AuthService', () => {
5259
done();
5360
});
5461

55-
const req = httpMock.expectOne(`${service.url}check`);
62+
const req = httpMock.expectOne(`${service['url']}/check`);
5663
expect(req.request.method).toBe('POST');
5764

5865
req.flush({});

libs/auth/driver/in-memory/src/drivers/auth/auth.service.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
34
import { Observable } from 'rxjs';
45
import { map } from 'rxjs/operators';
56

67
import { DaffAuthServiceInterface } from '@daffodil/auth/driver';
8+
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';
9+
10+
import { DAFF_AUTH_IN_MEMORY_COLLECTION_NAME } from '../../collection-name.const';
711

812
/**
913
* @inheritdoc
1014
*/
1115
@Injectable({
1216
providedIn: 'root',
1317
})
14-
export class DaffInMemoryAuthService implements DaffAuthServiceInterface {
15-
url = '/api/auth/';
16-
17-
constructor(private http: HttpClient) {}
18+
export class DaffInMemoryAuthService extends DaffInMemoryDriverBase implements DaffAuthServiceInterface {
19+
constructor(
20+
private http: HttpClient,
21+
config: InMemoryBackendConfig,
22+
) {
23+
super(config, DAFF_AUTH_IN_MEMORY_COLLECTION_NAME);
24+
}
1825

1926
check(): Observable<void> {
20-
return this.http.post(`${this.url}check`, {}).pipe(
27+
return this.http.post(`${this.url}/check`, {}).pipe(
2128
map(() => undefined),
2229
);
2330
}

libs/auth/driver/in-memory/src/drivers/login/login.service.spec.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
provideHttpClientTesting,
88
} from '@angular/common/http/testing';
99
import { TestBed } from '@angular/core/testing';
10+
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
1011

1112
import {
1213
DaffAuthToken,
@@ -20,7 +21,7 @@ import {
2021
import { DaffInMemoryLoginService } from './login.service';
2122

2223
describe('@daffodil/auth/driver/in-memory | LoginService', () => {
23-
let loginService;
24+
let service;
2425
let httpMock: HttpTestingController;
2526

2627
let registrationFactory: DaffAccountRegistrationFactory;
@@ -37,13 +38,19 @@ describe('@daffodil/auth/driver/in-memory | LoginService', () => {
3738
imports: [],
3839
providers: [
3940
DaffInMemoryLoginService,
41+
{
42+
provide: InMemoryBackendConfig,
43+
useValue: {
44+
apiBase: 'api',
45+
},
46+
},
4047
provideHttpClient(withInterceptorsFromDi()),
4148
provideHttpClientTesting(),
4249
],
4350
});
4451

4552
httpMock = TestBed.inject(HttpTestingController);
46-
loginService = TestBed.inject(DaffInMemoryLoginService);
53+
service = TestBed.inject(DaffInMemoryLoginService);
4754
registrationFactory = TestBed.inject(DaffAccountRegistrationFactory);
4855

4956
mockRegistration = registrationFactory.create();
@@ -55,7 +62,7 @@ describe('@daffodil/auth/driver/in-memory | LoginService', () => {
5562
});
5663

5764
it('should be created', () => {
58-
expect(loginService).toBeTruthy();
65+
expect(service).toBeTruthy();
5966
});
6067

6168
describe('login | getting a token', () => {
@@ -64,12 +71,12 @@ describe('@daffodil/auth/driver/in-memory | LoginService', () => {
6471
});
6572

6673
it('should send a post request and return an AuthToken', done => {
67-
loginService.login({ email, password }).subscribe(auth => {
74+
service.login({ email, password }).subscribe(auth => {
6875
expect(auth).toEqual(mockAuth);
6976
done();
7077
});
7178

72-
const req = httpMock.expectOne(`${loginService.url}login`);
79+
const req = httpMock.expectOne(`${service.url}/login`);
7380
expect(req.request.method).toBe('POST');
7481
expect(req.request.body).toEqual({ email, password });
7582

@@ -83,12 +90,12 @@ describe('@daffodil/auth/driver/in-memory | LoginService', () => {
8390
});
8491

8592
it('should send a post request and return an empty Observable', done => {
86-
loginService.logout().subscribe(resp => {
93+
service.logout().subscribe(resp => {
8794
expect(resp).toBeUndefined();
8895
done();
8996
});
9097

91-
const req = httpMock.expectOne(`${loginService.url}logout`);
98+
const req = httpMock.expectOne(`${service.url}/logout`);
9299
expect(req.request.method).toBe('POST');
93100

94101
req.flush({ success: true });

libs/auth/driver/in-memory/src/drivers/login/login.service.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
34
import {
45
Observable,
56
of,
@@ -12,24 +13,30 @@ import {
1213
DaffAuthToken,
1314
} from '@daffodil/auth';
1415
import { DaffLoginServiceInterface } from '@daffodil/auth/driver';
16+
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';
17+
18+
import { DAFF_AUTH_IN_MEMORY_COLLECTION_NAME } from '../../collection-name.const';
1519

1620
/**
1721
* @inheritdoc
1822
*/
1923
@Injectable({
2024
providedIn: 'root',
2125
})
22-
export class DaffInMemoryLoginService implements DaffLoginServiceInterface {
23-
url = '/api/auth/';
24-
25-
constructor(private http: HttpClient) {}
26+
export class DaffInMemoryLoginService extends DaffInMemoryDriverBase implements DaffLoginServiceInterface {
27+
constructor(
28+
private http: HttpClient,
29+
config: InMemoryBackendConfig,
30+
) {
31+
super(config, DAFF_AUTH_IN_MEMORY_COLLECTION_NAME);
32+
}
2633

2734
login(request: DaffLoginInfo): Observable<DaffAuthToken> {
28-
return this.http.post<DaffAuthToken>(`${this.url}login`, request);
35+
return this.http.post<DaffAuthToken>(`${this.url}/login`, request);
2936
}
3037

3138
logout(): Observable<void> {
32-
return this.http.post<{success: boolean}>(`${this.url}logout`, {}).pipe(
39+
return this.http.post<{success: boolean}>(`${this.url}/logout`, {}).pipe(
3340
switchMap(({ success }) => success ? of(undefined) : throwError(() => new Error('Logout failed'))),
3441
);
3542
}

libs/auth/driver/in-memory/src/drivers/register/register.service.spec.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
provideHttpClientTesting,
88
} from '@angular/common/http/testing';
99
import { TestBed } from '@angular/core/testing';
10+
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
1011
import { of } from 'rxjs';
1112

1213
import {
@@ -42,6 +43,12 @@ describe('@daffodil/auth/driver/in-memory | DaffInMemoryRegisterService', () =>
4243
provide: DaffInMemoryLoginService,
4344
useValue: loginServiceSpy,
4445
},
46+
{
47+
provide: InMemoryBackendConfig,
48+
useValue: {
49+
apiBase: 'api',
50+
},
51+
},
4552
provideHttpClient(withInterceptorsFromDi()),
4653
provideHttpClientTesting(),
4754
],
@@ -78,7 +85,7 @@ describe('@daffodil/auth/driver/in-memory | DaffInMemoryRegisterService', () =>
7885
it('should send a post request', () => {
7986
service.register(mockRegistration).subscribe(auth => {});
8087

81-
const req = httpMock.expectOne(`${service.url}register`);
88+
const req = httpMock.expectOne(`${service['url']}/register`);
8289

8390
expect(req.request.method).toBe('POST');
8491
expect(req.request.body).toEqual(mockRegistration);
@@ -92,7 +99,7 @@ describe('@daffodil/auth/driver/in-memory | DaffInMemoryRegisterService', () =>
9299
done();
93100
});
94101

95-
const req = httpMock.expectOne(`${service.url}register`);
102+
const req = httpMock.expectOne(`${service['url']}/register`);
96103

97104
req.flush({});
98105
});
@@ -106,7 +113,7 @@ describe('@daffodil/auth/driver/in-memory | DaffInMemoryRegisterService', () =>
106113
it('should send a post request', () => {
107114
service.registerOnly(mockRegistration).subscribe(auth => {});
108115

109-
const req = httpMock.expectOne(`${service.url}register`);
116+
const req = httpMock.expectOne(`${service['url']}/register`);
110117

111118
expect(req.request.method).toBe('POST');
112119
expect(req.request.body).toEqual(mockRegistration);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
34
import { Observable } from 'rxjs';
45
import {
56
map,
@@ -8,7 +9,9 @@ import {
89

910
import { DaffAccountRegistration } from '@daffodil/auth';
1011
import { DaffRegisterServiceInterface } from '@daffodil/auth/driver';
12+
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';
1113

14+
import { DAFF_AUTH_IN_MEMORY_COLLECTION_NAME } from '../../collection-name.const';
1215
import { DaffInMemoryLoginService } from '../login/login.service';
1316

1417
/**
@@ -17,13 +20,14 @@ import { DaffInMemoryLoginService } from '../login/login.service';
1720
@Injectable({
1821
providedIn: 'root',
1922
})
20-
export class DaffInMemoryRegisterService implements DaffRegisterServiceInterface {
21-
url = '/api/auth/';
22-
23+
export class DaffInMemoryRegisterService extends DaffInMemoryDriverBase implements DaffRegisterServiceInterface {
2324
constructor(
2425
private http: HttpClient,
2526
private loginService: DaffInMemoryLoginService,
26-
) {}
27+
config: InMemoryBackendConfig,
28+
) {
29+
super(config, DAFF_AUTH_IN_MEMORY_COLLECTION_NAME);
30+
}
2731

2832
register(registration: DaffAccountRegistration): Observable<string> {
2933
return this.registerOnly(registration).pipe(
@@ -33,6 +37,6 @@ export class DaffInMemoryRegisterService implements DaffRegisterServiceInterface
3337
}
3438

3539
registerOnly(registration: DaffAccountRegistration): Observable<void> {
36-
return this.http.post<void>(`${this.url}register`, registration);
40+
return this.http.post<void>(`${this.url}/register`, registration);
3741
}
3842
}

0 commit comments

Comments
 (0)