diff options
Diffstat (limited to 'api')
| -rw-r--r-- | api/cmd/main_test.go | 9 | ||||
| -rw-r--r-- | api/internal/controllers/auth.go | 11 | ||||
| -rw-r--r-- | api/internal/controllers/stats.go | 6 | ||||
| -rw-r--r-- | api/internal/database/database.go | 4 |
4 files changed, 22 insertions, 8 deletions
diff --git a/api/cmd/main_test.go b/api/cmd/main_test.go index a6c8381..049cf6e 100644 --- a/api/cmd/main_test.go +++ b/api/cmd/main_test.go | |||
| @@ -6,6 +6,7 @@ import ( | |||
| 6 | "net/http" | 6 | "net/http" |
| 7 | "net/http/httptest" | 7 | "net/http/httptest" |
| 8 | "testing" | 8 | "testing" |
| 9 | "water/api/internal/router" | ||
| 9 | 10 | ||
| 10 | "github.com/spf13/viper" | 11 | "github.com/spf13/viper" |
| 11 | "github.com/stretchr/testify/assert" | 12 | "github.com/stretchr/testify/assert" |
| @@ -26,7 +27,7 @@ func getTestUserCredentials() (string, string) { | |||
| 26 | } | 27 | } |
| 27 | 28 | ||
| 28 | func TestAuthRoute(t *testing.T) { | 29 | func TestAuthRoute(t *testing.T) { |
| 29 | router := setupRouter() | 30 | r := router.SetupRouter() |
| 30 | 31 | ||
| 31 | username, password := getTestUserCredentials() | 32 | username, password := getTestUserCredentials() |
| 32 | 33 | ||
| @@ -39,7 +40,7 @@ func TestAuthRoute(t *testing.T) { | |||
| 39 | t.Fatalf("Failed to create request: %v", err) | 40 | t.Fatalf("Failed to create request: %v", err) |
| 40 | } | 41 | } |
| 41 | req.SetBasicAuth(username, password) | 42 | req.SetBasicAuth(username, password) |
| 42 | router.ServeHTTP(w, req) | 43 | r.ServeHTTP(w, req) |
| 43 | 44 | ||
| 44 | assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") | 45 | assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") |
| 45 | 46 | ||
| @@ -56,12 +57,12 @@ func TestAuthRoute(t *testing.T) { | |||
| 56 | } | 57 | } |
| 57 | 58 | ||
| 58 | func TestAuthRouteFailure(t *testing.T) { | 59 | func TestAuthRouteFailure(t *testing.T) { |
| 59 | router := setupRouter() | 60 | r := router.SetupRouter() |
| 60 | 61 | ||
| 61 | w := httptest.NewRecorder() | 62 | w := httptest.NewRecorder() |
| 62 | req, _ := http.NewRequest("POST", "/api/v1/auth", nil) | 63 | req, _ := http.NewRequest("POST", "/api/v1/auth", nil) |
| 63 | req.SetBasicAuth("asdf", "asdf") | 64 | req.SetBasicAuth("asdf", "asdf") |
| 64 | router.ServeHTTP(w, req) | 65 | r.ServeHTTP(w, req) |
| 65 | 66 | ||
| 66 | assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") | 67 | assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") |
| 67 | } | 68 | } |
diff --git a/api/internal/controllers/auth.go b/api/internal/controllers/auth.go index de9ed05..58653d0 100644 --- a/api/internal/controllers/auth.go +++ b/api/internal/controllers/auth.go | |||
| @@ -14,6 +14,11 @@ import ( | |||
| 14 | "water/api/internal/database" | 14 | "water/api/internal/database" |
| 15 | ) | 15 | ) |
| 16 | 16 | ||
| 17 | |||
| 18 | |||
| 19 | // AuthHandler is a function that handles users' authentication. It checks if the request | ||
| 20 | // has valid credentials, authenticates the user and sets the user's session. | ||
| 21 | // If the authentication is successful, it will allow the user to access protected routes. | ||
| 17 | func AuthHandler (c *gin.Context) { | 22 | func AuthHandler (c *gin.Context) { |
| 18 | username, password, ok := c.Request.BasicAuth() | 23 | username, password, ok := c.Request.BasicAuth() |
| 19 | if !ok { | 24 | if !ok { |
| @@ -55,7 +60,11 @@ func AuthHandler (c *gin.Context) { | |||
| 55 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) | 60 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) |
| 56 | } | 61 | } |
| 57 | 62 | ||
| 58 | // generatToken will g | 63 | |
| 64 | // generateToken is a helper function used in the AuthHandler. It generates a random token for API authentication. | ||
| 65 | // This function creates an empty byte slice of length 32 and fills it with cryptographic random data using the rand.Read function. | ||
| 66 | // If an error occurs during the generation, it will return an empty string. | ||
| 67 | // The generated cryptographic random data is then encoded into a base64 string and returned. | ||
| 59 | func generateToken() string { | 68 | func generateToken() string { |
| 60 | token := make([]byte, 32) | 69 | token := make([]byte, 32) |
| 61 | _, err := rand.Read(token) | 70 | _, err := rand.Read(token) |
diff --git a/api/internal/controllers/stats.go b/api/internal/controllers/stats.go index d8ed434..2234787 100644 --- a/api/internal/controllers/stats.go +++ b/api/internal/controllers/stats.go | |||
| @@ -8,6 +8,10 @@ import ( | |||
| 8 | "water/api/internal/models" | 8 | "water/api/internal/models" |
| 9 | ) | 9 | ) |
| 10 | 10 | ||
| 11 | // TODO: add comments to all exported members of package. | ||
| 12 | |||
| 13 | // GetAllStatistics connects to the database and queries for all statistics in the database. | ||
| 14 | // If none have been found it will return an error, otherwise a 200 code is sent along with the list of statistics. | ||
| 11 | func GetAllStatistics(c *gin.Context) { | 15 | func GetAllStatistics(c *gin.Context) { |
| 12 | db := database.EstablishDBConnection() | 16 | db := database.EstablishDBConnection() |
| 13 | defer func(db *sql.DB) { | 17 | defer func(db *sql.DB) { |
| @@ -78,7 +82,7 @@ func PostNewStatistic(c *gin.Context) { | |||
| 78 | c.JSON(http.StatusCreated, gin.H{"status": "created", "id": id}) | 82 | c.JSON(http.StatusCreated, gin.H{"status": "created", "id": id}) |
| 79 | } | 83 | } |
| 80 | 84 | ||
| 81 | func GetWeeklyStatistics (c *gin.Context) { | 85 | func GetWeeklyStatistics(c *gin.Context) { |
| 82 | db := database.EstablishDBConnection() | 86 | db := database.EstablishDBConnection() |
| 83 | defer func(db *sql.DB) { | 87 | defer func(db *sql.DB) { |
| 84 | err := db.Close() | 88 | err := db.Close() |
diff --git a/api/internal/database/database.go b/api/internal/database/database.go index 19ae818..7af9780 100644 --- a/api/internal/database/database.go +++ b/api/internal/database/database.go | |||
| @@ -7,14 +7,14 @@ import ( | |||
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | func SetupDatabase() { | 9 | func SetupDatabase() { |
| 10 | _, err := sql.Open("sqlite3", "water.db") | 10 | _, err := sql.Open("sqlite3", "water.sqlite3") |
| 11 | if err != nil { | 11 | if err != nil { |
| 12 | log.Fatal(err) | 12 | log.Fatal(err) |
| 13 | } | 13 | } |
| 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 | } |
