Skip to content

Commit 19fae1e

Browse files
author
FrancescoMauto
committed
[DSC-1786] add: missing feedback recaptcha cookie in cookie popup window
1 parent c727651 commit 19fae1e

5 files changed

Lines changed: 188 additions & 6 deletions

File tree

src/app/register-email-form/register-email-form.component.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import {
2424
import { RestResponse } from '../core/cache/response.models';
2525
import { ConfigurationDataService } from '../core/data/configuration-data.service';
2626
import { EpersonRegistrationService } from '../core/data/eperson-registration.service';
27-
import { GoogleRecaptchaService } from '../core/google-recaptcha/google-recaptcha.service';
27+
import {
28+
CAPTCHA_REGISTRATION_NAME,
29+
GoogleRecaptchaService,
30+
} from '../core/google-recaptcha/google-recaptcha.service';
2831
import { CookieService } from '../core/services/cookie.service';
2932
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
3033
import { AlertComponent } from '../shared/alert/alert.component';
@@ -209,5 +212,15 @@ describe('RegisterEmailFormComponent', () => {
209212
expect(notificationsService.error).toHaveBeenCalled();
210213
expect(router.navigate).not.toHaveBeenCalled();
211214
}));
215+
it('isRecaptchaCookieAccepted should return true when registration recaptcha cookie is accepted', () => {
216+
const cookieService = TestBed.inject(CookieService) as unknown as CookieServiceMock;
217+
cookieService.set('klaro-anonymous', { [CAPTCHA_REGISTRATION_NAME]: true });
218+
expect(comp.isRecaptchaCookieAccepted()).toBeTrue();
219+
});
220+
it('isRecaptchaCookieAccepted should return false when registration recaptcha cookie is not accepted', () => {
221+
const cookieService = TestBed.inject(CookieService) as unknown as CookieServiceMock;
222+
cookieService.set('klaro-anonymous', { [CAPTCHA_REGISTRATION_NAME]: false });
223+
expect(comp.isRecaptchaCookieAccepted()).toBeFalse();
224+
});
212225
});
213226
});

