-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
120 lines (108 loc) · 3.07 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var test = require('tape')
var PouchDB = require('pouchdb')
var memdown = require('memdown')
var nacl = require('tweetnacl')
PouchDB.plugin(require('./'))
var keyPair = nacl.box.keyPair()
var permitId = 'permit/' + nacl.util.encodeBase64(keyPair.publicKey)
var dbname = 'test'
test('basics', function(t) {
var db = new PouchDB(dbname, { db: memdown })
var receiver
db.box(keyPair)
.then(function(permit) {
receiver = permit.receiver()
t.ok(permit.databaseKey.publicKey, 'returns database public key')
t.ok(permit.databaseKey.secretKey, 'returns database secret key')
})
.then(function() {
return db.put({ box: { foo: 'bar' } }, 'baz')
})
.then(function() {
return db.get('baz')
})
.then(function(doc) {
t.equals(doc.box.foo, 'bar', 'decrypts data')
t.ok(doc.box.receivers, 'has receivers')
t.ok(receiver in doc.box.receivers, 'has the receiver')
})
.then(function() {
db.closeBox()
})
.then(function() {
return db.get('baz')
})
.then(function(doc) {
t.notOk(doc.box.foo, 'does not have foo')
t.ok(doc.box.receivers, 'has receivers')
t.ok(receiver in doc.box.receivers, 'has the receiver')
})
.then(t.end)
})
test('reopen', function(t) {
var db = new PouchDB(dbname, { db: memdown })
db.box(keyPair)
.then(function() {
return db.get('baz')
})
.then(function(doc) {
t.equals(doc.box.foo, 'bar', 'decrypts data')
})
.then(t.end)
})
test('share', function(t) {
var alice = new PouchDB(dbname + '-share', { db: memdown })
var bob = new PouchDB(dbname + '-share', { db: memdown })
var aliceKey = nacl.box.keyPair()
var bobKey = nacl.box.keyPair()
alice.box(aliceKey)
.then(function(alicePermit) {
return bob.box(bobKey, [alicePermit.databaseKey.publicKey])
.then(function() {
return bob.put({ box: { foo: 'bar' } }, 'baz')
})
.then(function(asd) {
return alice.get('baz')
})
.then(function(doc) {
t.equals(doc.box.foo, 'bar', 'decrypts data')
})
.then(t.end)
})
})
test('conflicts', function(t) {
var db = new PouchDB(dbname, { db: memdown })
var other = new PouchDB(dbname + '-other', { db: memdown })
var receiver
other.box(keyPair)
.then(function() {
return other.put({ box: { foo: 'otherbar' } }, 'otherbaz')
})
.then(function() {
other.closeBox()
})
.then(function() {
return other.replicate.to(db)
})
.then(function() {
return db.box(keyPair)
})
.then(function(permit) {
receiver = permit.receiver()
})
.then(function() {
return db.get(permitId, { conflicts: true })
})
.then(function(doc) {
t.notOk(doc._conflicts, 'does not have conflicts')
})
.then(function() {
return db.get('otherbaz')
})
.then(function(doc) {
t.equals(doc.box.foo, 'otherbar', 'decrypts data')
t.ok(doc.box.receivers, 'has receivers')
t.ok(receiver in doc.box.receivers, 'has the receiver')
})
.then(t.end)
})