From d701460e91b5fc18a3ca9308e59a071e1d2da8ef Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sun, 23 Apr 2017 00:19:47 -0500 Subject: [PATCH] Start adding actual HTTP tests. --- src/test/json/strawboss.config.json | 2 +- src/test/nim/tserver.nim | 52 ++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/test/json/strawboss.config.json b/src/test/json/strawboss.config.json index e608d60..8917904 100644 --- a/src/test/json/strawboss.config.json +++ b/src/test/json/strawboss.config.json @@ -3,7 +3,7 @@ "authSecret": "change me", "debug": true, "users": [ - { "name": "bob@builder.com", "hashedPwd": "testvalue" }, + { "name": "bob@builder.com", "hashedPwd": "$2a$11$lVZ9U4optQMhzPh0E9A7Yu6XndXblUF3gCa.zmEvJy4F.4C4718b." }, { "name": "sam@sousa.com", "hashedPwd": "testvalue" } ], "pwdCost": 11, diff --git a/src/test/nim/tserver.nim b/src/test/nim/tserver.nim index 21b86ff..7456529 100644 --- a/src/test/nim/tserver.nim +++ b/src/test/nim/tserver.nim @@ -1,21 +1,35 @@ -import times, unittest +import asyncdispatch, httpclient, os, osproc, strutils, times, unittest import ./testutil import ../../main/nim/strawbosspkg/configuration import ../../main/nim/strawbosspkg/server +import ../../main/nim/strawbosspkg/private/util -let testuser = UserRef( # note: needs to correspond to an actual user - name: "bob@builder.com", - hashedPwd: "$2a$11$lVZ9U4optQMhzPh0E9A7Yu6XndXblUF3gCa.zmEvJy4F.4C4718b.") - -let cfg = loadStrawBossConfig("src/test/json/strawboss.config.json") - -## UNIT TESTS suite "strawboss server": + + # suite setup code + let cfgFilePath = "src/test/json/strawboss.config.json" + let cfg = loadStrawBossConfig(cfgFilePath) + + discard startProcess("./strawboss", ".", @["serve", "-c", cfgFilePath], loadEnv(), {poUsePath}) + + let http = newHttpClient() + let apiBase = "http://localhost:8180/api" + + let testuser = UserRef( # note: needs to correspond to an actual user + name: "bob@builder.com", + hashedPwd: "$2a$11$lVZ9U4optQMhzPh0E9A7Yu6XndXblUF3gCa.zmEvJy4F.4C4718b.") + + # give the server time to spin up + sleep(100) + + ## UNIT TESTS + test "can validate hashed pwd": + check validatePwd(testuser, "password") test "can detect invalid pwds": - check (not validatePwd(testuser, "Password")) + check(not validatePwd(testuser, "Password")) test "can make and extract a JWT token from a session": let session = newSession(testuser) @@ -23,3 +37,23 @@ suite "strawboss server": check: fromJWT(cfg, tok) == session + + test "can ping": + let resp = http.get(apiBase & "/ping") + check: + resp.status.startsWith("200") + resp.body == "\"pong\"" + + test "can fail auth": + let resp = http.get(apiBase & "/auth-token?username=bob@builder.com&password=notpassword") + check: + resp.status.startsWith("401") + + test "can auth": + let resp = http.get(apiBase & "/auth-token?username=bob@builder.com&password=password") + check: + resp.status.startsWith("200") + + # suite tear-down + try: discard http.post(apiBase & "/service/debug/stop") + except: discard ""