Skip to content

Commit

Permalink
test tenantsGetHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
briskt committed Dec 9, 2023
1 parent 31c4954 commit 26c8bf9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
20 changes: 20 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package server_test

import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -114,3 +117,20 @@ func RandStr(n int) string {
}
return string(b)
}

func (ts *TestSuite) request(method, path, token string, input any) ([]byte, int) {
var r io.Reader
if input != nil {
j, _ := json.Marshal(&input)
r = bytes.NewReader(j)
}
req := httptest.NewRequest(method, path, r)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, "Bearer "+token)

res := httptest.NewRecorder()
ts.server.ServeHTTP(res, req)
body, err := io.ReadAll(res.Body)
ts.NoError(err)
return body, res.Code
}
56 changes: 40 additions & 16 deletions server/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,53 @@ func (ts *TestSuite) Test_tenantsCreateHandler() {

func (ts *TestSuite) Test_tenantsGetHandler() {
f := ts.createUserFixture()
admin := f.Users[0]
userToken := f.Tokens[0]

f2 := ts.createUserFixture()
admin := f2.Users[0]
admin.Role = app.UserRoleAdmin
ts.NoError(db.Tx(ts.ctx).Save(&admin).Error)
token := f.Tokens[0]
tenant := ts.createTenantFixture().Tenants[0]
adminToken := f2.Tokens[0]

req := httptest.NewRequest(http.MethodGet, "/api/tenants/"+tenant.ID, nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, "Bearer "+token.PlainText)
tenant := ts.createTenantFixture().Tenants[0]

res := httptest.NewRecorder()
ts.server.ServeHTTP(res, req)
body, err := io.ReadAll(res.Body)
ts.NoError(err)
tests := []struct {
name string
token string
wantStatus int
}{
{
name: "not a valid token",
token: "x",
wantStatus: http.StatusUnauthorized,
},
{
name: "a user cannot access a tenant",
token: userToken.PlainText,
wantStatus: http.StatusNotFound,
},
{
name: "admin can access a tenant",
token: adminToken.PlainText,
wantStatus: http.StatusOK,
},
}
for _, tt := range tests {
ts.T().Run(tt.name, func(t *testing.T) {
body, status := ts.request(http.MethodGet, "/api/tenants/"+tenant.ID, tt.token, nil)

// Assertions
ts.Equal(http.StatusOK, res.Code, "incorrect http status, body: \n%s", body)
// Assertions
ts.Equal(tt.wantStatus, status, "incorrect http status, body: \n%s", body)

var gotTenant app.Tenant
ts.NoError(json.Unmarshal(body, &gotTenant))
ts.Equal(tenant.ID, gotTenant.ID, "incorrect Tenant data, body: \n%s", body)
if tt.wantStatus != http.StatusOK {
return
}

// TODO: test error response
var gotTenant app.Tenant
ts.NoError(json.Unmarshal(body, &gotTenant))
ts.Equal(tenant.ID, gotTenant.ID, "incorrect Tenant data, body: \n%s", body)
})
}
}

func (ts *TestSuite) Test_tenantsListHandler() {
Expand Down

0 comments on commit 26c8bf9

Please sign in to comment.