-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from shallwet/login
feat(api): login test(api): add pytest
- Loading branch information
Showing
20 changed files
with
538 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: Pythontest Check | ||
|
||
on: [pull_request] | ||
|
||
env: | ||
TOKEN_EXPIRE_TIME: 15s | ||
RABBITMQ_STR: amqp://guest:guest@localhost:5672// | ||
|
||
jobs: | ||
pytest: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Start Redis Docker container | ||
run: | | ||
docker run -d --name my-redis -p 6379:6379 redis | ||
- name: Start RabbitMQ Docker container | ||
run: | | ||
docker run -d --name my-rabbitmq -p 5672:5672 -p :15672:15672 rabbitmq:management | ||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '^1.21' | ||
cache-dependency-path: "**/*.sum" | ||
|
||
- name: Run main.go in test | ||
run: | | ||
cd master | ||
export ENV=test | ||
env env=test go run main.go > output.log 2>&1 & | ||
sleep 60 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.8' | ||
|
||
- name: Install dependencies | ||
run: | | ||
cd master/test | ||
pip install -r requirements.txt | ||
- name: Run pytest in test folder | ||
run: | | ||
cd master/test | ||
pytest |
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,72 @@ | ||
package auth | ||
|
||
import ( | ||
"GalaxyEmpireWeb/api" | ||
"GalaxyEmpireWeb/models" | ||
"GalaxyEmpireWeb/services/jwtservice" | ||
"GalaxyEmpireWeb/services/userservice" | ||
"GalaxyEmpireWeb/utils" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type authResponse struct { | ||
Token string `json:"token"` | ||
} | ||
|
||
// @Summary User Login | ||
// @Description Authenticate user and generate JWT token | ||
// @Tags auth | ||
// @Accept json | ||
// @Produce json | ||
// @Param user body models.User required "UserID and Password" | ||
// @Success 200 {object} authResponse "Successful response with JWT token" | ||
// @Failure 400 {object} api.ErrorResponse "Bad request with error message" | ||
// @Failure 500 {object} api.ErrorResponse "Internal server error with error message" | ||
// @Router /login [post] | ||
func LoginHandler(c *gin.Context) { | ||
traceID := utils.TraceIDFromContext(c) | ||
userService, err := userservice.GetService(c) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, api.ErrorResponse{ | ||
Succeed: false, | ||
Error: err.Error(), | ||
Message: "Failed to get user service", | ||
TraceID: traceID, | ||
}) | ||
} | ||
user := &models.User{} | ||
if err := c.ShouldBindJSON(user); err != nil { | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse{ | ||
Succeed: false, | ||
Error: err.Error(), | ||
Message: "Failed to bind json", | ||
TraceID: traceID, | ||
}) | ||
return | ||
} | ||
err2 := userService.LoginUser(c, user) | ||
if err2 != nil { | ||
c.JSON(http.StatusUnauthorized, api.ErrorResponse{ | ||
Succeed: false, | ||
Error: err2.Error(), | ||
Message: "Wrong Username or Password", | ||
TraceID: traceID, | ||
}) | ||
return | ||
} | ||
token, err3 := jwtservice.GenerateToken(user.ID) | ||
if err3 != nil { | ||
c.JSON(http.StatusInternalServerError, api.ErrorResponse{ | ||
Succeed: false, | ||
Error: err3.Error(), | ||
Message: "Failed to generate token", | ||
TraceID: traceID, | ||
}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, authResponse{ | ||
Token: token, | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -1 +1,33 @@ | ||
package middleware | ||
|
||
import ( | ||
"GalaxyEmpireWeb/services/jwtservice" | ||
"GalaxyEmpireWeb/services/userservice" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func JWTAuthMiddleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// Get token from header | ||
authHeader := c.GetHeader("Authorization") | ||
if authHeader == "" { | ||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is missing"}) | ||
return | ||
} | ||
claims, err := jwtservice.ParseToken(authHeader) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
userService, err := userservice.GetService(c) | ||
role := userService.GetUserRole(c, claims.UserID) | ||
// 设置上下文 | ||
c.Set("claims", claims) | ||
c.Set("role", role) | ||
c.Set("userID", claims.UserID) | ||
c.Next() | ||
|
||
} | ||
} |
Oops, something went wrong.