Skip to content

Commit 5c58486

Browse files
Merge pull request #4 from pangolin-do-golang/feature/add-logged-user-context
Add logged user context
2 parents c5798b8 + c51fc8b commit 5c58486

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

internal/adapters/db/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package db
22

33
type User struct {
4+
ID int
45
Nickname string
56
Password string
67
}

internal/adapters/rest/middleware/authmiddleware.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middleware
22

33
import (
44
"github.com/gin-gonic/gin"
5+
"github.com/pangolin-do-golang/thumb-processor-api/internal/core/users"
56
"net/http"
67
)
78

@@ -21,6 +22,10 @@ func AuthMiddleware(allowedUsersFunc func() gin.Accounts) gin.HandlerFunc {
2122
return
2223
}
2324

25+
loggedUser := users.GetUserByNickname(username)
26+
27+
c.Set("logged_user_id", loggedUser.ID)
28+
2429
c.Next() // Continue to the handler
2530
}
2631
}

internal/adapters/rest/middleware/authmiddleware_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ func TestAuthMiddleware(t *testing.T) {
2222
}{
2323
{
2424
name: "Valid Credentials",
25-
username: "testuser",
25+
username: "test",
2626
password: "testpassword",
27-
allowedUsersFunc: func() gin.Accounts { return gin.Accounts{"testuser": "testpassword"} },
27+
allowedUsersFunc: func() gin.Accounts { return gin.Accounts{"test": "testpassword"} },
2828
expectedStatus: http.StatusOK, // Or 200 if you're checking for a successful continuation
2929
expectedMessage: "", // No message expected on success
3030
},

internal/core/users/users.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package users
33
import (
44
"github.com/gin-gonic/gin"
55
"github.com/pangolin-do-golang/thumb-processor-api/internal/adapters/db"
6+
"math/rand"
67
)
78

89
var users = []db.User{
9-
{Nickname: "user", Password: "user"},
10-
{Nickname: "test", Password: "test"},
11-
{Nickname: "prod", Password: "prod"},
10+
{ID: 1, Nickname: "user", Password: "user"},
11+
{ID: 2, Nickname: "test", Password: "test"},
12+
{ID: 3, Nickname: "prod", Password: "prod"},
1213
}
1314

1415
func GetAllowedUsers() gin.Accounts {
@@ -20,5 +21,14 @@ func GetAllowedUsers() gin.Accounts {
2021
}
2122

2223
func CreateUser(nickname, password string) {
23-
users = append(users, db.User{Nickname: nickname, Password: password})
24+
users = append(users, db.User{ID: rand.Int(), Nickname: nickname, Password: password})
25+
}
26+
27+
func GetUserByNickname(nickname string) *db.User {
28+
for _, user := range users {
29+
if user.Nickname == nickname {
30+
return &user
31+
}
32+
}
33+
return nil
2434
}

internal/core/users/users_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,54 @@ func TestCreateUser(t *testing.T) {
5252
{Nickname: "prod", Password: "prod"},
5353
}
5454
}
55+
56+
func TestGetUserByNickname(t *testing.T) {
57+
// Test case 1: User exists
58+
existingUser := "user"
59+
user := GetUserByNickname(existingUser)
60+
61+
if user == nil {
62+
t.Errorf("GetUserByNickname(%s) returned nil, expected a user", existingUser)
63+
} else if user.Nickname != existingUser {
64+
t.Errorf("GetUserByNickname(%s) returned user with nickname %s, expected %s", existingUser, user.Nickname, existingUser)
65+
}
66+
67+
// Test case 2: User does not exist
68+
nonExistingUser := "nonexistent"
69+
user = GetUserByNickname(nonExistingUser)
70+
71+
if user != nil {
72+
t.Errorf("GetUserByNickname(%s) returned a user, expected nil", nonExistingUser)
73+
}
74+
75+
// Test case 3: Empty nickname
76+
emptyNickname := ""
77+
user = GetUserByNickname(emptyNickname)
78+
79+
if user != nil {
80+
t.Errorf("GetUserByNickname(%s) returned a user, expected nil", emptyNickname)
81+
}
82+
83+
// Test case 4: Check if the returned user is a copy, not a reference to the original slice
84+
originalUsers := users // Keep a copy of the original users slice
85+
86+
testNickname := "test_copy"
87+
testPassword := "password_copy"
88+
CreateUser(testNickname, testPassword) // Add a new user
89+
90+
retrievedUser := GetUserByNickname(testNickname)
91+
if retrievedUser == nil {
92+
t.Errorf("GetUserByNickname(%s) returned nil, expected a user", testNickname)
93+
}
94+
95+
retrievedUser.Password = "modified_password" // Modify the retrieved user's password
96+
97+
originalRetrievedUser := GetUserByNickname(testNickname) //Get again the user
98+
99+
if originalRetrievedUser.Password == "modified_password" {
100+
t.Errorf("GetUserByNickname returned a pointer to the original user in the slice. Should return a copy")
101+
}
102+
103+
users = originalUsers
104+
105+
}

0 commit comments

Comments
 (0)