From 6ab8a9688898114d2461493343520d888eac57eb Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Wed, 9 Feb 2022 22:25:51 +0000 Subject: [PATCH] Implement first db-test using go-txdb --- cmd/budgeteer/main.go | 2 +- http/account_test.go | 61 +++++++++++++++++++++++++++++++++---------- http/session.go | 22 +++++++--------- postgres/conn.go | 4 +-- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/cmd/budgeteer/main.go b/cmd/budgeteer/main.go index 9ce1ef2..cb57d20 100644 --- a/cmd/budgeteer/main.go +++ b/cmd/budgeteer/main.go @@ -16,7 +16,7 @@ func main() { log.Fatalf("Could not load config: %v", err) } - q, err := postgres.Connect(cfg.DatabaseConnection) + q, err := postgres.Connect("pgx", cfg.DatabaseConnection) if err != nil { log.Fatalf("Failed connecting to DB: %v", err) } diff --git a/http/account_test.go b/http/account_test.go index 0069268..df8a430 100644 --- a/http/account_test.go +++ b/http/account_test.go @@ -4,16 +4,23 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "strings" "testing" "git.javil.eu/jacob1123/budgeteer/bcrypt" "git.javil.eu/jacob1123/budgeteer/jwt" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" + + txdb "github.com/DATA-DOG/go-txdb" ) +func init() { + txdb.Register("pgtx", "pgx", "postgres://budgeteer_test:budgeteer_test@localhost:5432/budgeteer_test") +} + func TestListTimezonesHandler(t *testing.T) { - db, err := postgres.Connect("postgres://budgeteer_test:budgeteer_test@localhost:5432/budgeteer_test") + db, err := postgres.Connect("pgtx", "example") if err != nil { t.Errorf("could not connect to db: %s", err) return @@ -29,18 +36,44 @@ func TestListTimezonesHandler(t *testing.T) { c, engine := gin.CreateTestContext(rr) h.LoadRoutes(engine) - c.Request, err = http.NewRequest(http.MethodGet, "/account/accountid/transactions", nil) - if rr.Code != http.StatusOK { - t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK) - } + t.Run("RegisterUser", func(t *testing.T) { + c.Request, err = http.NewRequest(http.MethodPost, "/api/v1/user/register", strings.NewReader(`{"password":"pass","email":"info@example.com","name":"Test"}`)) + if err != nil { + t.Errorf("error creating request: %s", err) + return + } - var response TransactionsResponse - err = json.NewDecoder(rr.Body).Decode(&response) - if err != nil { - t.Error(err.Error()) - t.Error("Error retreiving list of transactions.") - } - if len(response.Transactions) == 0 { - t.Error("Did not get any transactions.") - } + h.registerPost(c) + + if rr.Code != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK) + } + + var response LoginResponse + err = json.NewDecoder(rr.Body).Decode(&response) + if err != nil { + t.Error(err.Error()) + t.Error("Error registering") + } + if len(response.Token) == 0 { + t.Error("Did not get a token") + } + }) + + t.Run("GetTransactions", func(t *testing.T) { + c.Request, err = http.NewRequest(http.MethodGet, "/account/accountid/transactions", nil) + if rr.Code != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK) + } + + var response TransactionsResponse + err = json.NewDecoder(rr.Body).Decode(&response) + if err != nil { + t.Error(err.Error()) + t.Error("Error retreiving list of transactions.") + } + if len(response.Transactions) == 0 { + t.Error("Did not get any transactions.") + } + }) } diff --git a/http/session.go b/http/session.go index e784d03..a76298a 100644 --- a/http/session.go +++ b/http/session.go @@ -84,11 +84,13 @@ func (h *Handler) loginPost(c *gin.Context) { return } - c.JSON(http.StatusOK, struct { - Token string - User postgres.User - Budgets []postgres.Budget - }{t, user, budgets}) + c.JSON(http.StatusOK, LoginResponse{t, user, budgets}) +} + +type LoginResponse struct { + Token string + User postgres.User + Budgets []postgres.Budget } type registerInformation struct { @@ -108,13 +110,13 @@ func (h *Handler) registerPost(c *gin.Context) { _, err := h.Service.GetUserByUsername(c.Request.Context(), register.Email) if err == nil { - c.AbortWithError(http.StatusUnauthorized, err) + c.AbortWithError(http.StatusBadRequest, fmt.Errorf("email is already taken")) return } hash, err := h.CredentialsVerifier.Hash(register.Password) if err != nil { - c.AbortWithError(http.StatusUnauthorized, err) + c.AbortWithError(http.StatusBadRequest, err) return } @@ -140,9 +142,5 @@ func (h *Handler) registerPost(c *gin.Context) { return } - c.JSON(http.StatusOK, struct { - Token string - User postgres.User - Budgets []postgres.Budget - }{t, user, budgets}) + c.JSON(http.StatusOK, LoginResponse{t, user, budgets}) } diff --git a/postgres/conn.go b/postgres/conn.go index 7df859e..046e127 100644 --- a/postgres/conn.go +++ b/postgres/conn.go @@ -18,8 +18,8 @@ type Database struct { } // Connect to a database -func Connect(connString string) (*Database, error) { - conn, err := sql.Open("pgx", connString) +func Connect(typ string, connString string) (*Database, error) { + conn, err := sql.Open(typ, connString) if err != nil { return nil, fmt.Errorf("open connection: %w", err) }