Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ced23aa

Browse files
thimovssrngadam
authored andcommitted
Fixed and added tests for associateEntry
1 parent 5578728 commit ced23aa

File tree

3 files changed

+123
-37
lines changed

3 files changed

+123
-37
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Meteor } from 'meteor/meteor';
2+
import { chai } from 'meteor/practicalmeteor:chai';
3+
import { associateEntry } from '../../../server/api/arpTable.js';
4+
import '../../../lib/collections/ARPEntries.js';
5+
6+
var assert = chai.assert,
7+
expect = chai.expect;
8+
9+
var ARPEntries = App.Collections.ARPEntries;
10+
11+
const entries = [{
12+
IP: '192.168.1.11',
13+
MAC: '8C:3A:E3:93:83:93',
14+
updatedAt: new Date()
15+
}, {
16+
IP: '192.168.1.50',
17+
MAC: '00:26:BB:07:54:80',
18+
updatedAt: new Date()
19+
}, {
20+
IP: '192.168.1.18',
21+
MAC: '3C:15:C2:CC:DC:2A',
22+
updatedAt: new Date()
23+
}];
24+
25+
const ExtraEntry = {
26+
IP: '192.168.1.19',
27+
MAC: '3C:15:C2:CC:DC:2B',
28+
updatedAt: new Date()
29+
};
30+
31+
const MissingMAC = {
32+
IP: '192.168.1.19',
33+
updatedAt: new Date()
34+
};
35+
36+
const MissingIP = {
37+
MAC: '3C:15:C2:CC:DC:2B',
38+
updatedAt: new Date()
39+
};
40+
41+
const MissingUpdatedAt = {
42+
IP: '192.168.1.19',
43+
MAC: '3C:15:C2:CC:DC:2B',
44+
};
45+
46+
describe('associateEntry', function() {
47+
beforeEach(function() {
48+
ARPEntries.remove({});
49+
entries.forEach((entry) => {
50+
ARPEntries.insert(entry);
51+
});
52+
});
53+
54+
it('adds the entry if it is not present yet',function(){
55+
associateEntry(ExtraEntry);
56+
assert.equal(ARPEntries.find({'MAC': ExtraEntry.MAC}).count(), 1);
57+
assert.equal(ARPEntries.find().count(), 4);
58+
});
59+
60+
it('doesn\'t add the the entry if it is present',function(){
61+
associateEntry(entries[0]);
62+
assert.equal(ARPEntries.find().count(), 3);
63+
});
64+
65+
it('doesn\'t add the the entry if it is missing the MAC address',function(){
66+
associateEntry(MissingMAC);
67+
assert.equal(ARPEntries.find().count(), 3);
68+
});
69+
70+
it('doesn\'t add the the entry if it is missing the IP address',function(){
71+
associateEntry(MissingIP);
72+
assert.equal(ARPEntries.find().count(), 3);
73+
});
74+
75+
it('doesn\'t add the the entry if it is missing the updatedAt field',function(){
76+
associateEntry(MissingUpdatedAt);
77+
assert.equal(ARPEntries.find().count(), 3);
78+
});
79+
80+
});

app/imports/server/api/clearExpiredEntries.test.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import '../../../lib/collections/ARPEntries.js';
66
var assert = chai.assert,
77
expect = chai.expect;
88

