From ec71df7da26f44b7a07c96fe9521b1f2d6e804b2 Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Sat, 25 Nov 2023 22:09:26 -0700 Subject: [PATCH] test tenantsCreateHandler --- server/tenant_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/server/tenant_test.go b/server/tenant_test.go index 50828c3..7108a5b 100644 --- a/server/tenant_test.go +++ b/server/tenant_test.go @@ -1,6 +1,7 @@ package server_test import ( + "bytes" "encoding/json" "io" "net/http" @@ -9,8 +10,38 @@ import ( "github.com/labstack/echo/v4" "github.com/briskt/keygo/app" + "github.com/briskt/keygo/db" ) +func (ts *TestSuite) Test_tenantsCreateHandler() { + f := ts.createUserFixture() + token := f.Tokens[0] + + input := app.TenantCreateInput{Name: "new tenant"} + j, _ := json.Marshal(&input) + req := httptest.NewRequest(http.MethodPost, "/api/tenants", bytes.NewReader(j)) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + req.Header.Set(echo.HeaderAuthorization, "Bearer "+token.PlainText) + + res := httptest.NewRecorder() + ts.server.ServeHTTP(res, req) + body, err := io.ReadAll(res.Body) + ts.NoError(err) + + // Assertions + ts.Equal(http.StatusOK, res.Code, "incorrect http status, body: \n%s", body) + + var gotTenant app.Tenant + ts.NoError(json.Unmarshal(body, &gotTenant)) + ts.Equal(input.Name, gotTenant.Name, "incorrect Tenant Name, body: \n%s", body) + + dbTenant, err := db.FindTenantByID(ts.ctx, gotTenant.ID) + ts.NoError(err) + ts.Equal(input.Name, dbTenant.Name, "incorrect Tenant Name in db") + + // TODO: test error response +} + func (ts *TestSuite) Test_GetTenant() { f := ts.createUserFixture() token := f.Tokens[0]