testRouter

Undocumented in source. Be warned that the author may not have intended to support it.
version(unittest)
testRouter
(
bool requireLogin = true
)

Examples

it should return 401 on missing auth

testRouter.request.get("/sites").expectStatusCode(401).end();

it should return 200 on valid credentials

auto router = testRouter;

router
  .request.get("/sites")
  .header("Authorization", "Bearer " ~ bearerToken.name)
  .expectStatusCode(200)
  .end;

it should set the email on valid mandatory credentials

auto router = testRouter;

router
  .request.get("/email")
  .header("Authorization", "Bearer " ~ bearerToken.name)
  .expectStatusCode(200)
  .end((Response response) => {
    response.bodyString.should.equal("user@gmail.com");
  });

it should return 200 on missing auth when it's not mandatory

auto router = testRouter(false);

router
  .request.get("/sites")
  .expectStatusCode(200)
  .end;

it should clear the username and email when auth it's not mandatory

auto router = testRouter(false);

void setUser(HTTPServerRequest req, HTTPServerResponse res) {
  req.username = "some user";
  req.password = "some password";
  req.context["email"] = "some random value";
}

void showAuth(HTTPServerRequest req, HTTPServerResponse res) {
  res.statusCode = 200;
  string hasEmail = "email" in req.context ? "yes" : "no";
  res.writeBody(req.username ~ ":" ~ req.password ~ ":" ~ hasEmail);
}

router.any("*", &setUser);
router.any("*", &auth.permisiveAuth);
router.get("/misc", &showAuth);

router
  .request.get("/misc")
  .expectStatusCode(200)
  .end((Response response) => {
    response.bodyString.should.equal("::no");
  });

it should return 200 on valid auth when it's not mandatory

auto router = testRouter(false);

router
  .request.get("/sites")
  .header("Authorization", "Bearer " ~ bearerToken.name)
  .expectStatusCode(200)
  .end;

it should set the email on valid credentials when they are not mandatory

auto router = testRouter(false);

router
  .request.get("/email")
  .header("Authorization", "Bearer " ~ bearerToken.name)
  .expectStatusCode(200)
  .end((Response response) => {
    response.bodyString.should.equal("user@gmail.com");
  });

it should return 401 on invalid auth when it's not mandatory

auto router = testRouter(false);

router
  .request.get("/sites")
  .header("Authorization", "Bearer invalid")
  .expectStatusCode(401)
  .end;

it should return 401 on invalid credentials

testRouter
  .request.post("/auth/token")
  .send(["grant_type": "password", "username": "invalid", "password": "invalid"])
  .expectStatusCode(401)
  .end((Response response) => {
    response.bodyJson.should.equal(`{ "error": "Invalid password or username" }`.parseJsonString);
  });

it should return tokens on valid email and password

testRouter
  .request
  .post("/auth/token")
  .send(["grant_type": "password", "username": "user@gmail.com", "password": "password"])
  .expectStatusCode(200)
  .end((Response response) => {
    response.bodyJson.keys.should.contain(["access_token", "expires_in", "refresh_token", "token_type"]);

    user.isValidToken(response.bodyJson["access_token"].to!string).should.be.equal(true);
    user.isValidToken(response.bodyJson["refresh_token"].to!string).should.be.equal(true);

    response.bodyJson["token_type"].to!string.should.equal("Bearer");
    response.bodyJson["expires_in"].to!int.should.equal(3600);
  });

it should return tokens on valid username and password

testRouter
  .request
  .post("/auth/token")
  .send(["grant_type": "password", "username": "test", "password": "password"])
  .expectStatusCode(200)
  .end((Response response) => {
    response.bodyJson.keys.should.contain(["access_token", "expires_in", "refresh_token", "token_type"]);

    user.isValidToken(response.bodyJson["access_token"].to!string).should.be.equal(true);
    user.isValidToken(response.bodyJson["refresh_token"].to!string).should.be.equal(true);

    response.bodyJson["token_type"].to!string.should.equal("Bearer");
    response.bodyJson["expires_in"].to!int.should.equal(3600);
  });

it should set the scope tokens on valid credentials

testRouter
  .request
  .post("/auth/token")
  .send(["grant_type": "password", "username": "user@gmail.com", "password": "password", "scope": "access1 access2"])
  .expectStatusCode(200)
  .end((Response response) => {
    user.isValidToken(response.bodyJson["refresh_token"].to!string, "refresh").should.equal(true);
    user.isValidToken(response.bodyJson["refresh_token"].to!string, "other").should.equal(false);

    user.isValidToken(response.bodyJson["access_token"].to!string, "access1").should.equal(true);
    user.isValidToken(response.bodyJson["access_token"].to!string, "access2").should.equal(true);
    user.isValidToken(response.bodyJson["access_token"].to!string, "other").should.equal(false);
  });

it should return a new access token on refresh token

auto router = testRouter;

router
  .request
  .post("/auth/token")
  .send(["grant_type": "refresh_token", "refresh_token": refreshToken.name ])
  .expectStatusCode(200)
  .end((Response response) => {
    response.bodyJson.keys.should.contain(["access_token", "expires_in", "token_type"]);

    user.isValidToken(response.bodyJson["access_token"].to!string).should.be.equal(true);
    user.isValidToken(response.bodyJson["access_token"].to!string, "doStuff").should.be.equal(true);
    user.isValidToken(response.bodyJson["access_token"].to!string, "refresh").should.be.equal(false);

    response.bodyJson["token_type"].to!string.should.equal("Bearer");
    response.bodyJson["expires_in"].to!int.should.equal(3600);
  });

it should be able to not block the requests without login

auto router = testRouter(false);

router
  .request
  .get("/path")
  .expectStatusCode(404)
  .end();

it should return 404 for GET on revocation path

auto router = testRouter(false);

router
  .request
  .get("/auth/revoke")
  .expectStatusCode(404)
  .end();

it should return 400 for POST on revocation path with missing token

auto router = testRouter(false);

router
  .request
  .post("/auth/revoke")
  .expectStatusCode(400)
  .end((Response response) => {
    response.bodyJson.should.equal("{
      \"error\": \"You must provide a `token` parameter.\"
    }".parseJsonString);
  });

Meta