Skip to content

Commit 855b5fc

Browse files
authored
Merge pull request #39 from SAP/feature/consentAsArgument
Add new methods to consent class
2 parents d8f0ec3 + 17c9343 commit 855b5fc

File tree

6 files changed

+97
-10
lines changed

6 files changed

+97
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,5 @@ typings/
112112
*.iml
113113

114114
# development time ignore rules
115-
usageTracking
115+
usageTracking
116+
dist/

src/common/consent.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, test, expect } from 'vitest'
2+
import WebConsent from '../web/webConsent'
3+
4+
describe('Consent', () => {
5+
test('should return the provided question answer false', async () => {
6+
let webConsent: WebConsent = new WebConsent()
7+
expect(await webConsent.provideConsentQuestionAnswer('no')).toBeFalsy()
8+
expect(await webConsent.provideConsentQuestionAnswer('No')).toBeFalsy()
9+
expect(await webConsent.provideConsentQuestionAnswer('fAlSe')).toBeFalsy()
10+
expect(await webConsent.provideConsentQuestionAnswer('1')).toBeFalsy()
11+
expect(await webConsent.provideConsentQuestionAnswer('0')).toBeFalsy()
12+
expect(await webConsent.provideConsentQuestionAnswer('')).toBeFalsy()
13+
expect(await webConsent.provideConsentQuestionAnswer(undefined)).toBeFalsy()
14+
})
15+
16+
test('should return the provided question answer yes', async () => {
17+
let webConsent: WebConsent = new WebConsent()
18+
expect(await webConsent.provideConsentQuestionAnswer('yes')).toBeTruthy()
19+
expect(await webConsent.provideConsentQuestionAnswer('YeS')).toBeTruthy()
20+
expect(await webConsent.provideConsentQuestionAnswer('y')).toBeTruthy()
21+
expect(await webConsent.provideConsentQuestionAnswer('Y')).toBeTruthy()
22+
})
23+
})

src/common/consent.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@ export default abstract class Consent {
66

77
abstract askConsentConfirm(): Promise<boolean>
88
abstract askConsentQuestion(): Promise<boolean>
9+
10+
provideConsentConfirmAnswer(consent: string = Consent.message): Promise<boolean> {
11+
return new Promise<boolean>((resolve, reject) => {
12+
consent?.toUpperCase() === 'Y' || consent?.toUpperCase() === 'YES' ? resolve(true) : reject(false)
13+
})
14+
}
15+
16+
provideConsentQuestionAnswer(consent: string = Consent.message): Promise<boolean> {
17+
return new Promise<boolean>((resolve, reject) => {
18+
resolve(consent?.toUpperCase() === 'Y' || consent?.toUpperCase() === 'YES' ? true : false)
19+
})
20+
}
921
}

src/common/tracker.test.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { describe, test, expect, vi, beforeEach } from 'vitest'
22
import CliTracker from '../cli/cliTracker'
3+
import WebTracker from '../web/webTracker'
34
import Tracker from '../common/tracker'
45

56
vi.mock('./storage')
67
vi.mock('../cli/fileStorage')
8+
vi.mock('../web/webStorage')
79
vi.mock('../gigya/account')
810

