| 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 | 1x
1x
1x
1x
1x
1x
1x
1x
44x
4x
4x
29x
4x
1x
1x
1x
1x
1x
1x
1x
16x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
20x
1x
1x
1x
1x
1x
1x
1x
8x
8x
24x
1x
1x
1x
1x
1x
1x
1x
24x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
16x
1x
1x
4x
1x
8x
1x
1x
8x
1x
1x
1x
8x
1x
1x
8x
1x
1x
1x
8x
1x
1x
8x
1x
1x
8x
1x
1x
1x
12x
1x
1x
12x
1x
1x
1x
1x
1x
12x
1x
1x
12x
1x
1x
1x
1x
1x
| import chai from 'chai';
import 'regenerator-runtime/runtime';
import rethinkdbdash from 'rethinkdbdash';
import RethinkDBDashDriver from './index';
const should = chai.should();
const host = process.env.RETHINKDB_HOST || '127.0.0.1';
const port = process.env.RETHINKDB_PORT || 28015;
const connection = rethinkdbdash({
servers: [ { host, port } ],
db: 'no-default-database-for-testing'
});
// not really disposable, no way to get an instance with a different
// database, too expensive to open new connection for each test, but
// this will still work as "intended" for single thread tests.
async function disposable() {
const name = "tmp" + Math.floor(Math.random() * 10000);
await connection.dbCreate(name).run();
const r = Object.create(connection);
r.dispose = async function() {
await this.dbDrop(name).run();
};
r._poolMaster._options.db = name;
return r;
}
describe('apollo-passport-rethinkdbdash', () => {
// sufficiently tested by other tests for now
describe('constructor()', () => {
it('accepts options', () => {
const r = { db() {} };
const options = {
init: false,
userTableName: 'personnel'
};
const db = new RethinkDBDashDriver(r, options);
db.userTableName.should.equal(options.userTableName);
});
});
it('ready()', async () => {
const r = await disposable();
const db = new RethinkDBDashDriver(r);
await db._ready();
// now do a fake init
db.initted = false;
db.readySubs.length.should.equal(0);
db._ready();
db.readySubs.length.should.equal(1);
db.readySubs.shift().call();
db.initted = true;
db.readySubs.length.should.equal(0);
db._ready();
db.readySubs.length.should.equal(0);
await r.dispose();
});
it('config; database & local output', async () => {
const r = await disposable();
const db = new RethinkDBDashDriver(r);
await db.setConfigKey('test', 'test1', { a: 1 });
const data = await db.fetchConfig();
data.should.deep.equal({
test: {
test1: {
type: 'test',
id: 'test1',
a: 1
}
}
});
await r.dispose();
});
describe('users', () => {
describe('createUser()', () => {
let r;
before(async () => { r = await disposable(); });
after(async () => { await r.dispose(); });
it('inserts a user and returns the id when no id given', async () => {
const db = new RethinkDBDashDriver(r);
await db._ready();
await db.users.delete({ durability: 'soft' });
const user = { name: 'John Sheppard' };
const id = await db.createUser(user);
const users = await db.users.run();
const added = users[0];
added.name.should.equal(user.name);
id.should.equal(added.id);
await db.users.delete({ durability: 'soft' });
});
it('inserts a user and returns the id when an id is given', async () => {
const db = new RethinkDBDashDriver(r);
await db._ready();
await db.users.delete({ durability: 'soft' });
const user = { id: 'sheppard', name: 'John Sheppard' };
const id = await db.createUser(user);
const users = await db.users.run();
const added = users[0];
added.name.should.equal(user.name);
id.should.equal(added.id);
await db.users.delete({ durability: 'soft' });
});
});
describe('fetching', () => {
const users = [
{
id: "sheppard",
emails: [
{ address: "sheppard@atlantis.net" }
],
services: {
facebook: {
id: "1"
}
}
},
{
id: "mckay",
emails: [
{ address: "mckay@atlantis.net"}
]
}
];
let r, db;
before(async () => {
r = await disposable();
db = new RethinkDBDashDriver(r);
await db._ready();
await db.users.insert(users).run();
});
after(async () => { r.dispose(); });
describe('fetchUserById', () => {
it('returns a matching user if one exists', async () => {
const user = await db.fetchUserById('sheppard');
user.id.should.equal("sheppard");
});
it('returns null on no match', async () => {
const user = await db.fetchUserById('todd');
should.equal(user, null);
});
});
describe('fetchUserByEmail()', () => {
it('returns a matching user if one exists', async () => {
const user = await db.fetchUserByEmail('mckay@atlantis.net');
user.id.should.equal("mckay");
});
it('returns null on no match', async () => {
const user = await db.fetchUserByEmail('non-existing-email');
should.equal(user, null);
});
});
describe('fetchUserByServiceIdOrEmail()', () => {
it('matches by email', async () => {
const user = await db.fetchUserByServiceIdOrEmail('facebook', "no-match", 'mckay@atlantis.net');
user.id.should.equal("mckay");
});
it('matches by service', async () => {
const user = await db.fetchUserByServiceIdOrEmail('facebook', "1", 'non-matching-email');
user.id.should.equal("sheppard");
});
it('should return null on no match', async () => {
const user = await db.fetchUserByServiceIdOrEmail('no-service', 'no-id', 'no-email');
should.equal(user, null);
});
});
describe('assertUserEmailData()', () => {
it('adds a new email address', async () => {
await db.assertUserEmailData('mckay', 'mckay@sgc.mil');
const user = await db.fetchUserByEmail('mckay@sgc.mil');
should.exist(user);
});
it('updates/replaces an existing email address + data', async () => {
const email = 'mckay@atlantis.net';
await db.assertUserEmailData('mckay', email, { verified: true });
const user = await db.fetchUserByEmail(email);
const data = user.emails.find(data => data.address === email);
data.should.deep.equal({ address: email, verified: true });
});
});
describe('assertUserServiceData()', () => {
it('adds a new service record', async () => {
await db.assertUserServiceData('mckay', 'facebook', { id: '5' });
const user = await db.fetchUserByServiceIdOrEmail('facebook', '5', null);
user.services.facebook.id.should.equal('5');
});
it('updates/replaces an existing service record', async () => {
await db.assertUserServiceData('mckay', 'facebook', { id: '5' });
const user = await db.fetchUserByServiceIdOrEmail('facebook', '5', null);
user.services.facebook.id.should.equal('5');
});
});
it('mapUserToServiceData', () => {
const fb = db.mapUserToServiceData(users[0], 'facebook');
fb.should.deep.equal(users[0].services.facebook);
});
});
});
});
|