fixed tests and added tests for passhash
This commit is contained in:
parent
ec0c7a64b5
commit
6fcb9969df
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
32
test/calendarwebapp/testpasshash.d
Normal file
32
test/calendarwebapp/testpasshash.d
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue