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 776c6a0..d5aea38 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 { @@ -288,6 +288,292 @@ 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 is 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 is 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 is not found') + } + + } + } catch (e) { + console.log(e) + 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)