-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: added the model to the user object of the response (#41) * Feat/logs (#43) * feat: Link or hook of the user register query to the system logs table and development of the route to provide access to them * feat: Link or hook of the remaining queries to the system logs table and development of the filter by username and date * docs: Logs routes added to api specification * feat: log and ipaddr util tests * docs: Fix typo on openapi specification * fix: Fix description on log test file for controller --------- Co-authored-by: Antonio Donis <[email protected]>
- Loading branch information
Showing
23 changed files
with
555 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hawks-atlanta/authentication-go/models" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Filter[T any] struct { | ||
Object T `json:"object"` | ||
ItemsPerPage int `json:"itemsPerPage"` | ||
Page int `json:"page"` | ||
} | ||
|
||
func (c *Controller) CreateLog(log *models.Log) (err error) { | ||
log.Model = models.Model{} | ||
return c.DB.Create(log).Error | ||
} | ||
|
||
func (c *Controller) GetLogs() (logs []models.Log, err error) { | ||
|
||
err = c.DB.Find(&logs).Error | ||
return logs, err | ||
} | ||
|
||
func (c *Controller) GetLogsByUser(filter *Filter[models.User]) (logs []models.Log, err error) { | ||
err = c.DB.Transaction(func(tx *gorm.DB) error { | ||
var user models.User | ||
err = tx. | ||
Where("username = ?", filter.Object.Username). | ||
First(&user). | ||
Error | ||
if err != nil { | ||
err = fmt.Errorf("failed to query user: %w", err) | ||
return err | ||
} | ||
|
||
err = tx. | ||
Limit(filter.ItemsPerPage). | ||
Offset((filter.Page-1)*filter.ItemsPerPage). | ||
Where("user_uuid = ?", user.UUID). | ||
Find(&logs). | ||
Error | ||
if err != nil { | ||
err = fmt.Errorf("failed to query user logs: %w", err) | ||
} | ||
return err | ||
}) | ||
return logs, err | ||
} | ||
|
||
func (c *Controller) GetLogsByDate(filter *Filter[time.Time]) (logs []models.Log, err error) { | ||
|
||
err = c.DB. | ||
Limit(filter.ItemsPerPage). | ||
Offset((filter.Page-1)*filter.ItemsPerPage). | ||
Where("log_time > ?", filter.Object). | ||
Find(&logs). | ||
Error | ||
|
||
return logs, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package controller | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/hawks-atlanta/authentication-go/database" | ||
"github.com/hawks-atlanta/authentication-go/models" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestLog(t *testing.T) { | ||
t.Run("Failed user logs query", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
c, err := New(WithDB(db)) | ||
assertions.Nil(err) | ||
|
||
u := "Test" | ||
_, err = c.GetLogsByUser(&Filter[models.User]{Object: models.User{Username: &u}, ItemsPerPage: -10, Page: 1}) | ||
assertions.EqualError(err, "failed to query user: record not found") | ||
})) | ||
|
||
t.Run("No available logs", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
c, err := New(WithDB(db)) | ||
assertions.Nil(err) | ||
|
||
logs, err := c.GetLogs() | ||
assertions.Nil(err) | ||
assertions.Equal(len(logs), 0, "The logs length should be equal to zero") | ||
|
||
u := "Test" | ||
_, err = c.GetLogsByUser(&Filter[models.User]{Object: models.User{Username: &u}, ItemsPerPage: 10, Page: 1}) | ||
assertions.NotNil(err) | ||
|
||
_, err = c.GetLogsByDate(&Filter[time.Time]{Object: time.Now(), ItemsPerPage: 10, Page: 1}) | ||
assertions.Nil(err) | ||
})) | ||
|
||
t.Run("Succeed", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
c, err := New(WithDB(db)) | ||
assertions.Nil(err) | ||
|
||
l, u := models.RandomLog() | ||
err = c.CreateLog(l) | ||
assertions.Nil(err) | ||
|
||
logs, err := c.GetLogs() | ||
assertions.Nil(err) | ||
|
||
assertions.Equal(len(logs), 1, "The logs length should be equal to 1") | ||
|
||
logs, err = c.GetLogsByUser(&Filter[models.User]{Object: models.User{Username: &u}, ItemsPerPage: 10, Page: 1}) | ||
assertions.Nil(err) | ||
assertions.Equal(len(logs), 1, "The logs length filtered by user should be equal to 1") | ||
|
||
logs, err = c.GetLogsByDate(&Filter[time.Time]{Object: time.Now().Add(-time.Hour * 1), ItemsPerPage: 10, Page: 1}) | ||
assertions.Nil(err) | ||
assertions.Equal(len(logs), 1, "The logs length filtered by date should be equal to 1") | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ipaddr | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func GetIpAddr(ctx *gin.Context) string { | ||
|
||
IPAddress := ctx.Request.Header.Get("X-Real-Ip") // Try to get the IP address of a client even if it's behind a proxy or a load balancer | ||
|
||
if IPAddress == "" { | ||
IPAddress = ctx.Request.Header.Get("X-Forwarded-For") // If first method get blank result, try to get the IP address through X-Forwarded-For header | ||
} | ||
if IPAddress == "" { | ||
IPAddress = ctx.Request.RemoteAddr // If the lasts methods don't get success, then get the IP from remote address request | ||
} | ||
|
||
return IPAddress | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ipaddr | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetIpAddr(t *testing.T) { | ||
|
||
req := httptest.NewRequest("GET", "/echo", nil) | ||
req.Header.Set("X-Real-Ip", "192.168.1.1") | ||
w := httptest.NewRecorder() | ||
ctx, _ := gin.CreateTestContext(w) | ||
ctx.Request = req | ||
ipAddress := GetIpAddr(ctx) | ||
assert.Equal(t, "192.168.1.1", ipAddress, "La dirección IP debe ser la misma que X-Real-Ip") | ||
|
||
req = httptest.NewRequest("GET", "/echo", nil) | ||
req.Header.Set("X-Forwarded-For", "192.168.2.2") | ||
w = httptest.NewRecorder() | ||
ctx, _ = gin.CreateTestContext(w) | ||
ctx.Request = req | ||
ipAddress = GetIpAddr(ctx) | ||
assert.Equal(t, "192.168.2.2", ipAddress, "La dirección IP debe ser la misma que X-Forwarded-For") | ||
|
||
req = httptest.NewRequest("GET", "/echo", nil) | ||
w = httptest.NewRecorder() | ||
ctx, _ = gin.CreateTestContext(w) | ||
ctx.Request = req | ||
ipAddress = GetIpAddr(ctx) | ||
assert.NotEmpty(t, ipAddress, "La dirección IP no debe estar vacía") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package models | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/ddosify/go-faker/faker" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type Log struct { | ||
Model | ||
User *User `json:"user,omitempty" gorm:"foreignKey:UserUUID;constraint:OnUpdate:CASCADE"` | ||
UserUUID uuid.UUID `json:"userUUID" gorm:"not null;"` | ||
Action string `json:"action" gorm:"not null"` | ||
IpAddress string `json:"ipaddr" gorm:"not null"` | ||
LogTime time.Time `json:"logTime" gorm:"not null;default:CURRENT_TIMESTAMP"` | ||
} | ||
|
||
func RandomLog() (*Log, string) { | ||
user := RandomUser() | ||
actions := []string{"User login", "User registration", "User JWT renewal", "User password update", "Got user by username"} | ||
return &Log{ | ||
User: user, | ||
UserUUID: user.UUID, | ||
Action: actions[rand.Intn(4)], | ||
IpAddress: faker.NewFaker().RandomIP(), | ||
}, *user.Username | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package models | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRandomLog(t *testing.T) { | ||
firstLog, _ := RandomLog() | ||
secondLog, _ := RandomLog() | ||
assert.NotEqual(t, firstLog, secondLog) | ||
} |
Oops, something went wrong.