From ba6206a9b8641a80f7253c8edf7af448b2f2251b Mon Sep 17 00:00:00 2001 From: SAMI BETTAYEB Date: Tue, 22 Jun 2021 21:53:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E2=9C=85=20Add=20encrypted=20data?= =?UTF-8?q?=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: SAMI BETTAYEB --- test/postgresql.encrypted.test.js | 79 +++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 test/postgresql.encrypted.test.js diff --git a/test/postgresql.encrypted.test.js b/test/postgresql.encrypted.test.js new file mode 100644 index 00000000..4dd7e5cd --- /dev/null +++ b/test/postgresql.encrypted.test.js @@ -0,0 +1,79 @@ +// Copyright IBM Corp. 2014,2019. All Rights Reserved. +// Node module: loopback-connector-postgresql +// This file is licensed under the Artistic License 2.0. +// License text available at https://opensource.org/licenses/Artistic-2.0 + +'use strict'; +process.env.NODE_ENV = 'test'; +require('should'); +const expect = require('chai').expect; +const async = require('async'); +const chai = require('chai'); +const chaiSubset = require('chai-subset'); +chai.use(chaiSubset); + +let db; + +before(function() { + db = global.getSchema(); +}); + +describe('Mapping models', function() { + it('should return encrypted data by filter', function(done) { + const schema = + { + 'name': 'EncryptedData', + 'options': { + 'idInjection': false, + 'postgresql': { + 'schema': 'public', 'table': 'encrypted_data', + }, + }, + 'properties': { + 'id': { + 'type': 'String', + 'id': true, + }, + 'data': { + 'type': 'String', + }, + }, + 'mixins': { + 'Encryption': { + 'fields': [ + 'data', + ], + }, + }, + }; + + const EncryptedData = db.createModel(schema.name, schema.properties, schema.options); + EncryptedData.settings.mixins = schema.mixins; + + db.automigrate('EncryptedData', function(err) { + if (err) console.error({err}); + EncryptedData.create({ + id: '2', + data: '1c93722e6cf53f93dd4eb15a18444dc3e910fded18239db612794059af1fa5e8', + }, function(err, encryptedData) { + if (err) console.log({err2: err}); + async.series([ + function(callback) { + EncryptedData.findOne({where: {data: {ilike: '%test%'}}}, function(err, retreivedData) { + if (err) console.error({err111: err}); + expect(retreivedData).to.containSubset(encryptedData); + callback(null, retreivedData); + }); + }, + function(callback) { + EncryptedData.find({where: {data: {ilike: '%not found%'}}}, function(err, retreivedData) { + if (err) console.error({err111: err}); + expect(retreivedData.length).to.equal(0); + callback(null, retreivedData); + }); + }, + ], done); + }); + }); + }); +});