Added ProjectDef parsing code. Unit test for , authentication logic.
This commit is contained in:
@ -1,9 +1,61 @@
|
||||
import strtabs, tables, unittest
|
||||
import json, strtabs, tables, unittest
|
||||
import ./testutil
|
||||
import ../../main/nim/strawbosspkg/configuration
|
||||
|
||||
suite "load and save configuration objects":
|
||||
|
||||
# suite setup & common data
|
||||
let testProjDefStr = """{ "name": "test-project-1", "repo":
|
||||
"/non-existent/dir",
|
||||
"cfgFilePath": "strawhat.json",
|
||||
"defaultBranch": "deploy",
|
||||
"envVars": { "VAR1": "value" } }"""
|
||||
|
||||
let testProjDef = ProjectDef(
|
||||
name: "test-project-1",
|
||||
repo: "/non-existent/dir",
|
||||
cfgFilePath: "strawhat.json",
|
||||
defaultBranch: "deploy",
|
||||
envVars: newStringTable("VAR1", "value", modeCaseInsensitive))
|
||||
|
||||
|
||||
test "parseProjectDef":
|
||||
let pd = parseProjectDef(parseJson(testProjDefStr))
|
||||
|
||||
check:
|
||||
pd.name == "test-project-1"
|
||||
pd.repo == "/non-existent/dir"
|
||||
pd.cfgFilePath == "strawhat.json"
|
||||
pd.defaultBranch == "deploy"
|
||||
pd.envVars.len == 1
|
||||
pd.envVars.hasKey("VAR1")
|
||||
pd.envVars["VAR1"] == "value"
|
||||
|
||||
test "ProjectDef ==":
|
||||
let pd1 = parseProjectDef(parseJson(testProjDefStr))
|
||||
|
||||
check pd1 == testProjDef
|
||||
|
||||
test "ProjectDef != (name)":
|
||||
var pd1 = testProjDef
|
||||
pd1.name = "different"
|
||||
check pd1 != testProjDef
|
||||
|
||||
test "ProjectDef != (repo)":
|
||||
var pd1 = testProjDef
|
||||
pd1.repo = "different"
|
||||
check pd1 != testProjDef
|
||||
|
||||
test "ProjectDef != (cfgFilePath)":
|
||||
var pd1 = testProjDef
|
||||
pd1.cfgFilePath = "different"
|
||||
check pd1 != testProjDef
|
||||
|
||||
test "ProjectDef != (defaultBranch)":
|
||||
var pd1 = testProjDef
|
||||
pd1.defaultBranch = "different"
|
||||
check pd1 != testProjDef
|
||||
|
||||
test "loadStrawBossConfig":
|
||||
let cfg = loadStrawBossConfig("src/test/json/strawboss.config.json")
|
||||
let expectedUsers = @[UserRef(name: "bob@builder.com", hashedPwd: "testvalue"),
|
||||
|
@ -1,10 +1,19 @@
|
||||
import asyncdispatch, httpclient, os, osproc, strutils, times, unittest
|
||||
import asyncdispatch, httpclient, json, os, osproc, sequtils, strutils, times, unittest
|
||||
import ./testutil
|
||||
import ../../main/nim/strawbosspkg/configuration
|
||||
import ../../main/nim/strawbosspkg/server
|
||||
import ../../main/nim/strawbosspkg/private/util
|
||||
|
||||
suite "strawboss server":
|
||||
import strtabs
|
||||
|
||||
# test helpers
|
||||
proc newAuthenticatedHttpClient(apiBase, uname, pwd: string): HttpClient =
|
||||
result = newHttpClient()
|
||||
let authResp = result.get(apiBase & "/auth-token?username=" & uname & "&password=" & pwd)
|
||||
assert authResp.status.startsWith("200")
|
||||
result.headers = newHttpHeaders({"Authorization": "Bearer " & parseJson(authResp.body).getStr})
|
||||
|
||||
suite "strawboss server can...":
|
||||
|
||||
# suite setup code
|
||||
let cfgFilePath = "src/test/json/strawboss.config.json"
|
||||
@ -24,35 +33,59 @@ suite "strawboss server":
|
||||
|
||||
## UNIT TESTS
|
||||
|
||||
test "can validate hashed pwd":
|
||||
test "validate hashed pwd":
|
||||
|
||||
check validatePwd(testuser, "password")
|
||||
|
||||
test "can detect invalid pwds":
|
||||
test "detect invalid pwds":
|
||||
check(not validatePwd(testuser, "Password"))
|
||||
|
||||
test "can make and extract a JWT token from a session":
|
||||
test "make and extract a JWT token from a session":
|
||||
let session = newSession(testuser)
|
||||
let tok = toJWT(cfg, session)
|
||||
check fromJWT(cfg, tok) == session
|
||||
|
||||
check:
|
||||
fromJWT(cfg, tok) == session
|
||||
|
||||
test "can ping":
|
||||
test "ping":
|
||||
let resp = http.get(apiBase & "/ping")
|
||||
check:
|
||||
resp.status.startsWith("200")
|
||||
resp.body == "\"pong\""
|
||||
|
||||
test "can fail auth":
|
||||
test "fail auth":
|
||||
let resp = http.get(apiBase & "/auth-token?username=bob@builder.com&password=notpassword")
|
||||
check:
|
||||
resp.status.startsWith("401")
|
||||
check resp.status.startsWith("401")
|
||||
|
||||
test "can auth":
|
||||
test "auth":
|
||||
let resp = http.get(apiBase & "/auth-token?username=bob@builder.com&password=password")
|
||||
check:
|
||||
resp.status.startsWith("200")
|
||||
check resp.status.startsWith("200")
|
||||
|
||||
test "verify valid auth token":
|
||||
let authHttp = newAuthenticatedHttpClient(apiBase, "bob@builder.com", "password")
|
||||
let resp = authHttp.get(apiBase & "/verify-auth")
|
||||
check resp.status.startsWith("200")
|
||||
|
||||
test "verify fails when no auth token is given":
|
||||
let resp = http.get(apiBase & "/verify-auth")
|
||||
check resp.status.startsWith("401")
|
||||
|
||||
test "verify fails when invalid auth token is given":
|
||||
let http1 = newHttpClient()
|
||||
http1.headers = newHttpHeaders({"Authorization": "Bearer nope"})
|
||||
let resp = http1.get(apiBase & "/verify-auth")
|
||||
check resp.status.startsWith("401")
|
||||
|
||||
test "fail to get projects when not authenticated":
|
||||
let resp = http.get(apiBase & "/projects")
|
||||
check resp.status.startsWith("401")
|
||||
|
||||
test "get projects":
|
||||
let authHttp = newAuthenticatedHttpClient(apiBase, "bob@builder.com", "password")
|
||||
let resp = authHttp.get(apiBase & "/projects")
|
||||
check resp.status.startsWith("200")
|
||||
|
||||
let projects: seq[ProjectDef] = parseJson(resp.body).getElems.mapIt(parseProjectDef(it))
|
||||
|
||||
check sameContents(projects, cfg.projects)
|
||||
|
||||
# suite tear-down
|
||||
try: discard http.post(apiBase & "/service/debug/stop")
|
||||
|
Reference in New Issue
Block a user