From 165d9b1500551a6eec03dfd99d12e36ddd6bd25c Mon Sep 17 00:00:00 2001 From: Chen22 Date: Thu, 22 Jun 2023 17:48:10 -0700 Subject: [PATCH 1/3] add ownership verify test --- tests/client_v2.test.ts | 132 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 2 deletions(-) diff --git a/tests/client_v2.test.ts b/tests/client_v2.test.ts index 776c6a0..736b63e 100644 --- a/tests/client_v2.test.ts +++ b/tests/client_v2.test.ts @@ -39,8 +39,8 @@ import { createDocumentDatabase, createEventDatabase, showDatabase, - createCollection, -} from '../src/store/database_v2' + createCollection, getDatabase, getCollection +} from '../src/store/database_v2'; import { Index, IndexType } from '../src/proto/db3_database_v2' interface Profile { @@ -294,6 +294,134 @@ describe('test db3.js client module', () => { expect(1).toBe(0) } }) + test('test ownership verify for update/delete document', async () => { + const client = await createTestClient() + const client2 = await createTestClient() + try { + const { db, result } = await createDocumentDatabase( + client, + 'db_for_update_delete' + ) + const index: Index = { + path: '/city', + indexType: IndexType.StringKey, + } + { + const { collection, result } = await createCollection( + db, + 'col', + [index] + ) + const collection2 = await getCollection(db.addr, 'col', client2) + + await new Promise((r) => setTimeout(r, 3000)) + const [txId2, block2, order2, docId2] = await addDoc( + collection, + { + city: 'beijing', + author: 'imotai', + age: 10, + } + ) + + const [txId3, block3, order3, docId3] = await addDoc( + collection2, + { + city: 'beijing2', + author: 'imotai1', + age: 1, + } + ) + await new Promise((r) => setTimeout(r, 3000)) + { + const queryStr = '/[city = beijing]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(1).toBe(resultSet.docs.length) + expect(resultSet.docs[0].doc.city).toBe('beijing') + expect(resultSet.docs[0].doc.author).toBe('imotai') + expect(resultSet.docs[0].doc.age).toBe(10) + expect(resultSet.docs[0].id).toBe(docId2) + } + { + const queryStr = '/[city = beijing]' + const resultSet = await queryDoc( + collection2, + queryStr + ) + expect(1).toBe(resultSet.docs.length) + expect(resultSet.docs[0].doc.city).toBe('beijing') + expect(resultSet.docs[0].doc.author).toBe('imotai') + expect(resultSet.docs[0].doc.age).toBe(10) + expect(resultSet.docs[0].id).toBe(docId2) + } + + try { + const [txId, block, order] = await deleteDoc(collection, [ + docId3, + ]) + fail('should not be here') + } catch (e) { + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc owner is not the sender') + } + // verify docId3 is not deleted + await new Promise((r) => setTimeout(r, 3000)) + { + const queryStr = '/[city = beijing2]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(1).toBe(resultSet.docs.length) + expect(resultSet.docs[0].id).toBe(docId3) + } + const [txId, block, order] = await deleteDoc(collection2, [ + docId3, + ]) + await new Promise((r) => setTimeout(r, 3000)) + { + const queryStr = '/[city = beijing2]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(0).toBe(resultSet.docs.length) + } + + // update doc fail with ownership verify error + try { + const [txId, block, order] = await updateDoc( + collection2, + docId2, + { + city: 'beijing_new', + author: 'imotai_new', + age: 3, + }, + [] + ) + } catch (e) { + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc owner is not the sender') + } + // verify docId2 is not updated + await new Promise((r) => setTimeout(r, 3000)) + { + const queryStr = '/[city = beijing]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(1).toBe(resultSet.docs.length) + expect(resultSet.docs[0].id).toBe(docId2) + } + } + } catch (e) { + console.log(e) + expect(1).toBe(0) + } + }) test('create database v2', async () => { const client = await createTestClient() try { From 12b1fd3f515924e77ab5d26cd82ec7d4d7df5096 Mon Sep 17 00:00:00 2001 From: Chen22 Date: Fri, 23 Jun 2023 19:53:49 -0700 Subject: [PATCH 2/3] add doc id not found ut --- src/store/document_v2.ts | 4 +- tests/client_v2.test.ts | 158 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 2 deletions(-) diff --git a/src/store/document_v2.ts b/src/store/document_v2.ts index ce665e0..86d9655 100644 --- a/src/store/document_v2.ts +++ b/src/store/document_v2.ts @@ -112,7 +112,7 @@ export async function deleteDoc(col: Collection, ids: string[]) { col.db.client.nonce += 1 return [response.id, response.block, response.order] } else { - throw new Error('fail to create collection') + throw new Error('fail to delete doc') } } @@ -151,7 +151,7 @@ export async function updateDoc( col.db.client.nonce += 1 return [response.id, response.block, response.order] } else { - throw new Error('fail to create collection') + throw new Error('fail to update doc') } } export async function addDoc(col: Collection, doc: DocumentData) { diff --git a/tests/client_v2.test.ts b/tests/client_v2.test.ts index 736b63e..53419a9 100644 --- a/tests/client_v2.test.ts +++ b/tests/client_v2.test.ts @@ -288,6 +288,164 @@ describe('test db3.js client module', () => { ) expect(0).toBe(resultSet.docs.length) } + try { + const [txId4, block4, order4] = await updateDoc( + collection, + docId2, + { + city: 'beijing3', + author: 'imotai3', + age: 3, + }, + [] + ) + fail('should not reach here') + } catch (e) { + console.log(e) + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id not found') + } + + } + } catch (e) { + console.log(e) + expect(1).toBe(0) + } + }) + test('test delete doc id not found', async () => { + const client = await createTestClient() + try { + const { db, result } = await createDocumentDatabase( + client, + 'db_for_update_delete' + ) + const index: Index = { + path: '/city', + indexType: IndexType.StringKey, + } + { + const { collection, result } = await createCollection( + db, + 'col', + [index] + ) + await new Promise((r) => setTimeout(r, 2000)) + const [txId2, block2, order2, docId2] = await addDoc( + collection, + { + city: 'beijing', + author: 'imotai', + age: 10, + } + ) + await new Promise((r) => setTimeout(r, 2000)) + { + const queryStr = '/[city = beijing]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(1).toBe(resultSet.docs.length) + expect(resultSet.docs[0].doc.city).toBe('beijing') + expect(resultSet.docs[0].doc.author).toBe('imotai') + expect(resultSet.docs[0].doc.age).toBe(10) + expect(resultSet.docs[0].id).toBe(docId2) + } + + const [txId, block, order] = await deleteDoc(collection, [ + docId2, + ]) + await new Promise((r) => setTimeout(r, 2000)) + { + const queryStr = '/[city = beijing3]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(0).toBe(resultSet.docs.length) + } + try { + const [txId, block, order] = await deleteDoc(collection, [ + docId2, + ]) + fail('should not reach here') + } catch (e) { + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id not found') + } + + } + } catch (e) { + console.log(e) + expect(1).toBe(0) + } + }) + test('test update doc id not found', async () => { + const client = await createTestClient() + try { + const { db, result } = await createDocumentDatabase( + client, + 'db_for_update_delete' + ) + const index: Index = { + path: '/city', + indexType: IndexType.StringKey, + } + { + const { collection, result } = await createCollection( + db, + 'col', + [index] + ) + await new Promise((r) => setTimeout(r, 3000)) + const [txId2, block2, order2, docId2] = await addDoc( + collection, + { + city: 'beijing', + author: 'imotai', + age: 10, + } + ) + await new Promise((r) => setTimeout(r, 2000)) + { + const queryStr = '/[city = beijing]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(1).toBe(resultSet.docs.length) + expect(resultSet.docs[0].doc.city).toBe('beijing') + expect(resultSet.docs[0].doc.author).toBe('imotai') + expect(resultSet.docs[0].doc.age).toBe(10) + expect(resultSet.docs[0].id).toBe(docId2) + } + + const [txId, block, order] = await deleteDoc(collection, [ + docId2, + ]) + await new Promise((r) => setTimeout(r, 2000)) + { + const queryStr = '/[city = beijing3]' + const resultSet = await queryDoc( + collection, + queryStr + ) + expect(0).toBe(resultSet.docs.length) + } + try { + const [txId4, block4, order4] = await updateDoc( + collection, + docId2, + { + city: 'beijing3', + author: 'imotai3', + age: 3, + }, + [] + ) + fail('should not reach here') + } catch (e) { + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id not found') + } + } } catch (e) { console.log(e) From 39319ff791911d17ea182c70779a8b079a2ea22e Mon Sep 17 00:00:00 2001 From: Chen22 Date: Sat, 24 Jun 2023 17:20:33 -0700 Subject: [PATCH 3/3] fix ut --- tests/client_v2.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/client_v2.test.ts b/tests/client_v2.test.ts index 53419a9..d5aea38 100644 --- a/tests/client_v2.test.ts +++ b/tests/client_v2.test.ts @@ -302,7 +302,7 @@ describe('test db3.js client module', () => { fail('should not reach here') } catch (e) { console.log(e) - expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id not found') + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id is not found') } } @@ -369,7 +369,7 @@ describe('test db3.js client module', () => { ]) fail('should not reach here') } catch (e) { - expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id not found') + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id is not found') } } @@ -443,7 +443,7 @@ describe('test db3.js client module', () => { ) fail('should not reach here') } catch (e) { - expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id not found') + expect(decodeURI(e.message)).toBe('fail to verify the owner with error doc id is not found') } }