Skip to content

Commit 949cb7f

Browse files
authored
unit tests for the sync down routines for the user folder cases (#99)
1 parent 5e20d0e commit 949cb7f

File tree

2 files changed

+316
-134
lines changed

2 files changed

+316
-134
lines changed

keeperapi/src/__tests__/SyncDownResponseBuilder.ts

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
import {Vault} from "../proto";
2+
import {platform, Platform} from "../platform";
3+
import {Auth} from "../auth";
4+
5+
type RecordFieldData = {
6+
type: string,
7+
value: string[]
8+
}
9+
10+
type DecryptedRecordData = {
11+
title: string
12+
fields?: RecordFieldData[]
13+
}
14+
15+
type DecryptedSecurityScoreDataData = {
16+
padding: string,
17+
password: string,
18+
score: number,
19+
version: number,
20+
}
221

322
export class SyncDownResponseBuilder {
423
private readonly data: Vault.ISyncDownResponse;
24+
private readonly platform: Platform
25+
private readonly auth: Auth
526

6-
constructor() {
27+
constructor(platform: Platform, auth: Auth) {
28+
this.platform = platform
29+
this.auth = auth
730
this.data = {
831
continuationToken: new Uint8Array([]),
932
users: [],
@@ -39,34 +62,74 @@ export class SyncDownResponseBuilder {
3962
}
4063
}
4164

42-
addSecurityScoreData(securityScoreData: Vault.ISecurityScoreData) {
43-
this.data.securityScoreData?.push(securityScoreData)
44-
return this
45-
}
46-
47-
addBreachWatchSecurityData(breachWatchSecurityData: Vault.IBreachWatchSecurityData) {
48-
this.data.breachWatchSecurityData?.push(breachWatchSecurityData)
49-
return this
50-
}
51-
52-
addUserFolderRecord(userFolderRecord: Vault.IUserFolderRecord) {
53-
this.data.userFolderRecords?.push(userFolderRecord)
54-
return this
65+
addUserFolderRecord(recordUid: Uint8Array, folderUid?: Uint8Array) {
66+
this.data.userFolderRecords?.push({recordUid, folderUid, revision: Date.now()})
5567
}
5668

5769
addRecordMetadata(recordMetadata: Vault.IRecordMetaData) {
5870
this.data.recordMetaData?.push(recordMetadata)
59-
return this
6071
}
6172

62-
addRecord(record: Vault.IRecord) {
73+
async addRecord(decryptedRecordData: DecryptedRecordData) {
74+
const decryptedRecordKey = this.platform.getRandomBytes(32)
75+
const recordKey = await this.platform.aesGcmEncrypt(decryptedRecordKey, this.auth.dataKey!)
76+
const recordUid = this.platform.getRandomBytes(16)
77+
const decodedRecordData = this.platform.stringToBytes(JSON.stringify(decryptedRecordData))
78+
const recordData = await this.platform.aesGcmEncrypt(decodedRecordData, decryptedRecordKey)
79+
const record: Vault.IRecord = {
80+
recordUid,
81+
version: 3,
82+
data: recordData,
83+
extra: new Uint8Array([]),
84+
revision: Date.now(),
85+
}
6386
this.data.records?.push(record)
64-
return this
87+
88+
const passwordField = decryptedRecordData.fields?.find(data => data.type === 'password')
89+
const passwordFieldValue = passwordField?.value ? passwordField.value[0] : undefined
90+
let decryptedSecurityScoreDataData: DecryptedSecurityScoreDataData | undefined;
91+
92+
// add breach watch / security score data if a password field value presents
93+
if (!!passwordFieldValue) {
94+
this.data.breachWatchSecurityData?.push({
95+
recordUid,
96+
revision: record.revision,
97+
})
98+
decryptedSecurityScoreDataData = {
99+
padding: '',
100+
password: passwordFieldValue,
101+
score: 1,
102+
version: 1,
103+
}
104+
this.data.securityScoreData?.push({
105+
recordUid,
106+
data: await platform.aesGcmEncrypt(platform.stringToBytes(JSON.stringify(decryptedSecurityScoreDataData)), decryptedRecordKey),
107+
revision: record.revision,
108+
})
109+
}
110+
111+
return {
112+
recordKey,
113+
recordUid,
114+
record,
115+
decryptedSecurityScoreDataData
116+
}
65117
}
66118

67119
addRemovedRecord(recordUid: Uint8Array) {
68120
this.data.removedRecords?.push(recordUid)
69-
return this
121+
}
122+
123+
addUserFolder(userFolder: Vault.IUserFolder) {
124+
this.data.userFolders?.push(userFolder)
125+
}
126+
127+
addRemovedUserFolder(userFolderId: Uint8Array) {
128+
this.data.removedUserFolders?.push(userFolderId)
129+
}
130+
131+
addRemovedUserFolderRecord(recordUid: Uint8Array, folderUid: Uint8Array) {
132+
this.data.removedUserFolderRecords?.push({recordUid, folderUid})
70133
}
71134

72135
build() {

0 commit comments

Comments
 (0)