snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
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
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
This commit is contained in:
125
server/tests/federation/main_test.go
Normal file
125
server/tests/federation/main_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package federation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/app"
|
||||
"github.com/cortezaproject/corteza/server/federation/rest"
|
||||
"github.com/cortezaproject/corteza/server/federation/service"
|
||||
"github.com/cortezaproject/corteza/server/pkg/api/server"
|
||||
"github.com/cortezaproject/corteza/server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza/server/pkg/cli"
|
||||
"github.com/cortezaproject/corteza/server/pkg/eventbus"
|
||||
"github.com/cortezaproject/corteza/server/pkg/id"
|
||||
"github.com/cortezaproject/corteza/server/pkg/logger"
|
||||
sysTypes "github.com/cortezaproject/corteza/server/system/types"
|
||||
"github.com/cortezaproject/corteza/server/tests/helpers"
|
||||
"github.com/go-chi/chi/v5"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/steinfletcher/apitest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type (
|
||||
helper struct {
|
||||
t *testing.T
|
||||
a *require.Assertions
|
||||
|
||||
cUser *sysTypes.User
|
||||
roleID uint64
|
||||
token []byte
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
testApp *app.CortezaApp
|
||||
r chi.Router
|
||||
|
||||
eventBus = eventbus.New()
|
||||
)
|
||||
|
||||
func init() {
|
||||
helpers.RecursiveDotEnvLoad()
|
||||
}
|
||||
|
||||
func InitTestApp() {
|
||||
if testApp == nil {
|
||||
ctx := cli.Context()
|
||||
|
||||
testApp = helpers.NewIntegrationTestApp(ctx, func(app *app.CortezaApp) (err error) {
|
||||
app.Opt.Federation.Enabled = true
|
||||
service.DefaultLogger = app.Log
|
||||
service.DefaultStore = app.Store
|
||||
|
||||
eventbus.Set(eventBus)
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
r = chi.NewRouter()
|
||||
r.Use(server.BaseMiddleware(false, logger.Default())...)
|
||||
helpers.BindAuthMiddleware(r)
|
||||
r.Group(rest.MountRoutes(testApp.Opt.Limit))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
InitTestApp()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func newHelper(t *testing.T) helper {
|
||||
h := helper{
|
||||
t: t,
|
||||
a: require.New(t),
|
||||
roleID: id.Next(),
|
||||
cUser: &sysTypes.User{
|
||||
ID: id.Next(),
|
||||
},
|
||||
}
|
||||
|
||||
h.cUser.SetRoles(h.roleID)
|
||||
helpers.UpdateRBAC(h.roleID)
|
||||
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
h.token, err = auth.TokenIssuer.Issue(ctx, auth.WithIdentity(h.cUser))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func (h helper) MyRole() uint64 {
|
||||
return h.roleID
|
||||
}
|
||||
|
||||
// Returns context w/ security details
|
||||
func (h helper) secCtx() context.Context {
|
||||
return auth.SetIdentityToContext(context.Background(), h.cUser)
|
||||
}
|
||||
|
||||
// apitest basics, initialize, set handler, add auth
|
||||
func (h helper) apiInit() *apitest.APITest {
|
||||
InitTestApp()
|
||||
|
||||
return apitest.
|
||||
New().
|
||||
Handler(r).
|
||||
Intercept(helpers.ReqHeaderRawAuthBearer(h.token))
|
||||
}
|
||||
|
||||
// Unwraps error before it passes it to the tester
|
||||
func (h helper) noError(err error) {
|
||||
for errors.Unwrap(err) != nil {
|
||||
err = errors.Unwrap(err)
|
||||
}
|
||||
|
||||
h.a.NoError(err)
|
||||
}
|
||||
242
server/tests/federation/node_pairing_test.go
Normal file
242
server/tests/federation/node_pairing_test.go
Normal file
@@ -0,0 +1,242 @@
|
||||
package federation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/federation/service"
|
||||
"github.com/cortezaproject/corteza/server/federation/types"
|
||||
"github.com/cortezaproject/corteza/server/store"
|
||||
st "github.com/cortezaproject/corteza/server/system/types"
|
||||
"github.com/cortezaproject/corteza/server/tests/helpers"
|
||||
jsonpath "github.com/steinfletcher/apitest-jsonpath"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type (
|
||||
// node handshake mocker, allows custom functions
|
||||
mockNodeHandshake struct {
|
||||
init func(context.Context, *types.Node, string) error
|
||||
complete func(context.Context, *types.Node, string) error
|
||||
}
|
||||
)
|
||||
|
||||
func (h mockNodeHandshake) Init(ctx context.Context, n *types.Node, t string) error {
|
||||
return h.init(ctx, n, t)
|
||||
}
|
||||
|
||||
func (h mockNodeHandshake) Complete(ctx context.Context, n *types.Node, t string) error {
|
||||
return h.complete(ctx, n, t)
|
||||
}
|
||||
|
||||
func (h helper) clearNodes() {
|
||||
h.noError(store.TruncateFederationNodes(context.Background(), service.DefaultStore))
|
||||
h.noError(store.TruncateAuthClients(context.Background(), service.DefaultStore))
|
||||
}
|
||||
|
||||
func (h helper) prepareRBAC() {
|
||||
helpers.AllowMe(h, types.ComponentRbacResource(), "node.create", "nodes.search", "pair")
|
||||
helpers.AllowMe(h, types.NodeRbacResource(0), "manage")
|
||||
|
||||
h.noError(service.DefaultStore.CreateRole(context.Background(), &st.Role{
|
||||
ID: h.roleID,
|
||||
Name: "Federation",
|
||||
Handle: "federation",
|
||||
}))
|
||||
|
||||
h.noError(service.DefaultStore.CreateUser(context.Background(), h.cUser))
|
||||
}
|
||||
|
||||
func (h helper) lookupNodeByID(ID uint64) *types.Node {
|
||||
n, err := service.DefaultStore.LookupFederationNodeByID(context.Background(), ID)
|
||||
h.a.NoError(err)
|
||||
return n
|
||||
}
|
||||
|
||||
func TestSuccessfulNodePairing(t *testing.T) {
|
||||
var (
|
||||
h = newHelper(t)
|
||||
aNodeID uint64
|
||||
aNodeURI string
|
||||
bNodeID uint64
|
||||
|
||||
rspWithNode struct{ Response *types.Node }
|
||||
rspWithURI struct{ Response string }
|
||||
|
||||
checkNodeStatus = func(ID uint64, status string) {
|
||||
n := h.lookupNodeByID(aNodeID)
|
||||
h.a.NotNil(n)
|
||||
h.a.Equal(status, n.Status)
|
||||
}
|
||||
|
||||
getNodeAuthToken = func(ID uint64) string {
|
||||
n := h.lookupNodeByID(ID)
|
||||
return n.AuthToken
|
||||
}
|
||||
|
||||
authClient = &st.AuthClient{
|
||||
ID: 42,
|
||||
Handle: "handle",
|
||||
Secret: "secret",
|
||||
}
|
||||
|
||||
req = require.New(t)
|
||||
)
|
||||
|
||||
req.NoError(store.CreateAuthClient(context.Background(), service.DefaultStore, authClient))
|
||||
|
||||
service.DefaultNode.SetHandshaker(nil)
|
||||
|
||||
h.prepareRBAC()
|
||||
h.clearNodes()
|
||||
|
||||
{
|
||||
// #############################################################################################################
|
||||
t.Log("Step #1, create node on server A")
|
||||
|
||||
h.apiInit().
|
||||
// Debug().
|
||||
Post("/nodes/").
|
||||
FormData("name", "Server B").
|
||||
FormData("baseURL", "https://api.server-b.tld/federation").
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
End().
|
||||
JSON(&rspWithNode)
|
||||
|
||||
// response ok?
|
||||
h.a.NotNil(rspWithNode.Response)
|
||||
aNodeID = rspWithNode.Response.ID
|
||||
checkNodeStatus(aNodeID, types.NodeStatusPending)
|
||||
}
|
||||
|
||||
{
|
||||
// #############################################################################################################
|
||||
t.Log("Step #2, request pairing URI")
|
||||
|
||||
h.apiInit().
|
||||
// Debug().
|
||||
Post(fmt.Sprintf("/nodes/%d/uri", aNodeID)).
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
End().
|
||||
JSON(&rspWithURI)
|
||||
|
||||
// response ok?
|
||||
h.a.NotNil(rspWithNode.Response)
|
||||
aNodeURI = rspWithURI.Response
|
||||
|
||||
checkNodeStatus(aNodeID, types.NodeStatusPending)
|
||||
}
|
||||
|
||||
{
|
||||
// #############################################################################################################
|
||||
t.Log("Step #3, use pairing URI to create node on the 2nd server")
|
||||
|
||||
h.apiInit().
|
||||
// Debug().
|
||||
Post("/nodes/").
|
||||
FormData("pairingURI", aNodeURI).
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
End().
|
||||
JSON(&rspWithNode)
|
||||
|
||||
// Node stored.
|
||||
h.a.NotNil(rspWithNode.Response)
|
||||
bNodeID = rspWithNode.Response.ID
|
||||
|
||||
checkNodeStatus(aNodeID, types.NodeStatusPending)
|
||||
checkNodeStatus(bNodeID, types.NodeStatusPending)
|
||||
}
|
||||
|
||||
{
|
||||
// #############################################################################################################
|
||||
t.Log("Step #4, admin of 2nd server requests list of nodes")
|
||||
|
||||
h.apiInit().
|
||||
// Debug().
|
||||
Get("/nodes/").
|
||||
Query("status", types.NodeStatusPending).
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
Assert(jsonpath.Present(fmt.Sprintf(`$.response.set[? @.sharedNodeID=="%d"]`, aNodeID))).
|
||||
Assert(jsonpath.Present(fmt.Sprintf(`$.response.set[? @.nodeID=="%d"]`, bNodeID))).
|
||||
End()
|
||||
}
|
||||
|
||||
{
|
||||
// #############################################################################################################
|
||||
t.Log("Step #5, request pairing procedure on 2nd server")
|
||||
|
||||
// fake remote call with local/direct change
|
||||
service.DefaultNode.SetHandshaker(&mockNodeHandshake{
|
||||
init: func(ctx context.Context, n *types.Node, authToken string) error {
|
||||
h.apiInit().
|
||||
Post(fmt.Sprintf("/nodes/%d/handshake", n.SharedNodeID)).
|
||||
FormData("pairToken", n.PairToken).
|
||||
FormData("authToken", authToken).
|
||||
FormData("sharedNodeID", strconv.FormatUint(n.SharedNodeID, 10)).
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
End()
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
h.apiInit().
|
||||
//Debug().
|
||||
Post(fmt.Sprintf("/nodes/%d/pair", bNodeID)).
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
End()
|
||||
|
||||
checkNodeStatus(aNodeID, types.NodeStatusPairRequested)
|
||||
checkNodeStatus(bNodeID, types.NodeStatusPairRequested)
|
||||
}
|
||||
|
||||
{
|
||||
// #############################################################################################################
|
||||
t.Log("Step #6, handshake confirmation on 1st server")
|
||||
|
||||
// fake remote call with local/direct change
|
||||
service.DefaultNode.SetHandshaker(&mockNodeHandshake{
|
||||
complete: func(ctx context.Context, n *types.Node, authToken string) error {
|
||||
h.apiInit().
|
||||
//Debug().
|
||||
// make sure we do not use test auth-token but
|
||||
// one provided to us in the initial handshake step
|
||||
Intercept(helpers.ReqHeaderRawAuthBearer([]byte(n.AuthToken))).
|
||||
Post(fmt.Sprintf("/nodes/%d/handshake-complete", n.SharedNodeID)).
|
||||
FormData("authToken", authToken).
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
End()
|
||||
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
h.apiInit().
|
||||
//Debug().
|
||||
Intercept(helpers.ReqHeaderRawAuthBearer([]byte(getNodeAuthToken(aNodeID)))).
|
||||
Post(fmt.Sprintf("/nodes/%d/handshake-confirm", aNodeID)).
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
Assert(helpers.AssertNoErrors).
|
||||
End()
|
||||
|
||||
checkNodeStatus(aNodeID, types.NodeStatusPaired)
|
||||
checkNodeStatus(bNodeID, types.NodeStatusPaired)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user