e6eb5a30087d258ab69421d6dfc58946392ff690
[gpgme.git] / lang / js / BrowserTestExtension / tests / KeyImportExport.js
1 /* gpgme.js - Javascript integration for gpgme
2  * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GPGME is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1+
19  */
20
21 /* global describe, it, expect, Gpgmejs, ImportablePublicKey */
22
23 describe('Key importing', function () {
24     it('Prepare test Key (deleting it from gnupg, if present)', function(done){
25         let prm = Gpgmejs.init();
26         prm.then(function (context) {
27             expect(context.Keyring.getKeys).to.be.a('function');
28             context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
29                 function(result){
30                     if (result.length === 1) {
31                         result[0].delete().then(function(result){
32                             expect(result).to.be.true;
33                             done();
34                         });
35                     } else {
36                         done();
37                     }
38                 });
39         });
40     });
41
42     it('importing, updating, then deleting public Key', function (done) {
43         //This test runs in one large step, to ensure the proper state of the
44         // key in all stages.
45         let prm = Gpgmejs.init();
46         prm.then(function (context) {
47             context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
48                 function(result){
49                     expect(result).to.be.an('array');
50                     expect(result.length).to.equal(0);
51                     context.Keyring.importKey(ImportablePublicKey.key, true)
52                         .then(function(result){
53                             expect(result[0]).to.not.be.undefined;
54                             expect(result[0].key).to.be.an('object');
55                             expect(result[0].key.fingerprint).to.equal(
56                                 ImportablePublicKey.fingerprint);
57                             expect(result[0].status).to.equal('newkey');
58                             context.Keyring.importKey(
59                                 ImportablePublicKey.keyChangedUserId,true)
60                                 .then(function(res){
61                                     expect(res[0]).to.not.be.undefined;
62                                     expect(res[0].key).to.be.an('object');
63                                     expect(res[0].key.fingerprint).to.equal(
64                                         ImportablePublicKey.fingerprint);
65                                     expect(res[0].status).to.equal(
66                                         'change');
67                                     expect(
68                                         res[0].changes.userId).to.be.true;
69                                     expect(
70                                         res[0].changes.subkey).to.be.false;
71                                     expect(
72                                         res[0].changes.signature).to.be.true;
73                                     res[0].key.delete().then(function(result){
74                                         expect(result).to.be.true;
75                                         done();
76                                     });
77                                 });
78                         });
79                 });
80         });
81     });
82
83 });