fixed tests and added tests for passhash

This commit is contained in:
Johannes Loher 2017-10-27 19:52:49 +02:00
parent ec0c7a64b5
commit 6fcb9969df
3 changed files with 52 additions and 4 deletions

View file

@ -5,7 +5,7 @@ import poodinis;
interface PasswordHasher
{
string generateHash(in string password) @safe;
bool checkHash(in string password, in string hash) @safe;
bool checkHash(in string password, in string hash) const @safe;
}
class BcryptPasswordHasher : PasswordHasher
@ -18,7 +18,7 @@ class BcryptPasswordHasher : PasswordHasher
return (() @trusted => generateBcrypt(password, rng, cost))();
}
bool checkHash(in string password, in string hash) @safe
bool checkHash(in string password, in string hash) const @safe
{
return (()@trusted => checkBcrypt(password, hash))();
}
@ -27,3 +27,16 @@ private:
@Autowire RandomNumberGenerator rng;
enum cost = 10;
}
class StubPasswordHasher : PasswordHasher
{
string generateHash(in string password) const @safe pure nothrow
{
return password;
}
bool checkHash(in string password, in string hash) const @safe pure nothrow
{
return password == hash;
}
}

View file

@ -1,6 +1,7 @@
module test.calendarwebapp.testauthenticator;
import calendarwebapp.authenticator;
import calendarwebapp.passhash : PasswordHasher, StubPasswordHasher;
import poodinis;
@ -43,10 +44,12 @@ public:
container.resolve!CollectionInjector.add("users", collection);
container.register!(Authenticator, MongoDBAuthenticator!(Collection))(
RegistrationOption.doNotAddConcreteTypeRegistration);
container.register!(PasswordHasher, StubPasswordHasher);
auto userBson = Bson(["_id" : Bson(BsonObjectID.fromString("5988ef4ae6c19089a1a53b79")),
"username" : Bson("foo"), "passwordHash"
: Bson("$2a$10$9LBqOZV99ARiE4Nx.2b7GeYfqk2.0A32PWGu2cRGyW2hRJ0xeDfnO"), "privilege" : Bson(1)]);
: Bson("bar"),
"privilege" : Bson(1)]);
collection.returnValue!"findOne"(Bson(null), userBson, userBson);
@ -102,4 +105,4 @@ public:
AuthInfo auth;
auth.privilege = Privilege.User;
auth.isNone.shouldBeFalse;
}
}

View file

@ -0,0 +1,32 @@
module test.calendarwebapp.testpasshash;
import calendarwebapp.passhash;
import poodinis;
//import unit_threaded.should;
import unit_threaded;
@("BcryptPasswordHasher")
@Values("", "test", "langesKompliziertesPasswort")
@system unittest
{
import botan.rng.rng : RandomNumberGenerator;
import botan.rng.auto_rng : AutoSeededRNG;
auto container = new shared DependencyContainer;
container.register!(RandomNumberGenerator, AutoSeededRNG);
container.register!(PasswordHasher, BcryptPasswordHasher);
auto hasher = container.resolve!PasswordHasher;
immutable testPassword = getValue!string;
hasher.checkHash(testPassword, hasher.generateHash(testPassword)).shouldBeTrue;
}
@("StubPasswordHasher")
@Values("", "test", "langesKompliziertesPasswort")
@safe unittest
{
immutable hasher = new StubPasswordHasher;
immutable testPassword = getValue!string;
hasher.checkHash(testPassword, hasher.generateHash(testPassword)).shouldBeTrue;
}