diff options
author | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-07 19:56:34 -0500 |
---|---|---|
committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-07 19:56:34 -0500 |
commit | 29f83e05270d0012ad9f273ac3364106fcff5f50 (patch) | |
tree | c7b5df7cc21d6d2eb69950c57575a73bc598d809 | |
parent | 6651daca670664f3de8af9c7bcb74b1e7c6c6be9 (diff) |
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.
-rw-r--r-- | api/cmd/main_test.go | 21 | ||||
-rw-r--r-- | api/internal/controllers/auth.go | 14 | ||||
-rw-r--r-- | api/internal/controllers/stats.go | 4 | ||||
-rw-r--r-- | api/internal/controllers/user.go | 2 | ||||
-rw-r--r-- | 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 ( | |||
12 | ) | 12 | ) |
13 | 13 | ||
14 | func getTestUserCredentials() (string, string) { | 14 | func getTestUserCredentials() (string, string) { |
15 | viper.SetConfigName(".env") | 15 | viper.SetConfigFile("../.env") |
16 | viper.AddConfigPath(".") | 16 | viper.AddConfigPath(".") |
17 | viper.AutomaticEnv() | ||
17 | err := viper.ReadInConfig() | 18 | err := viper.ReadInConfig() |
18 | if err != nil { | 19 | if err != nil { |
19 | log.Fatalf("Error while reading config file %s", err) | 20 | log.Fatalf("Error while reading config file %s", err) |
@@ -29,17 +30,29 @@ func TestAuthRoute(t *testing.T) { | |||
29 | 30 | ||
30 | username, password := getTestUserCredentials() | 31 | username, password := getTestUserCredentials() |
31 | 32 | ||
33 | log.Println("username", username) | ||
34 | log.Println("password", password) | ||
35 | |||
32 | w := httptest.NewRecorder() | 36 | w := httptest.NewRecorder() |
33 | req, _ := http.NewRequest("POST", "/api/v1/auth", nil) | 37 | req, err := http.NewRequest("POST", "/api/v1/auth", nil) |
38 | if err != nil { | ||
39 | t.Fatalf("Failed to create request: %v", err) | ||
40 | } | ||
34 | req.SetBasicAuth(username, password) | 41 | req.SetBasicAuth(username, password) |
35 | router.ServeHTTP(w, req) | 42 | router.ServeHTTP(w, req) |
36 | 43 | ||
37 | assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") | 44 | assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") |
38 | 45 | ||
39 | var response map[string]interface{} | 46 | var response map[string]interface{} |
40 | _ = json.Unmarshal(w.Body.Bytes(), &response) | 47 | err = json.Unmarshal(w.Body.Bytes(), &response) |
48 | if err != nil { | ||
49 | t.Fatalf("Failed to unmarshal response: %v", err) | ||
50 | } | ||
41 | _, exists := response["token"] | 51 | _, exists := response["token"] |
42 | assert.True(t, exists, "response should return a token") | 52 | assert.True(t, exists, "response should return a token") |
53 | if !exists { | ||
54 | t.Fatalf("response did not contain token") | ||
55 | } | ||
43 | } | 56 | } |
44 | 57 | ||
45 | func TestAuthRouteFailure(t *testing.T) { | 58 | func TestAuthRouteFailure(t *testing.T) { |
@@ -52,5 +65,3 @@ func TestAuthRouteFailure(t *testing.T) { | |||
52 | 65 | ||
53 | assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") | 66 | assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") |
54 | } | 67 | } |
55 | |||
56 | 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 @@ | |||
1 | package controllers | 1 | package controllers |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "encoding/base64" | ||
5 | "net/http" | ||
6 | "github.com/gin-gonic/gin" | ||
7 | "water/api/database" | ||
8 | "errors" | ||
9 | "crypto/rand" | 4 | "crypto/rand" |
10 | "database/sql" | 5 | "database/sql" |
11 | 6 | "encoding/base64" | |
12 | "water/api/models" | 7 | "errors" |
8 | "github.com/gin-gonic/gin" | ||
9 | "net/http" | ||
10 | "water/api/internal/models" | ||
11 | |||
13 | _ "github.com/mattn/go-sqlite3" | 12 | _ "github.com/mattn/go-sqlite3" |
14 | "golang.org/x/crypto/bcrypt" | 13 | "golang.org/x/crypto/bcrypt" |
14 | "water/api/internal/database" | ||
15 | ) | 15 | ) |
16 | 16 | ||
17 | func AuthHandler (c *gin.Context) { | 17 | 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 ( | |||
4 | "database/sql" | 4 | "database/sql" |
5 | "github.com/gin-gonic/gin" | 5 | "github.com/gin-gonic/gin" |
6 | "net/http" | 6 | "net/http" |
7 | "water/api/database" | 7 | "water/api/internal/database" |
8 | "water/api/models" | 8 | "water/api/internal/models" |
9 | ) | 9 | ) |
10 | 10 | ||
11 | func GetAllStatistics(c *gin.Context) { | 11 | 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 @@ | |||
1 | package controllers | 1 | package controllers |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "net/http" | ||
5 | "github.com/gin-gonic/gin" | 4 | "github.com/gin-gonic/gin" |
5 | "net/http" | ||
6 | ) | 6 | ) |
7 | 7 | ||
8 | func GetUser(c *gin.Context) { | 8 | 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() { | |||
14 | } | 14 | } |
15 | 15 | ||
16 | func EstablishDBConnection() *sql.DB { | 16 | func EstablishDBConnection() *sql.DB { |
17 | db, err := sql.Open("sqlite3", "../db/water.sqlite3") | 17 | db, err := sql.Open("sqlite3", "../../db/water.sqlite3") |
18 | if err != nil { | 18 | if err != nil { |
19 | panic(err) | 19 | panic(err) |
20 | } | 20 | } |