From a49ea821635e07d02b690b1af92ae058eee2a8c6 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:00:01 -0500 Subject: [PATCH] add server blackbox test --- backend/internal/tests/server_test.go | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 backend/internal/tests/server_test.go diff --git a/backend/internal/tests/server_test.go b/backend/internal/tests/server_test.go new file mode 100644 index 0000000..684fb2d --- /dev/null +++ b/backend/internal/tests/server_test.go @@ -0,0 +1,41 @@ +package tests + +import ( + "KonferCA/SPUR/internal/server" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +/* +This file contains blackbox testing for the server package which includes +all routes that are exposed by the server. The main objective is to test +that all routes are behaving how they should be from the perspective of +a client that doesn't know the inner implementation details. +*/ +func TestServer(t *testing.T) { + setupEnv() + + s, err := server.New() + assert.Nil(t, err) + + t.Run("Test API V1 Health Check Route", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/api/v1/health", nil) + rec := httptest.NewRecorder() + s.GetEcho().ServeHTTP(rec, req) + assert.Equal(t, rec.Code, http.StatusOK) + resBytes, err := io.ReadAll(rec.Body) + assert.Nil(t, err) + var resBody map[string]any + err = json.Unmarshal(resBytes, &resBody) + assert.Nil(t, err) + assert.Equal(t, resBody["status"], "healthy") + assert.NotEmpty(t, resBody["timestamp"]) + assert.NotEmpty(t, resBody["database"]) + assert.NotEmpty(t, resBody["system"]) + }) +}