package automation import ( "context" "errors" "os" "testing" "github.com/cortezaproject/corteza/server/app" "github.com/cortezaproject/corteza/server/automation/rest" "github.com/cortezaproject/corteza/server/automation/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/label" ltype "github.com/cortezaproject/corteza/server/pkg/label/types" "github.com/cortezaproject/corteza/server/pkg/locale" "github.com/cortezaproject/corteza/server/pkg/logger" "github.com/cortezaproject/corteza/server/pkg/rand" "github.com/cortezaproject/corteza/server/store" "github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers/sqlite" 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" "golang.org/x/text/language" ) 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() } // random string, 10 chars long by default func rs(a ...int) string { var l = 10 if len(a) > 0 { l = a[0] } return string(rand.Bytes(l)) } func InitTestApp() { if testApp == nil { ctx := cli.Context() testApp = helpers.NewIntegrationTestApp(ctx, func(app *app.CortezaApp) (err error) { service.DefaultStore, err = sqlite.ConnectInMemory(ctx) if err != nil { return err } // Tests should be executed w/o any locales locale.SetGlobal(locale.Static(&locale.Language{Tag: language.Und})) 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()) } } 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) } func (h helper) setLabel(res label.LabeledResource, name, value string) { h.a.NoError(store.UpsertLabel(h.secCtx(), service.DefaultStore, <ype.Label{ Kind: res.LabelResourceKind(), ResourceID: res.LabelResourceID(), Name: name, Value: ltype.LabelValue{Val: value}, })) }