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

enhance(mock): add current user responses #1177

Merged
merged 1 commit into from
Sep 5, 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
2 changes: 2 additions & 0 deletions mock/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func FakeHandler() http.Handler {
e.POST("/api/v1/users", addUser)
e.PUT("/api/v1/users/:user", updateUser)
e.DELETE("/api/v1/users/:user", removeUser)
e.GET("/api/v1/user", currentUser)
e.PUT("/api/v1/user", currentUser)

// mock endpoints for worker calls
e.GET("/api/v1/workers", getWorkers)
Expand Down
24 changes: 23 additions & 1 deletion mock/server/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

//nolint:dupl // ignore duplicate with user code
package server

import (
Expand All @@ -12,6 +11,7 @@ import (
"github.com/gin-gonic/gin"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/router/middleware/auth"
"github.com/go-vela/types"
)

Expand Down Expand Up @@ -82,6 +82,28 @@ func getUser(c *gin.Context) {
c.JSON(http.StatusOK, body)
}

// currentUser returns mock JSON for a http GET and http PUT.
//
// Pass "invalid" to auth header to test receiving 401 response.
func currentUser(c *gin.Context) {
tkn, _ := auth.RetrieveAccessToken(c.Request)

if strings.Contains(tkn, "invalid") {
msg := "unauthorized"

c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &msg})

return
}

data := []byte(UserResp)

var body api.User
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// addUser returns mock JSON for a http POST.
func addUser(c *gin.Context) {
data := []byte(UserResp)
Expand Down
Loading