authelia/test/unitary/user_data_store/totp_secret.test.ts

74 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-05-16 21:17:46 +00:00
import * as assert from "assert";
import * as Promise from "bluebird";
import * as sinon from "sinon";
import * as MockDate from "mockdate";
import UserDataStore from "../../../src/lib/UserDataStore";
2017-05-21 10:14:59 +00:00
import nedb = require("nedb");
2017-05-16 21:17:46 +00:00
describe("test user data store", function() {
describe("test totp secrets store", test_totp_secrets);
});
function test_totp_secrets() {
it("should save and reload a totp secret", 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 userid = "user";
const secret = {
ascii: "abc",
base32: "ABCDKZLEFZGREJK",
otpauth_url: "totp://test"
};
return data_store.set_totp_secret(userid, secret)
.then(function() {
return data_store.get_totp_secret(userid);
})
.then(function(doc) {
assert("_id" in doc);
assert.equal(doc.userid, "user");
assert.equal(doc.secret.ascii, "abc");
assert.equal(doc.secret.base32, "ABCDKZLEFZGREJK");
return Promise.resolve();
});
});
it("should only remember last secret", 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 userid = "user";
const secret1 = {
ascii: "abc",
base32: "ABCDKZLEFZGREJK",
otpauth_url: "totp://test"
};
const secret2 = {
ascii: "def",
base32: "XYZABC",
otpauth_url: "totp://test"
};
return data_store.set_totp_secret(userid, secret1)
.then(function() {
return data_store.set_totp_secret(userid, secret2);
})
.then(function() {
return data_store.get_totp_secret(userid);
})
.then(function(doc) {
assert("_id" in doc);
assert.equal(doc.userid, "user");
assert.equal(doc.secret.ascii, "def");
assert.equal(doc.secret.base32, "XYZABC");
return Promise.resolve();
});
});
}