2017-09-17 17:52:41 +02:00
|
|
|
module test.calendarwebapp.testauthenticator;
|
|
|
|
|
|
|
|
import calendarwebapp.authenticator;
|
2017-10-27 19:52:49 +02:00
|
|
|
import calendarwebapp.passhash : PasswordHasher, StubPasswordHasher;
|
2017-09-17 17:52:41 +02:00
|
|
|
|
|
|
|
import poodinis;
|
|
|
|
|
|
|
|
import unit_threaded.mock;
|
|
|
|
import unit_threaded.should;
|
|
|
|
|
2017-10-15 16:43:39 +02:00
|
|
|
import vibe.data.bson : Bson, BsonObjectID;
|
2017-09-17 17:52:41 +02:00
|
|
|
|
|
|
|
interface Collection
|
|
|
|
{
|
2017-10-27 17:09:55 +02:00
|
|
|
Bson[] find() @safe;
|
2017-09-17 17:52:41 +02:00
|
|
|
Bson findOne(string[string] query) @safe;
|
2017-10-27 17:09:55 +02:00
|
|
|
void insert(Bson document) @safe;
|
|
|
|
void remove(BsonObjectID[string] selector) @safe;
|
2017-09-17 17:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class CollectionInjector : ValueInjector!Collection
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Collection[string] collections;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void add(string key, Collection collection)
|
|
|
|
{
|
|
|
|
collections[key] = collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
override Collection get(string key) @safe
|
|
|
|
{
|
|
|
|
return collections[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:23:02 +02:00
|
|
|
@("MongoDBAuthenticator.checkUser")
|
2017-09-17 17:52:41 +02:00
|
|
|
@system unittest
|
|
|
|
{
|
|
|
|
auto collection = mock!Collection;
|
|
|
|
auto container = new shared DependencyContainer;
|
|
|
|
container.register!(ValueInjector!Collection, CollectionInjector);
|
|
|
|
container.resolve!CollectionInjector.add("users", collection);
|
|
|
|
container.register!(Authenticator, MongoDBAuthenticator!(Collection))(
|
|
|
|
RegistrationOption.doNotAddConcreteTypeRegistration);
|
2017-10-27 19:52:49 +02:00
|
|
|
container.register!(PasswordHasher, StubPasswordHasher);
|
2017-09-17 17:52:41 +02:00
|
|
|
|
2017-10-26 20:08:16 +02:00
|
|
|
auto userBson = Bson(["_id" : Bson(BsonObjectID.fromString("5988ef4ae6c19089a1a53b79")),
|
|
|
|
"username" : Bson("foo"), "passwordHash"
|
2017-10-27 19:52:49 +02:00
|
|
|
: Bson("bar"),
|
|
|
|
"privilege" : Bson(1)]);
|
2017-09-17 17:52:41 +02:00
|
|
|
|
2017-10-15 16:43:39 +02:00
|
|
|
collection.returnValue!"findOne"(Bson(null), userBson, userBson);
|
2017-09-17 17:52:41 +02:00
|
|
|
|
2017-10-15 16:43:39 +02:00
|
|
|
auto authenticator = container.resolve!(Authenticator);
|
2017-10-26 20:08:16 +02:00
|
|
|
authenticator.checkUser("", "").isNull.shouldBeTrue;
|
|
|
|
authenticator.checkUser("foo", "bar").isNull.shouldBeFalse;
|
|
|
|
authenticator.checkUser("foo", "baz").isNull.shouldBeTrue;
|
2017-09-17 17:52:41 +02:00
|
|
|
}
|
2017-10-26 20:23:02 +02:00
|
|
|
|
|
|
|
@("AuthInfo.isUser success")
|
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
AuthInfo auth;
|
2017-10-27 18:03:55 +02:00
|
|
|
auth.privilege = Privilege.User;
|
2017-10-26 20:23:02 +02:00
|
|
|
auth.isUser.shouldBeTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@("AuthInfo.isUser failure")
|
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
AuthInfo auth;
|
2017-10-27 18:03:55 +02:00
|
|
|
auth.privilege = Privilege.None;
|
2017-10-26 20:23:02 +02:00
|
|
|
auth.isUser.shouldBeFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
@("AuthInfo.isAdmin success")
|
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
AuthInfo auth;
|
2017-10-27 18:03:55 +02:00
|
|
|
auth.privilege = Privilege.Admin;
|
2017-10-26 20:23:02 +02:00
|
|
|
auth.isAdmin.shouldBeTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@("AuthInfo.isAdmin failure")
|
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
AuthInfo auth;
|
2017-10-27 18:03:55 +02:00
|
|
|
auth.privilege = Privilege.None;
|
2017-10-26 20:23:02 +02:00
|
|
|
auth.isAdmin.shouldBeFalse;
|
|
|
|
}
|
2017-10-27 18:03:55 +02:00
|
|
|
|
|
|
|
@("AuthInfo.isNone success")
|
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
AuthInfo auth;
|
|
|
|
auth.privilege = Privilege.None;
|
|
|
|
auth.isNone.shouldBeTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@("AuthInfo.isNone failure")
|
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
AuthInfo auth;
|
|
|
|
auth.privilege = Privilege.User;
|
|
|
|
auth.isNone.shouldBeFalse;
|
2017-10-27 19:52:49 +02:00
|
|
|
}
|