Skip to content

Commit 7142881

Browse files
committed
test(cluster): compare docs and security
1 parent 681192b commit 7142881

File tree

1 file changed

+78
-25
lines changed

1 file changed

+78
-25
lines changed

test/node-and-browser.js

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,39 @@ describe('node and browser', function () {
1010
var slouch = null,
1111
id = 0;
1212

13-
// TODO: also need to test security
14-
1513
var data = {
16-
test_db1: [{
17-
_id: '1',
18-
foo: 'bar'
19-
},
20-
{
21-
_id: '2',
22-
yar: 'nar'
23-
},
24-
],
25-
test_db2: [{
26-
_id: '3',
27-
foo: 'star'
28-
},
29-
{
30-
_id: '4',
31-
yar: 'raar'
14+
test_db1: {
15+
security: {
16+
admins: {
17+
names: ['joe', 'phil'],
18+
roles: ['boss']
19+
},
20+
members: {
21+
names: ['dave'],
22+
roles: ['producer', 'consumer']
23+
}
3224
},
33-
]
25+
docs: [{
26+
_id: '1',
27+
foo: 'bar'
28+
},
29+
{
30+
_id: '2',
31+
yar: 'nar'
32+
}
33+
]
34+
},
35+
test_db2: {
36+
docs: [{
37+
_id: '3',
38+
foo: 'star'
39+
},
40+
{
41+
_id: '4',
42+
yar: 'raar'
43+
}
44+
]
45+
}
3446
};
3547

3648
var createDocs = function (db, docs) {
@@ -41,28 +53,67 @@ describe('node and browser', function () {
4153
return Promise.all(promises);
4254
};
4355

44-
var createDatabase = function (db, docs) {
56+
var createDatabase = function (db, data) {
4557
return slouch.db.create(db).then(function () {
46-
return createDocs(db, docs);
58+
if (data.security) {
59+
return slouch.security.set(db, data.security);
60+
}
61+
}).then(function () {
62+
return createDocs(db, data.docs);
4763
});
4864
};
4965

5066
var createData = function () {
5167
var promises = [];
52-
sporks.each(data, function (docs, db) {
53-
promises.push(createDatabase(db + '_' + id, docs));
68+
sporks.each(data, function (data, db) {
69+
promises.push(createDatabase(db + '_' + id, data));
5470
});
5571
return Promise.all(promises);
5672
};
5773

5874
var destroyData = function () {
5975
var promises = [];
60-
sporks.each(data, function (docs, db) {
76+
sporks.each(data, function (data, db) {
6177
promises.push(slouch.db.destroy(db + '_' + id));
6278
});
6379
return Promise.all(promises);
6480
};
6581

82+
var docsShouldEql = function (db, docs) {
83+
var savedDocs = {};
84+
return slouch.doc.all(db, { include_docs: true }).each(function (item) {
85+
// Delete _rev as not important for comparison
86+
delete item.doc._rev;
87+
savedDocs[item.doc._id] = item.doc;
88+
}).then(function () {
89+
var expDocs = {};
90+
docs.forEach(function (doc) {
91+
expDocs[doc._id] = doc;
92+
});
93+
savedDocs.should.eql(expDocs);
94+
});
95+
};
96+
97+
var dbDataShouldEql = function (db, data) {
98+
return Promise.resolve().then(function () {
99+
if (data.security) {
100+
return slouch.security.get(db).then(function (security) {
101+
security.should.eql(data.security);
102+
});
103+
}
104+
}).then(function () {
105+
return docsShouldEql(db, data.docs);
106+
});
107+
};
108+
109+
var dataShouldEql = function () {
110+
var promises = [];
111+
sporks.each(data, function (data, db) {
112+
promises.push(dbDataShouldEql(db + '_' + id, data));
113+
});
114+
return Promise.all(promises);
115+
};
116+
66117
beforeEach(function () {
67118
slouch = new Slouch('http://admin:admin@localhost:5984');
68119

@@ -81,7 +132,9 @@ describe('node and browser', function () {
81132
source: 'http://admin:admin@localhost:5984',
82133
target: 'http://admin:admin@localhost:5984'
83134
});
84-
return cluster.replicate();
135+
return cluster.replicate().then(function () {
136+
return dataShouldEql();
137+
});
85138
});
86139

87140
// it('should skip when replicating', function () {

0 commit comments

Comments
 (0)