Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/CXSPA-6934/9292/9315: CDC Consent Management Issue #19916

Draft
wants to merge 16 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import { of } from 'rxjs';
import { RegisterComponentService } from './register-component.service';

import createSpy = jasmine.createSpy;
const mockRegisterFormData: any = {
titleCode: 'Mr',
firstName: 'John',
lastName: 'Doe',
email: 'JohnDoe@thebest.john.intheworld.com',
email_lowercase: 'johndoe@thebest.john.intheworld.com',
termsandconditions: true,
password: 'strongPass$!123',
passwordconf: 'strongPass$!123',
newsletter: true,
captcha: true,
};

class MockUserRegisterFacade implements Partial<UserRegisterFacade> {
getTitles = createSpy().and.returnValue(of([]));
Expand Down Expand Up @@ -98,4 +110,14 @@ describe('RegisterComponentService', () => {
let result = service.getAdditionalConsents();
expect(result).toEqual([]);
});
it('collectDataFromRegisterForm()', () => {
const form = mockRegisterFormData;
expect(service.collectDataFromRegisterForm(form)).toEqual({
firstName: form.firstName,
lastName: form.lastName,
uid: form.email_lowercase,
password: form.password,
titleCode: form.titleCode,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,15 @@ export class RegisterComponentService {
generateAdditionalConsentsFormControl(): UntypedFormArray | undefined {
return this.fb?.array([]) ?? undefined;
}

collectDataFromRegisterForm(formData: any): UserSignUp {
const { firstName, lastName, email, password, titleCode } = formData;
return {
firstName,
lastName,
password,
titleCode,
uid: email.toLowerCase(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
<div
class="form-check"
*ngFor="let control of additionalConsents.controls; let i = index"
[formGroupName]="i"
>
<div *ngIf="consents[i]?.template?.id as id">
<label>
Expand All @@ -257,12 +258,18 @@
[required]="consents[i].required"
[name]="id"
(change)="updateAdditionalConsents($any($event), i)"
[formControlName]="i"
formControlName="isConsentGranted"
/>
<span class="form-check-label">
{{ consents[i].template.description }}
<ng-template
*ngIf="consents[i].required"
[ngTemplateOutlet]="requiredAsterisk"
></ng-template>
</span>
<cx-form-errors [control]="control"></cx-form-errors>
<cx-form-errors
[control]="control.get('isConsentGranted')"
></cx-form-errors>
</label>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class MockRegisterComponentService
postRegisterMessage = createSpy();
getAdditionalConsents = createSpy();
generateAdditionalConsentsFormControl = createSpy();
collectDataFromRegisterForm = createSpy();
}

class MockSiteAdapter {
Expand Down Expand Up @@ -309,19 +310,23 @@ describe('RegisterComponent', () => {
describe('collectDataFromRegisterForm()', () => {
it('should return correct register data', () => {
const form = mockRegisterFormData;

expect(component.collectDataFromRegisterForm(form)).toEqual({
firstName: form.firstName,
lastName: form.lastName,
uid: form.email_lowercase,
password: form.password,
titleCode: form.titleCode,
});
component.collectDataFromRegisterForm(form);
expect(
registerComponentService.collectDataFromRegisterForm
).toHaveBeenCalledWith(form);
});
});

describe('register', () => {
it('should register with valid form', () => {
regComponentService.collectDataFromRegisterForm =
createSpy().and.returnValue({
firstName: mockRegisterFormData.firstName,
lastName: mockRegisterFormData.lastName,
uid: mockRegisterFormData.email_lowercase,
password: mockRegisterFormData.password,
titleCode: mockRegisterFormData.titleCode,
});
component.registerForm.patchValue(mockRegisterFormData);
component.ngOnInit();
component.submitForm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export class RegisterComponent implements OnInit, OnDestroy {

updateAdditionalConsents(event: MouseEvent, index: number) {
const { checked } = event.target as HTMLInputElement;
this.registerForm.value.additionalConsents[index] = checked;
this.registerForm.value.additionalConsents[index].isConsentGranted =
checked;
}

constructor(
Expand Down Expand Up @@ -215,15 +216,7 @@ export class RegisterComponent implements OnInit, OnDestroy {
}

collectDataFromRegisterForm(formData: any): UserSignUp {
const { firstName, lastName, email, password, titleCode } = formData;

return {
firstName,
lastName,
uid: email.toLowerCase(),
password,
titleCode,
};
return this.registerComponentService.collectDataFromRegisterForm(formData);
}

isConsentGiven(consent: AnonymousConsent | undefined): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import { CdcUserPreferenceSerializer } from './converters/cdc-user-preference.se
import { CommonModule } from '@angular/common';
import { CdcConsentManagementComponentService } from './services/cdc-consent-management-component.service';
import { ConsentManagementComponentService } from '@spartacus/storefront';
import { CDC_USER_PREFERENCE_SERIALIZER } from './converters/converter';
import {
CDC_PREFERENCE_SERIALIZER,
CDC_USER_PREFERENCE_SERIALIZER,
} from './converters/converter';
import { CdcUserConsentAdapter } from './cdc-user-consent.adapter';
import { CdcPreferenceSerializer } from './converters';

@NgModule({
imports: [CommonModule, I18nModule],
Expand All @@ -26,6 +30,11 @@ import { CdcUserConsentAdapter } from './cdc-user-consent.adapter';
useExisting: CdcUserPreferenceSerializer,
multi: true,
},
{
provide: CDC_PREFERENCE_SERIALIZER,
useExisting: CdcPreferenceSerializer,
multi: true,
},
],
})
export class CdcConsentManagementModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import createSpy = jasmine.createSpy;
const consentTemplateId = 'xxxx';
const consentTemplateVersion = 0;
class MockCdcUserConsentService implements Partial<CdcUserConsentService> {
updateCdcConsent = createSpy();
updateCdcUserPreferences = createSpy();
}
class MockCdcConsentsLocalStorageService
implements Partial<CdcConsentsLocalStorageService>
Expand Down Expand Up @@ -61,16 +61,14 @@ describe('CdcUserConsentAdapter', () => {
describe('giveConsent()', () => {
it('should update cdc consent', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
of({ errorCode: 0 })
);
cdcUserConsentService.updateCdcUserPreferences =
createSpy().and.returnValue(of({ errorCode: 0 }));
service
.giveConsent('current', consentTemplateId, consentTemplateVersion)
.subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
true,
['xxxx']
);
expect(
cdcUserConsentService.updateCdcUserPreferences
).toHaveBeenCalledWith([{ id: 'xxxx', isConsentGranted: true }]);
httpMock.expectOne((req) => {
return (
req.method === 'POST' &&
Expand All @@ -82,25 +80,21 @@ describe('CdcUserConsentAdapter', () => {
});
it('should not call CDC SDK', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(false);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
of({ errorCode: 0 })
);
cdcUserConsentService.updateCdcUserPreferences =
createSpy().and.returnValue(of({ errorCode: 0 }));
service.giveConsent('current', 'xxxx', 0).subscribe();
expect(cdcUserConsentService.updateCdcConsent).not.toHaveBeenCalledWith(
true,
['xxxx']
);
expect(
cdcUserConsentService.updateCdcUserPreferences
).not.toHaveBeenCalledWith([{ id: 'xxxx', isConsentGranted: true }]);
});
it('should not call Commerce API', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
of({ errorCode: 2 })
);
cdcUserConsentService.updateCdcUserPreferences =
createSpy().and.returnValue(of({ errorCode: 2 }));
service.giveConsent('current', 'xxxx', 0).subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
true,
['xxxx']
);
expect(
cdcUserConsentService.updateCdcUserPreferences
).toHaveBeenCalledWith([{ id: 'xxxx', isConsentGranted: true }]);
httpMock.expectNone((req) => {
return (
req.method === 'POST' &&
Expand All @@ -114,40 +108,34 @@ describe('CdcUserConsentAdapter', () => {
describe('withdrawConsent()', () => {
it('should update cdc consent', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
of({ errorCode: 0 })
);
cdcUserConsentService.updateCdcUserPreferences =
createSpy().and.returnValue(of({ errorCode: 0 }));
service.withdrawConsent('current', 'code', 'xxxx').subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
false,
['xxxx']
);
expect(
cdcUserConsentService.updateCdcUserPreferences
).toHaveBeenCalledWith([{ id: 'xxxx', isConsentGranted: false }]);
httpMock.expectOne((req) => {
return req.method === 'DELETE';
});
httpMock.verify();
});
it('should not call CDC SDK', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(false);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
of({ errorCode: 0 })
);
cdcUserConsentService.updateCdcUserPreferences =
createSpy().and.returnValue(of({ errorCode: 0 }));
service.withdrawConsent('current', 'code', 'xxxx').subscribe();
expect(cdcUserConsentService.updateCdcConsent).not.toHaveBeenCalledWith(
false,
['xxxx']
);
expect(
cdcUserConsentService.updateCdcUserPreferences
).not.toHaveBeenCalledWith([{ id: 'xxxx', isConsentGranted: false }]);
});
it('should not call Commerce API', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
of({ errorCode: 2 })
);
cdcUserConsentService.updateCdcUserPreferences =
createSpy().and.returnValue(of({ errorCode: 2 }));
service.withdrawConsent('current', 'code', 'xxxx').subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
false,
['xxxx']
);
expect(
cdcUserConsentService.updateCdcUserPreferences
).toHaveBeenCalledWith([{ id: 'xxxx', isConsentGranted: false }]);
httpMock.expectNone((req) => {
return req.method === 'DELETE';
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export class CdcUserConsentAdapter extends OccUserConsentAdapter {
);
} else {
return this.cdcUserConsentService
.updateCdcConsent(true, [consentTemplateId])
.updateCdcUserPreferences([
{ id: consentTemplateId, isConsentGranted: true },
])
.pipe(
catchError((error: any) => throwError(error)),
switchMap((result) => {
Expand All @@ -71,7 +73,9 @@ export class CdcUserConsentAdapter extends OccUserConsentAdapter {
return super.withdrawConsent(userId, consentCode);
} else {
return this.cdcUserConsentService
.updateCdcConsent(false, consentId ? [consentId] : [])
.updateCdcUserPreferences([
{ id: consentId ?? '', isConsentGranted: false },
])
.pipe(
catchError((error: any) => throwError(error)),
switchMap((result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TestBed } from '@angular/core/testing';
import { CdcPreferenceSerializer } from './cdc-preference.serializer';
import { CdcConsent } from '../model';
const mockInput: CdcConsent[] = [
{ id: 'terms.of.use', isConsentGranted: true },
{ id: 'terms.marketing', isConsentGranted: false },
{ id: 'others.analytics', isConsentGranted: true },
];

const mockOutput = {
terms: {
of: {
use: {
isConsentGranted: true,
},
},
marketing: {
isConsentGranted: false,
},
},
others: {
analytics: {
isConsentGranted: true,
},
},
};
describe('CdcPreferenceSerializer', () => {
let service: CdcPreferenceSerializer;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
declarations: [],
providers: [],
});
service = TestBed.inject(CdcPreferenceSerializer);
TestBed.compileComponents();
});
it('should create service', () => {
expect(service).toBeTruthy();
});
describe('convert()', () => {
it('convert consent array into cdc user preference', () => {
let target = {};
target = service.convert(mockInput, target);
expect(target).toEqual(mockOutput);
});
});
});
Loading
Loading