Skip to content
Open
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
43 changes: 43 additions & 0 deletions internal/controller/context_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ type AppContextResponse struct {
App ACRApp `json:"app"`
}

// VR -> Version Response

type VersionResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Version string `json:"version"`
CommitHash string `json:"commitHash"`
BuildTimestamp string `json:"buildTimestamp"`
}
Comment thread
bsaurusrex marked this conversation as resolved.

type ContextControllerInput struct {
dig.In

Expand Down Expand Up @@ -105,6 +115,8 @@ func NewContextController(i ContextControllerInput) *ContextController {
contextGroup.GET("/user", controller.userContextHandler)
contextGroup.GET("/app", controller.appContextHandler)

i.RouterGroup.GET("/version", controller.versionHandler)

return controller
}

Expand Down Expand Up @@ -148,6 +160,37 @@ func (controller *ContextController) userContextHandler(c *gin.Context) {
c.JSON(200, userContext)
}

func (controller *ContextController) versionHandler(c *gin.Context) {
context, err := new(model.UserContext).NewFromGin(c)

if err != nil {
if !errors.Is(err, model.ErrUserContextNotFound) {
controller.log.App.Error().Err(err).Msg("Failed to create user context from request")
}
c.JSON(200, VersionResponse{
Status: 401,
Message: "Unauthorized",
})
return
}

if !context.IsAuthenticated() {
c.JSON(200, VersionResponse{
Status: 401,
Message: "Unauthorized",
})
return
}

c.JSON(200, VersionResponse{
Status: 200,
Message: "Success",
Version: model.Version,
CommitHash: model.CommitHash,
BuildTimestamp: model.BuildTimestamp,
})
}

func (controller *ContextController) appContextHandler(c *gin.Context) {
c.JSON(200, AppContextResponse{
Status: 200,
Expand Down
64 changes: 64 additions & 0 deletions internal/controller/context_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,70 @@ func TestContextController(t *testing.T) {
return string(bytes)
}(),
},
{
description: "Ensure version returns 401 when unauthorized",
middlewares: []gin.HandlerFunc{},
path: "/api/version",
expected: func() string {
expectedVersionResponse := VersionResponse{
Status: 401,
Message: "Unauthorized",
}
bytes, err := json.Marshal(expectedVersionResponse)
require.NoError(t, err)
return string(bytes)
}(),
},
{
description: "Ensure version returns 401 when context is unauthenticated",
middlewares: []gin.HandlerFunc{
func(c *gin.Context) {
c.Set("context", &model.UserContext{
Authenticated: false,
Provider: model.ProviderTailscale,
})
},
},
path: "/api/version",
expected: func() string {
expectedVersionResponse := VersionResponse{
Status: 401,
Message: "Unauthorized",
}
bytes, err := json.Marshal(expectedVersionResponse)
require.NoError(t, err)
return string(bytes)
}(),
},
{
description: "Ensure version returns the build info when authorized",
middlewares: []gin.HandlerFunc{
func(c *gin.Context) {
c.Set("context", &model.UserContext{
Authenticated: true,
Provider: model.ProviderLocal,
Local: &model.LocalContext{
BaseContext: model.BaseContext{
Username: "johndoe",
},
},
})
},
},
path: "/api/version",
expected: func() string {
expectedVersionResponse := VersionResponse{
Status: 200,
Message: "Success",
Version: model.Version,
CommitHash: model.CommitHash,
BuildTimestamp: model.BuildTimestamp,
}
bytes, err := json.Marshal(expectedVersionResponse)
require.NoError(t, err)
return string(bytes)
}(),
},
}

for _, test := range tests {
Expand Down