src/app/register-email-form/register-email-form.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { ConfigurationDataService } from '../core/data/configuration-data.servic
4242
import { EpersonRegistrationService } from '../core/data/eperson-registration.service';
4343
import { RemoteData } from '../core/data/remote-data';
4444
import {
45-
CAPTCHA_FEEDBACK_NAME,
45+
CAPTCHA_REGISTRATION_NAME,
4646
GoogleRecaptchaService,
4747
} from '../core/google-recaptcha/google-recaptcha.service';
4848
import { CookieService } from '../core/services/cookie.service';
@@ -247,7 +247,7 @@ export class RegisterEmailFormComponent implements OnDestroy, OnInit {
247247
*/
248248
isRecaptchaCookieAccepted(): boolean {
249249
const klaroAnonymousCookie = this.cookieService.get('klaro-anonymous');
250-
return isNotEmpty(klaroAnonymousCookie) ? klaroAnonymousCookie[CAPTCHA_FEEDBACK_NAME] : false;
250+
return isNotEmpty(klaroAnonymousCookie) ? klaroAnonymousCookie[CAPTCHA_REGISTRATION_NAME] : false;
251251
}
252252

253253
/**

src/app/shared/cookies/browser-klaro.service.spec.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import {
2626
COOKIE_MDFIELD,
2727
} from './browser-klaro.service';
2828
import { ANONYMOUS_STORAGE_NAME_KLARO } from './klaro-configuration';
29+
import {
30+
CAPTCHA_FEEDBACK_NAME,
31+
CAPTCHA_REGISTRATION_NAME,
32+
} from '../../core/google-recaptcha/google-recaptcha.service';
2933

3034
describe('BrowserKlaroService', () => {
3135
const trackingIdProp = 'google.analytics.key';
@@ -128,6 +132,12 @@ describe('BrowserKlaroService', () => {
128132
}, {
129133
name: googleAnalytics,
130134
purposes: [purpose],
135+
}, {
136+
name: CAPTCHA_REGISTRATION_NAME,
137+
purposes: [purpose],
138+
}, {
139+
name: CAPTCHA_FEEDBACK_NAME,
140+
purposes: [purpose],
131141
}],
132142

133143
};
@@ -466,5 +476,146 @@ describe('BrowserKlaroService', () => {
466476
service.initialize();
467477
expect(service.klaroConfig.services).not.toContain(jasmine.objectContaining({ name: googleAnalytics }));
468478
});
479+
it('should show both recaptcha services when both verifications are enabled', () => {
480+
configurationDataService.findByPropertyName = jasmine.createSpy('configurationDataService').and.returnValue(
481+
createSuccessfulRemoteDataObject$({
482+
... new ConfigurationProperty(),
483+
name: trackingIdTestValue,
484+
values: ['true'],
485+
}),
486+
);
487+
service.initialize();
488+
expect(service.klaroConfig.services).toContain(jasmine.objectContaining({ name: CAPTCHA_REGISTRATION_NAME }));
489+
expect(service.klaroConfig.services).toContain(jasmine.objectContaining({ name: CAPTCHA_FEEDBACK_NAME }));
490+
});
491+
it('should hide registration recaptcha service when registration verification is disabled', () => {
492+
configurationDataService.findByPropertyName =
493+
jasmine.createSpy('configurationDataService')
494+
.withArgs(GOOGLE_ANALYTICS_KEY)
495+
.and
496+
.returnValue(
497+
createSuccessfulRemoteDataObject$({
498+
... new ConfigurationProperty(),
499+
name: trackingIdProp,
500+
values: [googleAnalytics],
501+
}),
502+
)
503+
.withArgs(REGISTRATION_VERIFICATION_ENABLED_KEY)
504+
.and
505+
.returnValue(
506+
createSuccessfulRemoteDataObject$({
507+
... new ConfigurationProperty(),
508+
name: trackingIdTestValue,
509+
values: ['false'],
510+
}),
511+
)
512+
.withArgs(FEEDBACK_VERIFICATION_ENABLED_KEY)
513+
.and
514+
.returnValue(
515+
createSuccessfulRemoteDataObject$({
516+
... new ConfigurationProperty(),
517+
name: trackingIdTestValue,
518+
values: ['true'],
519+
}),
520+
)
521+
.withArgs(MATOMO_ENABLED)
522+
.and
523+
.returnValue(
524+
createSuccessfulRemoteDataObject$({
525+
... new ConfigurationProperty(),
526+
name: matomoTrackingId,
527+
values: ['true'],
528+
}),
529+
);
530+
service.initialize();
531+
expect(service.klaroConfig.services).not.toContain(jasmine.objectContaining({ name: CAPTCHA_REGISTRATION_NAME }));
532+
expect(service.klaroConfig.services).toContain(jasmine.objectContaining({ name: CAPTCHA_FEEDBACK_NAME }));
533+
});
534+
it('should hide feedback recaptcha service when feedback verification is disabled', () => {
535+
configurationDataService.findByPropertyName =
536+
jasmine.createSpy('configurationDataService')
537+
.withArgs(GOOGLE_ANALYTICS_KEY)
538+
.and
539+
.returnValue(
540+
createSuccessfulRemoteDataObject$({
541+
... new ConfigurationProperty(),
542+
name: trackingIdProp,
543+
values: [googleAnalytics],
544+
}),
545+
)
546+
.withArgs(REGISTRATION_VERIFICATION_ENABLED_KEY)
547+
.and
548+
.returnValue(
549+
createSuccessfulRemoteDataObject$({
550+
... new ConfigurationProperty(),
551+
name: trackingIdTestValue,
552+
values: ['true'],
553+
}),
554+
)
555+
.withArgs(FEEDBACK_VERIFICATION_ENABLED_KEY)
556+
.and
557+
.returnValue(
558+
createSuccessfulRemoteDataObject$({
559+
... new ConfigurationProperty(),
560+
name: trackingIdTestValue,
561+
values: ['false'],
562+
}),
563+
)
564+
.withArgs(MATOMO_ENABLED)
565+
.and
566+
.returnValue(
567+
createSuccessfulRemoteDataObject$({
568+
... new ConfigurationProperty(),
569+
name: matomoTrackingId,
570+
values: ['true'],
571+
}),
572+
);
573+
service.initialize();
574+
expect(service.klaroConfig.services).toContain(jasmine.objectContaining({ name: CAPTCHA_REGISTRATION_NAME }));
575+
expect(service.klaroConfig.services).not.toContain(jasmine.objectContaining({ name: CAPTCHA_FEEDBACK_NAME }));
576+
});
577+
it('should hide both recaptcha services when both verifications are disabled', () => {
578+
configurationDataService.findByPropertyName =
579+
jasmine.createSpy('configurationDataService')
580+
.withArgs(GOOGLE_ANALYTICS_KEY)
581+
.and
582+
.returnValue(
583+
createSuccessfulRemoteDataObject$({
584+
... new ConfigurationProperty(),
585+
name: trackingIdProp,
586+
values: [googleAnalytics],
587+
}),
588+
)
589+
.withArgs(REGISTRATION_VERIFICATION_ENABLED_KEY)
590+
.and
591+
.returnValue(
592+
createSuccessfulRemoteDataObject$({
593+
... new ConfigurationProperty(),
594+
name: trackingIdTestValue,
595+
values: ['false'],
596+
}),
597+
)
598+
.withArgs(FEEDBACK_VERIFICATION_ENABLED_KEY)
599+
.and
600+
.returnValue(
601+
createSuccessfulRemoteDataObject$({
602+
... new ConfigurationProperty(),
603+
name: trackingIdTestValue,
604+
values: ['false'],
605+
}),
606+
)
607+
.withArgs(MATOMO_ENABLED)
608+
.and
609+
.returnValue(
610+
createSuccessfulRemoteDataObject$({
611+
... new ConfigurationProperty(),
612+
name: matomoTrackingId,
613+
values: ['true'],
614+
}),
615+
);
616+
service.initialize();
617+
expect(service.klaroConfig.services).not.toContain(jasmine.objectContaining({ name: CAPTCHA_REGISTRATION_NAME }));
618+
expect(service.klaroConfig.services).not.toContain(jasmine.objectContaining({ name: CAPTCHA_FEEDBACK_NAME }));
619+
});
469620
});
470621
});

src/app/shared/cookies/browser-klaro.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import { AuthService } from '../../core/auth/auth.service';
2929
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
3030
import { EPersonDataService } from '../../core/eperson/eperson-data.service';
3131
import { EPerson } from '../../core/eperson/models/eperson.model';
32-
import { CAPTCHA_FEEDBACK_NAME } from '../../core/google-recaptcha/google-recaptcha.service';
32+
import {
33+
CAPTCHA_FEEDBACK_NAME,
34+
CAPTCHA_REGISTRATION_NAME,
35+
} from '../../core/google-recaptcha/google-recaptcha.service';
3336
import { CookieService } from '../../core/services/cookie.service';
3437
import {
3538
NativeWindowRef,
@@ -209,7 +212,10 @@ export class BrowserKlaroService extends KlaroService {
209212
if (hideGoogleAnalytics) {
210213
servicesToHideArray.push(this.GOOGLE_ANALYTICS_SERVICE_NAME);
211214
}
212-
if (hideRegistrationVerification && hideFeedbackVerification) {
215+
if (hideRegistrationVerification) {
216+
servicesToHideArray.push(CAPTCHA_REGISTRATION_NAME);
217+
}
218+
if (hideFeedbackVerification) {
213219
servicesToHideArray.push(CAPTCHA_FEEDBACK_NAME);
214220
}
215221
if (hideMatomo) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { TOKENITEM } from '../../core/auth/models/auth-token-info.model';
77
import {
88
CAPTCHA_COOKIE,
99
CAPTCHA_FEEDBACK_NAME,
10+
CAPTCHA_REGISTRATION_NAME,
1011
} from '../../core/google-recaptcha/google-recaptcha.service';
1112
import { LANG_COOKIE } from '../../core/locale/locale.service';
1213

@@ -209,7 +210,7 @@ export const klaroConfiguration: any = {
209210
onlyOnce: true,
210211
},
211212
{
212-
name: CAPTCHA_FEEDBACK_NAME,
213+
name: CAPTCHA_REGISTRATION_NAME,
213214
purposes: ['registration-password-recovery'],
214215
required: false,
215216
cookies: [
@@ -219,6 +220,17 @@ export const klaroConfiguration: any = {
219220
onDecline: `window.refreshCaptchaScript?.call()`,
220221
onlyOnce: true,
221222
},
223+
{
224+
name: CAPTCHA_FEEDBACK_NAME,
225+
purposes: ['feedback-form-submission'],
226+
required: false,
227+
cookies: [
228+
CAPTCHA_COOKIE,
229+
],
230+
onAccept: `window.refreshCaptchaScript?.call()`,
231+
onDecline: `window.refreshCaptchaScript?.call()`,
232+
onlyOnce: true,
233+
},
222234
{
223235
name: 'accessibility',
224236
purposes: ['functional'],

0 commit comments

Comments
 (0)