|
| 1 | +// Copyright IBM Corp. 2014,2019. All Rights Reserved. |
| 2 | +// Node module: loopback-connector-postgresql |
| 3 | +// This file is licensed under the Artistic License 2.0. |
| 4 | +// License text available at https://opensource.org/licenses/Artistic-2.0 |
| 5 | + |
| 6 | +'use strict'; |
| 7 | +process.env.NODE_ENV = 'test'; |
| 8 | +require('should'); |
| 9 | +const expect = require('chai').expect; |
| 10 | +const async = require('async'); |
| 11 | +const chai = require('chai'); |
| 12 | +const chaiSubset = require('chai-subset'); |
| 13 | +chai.use(chaiSubset); |
| 14 | + |
| 15 | +let db; |
| 16 | + |
| 17 | +before(function() { |
| 18 | + db = global.getSchema(); |
| 19 | +}); |
| 20 | + |
| 21 | +describe('Mapping models', function() { |
| 22 | + it('should return encrypted data by filter', function(done) { |
| 23 | + const schema = |
| 24 | + { |
| 25 | + 'name': 'EncryptedData', |
| 26 | + 'options': { |
| 27 | + 'idInjection': false, |
| 28 | + 'postgresql': { |
| 29 | + 'schema': 'public', 'table': 'encrypted_data', |
| 30 | + }, |
| 31 | + }, |
| 32 | + 'properties': { |
| 33 | + 'id': { |
| 34 | + 'type': 'String', |
| 35 | + 'id': true, |
| 36 | + }, |
| 37 | + 'data': { |
| 38 | + 'type': 'String', |
| 39 | + }, |
| 40 | + }, |
| 41 | + 'mixins': { |
| 42 | + 'Encryption': { |
| 43 | + 'fields': [ |
| 44 | + 'data', |
| 45 | + ], |
| 46 | + }, |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + const EncryptedData = db.createModel(schema.name, schema.properties, schema.options); |
| 51 | + EncryptedData.settings.mixins = schema.mixins; |
| 52 | + |
| 53 | + db.automigrate('EncryptedData', function(err) { |
| 54 | + if (err) console.error({err}); |
| 55 | + EncryptedData.create({ |
| 56 | + id: '2', |
| 57 | + data: '1c93722e6cf53f93dd4eb15a18444dc3e910fded18239db612794059af1fa5e8', |
| 58 | + }, function(err, encryptedData) { |
| 59 | + if (err) console.log({err2: err}); |
| 60 | + async.series([ |
| 61 | + function(callback) { |
| 62 | + EncryptedData.findOne({where: {data: {ilike: '%test%'}}}, function(err, retreivedData) { |
| 63 | + if (err) console.error({err111: err}); |
| 64 | + expect(retreivedData).to.containSubset(encryptedData); |
| 65 | + callback(null, retreivedData); |
| 66 | + }); |
| 67 | + }, |
| 68 | + function(callback) { |
| 69 | + EncryptedData.find({where: {data: {ilike: '%not found%'}}}, function(err, retreivedData) { |
| 70 | + if (err) console.error({err111: err}); |
| 71 | + expect(retreivedData.length).to.equal(0); |
| 72 | + callback(null, retreivedData); |
| 73 | + }); |
| 74 | + }, |
| 75 | + ], done); |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments