Skip to content

Commit

Permalink
Put privacy policy version in shared settings file and implement in t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
HildoBijl committed Mar 6, 2024
1 parent 4a0baa1 commit 8b22ba9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
10 changes: 4 additions & 6 deletions api/src/graphql/resolvers/User.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const { UserInputError } = require('apollo-server-express')

const { languages } = require('step-wise/settings/i18n')
const { languages, currentPrivacyPolicyVersion } = require('step-wise/settings')
const { ensureSkillIds } = require('step-wise/eduTools')

const { getUser, getAllUsers } = require('../util/User')

const CURRENT_PRIVACY_POLICY_VERSION = 2

const resolvers = {
User: {
skills: async (user, { ids }) => {
Expand All @@ -19,7 +17,7 @@ const resolvers = {
return {
version: user.privacyPolicyAcceptedVersion,
acceptedAt: user.privacyPolicyAcceptedAt,
isLatestVersion: user.privacyPolicyAcceptedVersion === CURRENT_PRIVACY_POLICY_VERSION,
isLatestVersion: user.privacyPolicyAcceptedVersion === currentPrivacyPolicyVersion,
}
}
},
Expand All @@ -40,9 +38,9 @@ const resolvers = {
const user = await getCurrentUser()

// Only run update if we are behind.
if (!user.privacyPolicyAcceptedVersion || user.privacyPolicyAcceptedVersion < CURRENT_PRIVACY_POLICY_VERSION) {
if (!user.privacyPolicyAcceptedVersion || user.privacyPolicyAcceptedVersion < currentPrivacyPolicyVersion) {
await user.update({
privacyPolicyAcceptedVersion: CURRENT_PRIVACY_POLICY_VERSION,
privacyPolicyAcceptedVersion: currentPrivacyPolicyVersion,
privacyPolicyAcceptedAt: new Date(),
})
}
Expand Down
7 changes: 4 additions & 3 deletions api/tests/server/api/User/user.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { currentPrivacyPolicyVersion } = require('../../../../../shared/settings')
const surfConextMockData = require('../../../../surfConextMockData.json')
const { createClient } = require('../../../client')

Expand Down Expand Up @@ -81,7 +82,7 @@ describe('privacy policy consent', () => {
const after = new Date().getTime()

expect(errors).toBeUndefined()
expect(acceptLatestPrivacyPolicy.version).toEqual(1)
expect(acceptLatestPrivacyPolicy.version).toEqual(currentPrivacyPolicyVersion)
const acceptedAt = new Date(acceptLatestPrivacyPolicy.acceptedAt).getTime()
expect(acceptedAt).toBeGreaterThanOrEqual(before)
expect(acceptedAt).toBeLessThanOrEqual(after)
Expand All @@ -94,15 +95,15 @@ describe('privacy policy consent', () => {
expect(privacyPolicyConsent.isLatestVersion).toEqual(acceptLatestPrivacyPolicy.isLatestVersion)
})

it('does not overwrite the `acceptedAt` date if version didnt advance', async () => {
it('does not overwrite the `acceptedAt` date if version didn\'t advance', async () => {
const client = await createClient(seed)
await client.loginSurfConext(BOB_SURFSUB)

// Accept the privacy policy.
const { data: { acceptLatestPrivacyPolicy: firstConsent } } = await client.graphql({ query: `mutation {acceptLatestPrivacyPolicy {version, acceptedAt, isLatestVersion}}` })

// Let time progress a little bit and try to accept it again. It should not change things.
await new Promise(resolve => setTimeout(resolve, 5));
await new Promise(resolve => setTimeout(resolve, 5))
const { data: { acceptLatestPrivacyPolicy: secondConsent } } = await client.graphql({ query: `mutation {acceptLatestPrivacyPolicy {version, acceptedAt, isLatestVersion}}` })
expect(firstConsent).toMatchObject(secondConsent)
})
Expand Down
5 changes: 5 additions & 0 deletions shared/settings/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
...require('./numbers'),
...require('./i18n'),
...require('./users'),
}
1 change: 1 addition & 0 deletions shared/settings/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.currentPrivacyPolicyVersion = 2

0 comments on commit 8b22ba9

Please sign in to comment.