From 7398c122d8f70dfd671d83d92584c1548dc6e18e Mon Sep 17 00:00:00 2001 From: Islam Aleiv Date: Tue, 9 Jul 2024 23:06:52 +0200 Subject: [PATCH] Add relation tests --- .../encryption/commit_relation_test.go | 102 +++++++++ .../encryption/field_query_test.go | 54 +++++ .../encryption/query_relation_test.go | 198 ++++++++++++++++++ 3 files changed, 354 insertions(+) create mode 100644 tests/integration/encryption/commit_relation_test.go create mode 100644 tests/integration/encryption/field_query_test.go create mode 100644 tests/integration/encryption/query_relation_test.go diff --git a/tests/integration/encryption/commit_relation_test.go b/tests/integration/encryption/commit_relation_test.go new file mode 100644 index 0000000000..848afa5fd3 --- /dev/null +++ b/tests/integration/encryption/commit_relation_test.go @@ -0,0 +1,102 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package encryption + +import ( + "testing" + + testUtils "github.com/sourcenetwork/defradb/tests/integration" +) + +func TestDocEncryption_WithEncryptionSecondaryRelations_ShouldStoreEncryptedCommit(t *testing.T) { + const userDocID = "bae-4d563681-e131-5e01-8ab4-6c65ac0d0478" + const deviceDocID = "bae-50211587-fde7-5d75-8034-e7040dfba203" + + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type User { + name: String + devices: [Device] + } + + type Device { + model: String + manufacturer: String + owner: User + } + `, + }, + testUtils.CreateDoc{ + CollectionID: 0, + Doc: `{ + "name": "Chris" + }`, + IsDocEncrypted: true, + }, + testUtils.CreateDoc{ + CollectionID: 1, + DocMap: map[string]any{ + "model": "Walkman", + "manufacturer": "Sony", + "owner": testUtils.NewDocIndex(0, 0), + }, + IsDocEncrypted: true, + }, + testUtils.Request{ + Request: ` + query { + commits { + delta + docID + fieldName + } + } + `, + Results: []map[string]any{ + { + "delta": encrypt(testUtils.CBORValue("Chris"), userDocID, ""), + "docID": userDocID, + "fieldName": "name", + }, + { + "delta": nil, + "docID": userDocID, + "fieldName": nil, + }, + { + "delta": encrypt(testUtils.CBORValue("Sony"), deviceDocID, ""), + "docID": deviceDocID, + "fieldName": "manufacturer", + }, + { + "delta": encrypt(testUtils.CBORValue("Walkman"), deviceDocID, ""), + "docID": deviceDocID, + "fieldName": "model", + }, + { + "delta": encrypt(testUtils.CBORValue(userDocID), deviceDocID, ""), + "docID": deviceDocID, + "fieldName": "owner_id", + }, + { + "delta": nil, + "docID": deviceDocID, + "fieldName": nil, + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +} diff --git a/tests/integration/encryption/field_query_test.go b/tests/integration/encryption/field_query_test.go new file mode 100644 index 0000000000..d008960448 --- /dev/null +++ b/tests/integration/encryption/field_query_test.go @@ -0,0 +1,54 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package encryption + +import ( + "testing" + + testUtils "github.com/sourcenetwork/defradb/tests/integration" +) + +func TestDocEncryptionField_WithEncryption_ShouldFetchDecrypted(t *testing.T) { + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type Users { + name: String + age: Int + } + `}, + testUtils.CreateDoc{ + Doc: john21Doc, + EncryptedFields: []string{"name"}, + }, + testUtils.Request{ + Request: ` + query { + Users { + _docID + name + age + } + }`, + Results: []map[string]any{ + { + "_docID": testUtils.NewDocIndex(0, 0), + "name": "John", + "age": int64(21), + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +} diff --git a/tests/integration/encryption/query_relation_test.go b/tests/integration/encryption/query_relation_test.go new file mode 100644 index 0000000000..8ca51c03aa --- /dev/null +++ b/tests/integration/encryption/query_relation_test.go @@ -0,0 +1,198 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package encryption + +import ( + "testing" + + testUtils "github.com/sourcenetwork/defradb/tests/integration" +) + +func TestDocEncryption_WithEncryptionOnBothRelations_ShouldFetchDecrypted(t *testing.T) { + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type User { + name: String + devices: [Device] + } + + type Device { + model: String + manufacturer: String + owner: User + } + `, + }, + testUtils.CreateDoc{ + CollectionID: 0, + Doc: `{ + "name": "Chris" + }`, + IsDocEncrypted: true, + }, + testUtils.CreateDoc{ + CollectionID: 1, + DocMap: map[string]any{ + "model": "Walkman", + "manufacturer": "Sony", + "owner": testUtils.NewDocIndex(0, 0), + }, + IsDocEncrypted: true, + }, + testUtils.Request{ + Request: `query { + User { + name + devices { + model + manufacturer + } + } + }`, + Results: []map[string]any{ + { + "name": "Chris", + "devices": []map[string]any{ + { + "model": "Walkman", + "manufacturer": "Sony", + }, + }, + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +} + +func TestDocEncryption_WithEncryptionOnPrimaryRelations_ShouldFetchDecrypted(t *testing.T) { + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type User { + name: String + devices: [Device] + } + + type Device { + model: String + manufacturer: String + owner: User + } + `, + }, + testUtils.CreateDoc{ + CollectionID: 0, + Doc: `{ + "name": "Chris" + }`, + }, + testUtils.CreateDoc{ + CollectionID: 1, + DocMap: map[string]any{ + "model": "Walkman", + "manufacturer": "Sony", + "owner": testUtils.NewDocIndex(0, 0), + }, + IsDocEncrypted: true, + }, + testUtils.Request{ + Request: `query { + User { + name + devices { + model + manufacturer + } + } + }`, + Results: []map[string]any{ + { + "name": "Chris", + "devices": []map[string]any{ + { + "model": "Walkman", + "manufacturer": "Sony", + }, + }, + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +} + +func TestDocEncryption_WithEncryptionOnSecondaryRelations_ShouldFetchDecrypted(t *testing.T) { + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type User { + name: String + devices: [Device] + } + + type Device { + model: String + manufacturer: String + owner: User + } + `, + }, + testUtils.CreateDoc{ + CollectionID: 0, + Doc: `{ + "name": "Chris" + }`, + IsDocEncrypted: true, + }, + testUtils.CreateDoc{ + CollectionID: 1, + DocMap: map[string]any{ + "model": "Walkman", + "manufacturer": "Sony", + "owner": testUtils.NewDocIndex(0, 0), + }, + }, + testUtils.Request{ + Request: `query { + User { + name + devices { + model + manufacturer + } + } + }`, + Results: []map[string]any{ + { + "name": "Chris", + "devices": []map[string]any{ + { + "model": "Walkman", + "manufacturer": "Sony", + }, + }, + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +}