-
Notifications
You must be signed in to change notification settings - Fork 9
/
database.clients.js
156 lines (123 loc) · 5.59 KB
/
database.clients.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
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
const should = require('should');
const flow = require('nimble');
const requireNew = require('require-new');
const wipe = require('./fixture-helpers/wipe');
const setup = require('./fixture-helpers/setup');
const DuplicateRecordError = require('../lib/errors').DuplicateRecordError;
describe("Database", function() {
const user1 = requireNew('./fixtures/user1.json');
before(function(initDone) {
flow.series(
[
wipe.wipeAllData,
function(done) {
setup.createUser(user1, done);
}
],
initDone
);
});
describe("Clients", function() {
const client1 = requireNew('./fixtures/client1.json');
const client2 = requireNew('./fixtures/client2.json');
const client3 = requireNew('./fixtures/client3.json');
describe("Create", function() {
it("Should be able to create a client (with a null creatorUserId)", function(done) {
global.db.clients.create(client1, null, function(err, result) {
should.not.exist(err);
should.exist(result);
result.should.have.property("insertId");
result.should.have.properties({
displayName : client1.displayName,
clientName : client1.clientName
});
done();
});
});
it("Should be able to create a different client (with a non-null creatorUserId)", function(done) {
// make user1 the creator of this client
global.db.clients.create(client2, user1.id, function(err, result) {
should.not.exist(err);
should.exist(result);
result.should.have.property("insertId");
result.should.have.properties({
displayName : client2.displayName,
clientName : client2.clientName
});
done();
});
});
it("Should be able to create a another client (with a null creatorUserId)", function(done) {
global.db.clients.create(client3, null, function(err, result) {
should.not.exist(err);
should.exist(result);
result.should.have.property("insertId");
result.should.have.properties({
displayName : client3.displayName,
clientName : client3.clientName
});
done();
});
});
it("A client created with a null creatorUserId and no isPublic field will be forced to be public", function(done) {
global.db.clients.findByNameAndSecret(client1.clientName, client1.clientSecret, function(err, client) {
should.not.exist(err);
should.exist(client);
client.should.have.property("isPublic", 1);
done();
})
});
it("A client created with a null creatorUserId and a false isPublic field will be forced to be public", function(done) {
global.db.clients.findByNameAndSecret(client3.clientName, client3.clientSecret, function(err, client) {
should.not.exist(err);
should.exist(client);
client.should.have.property("isPublic", 1);
done();
})
});
it("Should not be able to create a duplicate client", function(done) {
global.db.clients.create(client1, null, function(err, result) {
should.exist(err);
should.not.exist(result);
err.should.be.an.instanceOf(DuplicateRecordError);
err.should.have.property("data");
err.data.should.have.property("code", "ER_DUP_ENTRY");
done();
});
});
}); // End Create
describe("Find", function() {
it("Should be able to find a client by name and secret", function(done) {
global.db.clients.findByNameAndSecret(client1.clientName, client1.clientSecret, function(err, client) {
if (err) {
return done(err);
}
client.should.have.properties(["id", "clientSecret", "created"]);
client.should.have.properties({
displayName : client1.displayName,
clientName : client1.clientName
});
done();
});
});
it("Should not be able to find a client by name and secret with a non-existent name", function(done) {
global.db.clients.findByNameAndSecret("bogus", client1.clientSecret, function(err, client) {
if (err) {
return done(err);
}
should.not.exist(client);
done();
});
});
it("Should not be able to find a client by name and secret with an incorrect secret", function(done) {
global.db.clients.findByNameAndSecret(client1.clientName, "bogus", function(err, client) {
if (err) {
return done(err);
}
should.not.exist(client);
done();
});
});
}); // End Find
}); // End Clients
}); // End Database