diff --git a/pkg/module/authorization.go b/pkg/module/authorization.go deleted file mode 100644 index 4bc2650..0000000 --- a/pkg/module/authorization.go +++ /dev/null @@ -1,109 +0,0 @@ -package module - -// import ( -// "context" -// "sync" - -// "forge.cadoles.com/arcad/edge/pkg/app" -// "forge.cadoles.com/arcad/edge/pkg/bus" -// "forge.cadoles.com/arcad/edge/pkg/repository" -// "github.com/dop251/goja" -// "github.com/pkg/errors" -// "gitlab.com/wpetit/goweb/logger" -// ) - -// type AuthorizationModule struct { -// appID app.ID -// bus bus.Bus -// backend *app.Server -// admins sync.Map -// } - -// func (m *AuthorizationModule) Name() string { -// return "authorization" -// } - -// func (m *AuthorizationModule) Export(export *goja.Object) { -// if err := export.Set("isAdmin", m.isAdmin); err != nil { -// panic(errors.Wrap(err, "could not set 'register' function")) -// } -// } - -// func (m *AuthorizationModule) isAdmin(call goja.FunctionCall) goja.Value { -// userID := call.Argument(0).String() -// if userID == "" { -// panic(errors.New("first argument must be a user id")) -// } - -// rawValue, exists := m.admins.Load(repository.UserID(userID)) -// if !exists { -// return m.backend.ToValue(false) -// } - -// isAdmin, ok := rawValue.(bool) -// if !ok { -// return m.backend.ToValue(false) -// } - -// return m.backend.ToValue(isAdmin) -// } - -// func (m *AuthorizationModule) handleEvents() { -// ctx := logger.With(context.Background(), logger.F("moduleAppID", m.appID)) - -// ns := AppMessageNamespace(m.appID) - -// userConnectedMessages, err := m.bus.Subscribe(ctx, ns, MessageTypeUserConnected) -// if err != nil { -// panic(errors.WithStack(err)) -// } - -// userDisconnectedMessages, err := m.bus.Subscribe(ctx, ns, MessageTypeUserDisconnected) -// if err != nil { -// panic(errors.WithStack(err)) -// } - -// defer func() { -// m.bus.Unsubscribe(ctx, ns, MessageTypeUserConnected, userConnectedMessages) -// m.bus.Unsubscribe(ctx, ns, MessageTypeUserDisconnected, userDisconnectedMessages) -// }() - -// for { -// select { -// case msg := <-userConnectedMessages: -// userConnectedMsg, ok := msg.(*MessageUserConnected) -// if !ok { -// continue -// } - -// logger.Debug(ctx, "user connected", logger.F("msg", userConnectedMsg)) - -// m.admins.Store(userConnectedMsg.UserID, userConnectedMsg.IsAdmin) - -// case msg := <-userDisconnectedMessages: -// userDisconnectedMsg, ok := msg.(*MessageUserDisconnected) -// if !ok { -// continue -// } - -// logger.Debug(ctx, "user disconnected", logger.F("msg", userDisconnectedMsg)) - -// m.admins.Delete(userDisconnectedMsg.UserID) -// } -// } -// } - -// func AuthorizationModuleFactory(b bus.Bus) app.ServerModuleFactory { -// return func(appID app.ID, backend *app.Server) app.ServerModule { -// mod := &AuthorizationModule{ -// appID: appID, -// bus: b, -// backend: backend, -// admins: sync.Map{}, -// } - -// go mod.handleEvents() - -// return mod -// } -// } diff --git a/pkg/module/authorization_test.go b/pkg/module/authorization_test.go deleted file mode 100644 index bd3990b..0000000 --- a/pkg/module/authorization_test.go +++ /dev/null @@ -1,103 +0,0 @@ -package module - -// import ( -// "context" -// "io/ioutil" -// "testing" -// "time" - -// "forge.cadoles.com/arcad/edge/pkg/app" -// "forge.cadoles.com/arcad/edge/pkg/bus/memory" -// ) - -// func TestAuthorizationModule(t *testing.T) { -// t.Parallel() - -// testAppID := app.ID("test-app") - -// b := memory.NewBus() - -// backend := app.NewServer(testAppID, -// ConsoleModuleFactory(), -// AuthorizationModuleFactory(b), -// ) - -// data, err := ioutil.ReadFile("testdata/authorization.js") -// if err != nil { -// t.Fatal(err) -// } - -// if err := backend.Load(string(data)); err != nil { -// t.Fatal(err) -// } - -// backend.Start() -// defer backend.Stop() - -// if err := backend.OnInit(); err != nil { -// t.Error(err) -// } - -// // Test non connected user - -// retValue, err := backend.ExecFuncByName("isAdmin", testUserID) -// if err != nil { -// t.Error(err) -// } - -// isAdmin := retValue.ToBoolean() - -// if e, g := false, isAdmin; e != g { -// t.Errorf("isAdmin: expected '%v', got '%v'", e, g) -// } - -// // Test user connection as normal user - -// ctx := context.Background() - -// b.Publish(ctx, NewMessageUserConnected(testAppID, testUserID, false)) -// time.Sleep(2 * time.Second) - -// retValue, err = backend.ExecFuncByName("isAdmin", testUserID) -// if err != nil { -// t.Error(err) -// } - -// isAdmin = retValue.ToBoolean() - -// if e, g := false, isAdmin; e != g { -// t.Errorf("isAdmin: expected '%v', got '%v'", e, g) -// } - -// // Test user connection as admin - -// b.Publish(ctx, NewMessageUserConnected(testAppID, testUserID, true)) -// time.Sleep(2 * time.Second) - -// retValue, err = backend.ExecFuncByName("isAdmin", testUserID) -// if err != nil { -// t.Error(err) -// } - -// isAdmin = retValue.ToBoolean() - -// if e, g := true, isAdmin; e != g { -// t.Errorf("isAdmin: expected '%v', got '%v'", e, g) -// } - -// // Test user disconnection - -// b.Publish(ctx, NewMessageUserDisconnected(testAppID, testUserID)) -// time.Sleep(2 * time.Second) - -// retValue, err = backend.ExecFuncByName("isAdmin", testUserID) -// if err != nil { -// t.Error(err) -// } - -// isAdmin = retValue.ToBoolean() - -// if e, g := false, isAdmin; e != g { -// t.Errorf("isAdmin: expected '%v', got '%v'", e, g) -// } -// } diff --git a/pkg/module/user.go b/pkg/module/user.go deleted file mode 100644 index f3aab3e..0000000 --- a/pkg/module/user.go +++ /dev/null @@ -1,59 +0,0 @@ -package module - -// import ( -// "context" - -// "github.com/dop251/goja" -// "github.com/pkg/errors" -// "forge.cadoles.com/arcad/edge/pkg/app" -// "forge.cadoles.com/arcad/edge/pkg/repository" -// "gitlab.com/wpetit/goweb/logger" -// ) - -// type UserModule struct { -// appID app.ID -// repo repository.UserRepository -// backend *app.Server -// ctx context.Context -// } - -// func (m *UserModule) Name() string { -// return "user" -// } - -// func (m *UserModule) Export(export *goja.Object) { -// if err := export.Set("getUserById", m.getUserByID); err != nil { -// panic(errors.Wrap(err, "could not set 'getUserById' function")) -// } -// } - -// func (m *UserModule) getUserByID(call goja.FunctionCall) goja.Value { -// if len(call.Arguments) != 1 { -// panic(m.backend.ToValue("invalid number of arguments")) -// } - -// userID := repository.UserID(call.Arguments[0].String()) - -// user, err := m.repo.Get(userID) -// if err != nil { -// err = errors.Wrapf(err, "could not find user '%s'", userID) -// logger.Error(m.ctx, "could not find user", logger.E(err), logger.F("userID", userID)) -// panic(m.backend.ToValue(err)) -// } - -// return m.backend.ToValue(user) -// } - -// func UserModuleFactory(repo repository.UserRepository) app.ServerModuleFactory { -// return func(appID app.ID, backend *app.Server) app.ServerModule { -// return &UserModule{ -// appID: appID, -// repo: repo, -// backend: backend, -// ctx: logger.With( -// context.Background(), -// logger.F("appID", appID), -// ), -// } -// } -// } diff --git a/pkg/module/user_test.go b/pkg/module/user_test.go deleted file mode 100644 index aa60974..0000000 --- a/pkg/module/user_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package module - -// import ( -// "errors" -// "io/ioutil" -// "testing" - -// "gitlab.com/arcadbox/arcad/internal/app" -// "gitlab.com/arcadbox/arcad/internal/repository" -// ) - -// func TestUserModuleGetUserByID(t *testing.T) { -// repo := &fakeUserRepository{} - -// appID := app.ID("test") -// backend := app.NewServer(appID, -// ConsoleModuleFactory(), -// UserModuleFactory(repo), -// ) - -// data, err := ioutil.ReadFile("testdata/user_getbyid.js") -// if err != nil { -// t.Fatal(err) -// } - -// if err := backend.Load(string(data)); err != nil { -// t.Fatal(err) -// } - -// backend.Start() -// defer backend.Stop() - -// if err := backend.OnInit(); err != nil { -// t.Error(err) -// } -// } - -// type fakeUserRepository struct{} - -// func (r *fakeUserRepository) Create() (*repository.User, error) { -// return nil, errors.New("not implemented") -// } - -// func (r *fakeUserRepository) Save(user *repository.User) error { -// return errors.New("not implemented") -// } - -// func (r *fakeUserRepository) Get(userID repository.UserID) (*repository.User, error) { -// if userID == "0" { -// return &repository.User{}, nil -// } - -// return nil, errors.New("not implemented") -// } - -// func (r *fakeUserRepository) Delete(userID repository.UserID) error { -// return errors.New("not implemented") -// } - -// func (r *fakeUserRepository) Touch(userID repository.UserID, rawUserAgent string) error { -// return errors.New("not implemented") -// } - -// func (r *fakeUserRepository) List() ([]*repository.User, error) { -// return nil, errors.New("not implemented") -// } - -// func (r *fakeUserRepository) ListByID(userIDs ...repository.UserID) ([]*repository.User, error) { -// return nil, errors.New("not implemented") -// }