authelia/test/unit/server/AuthenticationRegulator.tes...

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2017-05-16 21:17:46 +00:00
import { AuthenticationRegulator } from "../../../src/server/lib/AuthenticationRegulator";
import UserDataStore from "../../../src/server/lib/UserDataStore";
2017-05-20 20:55:37 +00:00
import MockDate = require("mockdate");
import exceptions = require("../../../src/server/lib/Exceptions");
2017-05-21 10:14:59 +00:00
import nedb = require("nedb");
2017-05-16 21:17:46 +00:00
describe("test authentication regulator", function() {
it("should mark 2 authentication and regulate (resolve)", function() {
const options = {
inMemoryOnly: true
};
2017-05-21 10:14:59 +00:00
const data_store = new UserDataStore(options, nedb);
2017-05-16 21:17:46 +00:00
const regulator = new AuthenticationRegulator(data_store, 10);
const user = "user";
return regulator.mark(user, false)
.then(function() {
return regulator.mark(user, true);
})
.then(function() {
return regulator.regulate(user);
});
});
it("should mark 3 authentications and regulate (reject)", function(done) {
const options = {
inMemoryOnly: true
};
2017-05-21 10:14:59 +00:00
const data_store = new UserDataStore(options, nedb);
2017-05-16 21:17:46 +00:00
const regulator = new AuthenticationRegulator(data_store, 10);
const user = "user";
regulator.mark(user, false)
.then(function() {
return regulator.mark(user, false);
})
.then(function() {
return regulator.mark(user, false);
})
.then(function() {
return regulator.regulate(user);
})
.catch(exceptions.AuthenticationRegulationError, function() {
done();
});
});
it("should mark 3 authentications and regulate (resolve)", function(done) {
const options = {
inMemoryOnly: true
};
2017-05-21 10:14:59 +00:00
const data_store = new UserDataStore(options, nedb);
2017-05-16 21:17:46 +00:00
const regulator = new AuthenticationRegulator(data_store, 10);
const user = "user";
MockDate.set("1/2/2000 00:00:00");
regulator.mark(user, false)
.then(function() {
MockDate.set("1/2/2000 00:00:15");
return regulator.mark(user, false);
})
.then(function() {
return regulator.mark(user, false);
})
.then(function() {
return regulator.regulate(user);
})
.then(function() {
done();
});
});
});