911
beforeEach(() => {
@@ -13,26 +15,27 @@ beforeEach(() => {
1315
describe('Tracker', () => {
1416
const apiKey: string = 'apiKey'
1517
const dataCenter: string = 'eu1'
16-
const tracker: Tracker = new CliTracker({ apiKey, dataCenter })
18+
const cliTracker: Tracker = new CliTracker({ apiKey, dataCenter })
19+
const webTracker: Tracker = new WebTracker({ apiKey, dataCenter })
1720
const email: string = '[email protected]'
1821
const toolName: string = 'tool name'
1922

20-
test('consent is already granted', () => {
23+
test.each([cliTracker, webTracker])('consent is already granted', (tracker) => {
2124
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(true)
2225
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
2326
tracker.requestConsentConfirmation({})
2427
expect(spySetConsentGranted).not.toHaveBeenCalled()
2528
})
2629

27-
test('consent confirm is not granted', () => {
30+
test.each([cliTracker, webTracker])('consent confirm is not granted', (tracker) => {
2831
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
2932
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
3033
vi.spyOn(tracker.consent, 'askConsentConfirm').mockReturnValue(Promise.reject(false))
3134
expect(tracker.requestConsentConfirmation({})).rejects.toBeFalsy()
3235
expect(spySetConsentGranted).not.toHaveBeenCalled()
3336
})
3437

35-
test('consent confirm is granted', async () => {
38+
test.each([cliTracker, webTracker])('consent confirm is granted', async (tracker) => {
3639
const consentResponse: boolean = true
3740
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
3841
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
@@ -43,15 +46,15 @@ describe('Tracker', () => {
4346
expect(spyAccount).toHaveBeenCalledWith(consentResponse, email)
4447
})
4548

46-
test('consent question is not granted', () => {
49+
test.each([cliTracker, webTracker])('consent question is not granted', (tracker) => {
4750
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
4851
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
4952
vi.spyOn(tracker.consent, 'askConsentQuestion').mockReturnValue(Promise.resolve(false))
5053
expect(tracker.requestConsentQuestion({})).resolves.toBeFalsy()
5154
expect(spySetConsentGranted).not.toHaveBeenCalled()
5255
})
5356

54-
test('consent question is granted', async () => {
57+
test.each([cliTracker, webTracker])('consent question is granted', async (tracker) => {
5558
const consentResponse: boolean = true
5659
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
5760
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
@@ -62,14 +65,14 @@ describe('Tracker', () => {
6265
expect(spyAccount).toHaveBeenCalledWith(consentResponse, expect.stringContaining('@automated-usage-tracking-tool.sap'))
6366
})
6467

65-
test('track usage without consent', () => {
68+
test.each([cliTracker, webTracker])('track usage without consent', (tracker) => {
6669
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
6770
const spySetLatestUsage = vi.spyOn(tracker.storage, 'setLatestUsage')
6871
tracker.trackUsage({ toolName })
6972
expect(spySetLatestUsage).not.toHaveBeenCalled()
7073
})
7174

72-
test('track usage with consent', async () => {
75+
test.each([cliTracker, webTracker])('track usage with consent', async (tracker) => {
7376
const featureName: string = 'feature name'
7477
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(true)
7578
const spySetLatestUsage = vi.spyOn(tracker.storage, 'setLatestUsage')
@@ -78,4 +81,44 @@ describe('Tracker', () => {
7881
expect(spySetLatestUsage).toHaveBeenCalledWith(toolName, featureName)
7982
expect(spyAccount).toHaveBeenCalled()
8083
})
84+
85+
test.each([cliTracker, webTracker])('track usage with provided consent question answer true', async (tracker) => {
86+
const featureName: string = 'feature name'
87+
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
88+
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
89+
const spyAccount = vi.spyOn(tracker.account, 'setConsent')
90+
await expect(tracker.provideConsentQuestionAnswer({ email, message: 'yes' })).resolves.toBeTruthy()
91+
expect(spySetConsentGranted).toHaveBeenCalledWith(true, email)
92+
expect(spyAccount).toHaveBeenCalled()
93+
})
94+
95+
test.each([cliTracker, webTracker])('track usage with provided consent question answer false', async (tracker) => {
96+
const featureName: string = 'feature name'
97+
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
98+
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
99+
const spyAccount = vi.spyOn(tracker.account, 'setConsent')
100+
await expect(tracker.provideConsentQuestionAnswer({ email, message: 'no' })).resolves.toBeFalsy()
101+
expect(spySetConsentGranted).not.toHaveBeenCalled()
102+
expect(spyAccount).not.toHaveBeenCalled()
103+
})
104+
105+
test.each([cliTracker, webTracker])('track usage with provided consent confirm answer true', async (tracker) => {
106+
const featureName: string = 'feature name'
107+
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
108+
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
109+
const spyAccount = vi.spyOn(tracker.account, 'setConsent')
110+
await expect(tracker.provideConsentConfirmAnswer({ email, message: 'yes' })).resolves.toBeTruthy()
111+
expect(spySetConsentGranted).toHaveBeenCalledWith(true, email)
112+
expect(spyAccount).toHaveBeenCalled()
113+
})
114+
115+
test.each([cliTracker, webTracker])('track usage with provided consent confirm answer false', async (tracker) => {
116+
const featureName: string = 'feature name'
117+
vi.spyOn(tracker.storage, 'isConsentGranted').mockReturnValue(false)
118+
const spySetConsentGranted = vi.spyOn(tracker.storage, 'setConsentGranted')
119+
const spyAccount = vi.spyOn(tracker.account, 'setConsent')
120+
await expect(tracker.provideConsentConfirmAnswer({ email, message: 'no' })).rejects.toBeFalsy()
121+
expect(spySetConsentGranted).not.toHaveBeenCalled()
122+
expect(spyAccount).not.toHaveBeenCalled()
123+
})
81124
})

src/common/tracker.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export default abstract class Tracker {
2525
return await this.requestConsent(this.consent.askConsentConfirm.bind(this.consent), consentArguments)
2626
}
2727

28+
public async provideConsentQuestionAnswer(consentArguments: ConsentArguments): Promise<boolean> {
29+
return await this.requestConsent(this.consent.provideConsentQuestionAnswer.bind(this.consent), consentArguments)
30+
}
31+
32+
public async provideConsentConfirmAnswer(consentArguments: ConsentArguments): Promise<boolean> {
33+
return await this.requestConsent(this.consent.provideConsentConfirmAnswer.bind(this.consent), consentArguments)
34+
}
35+
2836
async trackUsage(trackUsageArguments: TrackUsageArguments): Promise<void> {
2937
if (this.storage.isConsentGranted()) {
3038
this.storage.setLatestUsage(trackUsageArguments.toolName, trackUsageArguments.featureName)

src/web/webConsent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Consent from '../common/consent'
22

3-
export default class WebConsent implements Consent {
3+
export default class WebConsent extends Consent {
44
#dialogId = 'automated-usage-tracking-tool-dialog'
55
#dialogContentId = `${this.#dialogId}-content`
66
#dialogFooterId = `${this.#dialogId}-footer`

0 commit comments

Comments
 (0)