From 3972ac3faa55670cc9edba4da1961280b3ec8886 Mon Sep 17 00:00:00 2001 From: Paul Gellai Date: Mon, 12 Aug 2024 13:41:04 -0600 Subject: [PATCH 1/8] working now --- docker-compose.yaml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 3c5186a..7ed7ad0 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -15,8 +15,8 @@ services: max-size: 10m ports: - '3000:3000' - image: b44427a64de93c20123c068387b0adc0434434ba709fbd91dd03d33ade489c3e - container_name: sshdbg + image: therealpaulgg/ssh-sync-server + container_name: ssh-sync-server ssh-sync-db: image: therealpaulgg/ssh-sync-db:latest container_name: ssh-sync-db-debug @@ -26,18 +26,17 @@ services: - POSTGRES_DB=sshsync restart: always ssh-sync: - image: 62eab8fb32b34e0a2cf36e8635d810c20a38baa2d7beaf5b6918139339e23c23 + image: 2d277cde6d3ba4b20c2743a62000be589ad767febc24bf04b33106285d7c0457 container_name: ssh-sync stdin_open: true # Allows Docker container to keep STDIN open tty: true # Allocates a pseudo-TTY ssh-sync-2: - image: 62eab8fb32b34e0a2cf36e8635d810c20a38baa2d7beaf5b6918139339e23c23 + image: 2d277cde6d3ba4b20c2743a62000be589ad767febc24bf04b33106285d7c0457 container_name: ssh-sync-2 stdin_open: true # Allows Docker container to keep STDIN open tty: true # Allocates a pseudo-TTY ssh-sync-3: - image: 62eab8fb32b34e0a2cf36e8635d810c20a38baa2d7beaf5b6918139339e23c23 + image: 2d277cde6d3ba4b20c2743a62000be589ad767febc24bf04b33106285d7c0457 container_name: ssh-sync-3 stdin_open: true # Allows Docker container to keep STDIN open tty: true # Allocates a pseudo-TTY - #http://ssh-sync-server-debug:3000 \ No newline at end of file From 802f2921d9b29ddbf354a57b305c003a72903972 Mon Sep 17 00:00:00 2001 From: Paul Gellai Date: Mon, 12 Aug 2024 13:41:07 -0600 Subject: [PATCH 2/8] working now --- pkg/database/query/transaction.go | 22 ++++++++++++ pkg/database/repository/machine.go | 3 +- pkg/database/repository/user.go | 19 ++++++++++ pkg/web/router/routes/data.go | 58 ++++++++++++++++++++---------- pkg/web/router/routes/data_test.go | 7 ++++ pkg/web/router/routes/setup.go | 19 +--------- 6 files changed, 90 insertions(+), 38 deletions(-) diff --git a/pkg/database/query/transaction.go b/pkg/database/query/transaction.go index 64dfd38..2b114ff 100644 --- a/pkg/database/query/transaction.go +++ b/pkg/database/query/transaction.go @@ -2,9 +2,11 @@ package query import ( "context" + "net/http" "github.com/georgysavva/scany/v2/pgxscan" "github.com/jackc/pgx/v5" + "github.com/rs/zerolog/log" "github.com/therealpaulgg/ssh-sync-server/pkg/database" ) @@ -63,3 +65,23 @@ func (q *QueryServiceTxImpl[T]) Insert(tx pgx.Tx, query string, args ...interfac _, err := tx.Exec(context.Background(), query, args...) return err } + +func RollbackFunc(txQueryService TransactionService, tx pgx.Tx, w http.ResponseWriter) { + var err error + rb := func(tx pgx.Tx) { + err := txQueryService.Rollback(tx) + if err != nil { + log.Err(err).Msg("error rolling back transaction") + } + } + if err != nil { + rb(tx) + } else { + internalErr := txQueryService.Commit(tx) + if internalErr != nil { + log.Err(err).Msg("error committing transaction") + rb(tx) + w.WriteHeader(http.StatusInternalServerError) + } + } +} diff --git a/pkg/database/repository/machine.go b/pkg/database/repository/machine.go index b65099b..4049329 100644 --- a/pkg/database/repository/machine.go +++ b/pkg/database/repository/machine.go @@ -45,8 +45,7 @@ func (repo *MachineRepo) DeleteMachine(id uuid.UUID) error { if _, err := tx.Exec(context.TODO(), "delete from machines where id = $1", id); err != nil { return err } - err = tx.Commit(context.TODO()) - return err + return tx.Commit(context.TODO()) } func (repo *MachineRepo) GetMachine(id uuid.UUID) (*models.Machine, error) { diff --git a/pkg/database/repository/user.go b/pkg/database/repository/user.go index 2670438..a2036a0 100644 --- a/pkg/database/repository/user.go +++ b/pkg/database/repository/user.go @@ -23,10 +23,12 @@ type UserRepository interface { DeleteUser(id uuid.UUID) error GetUserConfig(id uuid.UUID) ([]models.SshConfig, error) GetUserKeys(id uuid.UUID) ([]models.SshKey, error) + GetUserKey(userId uuid.UUID, keyId uuid.UUID) (*models.SshKey, error) AddAndUpdateKeys(user *models.User) error AddAndUpdateKeysTx(user *models.User, tx pgx.Tx) error AddAndUpdateConfig(user *models.User) error AddAndUpdateConfigTx(user *models.User, tx pgx.Tx) error + DeleteUserKeyTx(user *models.User, id uuid.UUID, tx pgx.Tx) error } type UserRepo struct { @@ -195,3 +197,20 @@ func (repo *UserRepo) AddAndUpdateConfigTx(user *models.User, tx pgx.Tx) error { } return nil } + +func (repo *UserRepo) GetUserKey(userId uuid.UUID, keyId uuid.UUID) (*models.SshKey, error) { + q := do.MustInvoke[query.QueryService[models.SshKey]](repo.Injector) + key, err := q.QueryOne("select * from ssh_keys where user_id = $1 and id = $2", userId, keyId) + if err != nil { + return nil, err + } + if key == nil { + return nil, sql.ErrNoRows + } + return key, nil +} + +func (repo *UserRepo) DeleteUserKeyTx(user *models.User, id uuid.UUID, tx pgx.Tx) error { + _, err := tx.Exec(context.TODO(), "delete from ssh_keys where user_id = $1 and id = $2", user.ID, id) + return err +} diff --git a/pkg/web/router/routes/data.go b/pkg/web/router/routes/data.go index 1247c54..d317c15 100644 --- a/pkg/web/router/routes/data.go +++ b/pkg/web/router/routes/data.go @@ -8,6 +8,7 @@ import ( "net/http" "github.com/go-chi/chi" + "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/rs/zerolog/log" "github.com/samber/do" @@ -115,24 +116,7 @@ func addData(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } - defer func() { - rb := func(tx pgx.Tx) { - err := txQueryService.Rollback(tx) - if err != nil { - log.Err(err).Msg("error rolling back transaction") - } - } - if err != nil { - rb(tx) - } else { - internalErr := txQueryService.Commit(tx) - if internalErr != nil { - log.Err(err).Msg("error committing transaction") - rb(tx) - w.WriteHeader(http.StatusInternalServerError) - } - } - }() + defer query.RollbackFunc(txQueryService, tx, w) if err = userRepo.AddAndUpdateConfigTx(user, tx); err != nil { log.Err(err).Msg("could not add config") w.WriteHeader(http.StatusInternalServerError) @@ -168,10 +152,48 @@ func addData(i *do.Injector) http.HandlerFunc { } } +func deleteData(i *do.Injector) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + user, ok := r.Context().Value(context_keys.UserContextKey).(*models.User) + if !ok { + log.Err(errors.New("could not get user from context")) + w.WriteHeader(http.StatusInternalServerError) + return + } + keyIdStr := chi.URLParam(r, "id") + keyId, err := uuid.Parse(keyIdStr) + if err != nil { + log.Err(err).Msg("could not parse key id") + w.WriteHeader(http.StatusBadRequest) + return + } + userRepo := do.MustInvoke[repository.UserRepo](i) + key, err := userRepo.GetUserKey(user, keyId) + if err != nil { + log.Err(err).Msg("could not get key") + w.WriteHeader(http.StatusNotFound) + return + } + txQueryService := do.MustInvoke[query.TransactionService](i) + tx, err := txQueryService.StartTx(pgx.TxOptions{}) + if err != nil { + log.Err(err).Msg("error starting transaction") + w.WriteHeader(http.StatusInternalServerError) + return + } + if err = userRepo.DeleteUserKeyTx(user, key.ID, tx); err != nil { + log.Err(err).Msg("could not delete key") + w.WriteHeader(http.StatusInternalServerError) + return + } + } +} + func DataRoutes(i *do.Injector) chi.Router { r := chi.NewRouter() r.Use(middleware.ConfigureAuth(i)) r.Get("/", getData(i)) r.Post("/", addData(i)) + r.Delete("/key/{id}", deleteData(i)) return r } diff --git a/pkg/web/router/routes/data_test.go b/pkg/web/router/routes/data_test.go index cdebfed..a39bca1 100644 --- a/pkg/web/router/routes/data_test.go +++ b/pkg/web/router/routes/data_test.go @@ -258,3 +258,10 @@ func TestAddDataError(t *testing.T) { status, http.StatusOK) } } + +func TestDeleteKe(t *testing.T) { + // Arrange + + // Act + // Assert +} diff --git a/pkg/web/router/routes/setup.go b/pkg/web/router/routes/setup.go index eddf704..9b8f684 100644 --- a/pkg/web/router/routes/setup.go +++ b/pkg/web/router/routes/setup.go @@ -67,24 +67,7 @@ func initialSetup(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } - defer func() { - rb := func(tx pgx.Tx) { - err := txQueryService.Rollback(tx) - if err != nil { - log.Err(err).Msg("error rolling back transaction") - } - } - if err != nil { - rb(tx) - } else { - internalErr := txQueryService.Commit(tx) - if internalErr != nil { - log.Err(err).Msg("error committing transaction") - rb(tx) - w.WriteHeader(http.StatusInternalServerError) - } - } - }() + defer query.RollbackFunc(txQueryService, tx, w) userRepo := do.MustInvoke[repository.UserRepository](i) user := &models.User{} user.Username = userDto.Username From 436ed61279f382793034be5001840ce375e599d1 Mon Sep 17 00:00:00 2001 From: Paul Gellai Date: Wed, 14 Aug 2024 21:44:37 -0700 Subject: [PATCH 3/8] start tests --- pkg/database/repository/usermock.go | 29 ++++++++++++++++++++++++++++ pkg/web/router/routes/data.go | 2 +- pkg/web/router/routes/data_test.go | 30 ++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/pkg/database/repository/usermock.go b/pkg/database/repository/usermock.go index 5345a84..bc5fbd4 100644 --- a/pkg/database/repository/usermock.go +++ b/pkg/database/repository/usermock.go @@ -136,6 +136,20 @@ func (mr *MockUserRepositoryMockRecorder) DeleteUser(id interface{}) *gomock.Cal return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteUser", reflect.TypeOf((*MockUserRepository)(nil).DeleteUser), id) } +// DeleteUserKeyTx mocks base method. +func (m *MockUserRepository) DeleteUserKeyTx(user *models.User, id uuid.UUID, tx pgx.Tx) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteUserKeyTx", user, id, tx) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteUserKeyTx indicates an expected call of DeleteUserKeyTx. +func (mr *MockUserRepositoryMockRecorder) DeleteUserKeyTx(user, id, tx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteUserKeyTx", reflect.TypeOf((*MockUserRepository)(nil).DeleteUserKeyTx), user, id, tx) +} + // GetUser mocks base method. func (m *MockUserRepository) GetUser(id uuid.UUID) (*models.User, error) { m.ctrl.T.Helper() @@ -181,6 +195,21 @@ func (mr *MockUserRepositoryMockRecorder) GetUserConfig(id interface{}) *gomock. return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserConfig", reflect.TypeOf((*MockUserRepository)(nil).GetUserConfig), id) } +// GetUserKey mocks base method. +func (m *MockUserRepository) GetUserKey(userId, keyId uuid.UUID) (*models.SshKey, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUserKey", userId, keyId) + ret0, _ := ret[0].(*models.SshKey) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetUserKey indicates an expected call of GetUserKey. +func (mr *MockUserRepositoryMockRecorder) GetUserKey(userId, keyId interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserKey", reflect.TypeOf((*MockUserRepository)(nil).GetUserKey), userId, keyId) +} + // GetUserKeys mocks base method. func (m *MockUserRepository) GetUserKeys(id uuid.UUID) ([]models.SshKey, error) { m.ctrl.T.Helper() diff --git a/pkg/web/router/routes/data.go b/pkg/web/router/routes/data.go index d317c15..0072b91 100644 --- a/pkg/web/router/routes/data.go +++ b/pkg/web/router/routes/data.go @@ -168,7 +168,7 @@ func deleteData(i *do.Injector) http.HandlerFunc { return } userRepo := do.MustInvoke[repository.UserRepo](i) - key, err := userRepo.GetUserKey(user, keyId) + key, err := userRepo.GetUserKey(user.ID, keyId) if err != nil { log.Err(err).Msg("could not get key") w.WriteHeader(http.StatusNotFound) diff --git a/pkg/web/router/routes/data_test.go b/pkg/web/router/routes/data_test.go index a39bca1..32d738e 100644 --- a/pkg/web/router/routes/data_test.go +++ b/pkg/web/router/routes/data_test.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/json" "errors" + "fmt" "mime/multipart" "net/http" "net/http/httptest" @@ -259,9 +260,36 @@ func TestAddDataError(t *testing.T) { } } -func TestDeleteKe(t *testing.T) { +func TestDeleteKey(t *testing.T) { // Arrange + keyId := uuid.New() + req := httptest.NewRequest("DELETE", fmt.Sprintf("/key/%s", keyId.String()), nil) + user := testutils.GenerateUser() + req = testutils.AddUserContext(req, user) + injector := do.New() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockUserRepo := repository.NewMockUserRepository(ctrl) + txMock := pgx.NewMockTx(ctrl) + mockUserRepo.EXPECT().GetUserKey(user.ID, keyId.String()).Return(nil) + mockUserRepo.EXPECT().DeleteUserKeyTx(gomock.Any(), keyId.String(), txMock).Return(nil, nil) + do.Provide(injector, func(i *do.Injector) (repository.UserRepository, error) { + return mockUserRepo, nil + }) + mockTransactionService := query.NewMockTransactionService(ctrl) + mockTransactionService.EXPECT().StartTx(gomock.Any()).Return(txMock, nil) + mockTransactionService.EXPECT().Commit(txMock).Return(nil) + do.Provide(injector, func(i *do.Injector) (query.TransactionService, error) { + return mockTransactionService, nil + }) // Act + rr := httptest.NewRecorder() + handler := http.HandlerFunc(addData(injector)) + handler.ServeHTTP(rr, req) // Assert + if status := rr.Code; status != http.StatusOK { + t.Errorf("addData returned wrong status code: got %v want %v", + status, http.StatusOK) + } } From 26c491775c4fa6376b79e08b413dc949b710be8a Mon Sep 17 00:00:00 2001 From: therealpaulgg Date: Wed, 14 Aug 2024 22:07:07 -0700 Subject: [PATCH 4/8] working on test --- pkg/web/router/routes/data.go | 2 +- pkg/web/router/routes/data_test.go | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/web/router/routes/data.go b/pkg/web/router/routes/data.go index 0072b91..4efc3b7 100644 --- a/pkg/web/router/routes/data.go +++ b/pkg/web/router/routes/data.go @@ -167,7 +167,7 @@ func deleteData(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusBadRequest) return } - userRepo := do.MustInvoke[repository.UserRepo](i) + userRepo := do.MustInvoke[repository.UserRepository](i) key, err := userRepo.GetUserKey(user.ID, keyId) if err != nil { log.Err(err).Msg("could not get key") diff --git a/pkg/web/router/routes/data_test.go b/pkg/web/router/routes/data_test.go index 32d738e..ef018c1 100644 --- a/pkg/web/router/routes/data_test.go +++ b/pkg/web/router/routes/data_test.go @@ -11,6 +11,7 @@ import ( "net/http/httptest" "testing" + "github.com/go-chi/chi" "github.com/golang/mock/gomock" "github.com/google/uuid" "github.com/samber/do" @@ -263,17 +264,21 @@ func TestAddDataError(t *testing.T) { func TestDeleteKey(t *testing.T) { // Arrange keyId := uuid.New() - req := httptest.NewRequest("DELETE", fmt.Sprintf("/key/%s", keyId.String()), nil) + req := httptest.NewRequest("DELETE", fmt.Sprintf("/%s", keyId.String()), nil) user := testutils.GenerateUser() req = testutils.AddUserContext(req, user) + key := &models.SshKey{ + ID: keyId, + UserID: user.ID, + } injector := do.New() ctrl := gomock.NewController(t) defer ctrl.Finish() mockUserRepo := repository.NewMockUserRepository(ctrl) txMock := pgx.NewMockTx(ctrl) - mockUserRepo.EXPECT().GetUserKey(user.ID, keyId.String()).Return(nil) - mockUserRepo.EXPECT().DeleteUserKeyTx(gomock.Any(), keyId.String(), txMock).Return(nil, nil) + mockUserRepo.EXPECT().GetUserKey(user.ID, keyId).Return(key, nil) + mockUserRepo.EXPECT().DeleteUserKeyTx(gomock.Any(), keyId, txMock).Return(nil) do.Provide(injector, func(i *do.Injector) (repository.UserRepository, error) { return mockUserRepo, nil }) @@ -285,7 +290,8 @@ func TestDeleteKey(t *testing.T) { }) // Act rr := httptest.NewRecorder() - handler := http.HandlerFunc(addData(injector)) + handler := chi.NewRouter() + handler.Delete("/{id}", deleteData(injector)) handler.ServeHTTP(rr, req) // Assert if status := rr.Code; status != http.StatusOK { From a7685cdbe86611fcecddeaf9f7235e90b7a0b122 Mon Sep 17 00:00:00 2001 From: therealpaulgg Date: Wed, 14 Aug 2024 22:23:03 -0700 Subject: [PATCH 5/8] tests pass --- pkg/database/query/transaction.go | 5 ++-- pkg/web/router/routes/data.go | 37 +++++++++++++++++++++++- pkg/web/router/routes/data_test.go | 45 +++++++++++++++++++++++++++++- pkg/web/router/routes/setup.go | 19 ++++++++++++- 4 files changed, 100 insertions(+), 6 deletions(-) diff --git a/pkg/database/query/transaction.go b/pkg/database/query/transaction.go index 2b114ff..2a84577 100644 --- a/pkg/database/query/transaction.go +++ b/pkg/database/query/transaction.go @@ -66,8 +66,7 @@ func (q *QueryServiceTxImpl[T]) Insert(tx pgx.Tx, query string, args ...interfac return err } -func RollbackFunc(txQueryService TransactionService, tx pgx.Tx, w http.ResponseWriter) { - var err error +func RollbackFunc(txQueryService TransactionService, tx pgx.Tx, w http.ResponseWriter, err *error) { rb := func(tx pgx.Tx) { err := txQueryService.Rollback(tx) if err != nil { @@ -79,7 +78,7 @@ func RollbackFunc(txQueryService TransactionService, tx pgx.Tx, w http.ResponseW } else { internalErr := txQueryService.Commit(tx) if internalErr != nil { - log.Err(err).Msg("error committing transaction") + log.Err(internalErr).Msg("error committing transaction") rb(tx) w.WriteHeader(http.StatusInternalServerError) } diff --git a/pkg/web/router/routes/data.go b/pkg/web/router/routes/data.go index 4efc3b7..4404096 100644 --- a/pkg/web/router/routes/data.go +++ b/pkg/web/router/routes/data.go @@ -116,7 +116,24 @@ func addData(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } - defer query.RollbackFunc(txQueryService, tx, w) + defer func() { + rb := func(tx pgx.Tx) { + err := txQueryService.Rollback(tx) + if err != nil { + log.Err(err).Msg("error rolling back transaction") + } + } + if err != nil { + rb(tx) + } else { + internalErr := txQueryService.Commit(tx) + if internalErr != nil { + log.Err(internalErr).Msg("error committing transaction") + rb(tx) + w.WriteHeader(http.StatusInternalServerError) + } + } + }() if err = userRepo.AddAndUpdateConfigTx(user, tx); err != nil { log.Err(err).Msg("could not add config") w.WriteHeader(http.StatusInternalServerError) @@ -181,6 +198,24 @@ func deleteData(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } + defer func() { + rb := func(tx pgx.Tx) { + err := txQueryService.Rollback(tx) + if err != nil { + log.Err(err).Msg("error rolling back transaction") + } + } + if err != nil { + rb(tx) + } else { + internalErr := txQueryService.Commit(tx) + if internalErr != nil { + log.Err(internalErr).Msg("error committing transaction") + rb(tx) + w.WriteHeader(http.StatusInternalServerError) + } + } + }() if err = userRepo.DeleteUserKeyTx(user, key.ID, tx); err != nil { log.Err(err).Msg("could not delete key") w.WriteHeader(http.StatusInternalServerError) diff --git a/pkg/web/router/routes/data_test.go b/pkg/web/router/routes/data_test.go index ef018c1..5346f03 100644 --- a/pkg/web/router/routes/data_test.go +++ b/pkg/web/router/routes/data_test.go @@ -295,7 +295,50 @@ func TestDeleteKey(t *testing.T) { handler.ServeHTTP(rr, req) // Assert if status := rr.Code; status != http.StatusOK { - t.Errorf("addData returned wrong status code: got %v want %v", + t.Errorf("deleteData returned wrong status code: got %v want %v", status, http.StatusOK) } } + +func TestDeleteKeyError(t *testing.T) { + // Arrange + keyId := uuid.New() + req := httptest.NewRequest("DELETE", fmt.Sprintf("/%s", keyId.String()), nil) + user := testutils.GenerateUser() + req = testutils.AddUserContext(req, user) + key := &models.SshKey{ + ID: keyId, + UserID: user.ID, + } + + injector := do.New() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockUserRepo := repository.NewMockUserRepository(ctrl) + txMock := pgx.NewMockTx(ctrl) + mockUserRepo.EXPECT().GetUserKey(user.ID, keyId).Return(key, nil) + mockUserRepo.EXPECT().DeleteUserKeyTx(gomock.Any(), keyId, txMock).Return(errors.New("error")) + do.Provide(injector, func(i *do.Injector) (repository.UserRepository, error) { + return mockUserRepo, nil + }) + mockTransactionService := query.NewMockTransactionService(ctrl) + mockTransactionService.EXPECT().StartTx(gomock.Any()).Return(txMock, nil) + mockTransactionService.EXPECT().Rollback(txMock).Return(nil) + do.Provide(injector, func(i *do.Injector) (query.TransactionService, error) { + return mockTransactionService, nil + }) + + // Act + rr := httptest.NewRecorder() + handler := chi.NewRouter() + handler.Delete("/{id}", deleteData(injector)) + handler.ServeHTTP(rr, req) + + // Assert + + if status := rr.Code; status != http.StatusInternalServerError { + t.Errorf("deleteData returned wrong status code: got %v want %v", + status, http.StatusInternalServerError) + } + +} diff --git a/pkg/web/router/routes/setup.go b/pkg/web/router/routes/setup.go index 9b8f684..13bb73f 100644 --- a/pkg/web/router/routes/setup.go +++ b/pkg/web/router/routes/setup.go @@ -67,7 +67,24 @@ func initialSetup(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } - defer query.RollbackFunc(txQueryService, tx, w) + defer func() { + rb := func(tx pgx.Tx) { + err := txQueryService.Rollback(tx) + if err != nil { + log.Err(err).Msg("error rolling back transaction") + } + } + if err != nil { + rb(tx) + } else { + internalErr := txQueryService.Commit(tx) + if internalErr != nil { + log.Err(internalErr).Msg("error committing transaction") + rb(tx) + w.WriteHeader(http.StatusInternalServerError) + } + } + }() userRepo := do.MustInvoke[repository.UserRepository](i) user := &models.User{} user.Username = userDto.Username From 12afa5865a43507b3e976e947dafb72979e9abab Mon Sep 17 00:00:00 2001 From: therealpaulgg Date: Wed, 14 Aug 2024 22:31:15 -0700 Subject: [PATCH 6/8] bring back refactor --- pkg/database/query/transaction.go | 2 +- pkg/web/router/routes/data.go | 56 +++++++++++------------------- pkg/web/router/routes/data_test.go | 1 - pkg/web/router/routes/setup.go | 19 +--------- 4 files changed, 22 insertions(+), 56 deletions(-) diff --git a/pkg/database/query/transaction.go b/pkg/database/query/transaction.go index 2a84577..979c202 100644 --- a/pkg/database/query/transaction.go +++ b/pkg/database/query/transaction.go @@ -73,7 +73,7 @@ func RollbackFunc(txQueryService TransactionService, tx pgx.Tx, w http.ResponseW log.Err(err).Msg("error rolling back transaction") } } - if err != nil { + if *err != nil { rb(tx) } else { internalErr := txQueryService.Commit(tx) diff --git a/pkg/web/router/routes/data.go b/pkg/web/router/routes/data.go index 4404096..cf7d0d2 100644 --- a/pkg/web/router/routes/data.go +++ b/pkg/web/router/routes/data.go @@ -116,24 +116,7 @@ func addData(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } - defer func() { - rb := func(tx pgx.Tx) { - err := txQueryService.Rollback(tx) - if err != nil { - log.Err(err).Msg("error rolling back transaction") - } - } - if err != nil { - rb(tx) - } else { - internalErr := txQueryService.Commit(tx) - if internalErr != nil { - log.Err(internalErr).Msg("error committing transaction") - rb(tx) - w.WriteHeader(http.StatusInternalServerError) - } - } - }() + defer query.RollbackFunc(txQueryService, tx, w, &err) if err = userRepo.AddAndUpdateConfigTx(user, tx); err != nil { log.Err(err).Msg("could not add config") w.WriteHeader(http.StatusInternalServerError) @@ -198,24 +181,25 @@ func deleteData(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } - defer func() { - rb := func(tx pgx.Tx) { - err := txQueryService.Rollback(tx) - if err != nil { - log.Err(err).Msg("error rolling back transaction") - } - } - if err != nil { - rb(tx) - } else { - internalErr := txQueryService.Commit(tx) - if internalErr != nil { - log.Err(internalErr).Msg("error committing transaction") - rb(tx) - w.WriteHeader(http.StatusInternalServerError) - } - } - }() + defer query.RollbackFunc(txQueryService, tx, w, &err) + // defer func() { + // rb := func(tx pgx.Tx) { + // err := txQueryService.Rollback(tx) + // if err != nil { + // log.Err(err).Msg("error rolling back transaction") + // } + // } + // if err != nil { + // rb(tx) + // } else { + // internalErr := txQueryService.Commit(tx) + // if internalErr != nil { + // log.Err(internalErr).Msg("error committing transaction") + // rb(tx) + // w.WriteHeader(http.StatusInternalServerError) + // } + // } + // }() if err = userRepo.DeleteUserKeyTx(user, key.ID, tx); err != nil { log.Err(err).Msg("could not delete key") w.WriteHeader(http.StatusInternalServerError) diff --git a/pkg/web/router/routes/data_test.go b/pkg/web/router/routes/data_test.go index 5346f03..d574940 100644 --- a/pkg/web/router/routes/data_test.go +++ b/pkg/web/router/routes/data_test.go @@ -340,5 +340,4 @@ func TestDeleteKeyError(t *testing.T) { t.Errorf("deleteData returned wrong status code: got %v want %v", status, http.StatusInternalServerError) } - } diff --git a/pkg/web/router/routes/setup.go b/pkg/web/router/routes/setup.go index 13bb73f..ec56315 100644 --- a/pkg/web/router/routes/setup.go +++ b/pkg/web/router/routes/setup.go @@ -67,24 +67,7 @@ func initialSetup(i *do.Injector) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) return } - defer func() { - rb := func(tx pgx.Tx) { - err := txQueryService.Rollback(tx) - if err != nil { - log.Err(err).Msg("error rolling back transaction") - } - } - if err != nil { - rb(tx) - } else { - internalErr := txQueryService.Commit(tx) - if internalErr != nil { - log.Err(internalErr).Msg("error committing transaction") - rb(tx) - w.WriteHeader(http.StatusInternalServerError) - } - } - }() + defer query.RollbackFunc(txQueryService, tx, w, &err) userRepo := do.MustInvoke[repository.UserRepository](i) user := &models.User{} user.Username = userDto.Username From a36861155daca29c271069dbc03eb7491c4a6ff5 Mon Sep 17 00:00:00 2001 From: therealpaulgg Date: Wed, 14 Aug 2024 22:37:49 -0700 Subject: [PATCH 7/8] remove comment --- pkg/web/router/routes/data.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/pkg/web/router/routes/data.go b/pkg/web/router/routes/data.go index cf7d0d2..fc80f6a 100644 --- a/pkg/web/router/routes/data.go +++ b/pkg/web/router/routes/data.go @@ -182,24 +182,6 @@ func deleteData(i *do.Injector) http.HandlerFunc { return } defer query.RollbackFunc(txQueryService, tx, w, &err) - // defer func() { - // rb := func(tx pgx.Tx) { - // err := txQueryService.Rollback(tx) - // if err != nil { - // log.Err(err).Msg("error rolling back transaction") - // } - // } - // if err != nil { - // rb(tx) - // } else { - // internalErr := txQueryService.Commit(tx) - // if internalErr != nil { - // log.Err(internalErr).Msg("error committing transaction") - // rb(tx) - // w.WriteHeader(http.StatusInternalServerError) - // } - // } - // }() if err = userRepo.DeleteUserKeyTx(user, key.ID, tx); err != nil { log.Err(err).Msg("could not delete key") w.WriteHeader(http.StatusInternalServerError) From e2c25c32cbf61582d955a85ff2dcd2c56d311449 Mon Sep 17 00:00:00 2001 From: Paul Gellai Date: Tue, 29 Oct 2024 22:34:54 -0700 Subject: [PATCH 8/8] add volumes to docker-compose --- docker-compose.yaml | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 7ed7ad0..449a350 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -15,7 +15,7 @@ services: max-size: 10m ports: - '3000:3000' - image: therealpaulgg/ssh-sync-server + image: ssh-sync-server-prerelease container_name: ssh-sync-server ssh-sync-db: image: therealpaulgg/ssh-sync-db:latest @@ -26,17 +26,28 @@ services: - POSTGRES_DB=sshsync restart: always ssh-sync: - image: 2d277cde6d3ba4b20c2743a62000be589ad767febc24bf04b33106285d7c0457 + image: ssh-debug container_name: ssh-sync stdin_open: true # Allows Docker container to keep STDIN open tty: true # Allocates a pseudo-TTY + volumes: + - ssh-sync-volume:/root ssh-sync-2: - image: 2d277cde6d3ba4b20c2743a62000be589ad767febc24bf04b33106285d7c0457 + image: ssh-debug container_name: ssh-sync-2 stdin_open: true # Allows Docker container to keep STDIN open tty: true # Allocates a pseudo-TTY + volumes: + - ssh-sync-2-volume:/root ssh-sync-3: - image: 2d277cde6d3ba4b20c2743a62000be589ad767febc24bf04b33106285d7c0457 + image: ssh-debug container_name: ssh-sync-3 stdin_open: true # Allows Docker container to keep STDIN open tty: true # Allocates a pseudo-TTY + volumes: + - ssh-sync-3-volume:/root + +volumes: + ssh-sync-volume: + ssh-sync-2-volume: + ssh-sync-3-volume: \ No newline at end of file