From 29f83e05270d0012ad9f273ac3364106fcff5f50 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Thu, 7 Mar 2024 19:56:34 -0500 Subject: chore: Update paths in test and database configuration This commit updates the paths in the test suite and database configuration to reflect the new directory structure. In the `api/cmd/main_test.go` file, the path for the config file is changed from `viper.SetConfigName(".env")` to `viper.SetConfigFile("../.env")` and `viper.AddConfigPath(".")` is added for configuration purposes. Additionally, `viper.AutomaticEnv()` is added to enable automatic environment variable configuration. In the same file, error handling is improved by adding explicit checks and error messages. The `api/internal/database/database.go` file is also modified, updating the path for database initialization from `"../db/water.sqlite3"` to `"../../db/water.sqlite3"`. These changes ensure proper configuration and functioning of the application. --- api/cmd/main_test.go | 21 ++++++++++++++++----- api/internal/controllers/auth.go | 14 +++++++------- api/internal/controllers/stats.go | 4 ++-- api/internal/controllers/user.go | 2 +- api/internal/database/database.go | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/api/cmd/main_test.go b/api/cmd/main_test.go index 8d0df8d..a6c8381 100644 --- a/api/cmd/main_test.go +++ b/api/cmd/main_test.go @@ -12,8 +12,9 @@ import ( ) func getTestUserCredentials() (string, string) { - viper.SetConfigName(".env") + viper.SetConfigFile("../.env") viper.AddConfigPath(".") + viper.AutomaticEnv() err := viper.ReadInConfig() if err != nil { log.Fatalf("Error while reading config file %s", err) @@ -29,17 +30,29 @@ func TestAuthRoute(t *testing.T) { username, password := getTestUserCredentials() + log.Println("username", username) + log.Println("password", password) + w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/api/v1/auth", nil) + req, err := http.NewRequest("POST", "/api/v1/auth", nil) + if err != nil { + t.Fatalf("Failed to create request: %v", err) + } req.SetBasicAuth(username, password) router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") var response map[string]interface{} - _ = json.Unmarshal(w.Body.Bytes(), &response) + err = json.Unmarshal(w.Body.Bytes(), &response) + if err != nil { + t.Fatalf("Failed to unmarshal response: %v", err) + } _, exists := response["token"] assert.True(t, exists, "response should return a token") + if !exists { + t.Fatalf("response did not contain token") + } } func TestAuthRouteFailure(t *testing.T) { @@ -52,5 +65,3 @@ func TestAuthRouteFailure(t *testing.T) { assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") } - -func Test \ No newline at end of file diff --git a/api/internal/controllers/auth.go b/api/internal/controllers/auth.go index 744a884..de9ed05 100644 --- a/api/internal/controllers/auth.go +++ b/api/internal/controllers/auth.go @@ -1,17 +1,17 @@ package controllers import ( - "encoding/base64" - "net/http" - "github.com/gin-gonic/gin" - "water/api/database" - "errors" "crypto/rand" "database/sql" - - "water/api/models" + "encoding/base64" + "errors" + "github.com/gin-gonic/gin" + "net/http" + "water/api/internal/models" + _ "github.com/mattn/go-sqlite3" "golang.org/x/crypto/bcrypt" + "water/api/internal/database" ) func AuthHandler (c *gin.Context) { diff --git a/api/internal/controllers/stats.go b/api/internal/controllers/stats.go index 9808ace..d8ed434 100644 --- a/api/internal/controllers/stats.go +++ b/api/internal/controllers/stats.go @@ -4,8 +4,8 @@ import ( "database/sql" "github.com/gin-gonic/gin" "net/http" - "water/api/database" - "water/api/models" + "water/api/internal/database" + "water/api/internal/models" ) func GetAllStatistics(c *gin.Context) { diff --git a/api/internal/controllers/user.go b/api/internal/controllers/user.go index 1f3f813..76dedc8 100644 --- a/api/internal/controllers/user.go +++ b/api/internal/controllers/user.go @@ -1,8 +1,8 @@ package controllers import ( - "net/http" "github.com/gin-gonic/gin" + "net/http" ) func GetUser(c *gin.Context) { diff --git a/api/internal/database/database.go b/api/internal/database/database.go index e313af5..19ae818 100644 --- a/api/internal/database/database.go +++ b/api/internal/database/database.go @@ -14,7 +14,7 @@ func SetupDatabase() { } func EstablishDBConnection() *sql.DB { - db, err := sql.Open("sqlite3", "../db/water.sqlite3") + db, err := sql.Open("sqlite3", "../../db/water.sqlite3") if err != nil { panic(err) } -- cgit v1.1