-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdatabase.devices.js
152 lines (123 loc) · 6.48 KB
/
database.devices.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
const should = require('should');
const flow = require('nimble');
const httpStatus = require('http-status');
const requireNew = require('require-new');
const wipe = require('./fixture-helpers/wipe');
const setup = require('./fixture-helpers/setup');
const DuplicateRecordError = require('../lib/errors').DuplicateRecordError;
const JSendError = require('jsend-utils').JSendError;
const JSendClientError = require('jsend-utils').JSendClientError;
describe("Database", function() {
const user1 = requireNew('./fixtures/user1.json');
const user2 = requireNew('./fixtures/user2.json');
const product4 = requireNew('./fixtures/product4.json');
before(function(initDone) {
flow.series(
[
wipe.wipeAllData,
function(done) {
setup.createUser(user1, done);
},
function(done) {
setup.createUser(user2, done);
},
function(done) {
setup.createProduct(product4, done);
}
],
initDone
);
});
describe("Devices", function() {
const device5 = requireNew('./fixtures/device5.json');
describe("Create", function() {
it("Should be able to create a device", function(done) {
global.db.devices.create(device5, product4.id, user1.id, function(err, device) {
should.not.exist(err);
should.exist(device);
device.should.have.property('insertId');
device.should.have.property('serialNumber', device5.serialNumber);
// remember the insert ID
device5.id = device.insertId;
done();
});
});
it("Should be able to create the same device, but for a different user", function(done) {
global.db.devices.create(device5, product4.id, user2.id, function(err, device) {
should.not.exist(err);
should.exist(device);
device.should.have.property('insertId');
device.should.have.property('serialNumber', device5.serialNumber);
done();
});
});
it("Should fail to create a duplicate device for the same user", function(done) {
global.db.devices.create(device5, product4.id, user1.id, function(err, device) {
should.exist(err);
should.not.exist(device);
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 device by ID and user ID", function(done) {
global.db.devices.findByIdForUser(device5.id, user1.id, null, function(err, device) {
should.not.exist(err);
should.exist(device);
device.should.have.properties({
"id" : device5.id,
"serialNumber" : device5.serialNumber,
"productId" : product4.id,
"userId" : user1.id
});
device.should.have.properties('created', 'modified');
done();
});
});
it("Should fail to find a device by ID and user ID if the device ID is wrong", function(done) {
global.db.devices.findByIdForUser(-1, user1.id, null, function(err, device) {
should.not.exist(err);
should.not.exist(device);
done();
});
});
it("Should fail to find a device by ID and user ID if the user ID is wrong", function(done) {
global.db.devices.findByIdForUser(device5.id, -1, null, function(err, device) {
should.exist(err);
should.not.exist(device);
err.should.be.an.instanceOf(JSendError);
err.should.be.an.instanceOf(JSendClientError);
err.should.have.property('data');
err.data.should.have.properties({
code : httpStatus.FORBIDDEN,
status : 'error'
});
done();
});
});
it("Should be able to find a device by product ID, serial number, and user ID", function(done) {
global.db.devices.findByProductIdAndSerialNumberForUser(product4.id,
device5.serialNumber,
user1.id,
'id,serialNumber,productId,userId',
function(err, device) {
if (err) {
return done(err);
}
device.should.have.properties({
id : device5.id,
serialNumber : device5.serialNumber,
productId : product4.id,
userId : user1.id
});
// shouldn't have these properties since I didn't ask for them
device.should.not.have.properties('created', 'modified');
done();
});
});
}); // End Find
}); // End Devices
}); // End Database