Skip to content

Commit 80ed0e0

Browse files
committed
refactor(test): extract deleteUsers in test-utils.js
1 parent 95738f9 commit 80ed0e0

File tree

4 files changed

+34
-60
lines changed

4 files changed

+34
-60
lines changed

test/test-utils.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var PouchDB = require('pouchdb-memory');
2+
3+
module.exports.deleteUsers = function (db, users) {
4+
var usersUrl = db.getUsersDatabaseUrl();
5+
var usersDb = new PouchDB(usersUrl);
6+
return usersDb.allDocs({
7+
include_docs: true,
8+
keys: users.map(function (user) {
9+
return 'org.couchdb.user:' + user;
10+
}),
11+
}).then(function (res) {
12+
var rows = res.rows.filter(function (row) {
13+
return row.doc;
14+
});
15+
var docs = rows.map(function (row) {
16+
row.doc._deleted = true;
17+
return row.doc;
18+
});
19+
return usersDb.bulkDocs({docs: docs});
20+
});
21+
};

test/test.authentication.js

+4-20
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
var PouchDB = require('pouchdb-memory');
44
var Authentication = require('../lib');
55
PouchDB.plugin(Authentication);
6+
67
var chai = require('chai');
78
var should = chai.should();
8-
chai.use(require('chai-as-promised'));
9-
10-
var users = ['aquaman'];
9+
var utils = require('./test-utils');
1110

1211
describe('authentication', function () {
1312

1413
var dbName = 'http://localhost:5984/testdb';
14+
var users = ['aquaman'];
1515

1616
var db;
1717

@@ -22,24 +22,8 @@ describe('authentication', function () {
2222
afterEach(function () {
2323
return db.logout().then(function () {
2424
return db.destroy().then(function () {
25-
var usersUrl = db.getUsersDatabaseUrl();
26-
var usersDb = new PouchDB(usersUrl);
2725
// remove the fake users, hopefully we're in the admin party
28-
return usersDb.allDocs({
29-
include_docs: true,
30-
keys: users.map(function (user) {
31-
return 'org.couchdb.user:' + user;
32-
})
33-
}).then(function (res) {
34-
var rows = res.rows.filter(function (row) {
35-
return row.doc;
36-
});
37-
var docs = rows.map(function (row) {
38-
row.doc._deleted = true;
39-
return row.doc;
40-
});
41-
return usersDb.bulkDocs({docs: docs});
42-
});
26+
return utils.deleteUsers(db, users);
4327
});
4428
});
4529
});

test/test.users.js

+4-20
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
var PouchDB = require('pouchdb-memory');
44
var Authentication = require('../lib');
55
PouchDB.plugin(Authentication);
6+
67
var chai = require('chai');
78
var should = chai.should();
8-
chai.use(require('chai-as-promised'));
9-
10-
var users = ['batman', 'superman', 'green_lantern', 'robin', 'aquaman', 'spiderman'];
9+
var utils = require('./test-utils');
1110

1211
describe('users', function () {
1312

1413
var dbName = 'http://localhost:5984/testdb';
14+
var users = ['batman', 'superman', 'green_lantern', 'robin', 'aquaman', 'spiderman'];
1515

1616
var db;
1717

@@ -22,24 +22,8 @@ describe('users', function () {
2222
afterEach(function () {
2323
return db.logout().then(function () {
2424
return db.destroy().then(function () {
25-
var usersUrl = db.getUsersDatabaseUrl();
26-
var usersDb = new PouchDB(usersUrl);
2725
// remove the fake users, hopefully we're in the admin party
28-
return usersDb.allDocs({
29-
include_docs: true,
30-
keys: users.map(function (user) {
31-
return 'org.couchdb.user:' + user;
32-
})
33-
}).then(function (res) {
34-
var rows = res.rows.filter(function (row) {
35-
return row.doc;
36-
});
37-
var docs = rows.map(function (row) {
38-
row.doc._deleted = true;
39-
return row.doc;
40-
});
41-
return usersDb.bulkDocs({docs: docs});
42-
});
26+
return utils.deleteUsers(db, users);
4327
});
4428
});
4529
});

test/test.users.metadata.js

+5-20
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,28 @@
33
var PouchDB = require('pouchdb-memory');
44
var Authentication = require('../lib');
55
PouchDB.plugin(Authentication);
6+
67
var chai = require('chai');
78
var should = chai.should();
8-
chai.use(require('chai-as-promised'));
9-
10-
var users = ['robin'];
9+
var utils = require('./test-utils');
1110

1211
describe('users.metadata', function () {
1312

1413
var dbName = 'http://localhost:5984/testdb';
14+
var users = ['robin'];
1515

1616
var db;
1717

1818
beforeEach(function () {
1919
db = new PouchDB(dbName);
2020
return db;
2121
});
22+
2223
afterEach(function () {
2324
return db.logout().then(function () {
2425
return db.destroy().then(function () {
25-
var usersUrl = db.getUsersDatabaseUrl();
26-
var usersDb = new PouchDB(usersUrl);
2726
// remove the fake users, hopefully we're in the admin party
28-
return usersDb.allDocs({
29-
include_docs: true,
30-
keys: users.map(function (user) {
31-
return 'org.couchdb.user:' + user;
32-
})
33-
}).then(function (res) {
34-
var rows = res.rows.filter(function (row) {
35-
return row.doc;
36-
});
37-
var docs = rows.map(function (row) {
38-
row.doc._deleted = true;
39-
return row.doc;
40-
});
41-
return usersDb.bulkDocs({docs: docs});
42-
});
27+
return utils.deleteUsers(db, users);
4328
});
4429
});
4530
});

0 commit comments

Comments
 (0)