-
Notifications
You must be signed in to change notification settings - Fork 9
/
database.feeds.js
177 lines (140 loc) · 6.56 KB
/
database.feeds.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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 shallowClone = require('./fixture-helpers/test-utils').shallowClone;
const DatabaseError = require('../lib/errors').DatabaseError;
const ValidationError = require('../lib/errors').ValidationError;
describe("Database", function() {
const user1 = requireNew('./fixtures/user1.json');
const product4 = requireNew('./fixtures/product4.json');
const device5 = requireNew('./fixtures/device5.json');
before(function(initDone) {
flow.series(
[
wipe.wipeAllData,
function(done) {
setup.createUser(user1, done);
},
function(done) {
setup.createProduct(product4, done);
},
function(done) {
device5.userId = user1['id'];
device5.productId = product4['id'];
setup.createDevice(device5, done);
}
],
initDone
);
});
describe("Feeds", function() {
const feed0 = requireNew('./fixtures/feed0.json');
describe("Create", function() {
it("Should be able to create a feed", function(done) {
global.db.feeds.create(feed0, device5['id'], product4['id'], user1['id'], function(err, feed) {
should.not.exist(err);
should.exist(feed);
feed.should.have.property('insertId');
feed.should.have.property('apiKey');
feed.should.have.property('apiKeyReadOnly');
// remember these for later
feed0['id'] = feed.insertId;
feed0.apiKey = feed.apiKey;
feed0.apiKeyReadOnly = feed.apiKeyReadOnly;
done();
});
});
it("Should fail to create a feed with an invalid name", function(done) {
const invalidFeed = shallowClone(feed0);
invalidFeed.name = "";
global.db.feeds.create(invalidFeed, device5['id'], product4['id'], user1['id'], function(err, feed) {
should.exist(err);
should.not.exist(feed);
console.log(JSON.stringify(err, null, 3));
err.should.be.an.instanceOf(ValidationError);
err.should.have.property('data');
err.data.errors.should.have.length(1);
err.data.errors[0].should.have.properties({
"keyword" : "required",
"dataPath" : "",
"schemaPath" : "#/required",
"params" : {
"missingProperty" : "name"
}
});
done();
});
});
it("Should fail to create a feed with an invalid user id", function(done) {
global.db.feeds.create(feed0, device5['id'], product4['id'], -1, function(err, feed) {
should.exist(err);
should.not.exist(feed);
err.should.be.an.instanceOf(DatabaseError);
done();
});
});
it("Should fail to create a feed with an invalid product id", function(done) {
global.db.feeds.create(feed0, device5['id'], -1, user1['id'], function(err, feed) {
should.exist(err);
should.not.exist(feed);
err.should.be.an.instanceOf(ValidationError);
err.should.have.property('data');
err.data.errors.should.have.length(1);
err.data.errors[0].should.have.properties({
keyword : 'type',
dataPath : '.channelSpecs',
schemaPath : '#/properties/channelSpecs/type',
params : { type : 'string' }
});
done();
});
});
it("Should fail to create a feed with an invalid device id", function(done) {
global.db.feeds.create(feed0, -1, product4['id'], user1['id'], function(err, feed) {
should.exist(err);
should.not.exist(feed);
err.should.be.an.instanceOf(DatabaseError);
done();
});
});
}); // End Create
describe("Find", function() {
const verifyResult = function(err, feed) {
should.not.exist(err);
should.exist(feed);
feed.should.have.properties(feed0);
feed.should.have.properties({
deviceId : device5['id'],
productId : product4['id'],
userId : user1['id'],
channelSpecs : product4['defaultChannelSpecs'],
channelBounds : null
});
feed.should.have.properties('id', 'apiKey', 'apiKeyReadOnly', 'created', 'modified', 'lastUpload', 'minTimeSecs', 'maxTimeSecs');
};
it("Should be able to find a feed by ID", function(done) {
global.db.feeds.findById(feed0['id'], null, function(err, feed) {
verifyResult(err, feed);
// remember the API keys for the next tests
feed0.apiKey = feed.apiKey;
feed0.apiKeyReadOnly = feed.apiKeyReadOnly;
done();
});
});
it("Should be able to find a feed by apiKey", function(done) {
global.db.feeds.findByApiKey(feed0.apiKey, null, function(err, feed) {
verifyResult(err, feed);
done();
});
});
it("Should be able to find a feed by apiKeyReadOnly", function(done) {
global.db.feeds.findByApiKey(feed0.apiKeyReadOnly, null, function(err, feed) {
verifyResult(err, feed);
done();
});
});
}); // End Find
}); // End Feeds
}); // End Database