Skip to content

Commit

Permalink
feat(auth): support in-memory backend delegate (#3181)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 10, 2024
1 parent 1fa2825 commit 1f775f6
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 40 deletions.
7 changes: 6 additions & 1 deletion libs/auth/driver/in-memory/src/backend/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
STATUS,
} from 'angular-in-memory-web-api';

import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';

import { DAFF_AUTH_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';
import { DaffInMemoryDbCustomer } from '../models/db-customer.type';

/**
Expand All @@ -13,7 +16,9 @@ import { DaffInMemoryDbCustomer } from '../models/db-customer.type';
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryBackendAuthService implements InMemoryDbService {
export class DaffInMemoryBackendAuthService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_AUTH_IN_MEMORY_COLLECTION_NAME;

customers: Record<DaffInMemoryDbCustomer['email'], DaffInMemoryDbCustomer> = {};

private generateToken(): string {
Expand Down
1 change: 1 addition & 0 deletions libs/auth/driver/in-memory/src/collection-name.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_AUTH_IN_MEMORY_COLLECTION_NAME = 'auth';
5 changes: 5 additions & 0 deletions libs/auth/driver/in-memory/src/drivers/auth-driver.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
DaffAuthDriver,
DaffResetPasswordDriver,
} from '@daffodil/auth/driver';
import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory';

import { DaffInMemoryAuthService } from './auth/auth.service';
import { DaffInMemoryLoginService } from './login/login.service';
import { DaffInMemoryRegisterService } from './register/register.service';
import { DaffInMemoryResetPasswordService } from './reset-password/service';
import { DaffInMemoryBackendAuthService } from '../backend/auth.service';

@NgModule({
imports: [
Expand Down Expand Up @@ -42,6 +44,9 @@ export class DaffAuthInMemoryDriverModule {
provide: DaffResetPasswordDriver,
useExisting: DaffInMemoryResetPasswordService,
},
provideDaffInMemoryBackends(
DaffInMemoryBackendAuthService,
),
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';

import { DaffAuthToken } from '@daffodil/auth';
import { DaffAuthTokenFactory } from '@daffodil/auth/testing';
Expand All @@ -26,6 +27,12 @@ describe('@daffodil/auth/driver/in-memory | AuthService', () => {
imports: [],
providers: [
DaffInMemoryAuthService,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
Expand All @@ -52,7 +59,7 @@ describe('@daffodil/auth/driver/in-memory | AuthService', () => {
done();
});

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

req.flush({});
Expand Down
17 changes: 12 additions & 5 deletions libs/auth/driver/in-memory/src/drivers/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { DaffAuthServiceInterface } from '@daffodil/auth/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

import { DAFF_AUTH_IN_MEMORY_COLLECTION_NAME } from '../../collection-name.const';

/**
* @inheritdoc
*/
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryAuthService implements DaffAuthServiceInterface {
url = '/api/auth/';

constructor(private http: HttpClient) {}
export class DaffInMemoryAuthService extends DaffInMemoryDriverBase implements DaffAuthServiceInterface {
constructor(
private http: HttpClient,
config: InMemoryBackendConfig,
) {
super(config, DAFF_AUTH_IN_MEMORY_COLLECTION_NAME);
}

check(): Observable<void> {
return this.http.post(`${this.url}check`, {}).pipe(
return this.http.post(`${this.url}/check`, {}).pipe(
map(() => undefined),
);
}
Expand Down
21 changes: 14 additions & 7 deletions libs/auth/driver/in-memory/src/drivers/login/login.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';

import {
DaffAuthToken,
Expand All @@ -20,7 +21,7 @@ import {
import { DaffInMemoryLoginService } from './login.service';

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

let registrationFactory: DaffAccountRegistrationFactory;
Expand All @@ -37,13 +38,19 @@ describe('@daffodil/auth/driver/in-memory | LoginService', () => {
imports: [],
providers: [
DaffInMemoryLoginService,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
});

httpMock = TestBed.inject(HttpTestingController);
loginService = TestBed.inject(DaffInMemoryLoginService);
service = TestBed.inject(DaffInMemoryLoginService);
registrationFactory = TestBed.inject(DaffAccountRegistrationFactory);

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

it('should be created', () => {
expect(loginService).toBeTruthy();
expect(service).toBeTruthy();
});

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

it('should send a post request and return an AuthToken', done => {
loginService.login({ email, password }).subscribe(auth => {
service.login({ email, password }).subscribe(auth => {
expect(auth).toEqual(mockAuth);
done();
});

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

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

it('should send a post request and return an empty Observable', done => {
loginService.logout().subscribe(resp => {
service.logout().subscribe(resp => {
expect(resp).toBeUndefined();
done();
});

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

req.flush({ success: true });
Expand Down
19 changes: 13 additions & 6 deletions libs/auth/driver/in-memory/src/drivers/login/login.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import {
Observable,
of,
Expand All @@ -12,24 +13,30 @@ import {
DaffAuthToken,
} from '@daffodil/auth';
import { DaffLoginServiceInterface } from '@daffodil/auth/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

import { DAFF_AUTH_IN_MEMORY_COLLECTION_NAME } from '../../collection-name.const';

/**
* @inheritdoc
*/
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryLoginService implements DaffLoginServiceInterface {
url = '/api/auth/';

constructor(private http: HttpClient) {}
export class DaffInMemoryLoginService extends DaffInMemoryDriverBase implements DaffLoginServiceInterface {
constructor(
private http: HttpClient,
config: InMemoryBackendConfig,
) {
super(config, DAFF_AUTH_IN_MEMORY_COLLECTION_NAME);
}

login(request: DaffLoginInfo): Observable<DaffAuthToken> {
return this.http.post<DaffAuthToken>(`${this.url}login`, request);
return this.http.post<DaffAuthToken>(`${this.url}/login`, request);
}

logout(): Observable<void> {
return this.http.post<{success: boolean}>(`${this.url}logout`, {}).pipe(
return this.http.post<{success: boolean}>(`${this.url}/logout`, {}).pipe(
switchMap(({ success }) => success ? of(undefined) : throwError(() => new Error('Logout failed'))),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { of } from 'rxjs';

import {
Expand Down Expand Up @@ -42,6 +43,12 @@ describe('@daffodil/auth/driver/in-memory | DaffInMemoryRegisterService', () =>
provide: DaffInMemoryLoginService,
useValue: loginServiceSpy,
},
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
Expand Down Expand Up @@ -78,7 +85,7 @@ describe('@daffodil/auth/driver/in-memory | DaffInMemoryRegisterService', () =>
it('should send a post request', () => {
service.register(mockRegistration).subscribe(auth => {});

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

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

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

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

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

expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(mockRegistration);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';
import {
map,
Expand All @@ -8,7 +9,9 @@ import {

import { DaffAccountRegistration } from '@daffodil/auth';
import { DaffRegisterServiceInterface } from '@daffodil/auth/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

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

/**
Expand All @@ -17,13 +20,14 @@ import { DaffInMemoryLoginService } from '../login/login.service';
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryRegisterService implements DaffRegisterServiceInterface {
url = '/api/auth/';

export class DaffInMemoryRegisterService extends DaffInMemoryDriverBase implements DaffRegisterServiceInterface {
constructor(
private http: HttpClient,
private loginService: DaffInMemoryLoginService,
) {}
config: InMemoryBackendConfig,
) {
super(config, DAFF_AUTH_IN_MEMORY_COLLECTION_NAME);
}

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

registerOnly(registration: DaffAccountRegistration): Observable<void> {
return this.http.post<void>(`${this.url}register`, registration);
return this.http.post<void>(`${this.url}/register`, registration);
}
}
Loading

0 comments on commit 1f775f6

Please sign in to comment.