9+
var ARPEntries = App.Collections.ARPEntries;
10+
911
const entries = [{
1012
IP: '192.168.1.11',
1113
MAC: '8C:3A:E3:93:83:93',
@@ -20,41 +22,43 @@ const entries = [{
2022
updatedAt: new Date()
2123
}];
2224

25+
const extraEntry = {
26+
IP: '192.168.1.19',
27+
MAC: '3C:15:C2:CC:DC:2B',
28+
updatedAt: new Date()
29+
};
30+
2331
describe('clearExpiredEntries', function() {
2432

2533
beforeEach(function() {
26-
App.Collections.ARPEntries.remove({});
34+
ARPEntries.remove({});
2735
entries.forEach((entry) => {
28-
App.Collections.ARPEntries.insert(entry);
36+
ARPEntries.insert(entry);
2937
});
3038
});
3139

3240
it('does nothing if nothing changed', function() {
3341
clearExpiredEntries(entries);
34-
assert.equal(App.Collections.ARPEntries.find().count(), 3);
42+
assert.equal(ARPEntries.find().count(), 3);
3543
});
3644

3745
it('removes 1 entries that has expired', function() {
3846
clearExpiredEntries([entries[0], entries[1]]);
39-
assert.equal(App.Collections.ARPEntries.find().count(), 2);
47+
assert.equal(ARPEntries.find().count(), 2);
4048
});
4149

4250
it('removes 2 entries that have expired', function() {
4351
clearExpiredEntries([entries[0]]);
44-
assert.equal(App.Collections.ARPEntries.find().count(), 1);
52+
assert.equal(ARPEntries.find().count(), 1);
4553
});
4654

4755
it('removes all entries that have expired', function() {
4856
clearExpiredEntries([]);
49-
assert.equal(App.Collections.ARPEntries.find().count(), 0);
57+
assert.equal(ARPEntries.find().count(), 0);
5058
});
5159

5260
it('doesn\'t add new entries', function() {
53-
clearExpiredEntries([entries[0], entries[1], entries[2], {
54-
IP: '192.168.1.19',
55-
MAC: '3C:15:C2:CC:DC:2B',
56-
updatedAt: new Date()
57-
}]);
58-
assert.equal(App.Collections.ARPEntries.find().count(), 3);
61+
clearExpiredEntries([entries[0], entries[1], entries[2], extraEntry]);
62+
assert.equal(ARPEntries.find().count(), 3);
5963
});
6064
});

app/server/api/arpTable.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,40 @@ var clearExpiredEntries = function(entries) {
6363
}
6464

6565
var associateEntry = function(entry) {
66-
var ARPEntry = ARPEntries.findOne({ MAC: entry.MAC });
66+
if (entry.MAC && entry.IP && entry.updatedAt) {
67+
var ARPEntry = ARPEntries.findOne({ MAC: entry.MAC });
6768

68-
// A new user connected to the router
69-
// If MAC address doesn't exist in mongodb, add it
70-
if (!ARPEntry) {
69+
// A new user connected to the router
70+
// If MAC address doesn't exist in mongodb, add it
71+
if (!ARPEntry) {
7172

72-
var response;
73+
var response;
7374

74-
try {
75-
response = HTTP.call("POST", "http://api.macvendors.com/" + entry.MAC);
76-
} catch (e) {
77-
// Got a network error, time-out or HTTP error in the 400 or 500 range.
78-
// Do nothing continue
79-
}
75+
try {
76+
response = HTTP.call("POST", "http://api.macvendors.com/" + entry.MAC);
77+
} catch (e) {
78+
// Got a network error, time-out or HTTP error in the 400 or 500 range.
79+
// Do nothing continue
80+
}
8081

81-
if (response && response.content) {
82-
entry.company = response.content;
83-
} else {
84-
console.log('error: could not get response from macvendors');
85-
console.log(response);
86-
}
82+
if (response && response.content) {
83+
entry.company = response.content;
84+
} else {
85+
console.log('error: could not get response from macvendors');
86+
console.log(response);
87+
}
8788

88-
ARPEntries.insert(entry);
89-
}
89+
ARPEntries.insert(entry);
90+
}
9091

91-
// The user changed IP address
92-
// If MAC address exists in mongodb and different IP address, update entry
93-
if (ARPEntry) {
94-
if (ARPEntry.IP !== entry.IP) {
95-
ARPEntries.update(ARPEntry, { $set: { IP: entry.IP } });
92+
// The user changed IP address
93+
// If MAC address exists in mongodb and different IP address, update entry
94+
if (ARPEntry) {
95+
if (ARPEntry.IP !== entry.IP) {
96+
ARPEntries.update(ARPEntry, { $set: { IP: entry.IP } });
97+
}
9698
}
9799
}
98100
}
99101

100-
export { getEntries, clearExpiredEntries, associateEntry };
102+
export { getEntries, clearExpiredEntries, associateEntry };

0 commit comments

Comments
 (0)