33e6bd299a940261fce96c2a55aacae12c856178
[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  * Author(s):
21  *     Maximilian Krambach <mkrambach@intevation.de>
22  *     Raimund Renkert <rrenkert@intevation.de>
23  */
24
25 /* global describe, it, expect, Gpgmejs, ImportablePublicKey */
26
27 describe('Key importing', function () {
28     it('Prepare test Key (deleting it from gnupg, if present)', function(done){
29         let prm = Gpgmejs.init();
30         prm.then(function (context) {
31             expect(context.Keyring.getKeys).to.be.a('function');
32             context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
33                 function(result){
34                     if (result.length === 1) {
35                         result[0].delete().then(function(result){
36                             expect(result).to.be.true;
37                             done();
38                         });
39                     } else {
40                         done();
41                     }
42                 });
43         });
44     });
45
46     it('importing, updating, then deleting public Key', function (done) {
47         //This test runs in one large step, to ensure the proper state of the
48         // key in all stages.
49         let prm = Gpgmejs.init();
50         prm.then(function (context) {
51             context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
52                 function(result){
53                     expect(result).to.be.an('array');
54                     expect(result.length).to.equal(0);
55                     context.Keyring.importKey(ImportablePublicKey.key, true)
56                         .then(function(result){
57                             expect(result.Keys[0]).to.not.be.undefined;
58                             expect(result.Keys[0].key).to.be.an('object');
59                             expect(result.Keys[0].key.fingerprint).to.equal(
60                                 ImportablePublicKey.fingerprint);
61                             expect(result.Keys[0].status).to.equal('newkey');
62                             context.Keyring.importKey(
63                                 ImportablePublicKey.keyChangedUserId,true)
64                                 .then(function(res){
65                                     expect(res.Keys[0]).to.not.be.undefined;
66                                     expect(res.Keys[0].key).to.be.an('object');
67                                     expect(res.Keys[0].key.fingerprint).to.equal(
68                                         ImportablePublicKey.fingerprint);
69                                     expect(res.Keys[0].status).to.equal(
70                                         'change');
71                                     expect(
72                                         res.Keys[0].changes.userId).to.be.true;
73                                     expect(
74                                         res.Keys[0].changes.subkey).to.be.false;
75                                     expect(
76                                         res.Keys[0].changes.signature).to.be.true;
77                                     res.Keys[0].key.delete().then(function(result){
78                                         expect(result).to.be.true;
79                                         done();
80                                     });
81                                 });
82                         });
83                 });
84         });
85     });
86
87 });