Skip to content

Commit

Permalink
test tenantsCreateHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
briskt committed Nov 26, 2023
1 parent cdd6bc1 commit ec71df7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions server/tenant_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server_test

import (
"bytes"
"encoding/json"
"io"
"net/http"
Expand All @@ -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]
Expand Down

0 comments on commit ec71df7

Please sign in to comment.