Skip to content

Commit a573770

Browse files
author
Andrea Barbasso
committed
[DURACOM-309] add new orejime cookie for correlation id
1 parent d653c01 commit a573770

File tree

6 files changed

+85
-18
lines changed

6 files changed

+85
-18
lines changed

src/app/core/log/log.interceptor.spec.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import {
66
import { TestBed } from '@angular/core/testing';
77
import { Router } from '@angular/router';
88
import { StoreModule } from '@ngrx/store';
9+
import { of } from 'rxjs';
910

1011
import {
1112
appReducers,
1213
storeModuleConfig,
1314
} from '../../app.reducer';
1415
import { CorrelationIdService } from '../../correlation-id/correlation-id.service';
16+
import { OrejimeService } from '../../shared/cookies/orejime.service';
17+
import { CORRELATION_ID_OREJIME_KEY } from '../../shared/cookies/orejime-configuration';
1518
import { CookieServiceMock } from '../../shared/mocks/cookie.service.mock';
1619
import { RouterStub } from '../../shared/testing/router.stub';
1720
import { RestRequestMethod } from '../data/rest-request-method';
@@ -26,7 +29,7 @@ describe('LogInterceptor', () => {
2629
let httpMock: HttpTestingController;
2730
let cookieService: CookieService;
2831
let correlationIdService: CorrelationIdService;
29-
const router = Object.assign(new RouterStub(),{ url : '/statistics' });
32+
const router = Object.assign(new RouterStub(), { url: '/statistics' });
3033

3134
// Mock payload/statuses are dummy content as we are not testing the results
3235
// of any below requests. We are only testing for X-XSRF-TOKEN header.
@@ -36,6 +39,8 @@ describe('LogInterceptor', () => {
3639
const mockStatusCode = 200;
3740
const mockStatusText = 'SUCCESS';
3841

42+
const mockOrejimeService = jasmine.createSpyObj('OrejimeService', ['getSavedPreferences']);
43+
3944

4045
beforeEach(() => {
4146
TestBed.configureTestingModule({
@@ -55,6 +60,7 @@ describe('LogInterceptor', () => {
5560
{ provide: Router, useValue: router },
5661
{ provide: CorrelationIdService, useClass: CorrelationIdService },
5762
{ provide: UUIDService, useClass: UUIDService },
63+
{ provide: OrejimeService, useValue: mockOrejimeService },
5864
],
5965
});
6066

@@ -63,12 +69,14 @@ describe('LogInterceptor', () => {
6369
cookieService = TestBed.inject(CookieService);
6470
correlationIdService = TestBed.inject(CorrelationIdService);
6571

66-
cookieService.set('CORRELATION-ID','123455');
72+
cookieService.set('CORRELATION-ID', '123455');
6773
correlationIdService.initCorrelationId();
6874
});
6975

7076

71-
it('headers should be set', (done) => {
77+
it('headers should be set when cookie is accepted', (done) => {
78+
mockOrejimeService.getSavedPreferences.and.returnValue(of({ [CORRELATION_ID_OREJIME_KEY]: true }));
79+
7280
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
7381
expect(response).toBeTruthy();
7482
done();
@@ -80,7 +88,23 @@ describe('LogInterceptor', () => {
8088
expect(httpRequest.request.headers.has('X-REFERRER')).toBeTrue();
8189
});
8290

83-
it('headers should have the right values', (done) => {
91+
it('headers should not be set when cookie is declined', (done) => {
92+
mockOrejimeService.getSavedPreferences.and.returnValue(of({ [CORRELATION_ID_OREJIME_KEY]: false }));
93+
94+
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
95+
expect(response).toBeTruthy();
96+
done();
97+
});
98+
99+
const httpRequest = httpMock.expectOne('server/api/core/items');
100+
httpRequest.flush(mockPayload, { status: mockStatusCode, statusText: mockStatusText });
101+
expect(httpRequest.request.headers.has('X-CORRELATION-ID')).toBeFalse();
102+
expect(httpRequest.request.headers.has('X-REFERRER')).toBeTrue();
103+
});
104+
105+
it('headers should have the right values when cookie is accepted', (done) => {
106+
mockOrejimeService.getSavedPreferences.and.returnValue(of({ [CORRELATION_ID_OREJIME_KEY]: true }));
107+
84108
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
85109
expect(response).toBeTruthy();
86110
done();

src/app/core/log/log.interceptor.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import {
77
import { Injectable } from '@angular/core';
88
import { Router } from '@angular/router';
99
import { Observable } from 'rxjs';
10+
import { switchMap } from 'rxjs/operators';
1011

1112
import { CorrelationIdService } from '../../correlation-id/correlation-id.service';
12-
import { hasValue } from '../../shared/empty.util';
13+
import { OrejimeService } from '../../shared/cookies/orejime.service';
14+
import { CORRELATION_ID_OREJIME_KEY } from '../../shared/cookies/orejime-configuration';
15+
import {
16+
hasValue,
17+
isEmpty,
18+
} from '../../shared/empty.util';
1319

1420
/**
1521
* Log Interceptor intercepting Http Requests & Responses to
@@ -19,22 +25,37 @@ import { hasValue } from '../../shared/empty.util';
1925
@Injectable()
2026
export class LogInterceptor implements HttpInterceptor {
2127

22-
constructor(private cidService: CorrelationIdService, private router: Router) {}
28+
constructor(
29+
private cidService: CorrelationIdService,
30+
private router: Router,
31+
private orejimeService: OrejimeService,
32+
) {
33+
}
2334

2435
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
36+
return this.orejimeService.getSavedPreferences().pipe(
37+
switchMap(preferences => {
38+
// Check if the user has declined correlation id tracking
39+
const correlationDeclined =
40+
isEmpty(preferences) ||
41+
isEmpty(preferences[CORRELATION_ID_OREJIME_KEY]) ||
42+
!preferences[CORRELATION_ID_OREJIME_KEY];
2543

26-
// Get the correlation id for the user from the store
27-
const correlationId = this.cidService.getCorrelationId();
28-
29-
// Add headers from the intercepted request
30-
let headers = request.headers;
31-
if (hasValue(correlationId)) {
32-
headers = headers.append('X-CORRELATION-ID', correlationId);
33-
}
34-
headers = headers.append('X-REFERRER', this.router.url);
44+
// Add headers from the intercepted request
45+
let headers = request.headers;
46+
if (!correlationDeclined) {
47+
// Get the correlation id for the user from the store
48+
const correlationId = this.cidService.getCorrelationId();
49+
if (hasValue(correlationId)) {
50+
headers = headers.append('X-CORRELATION-ID', correlationId);
51+
}
52+
}
53+
headers = headers.append('X-REFERRER', this.router.url);
3554

36-
// Add new headers to the intercepted request
37-
request = request.clone({ withCredentials: true, headers: headers });
38-
return next.handle(request);
55+
// Add new headers to the intercepted request
56+
request = request.clone({ withCredentials: true, headers: headers });
57+
return next.handle(request);
58+
}),
59+
);
3960
}
4061
}

src/app/correlation-id/correlation-id.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { isEmpty } from '../shared/empty.util';
1212
import { SetCorrelationIdAction } from './correlation-id.actions';
1313
import { correlationIdSelector } from './correlation-id.selector';
1414

15+
export const CORRELATION_ID_COOKIE = 'dsCorrelationId';
16+
1517
/**
1618
* Service to manage the correlation id, an id used to give context to server side logs
1719
*/

src/app/shared/cookies/orejime-configuration.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '../../core/google-recaptcha/google-recaptcha.service';
1010
import { LANG_COOKIE } from '../../core/locale/locale.service';
1111
import { NativeWindowRef } from '../../core/services/window.service';
12+
import { CORRELATION_ID_COOKIE } from '../../correlation-id/correlation-id.service';
1213

1314
/**
1415
* Cookie for has_agreed_end_user
@@ -19,6 +20,8 @@ export const ANONYMOUS_STORAGE_NAME_OREJIME = 'orejime-anonymous';
1920

2021
export const GOOGLE_ANALYTICS_OREJIME_KEY = 'google-analytics';
2122

23+
export const CORRELATION_ID_OREJIME_KEY = 'correlation-id';
24+
2225
/**
2326
* Orejime configuration
2427
* For more information see https://github.com/empreinte-digitale/orejime
@@ -134,6 +137,14 @@ export function getOrejimeConfiguration(_window: NativeWindowRef): any {
134137
HAS_AGREED_END_USER,
135138
],
136139
},
140+
{
141+
name: CORRELATION_ID_OREJIME_KEY,
142+
purposes: ['statistical'],
143+
required: false,
144+
cookies: [
145+
CORRELATION_ID_COOKIE,
146+
],
147+
},
137148
{
138149
name: GOOGLE_ANALYTICS_OREJIME_KEY,
139150
purposes: ['statistical'],

src/assets/i18n/en.json5

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,10 @@
16141614

16151615
"cookies.consent.app.description.authentication": "Required for signing you in",
16161616

1617+
"cookies.consent.app.title.correlation-id": "Correlation ID",
1618+
1619+
"cookies.consent.app.description.correlation-id": "Allow us to track your session for debugging purposes",
1620+
16171621
"cookies.consent.app.title.preferences": "Preferences",
16181622

16191623
"cookies.consent.app.description.preferences": "Required for saving your preferences",

src/assets/i18n/it.json5

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,11 @@
19971997
// "cookies.consent.app.description.authentication": "Required for signing you in",
19981998
"cookies.consent.app.description.authentication": "Necessario per l'accesso",
19991999

2000+
// "cookies.consent.app.title.correlation-id": "Correlation ID",
2001+
"cookies.consent.app.title.correlation-id": "ID di correlazione",
2002+
2003+
// "cookies.consent.app.description.correlation-id": "Allow us to track your session for debugging purposes",
2004+
"cookies.consent.app.description.correlation-id": "Consentici di tracciare la tua sessione per scopi di debug",
20002005

20012006
// "cookies.consent.app.title.preferences": "Preferences",
20022007
"cookies.consent.app.title.preferences": "Preferenze",

0 commit comments

Comments
 (0)