Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed /users API due to security concerns allowing user details visibility #133

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,6 @@ import (
"github.com/uptrace/bun"
)

func GetUserList(ctx *gin.Context, db *bun.DB) {
var users []models.User
err := db.NewSelect().Model(&users).OrderExpr("id ASC").Limit(10).Scan(ctx)

if err != nil {
ctx.JSON(http.StatusInternalServerError, dtos.UserListResponse{
Message: "Failed to fetch users: " + err.Error(),
})
return
}

if len(users) == 0 {
ctx.JSON(http.StatusNotFound, dtos.UserListResponse{
Message: "No users found",
})
return
}

var dtoUsers []dtos.User
for _, user := range users {
dtoUsers = append(dtoUsers, dtos.User{
ID: user.ID,
UserName: user.UserName,
Email: user.Email,
CreatedAt: user.CreatedAt,
})
}

ctx.JSON(http.StatusOK, dtos.UserListResponse{
Message: "users fetched successfully",
Data: dtoUsers,
})
}

func GetUserByID(ctx *gin.Context, db *bun.DB) {
id := ctx.Param("id")

Expand Down
4 changes: 0 additions & 4 deletions dtos/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ type URLNotFoundResponse struct {
Message string `json:"message"`
}

type UserListResponse struct {
Message string `json:"message"`
Data []User `json:"data,omitempty"`
}
type UserResponse struct {
Message string `json:"message"`
Data User `json:"data,omitempty"`
Expand Down
12 changes: 4 additions & 8 deletions routes/users.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package routes

import (
"github.com/gin-gonic/gin"
"github.com/uptrace/bun"
"github.com/Real-Dev-Squad/tiny-site-backend/controllers"
"github.com/Real-Dev-Squad/tiny-site-backend/middlewares"
controller "github.com/Real-Dev-Squad/tiny-site-backend/controllers"
middleware "github.com/Real-Dev-Squad/tiny-site-backend/middlewares"
"github.com/gin-gonic/gin"
"github.com/uptrace/bun"
)

func UserRoutes(rg *gin.RouterGroup, db *bun.DB) {
Expand All @@ -14,10 +14,6 @@ func UserRoutes(rg *gin.RouterGroup, db *bun.DB) {
users.Use(middleware.AuthMiddleware())
user.Use(middleware.AuthMiddleware())

users.GET("", func(ctx *gin.Context) {
controller.GetUserList(ctx, db)
})

users.GET("/:id", func(ctx *gin.Context) {
controller.GetUserByID(ctx, db)
})
Expand Down
16 changes: 0 additions & 16 deletions tests/integration/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ import (
"github.com/stretchr/testify/assert"
)

// TestGetUsersSuccess tests the successful retrieval of a list of users.
func (suite *AppTestSuite) TestGetUsersSuccess() {
// Setup the router and route
router := gin.Default()
router.GET("/v1/users", func(ctx *gin.Context) {
controller.GetUserList(ctx, suite.db)
})

// Create a request and recorder to test the endpoint
req, _ := http.NewRequest("GET", "/v1/users", nil)
w := httptest.NewRecorder()

router.ServeHTTP(w, req)
assert.Equal(suite.T(), http.StatusOK, w.Code, "Expected status code to be 200 for successful user retrieval")
}

// TestGetUserByIDExistingUser tests the retrieval of a user by ID for an existing user and expects a successful response.
func (suite *AppTestSuite) TestGetUserByIDExistingUser() {
router := gin.Default()
Expand Down
Loading