From b10fbd6142571ee76e786efcfb7f25f8def9bea6 Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 20 Sep 2021 19:06:50 +0200 Subject: [PATCH] add some backend unit tests --- .gitlab-ci.yml | 2 +- apiGo/api/api/Context.go | 2 +- apiGo/api/api/Hash_test.go | 11 +++++++++++ apiGo/api/api/Helpers_test.go | 22 ++++++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 apiGo/api/api/Hash_test.go create mode 100644 apiGo/api/api/Helpers_test.go diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 282e3c4..d16b7e7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -62,7 +62,7 @@ Backend_Tests: stage: test script: - cd apiGo - - go get -u github.com/jstemmer/go-junit-report + - go install github.com/jstemmer/go-junit-report@v0.9.1 - go test -v ./... 2>&1 | go-junit-report -set-exit-code > report.xml needs: [] artifacts: diff --git a/apiGo/api/api/Context.go b/apiGo/api/api/Context.go index d2bfb2d..edde0ef 100644 --- a/apiGo/api/api/Context.go +++ b/apiGo/api/api/Context.go @@ -56,5 +56,5 @@ func (r *apicontext) Error(msg string) { } func (r *apicontext) Errorf(msg string, args ...interface{}) { - r.Error(fmt.Sprintf(msg, args)) + r.Error(fmt.Sprintf(msg, &args)) } diff --git a/apiGo/api/api/Hash_test.go b/apiGo/api/api/Hash_test.go new file mode 100644 index 0000000..eee9f2b --- /dev/null +++ b/apiGo/api/api/Hash_test.go @@ -0,0 +1,11 @@ +package api + +import "testing" + +func TestHashlength(t *testing.T) { + h := HashPassword("test") + + if len(*h) != 64 { + t.Errorf("Invalid hash length: %d", len(*h)) + } +} diff --git a/apiGo/api/api/Helpers_test.go b/apiGo/api/api/Helpers_test.go new file mode 100644 index 0000000..7d3ad2b --- /dev/null +++ b/apiGo/api/api/Helpers_test.go @@ -0,0 +1,22 @@ +package api + +import "testing" + +func TestJsonify(t *testing.T) { + var obj = struct { + ID uint32 + Str string + Boo bool + }{ + ID: 42, + Str: "teststr", + Boo: true, + } + + res := Jsonify(obj) + exp := `{"ID":42,"Str":"teststr","Boo":true}` + + if string(res) != exp { + t.Errorf("Invalid json response: %s !== %s", string(res), exp) + } +}