Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
175 lines
4.0 KiB
Go
175 lines
4.0 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cortezaproject/corteza/server/pkg/id"
|
|
"github.com/cortezaproject/corteza/server/store"
|
|
"github.com/cortezaproject/corteza/server/system/service"
|
|
"github.com/cortezaproject/corteza/server/system/types"
|
|
"github.com/cortezaproject/corteza/server/tests/helpers"
|
|
jsonpath "github.com/steinfletcher/apitest-jsonpath"
|
|
)
|
|
|
|
func (h helper) clearAuthClients() {
|
|
h.noError(store.TruncateAuthClients(context.Background(), service.DefaultStore))
|
|
}
|
|
|
|
func (h helper) repoMakeAuthClient(ss ...string) *types.AuthClient {
|
|
res := &types.AuthClient{
|
|
ID: id.Next(),
|
|
CreatedAt: time.Now(),
|
|
}
|
|
|
|
if len(ss) > 0 {
|
|
res.Handle = ss[0]
|
|
} else {
|
|
res.Handle = "n_" + rs()
|
|
}
|
|
|
|
h.a.NoError(store.CreateAuthClient(context.Background(), service.DefaultStore, res))
|
|
|
|
return res
|
|
}
|
|
|
|
func (h helper) lookupAuthClientByID(id uint64) *types.AuthClient {
|
|
res, err := store.LookupAuthClientByID(context.Background(), service.DefaultStore, id)
|
|
h.noError(err)
|
|
return res
|
|
}
|
|
|
|
func (h helper) lookupAuthClientByHandle(handle string) *types.AuthClient {
|
|
res, err := store.LookupAuthClientByHandle(context.Background(), service.DefaultStore, handle)
|
|
h.noError(err)
|
|
return res
|
|
}
|
|
|
|
func TestAuthClientList(t *testing.T) {
|
|
h := newHelper(t)
|
|
h.clearAuthClients()
|
|
|
|
h.repoMakeAuthClient()
|
|
h.repoMakeAuthClient()
|
|
|
|
helpers.AllowMe(h, types.ComponentRbacResource(), "auth-clients.search")
|
|
helpers.AllowMe(h, types.AuthClientRbacResource(0), "read")
|
|
|
|
h.apiInit().
|
|
Get("/auth/clients/").
|
|
Expect(t).
|
|
Status(http.StatusOK).
|
|
Assert(helpers.AssertNoErrors).
|
|
Assert(jsonpath.Len(`$.response.set`, 2)).
|
|
End()
|
|
}
|
|
|
|
func TestAuthClientRead(t *testing.T) {
|
|
h := newHelper(t)
|
|
h.clearAuthClients()
|
|
|
|
client := h.repoMakeAuthClient()
|
|
|
|
helpers.AllowMe(h, types.AuthClientRbacResource(0), "read")
|
|
|
|
h.apiInit().
|
|
Get(fmt.Sprintf("/auth/clients/%d", client.ID)).
|
|
Expect(t).
|
|
Status(http.StatusOK).
|
|
Assert(helpers.AssertNoErrors).
|
|
End()
|
|
}
|
|
|
|
func TestAuthClientCreate(t *testing.T) {
|
|
h := newHelper(t)
|
|
h.clearAuthClients()
|
|
|
|
handle := rs()
|
|
|
|
helpers.AllowMe(h, types.ComponentRbacResource(), "auth-clients.search")
|
|
helpers.AllowMe(h, types.ComponentRbacResource(), "auth-client.create")
|
|
|
|
h.apiInit().
|
|
Post("/auth/clients/").
|
|
Header("Accept", "application/json").
|
|
FormData("meta", fmt.Sprintf(`{"name": "%s"}`, rs())).
|
|
FormData("handle", handle).
|
|
FormData("scope", "profile api").
|
|
Expect(t).
|
|
Status(http.StatusOK).
|
|
Assert(helpers.AssertNoErrors).
|
|
End()
|
|
|
|
res := h.lookupAuthClientByHandle(handle)
|
|
h.a.NotNil(res)
|
|
h.a.Equal(handle, res.Handle)
|
|
}
|
|
|
|
func TestAuthClientUpdate(t *testing.T) {
|
|
h := newHelper(t)
|
|
h.clearAuthClients()
|
|
|
|
client := h.repoMakeAuthClient()
|
|
client.Meta = &types.AuthClientMeta{Name: rs()}
|
|
client.Handle = rs()
|
|
|
|
helpers.AllowMe(h, types.AuthClientRbacResource(0), "update")
|
|
|
|
h.apiInit().
|
|
Put(fmt.Sprintf("/auth/clients/%d", client.ID)).
|
|
Header("Accept", "application/json").
|
|
JSON(helpers.JSON(client)).
|
|
Expect(t).
|
|
Status(http.StatusOK).
|
|
Assert(helpers.AssertNoErrors).
|
|
End()
|
|
|
|
res := h.lookupAuthClientByHandle(client.Handle)
|
|
h.a.NotNil(res)
|
|
}
|
|
|
|
func TestAuthClientDelete(t *testing.T) {
|
|
h := newHelper(t)
|
|
h.clearAuthClients()
|
|
|
|
client := h.repoMakeAuthClient()
|
|
|
|
helpers.AllowMe(h, types.AuthClientRbacResource(0), "delete")
|
|
|
|
h.apiInit().
|
|
Delete(fmt.Sprintf("/auth/clients/%d", client.ID)).
|
|
Header("Accept", "application/json").
|
|
Expect(t).
|
|
Status(http.StatusOK).
|
|
Assert(helpers.AssertNoErrors).
|
|
End()
|
|
|
|
res := h.lookupAuthClientByID(client.ID)
|
|
h.a.NotNil(res)
|
|
h.a.NotNil(res.DeletedAt)
|
|
}
|
|
|
|
func TestAuthClientUnDelete(t *testing.T) {
|
|
h := newHelper(t)
|
|
h.clearAuthClients()
|
|
|
|
client := h.repoMakeAuthClient()
|
|
|
|
helpers.AllowMe(h, types.AuthClientRbacResource(0), "delete")
|
|
|
|
h.apiInit().
|
|
Post(fmt.Sprintf("/auth/clients/%d/undelete", client.ID)).
|
|
Header("Accept", "application/json").
|
|
Expect(t).
|
|
Status(http.StatusOK).
|
|
Assert(helpers.AssertNoErrors).
|
|
End()
|
|
|
|
res := h.lookupAuthClientByID(client.ID)
|
|
h.a.NotNil(res)
|
|
h.a.Nil(res.DeletedAt)
|
|
}
|