-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
82 lines (72 loc) · 1.9 KB
/
model.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
var crypto = require('crypto');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect(process.env.MONGO_CONNECT || 'mongodb://localhost/hailstorm');
function hash(password) {
return crypto.createHash('sha1').update(password).digest('hex');
}
var Report = new Schema({
test_run_id : String,
status_code : Number,
method : String,
path : String,
end_time : Number,
start_time : Number,
count : Number,
last_update : Date
});
Report.virtual('response_time').get(function(){
return this.end_time - this.start_time;
});
var TestRun = new Schema({});
var Test = new Schema({
host : String,
port : Number,
protocol : String,
verified : Boolean,
requests : String,
test_runs : [TestRun],
running : Boolean,
yeti : String
});
var Account = new Schema({
username : String,
password : { type:String, set:hash },
tests : [Test]
});
Account.statics.find_by_username_and_password = function(username, password, cb){
this.find({ username:username, password:hash(password) }, function(err,docs){
if(err) {
cb(err);
} else {
if(docs.length == 0) {
cb('Invalid login');
} else {
cb(null, docs[0]);
}
}
})
};
exports.Test = mongoose.model('Test', Test);
exports.TestRun = mongoose.model('TestRun', TestRun);
exports.Account = mongoose.model('Account', Account);
exports.Report = mongoose.model('Report', Report);
exports.create_account = function(username, password, cb) {
var account = new exports.Account({ username:username, password:password });
account.save(function(err){
if(err) {
cb(err);
} else {
cb(null, account);
}
});
};
exports.does_username_exist = function(username, cb) {
exports.Account.find({ username:username }, function(err, docs){
if(err) {
cb(err);
} else {
cb(err, (docs.length > 0));
}
});
};