|
| 1 | +/** |
| 2 | + * Copyright 2016 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +var tape = require('tape'); |
| 18 | +var _test = require('tape-promise'); |
| 19 | +var test = _test(tape); |
| 20 | + |
| 21 | +var hfc = require('hfc'); |
| 22 | +var Client = hfc; |
| 23 | +var User = require('hfc/lib/User.js'); |
| 24 | +var FabricCOPServices = require('hfc-cop/lib/FabricCOPImpl'); |
| 25 | + |
| 26 | +var utils = require('hfc/lib/utils.js'); |
| 27 | +var couchdbUtil = require('./couchdb-util.js'); |
| 28 | + |
| 29 | +// Use the Cloudant specific config file |
| 30 | +hfc.addConfigFile('test/fixtures/cloudant.json'); |
| 31 | +var dbClient = couchdbUtil.getCloudantClient(); |
| 32 | +var keyValueStore = hfc.getConfigSetting('key-value-store'); |
| 33 | +console.log('Key Value Store = ' + keyValueStore); |
| 34 | + |
| 35 | +// This test first checks to see if a user has already been enrolled. If so, |
| 36 | +// the test terminates. If the user is not yet enrolled, the test uses the |
| 37 | +// FabricCOPImpl to enroll a user, and saves the enrollment materials into the |
| 38 | +// CouchDB KeyValueStore. Then the test uses the Chain class to load the member |
| 39 | +// from the key value store. |
| 40 | +test('Use FabricCOPServices wih a Cloudant CouchDB KeyValueStore', function(t) { |
| 41 | + |
| 42 | + //var user = new User(); |
| 43 | + var client = new Client(); |
| 44 | + |
| 45 | + // Set the relevant configuration values |
| 46 | + utils.setConfigSetting('crypto-keysize', 256); |
| 47 | + |
| 48 | + // Clean up the couchdb test database |
| 49 | + var dbname = 'member_db'; |
| 50 | + |
| 51 | + //ccd next needs to be changed |
| 52 | + couchdbUtil.destroy(dbname, dbClient) |
| 53 | + .then( function(status) { |
| 54 | + t.comment('Cleanup of existing ' + dbname + ' returned '+status); |
| 55 | + t.comment('Initilize the CouchDB KeyValueStore'); |
| 56 | + utils.newKeyValueStore({name: dbname, path: dbClient}) |
| 57 | + .then( |
| 58 | + function(kvs) { |
| 59 | + t.comment('Setting client keyValueStore to: ' + kvs); |
| 60 | + client.setStateStore(kvs); |
| 61 | + if (client.getStateStore() === kvs) { |
| 62 | + t.pass('Successfully set CouchDB KeyValueStore for client'); |
| 63 | + } else { |
| 64 | + t.pass('CouchDB KeyValStore is not set successfully on this client!'); |
| 65 | + t.end(); |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | + t.comment('Initialize the COP server connection and KeyValueStore'); |
| 69 | + return new FabricCOPServices('http://localhost:8888', kvs); |
| 70 | + }, |
| 71 | + function(err) { |
| 72 | + console.log(err); |
| 73 | + t.fail('Error initializing CouchDB KeyValueStore. Exiting.'); |
| 74 | + t.end(); |
| 75 | + process.exit(1); |
| 76 | + }) |
| 77 | + .then( |
| 78 | + function(copService) { |
| 79 | + console.log('ADD: copService - ' + copService); |
| 80 | + t.pass('Successfully initialized the Fabric COP service.'); |
| 81 | + |
| 82 | + client.setCryptoSuite(copService.getCrypto()); |
| 83 | + t.comment('Set cryptoSuite on client'); |
| 84 | + t.comment('Begin copService.enroll'); |
| 85 | + return copService.enroll({ |
| 86 | + enrollmentID: 'admin2', |
| 87 | + enrollmentSecret: 'adminpw2' |
| 88 | + }); |
| 89 | + }, |
| 90 | + function(err) { |
| 91 | + t.fail('Failed to initilize the Fabric COP service: ' + err); |
| 92 | + t.end(); |
| 93 | + } |
| 94 | + ) |
| 95 | + .then( |
| 96 | + function(admin2) { |
| 97 | + t.pass('Successfully enrolled admin2 with COP server'); |
| 98 | + |
| 99 | + // Persist the user state |
| 100 | + var member = new User('admin2', client); |
| 101 | + member.setEnrollment(admin2.key, admin2.certificate); |
| 102 | + if (member.isEnrolled()) { |
| 103 | + t.pass('Member isEnrolled successfully.'); |
| 104 | + } else { |
| 105 | + t.fail('Member isEnrolled failed.'); |
| 106 | + } |
| 107 | + return client.setUserContext(member); |
| 108 | + }, |
| 109 | + function(err) { |
| 110 | + t.fail('Failed to enroll admin2 with COP server. Error: ' + err); |
| 111 | + t.end(); |
| 112 | + }) |
| 113 | + .then( |
| 114 | + function(user) { |
| 115 | + return client.loadUserFromStateStore('admin2'); |
| 116 | + } |
| 117 | + ).then( |
| 118 | + function(user) { |
| 119 | + if (user && user.getName() === 'admin2') { |
| 120 | + t.pass('Successfully loaded the user from key value store'); |
| 121 | + t.end(); |
| 122 | + } else { |
| 123 | + t.fail('Failed to load the user from key value store'); |
| 124 | + t.end(); |
| 125 | + } |
| 126 | + }, |
| 127 | + function(err) { |
| 128 | + t.fail('Failed to load the user admin2 from key value store. Error: ' + err); |
| 129 | + t.end(); |
| 130 | + } |
| 131 | + ).catch( |
| 132 | + function(err) { |
| 133 | + t.fail('Failed couchdb-fabriccop-test with error:' + err.stack ? err.stack : err); |
| 134 | + t.end(); |
| 135 | + } |
| 136 | + ); |
| 137 | + }); |
| 138 | +}); |
0 commit comments