mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
console: rework a bit user management
Notably, HTTP headers are configurable and a provider is used for the frontend side.
This commit is contained in:
121
console/authentication/handlers_test.go
Normal file
121
console/authentication/handlers_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package authentication
|
||||
|
||||
import (
|
||||
netHTTP "net/http"
|
||||
"testing"
|
||||
|
||||
"akvorado/common/helpers"
|
||||
"akvorado/common/http"
|
||||
"akvorado/common/reporter"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestUserHandler(t *testing.T) {
|
||||
r := reporter.NewMock(t)
|
||||
h := http.NewMock(t, r)
|
||||
c, err := New(r, DefaultConfiguration())
|
||||
if err != nil {
|
||||
t.Fatalf("New() error:\n%+v", err)
|
||||
}
|
||||
|
||||
// Configure the two endpoints
|
||||
endpoint := h.GinRouter.Group("/api/v0/console/user", c.UserAuthentication())
|
||||
endpoint.GET("/info", c.UserInfoHandlerFunc)
|
||||
endpoint.GET("/avatar", c.UserAvatarHandlerFunc)
|
||||
|
||||
t.Run("default user configured", func(t *testing.T) {
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
Description: "user info, no user logged in",
|
||||
URL: "/api/v0/console/user/info",
|
||||
StatusCode: 200,
|
||||
JSONOutput: gin.H{"login": "default", "name": "Default User"},
|
||||
}, {
|
||||
Description: "user info, minimal user logged in",
|
||||
URL: "/api/v0/console/user/info",
|
||||
Header: func() netHTTP.Header {
|
||||
headers := make(netHTTP.Header)
|
||||
headers.Add("Remote-User", "alfred")
|
||||
return headers
|
||||
}(),
|
||||
StatusCode: 200,
|
||||
JSONOutput: gin.H{
|
||||
"login": "alfred",
|
||||
},
|
||||
}, {
|
||||
Description: "user info, complete user logged in",
|
||||
URL: "/api/v0/console/user/info",
|
||||
Header: func() netHTTP.Header {
|
||||
headers := make(netHTTP.Header)
|
||||
headers.Add("Remote-User", "alfred")
|
||||
headers.Add("Remote-Name", "Alfred Pennyworth")
|
||||
headers.Add("Remote-Email", "alfred@batman.com")
|
||||
headers.Add("X-Logout-URL", "/logout")
|
||||
return headers
|
||||
}(),
|
||||
StatusCode: 200,
|
||||
JSONOutput: gin.H{
|
||||
"login": "alfred",
|
||||
"name": "Alfred Pennyworth",
|
||||
"email": "alfred@batman.com",
|
||||
"logout-url": "/logout",
|
||||
},
|
||||
}, {
|
||||
Description: "user info, invalid user logged in",
|
||||
URL: "/api/v0/console/user/info",
|
||||
Header: func() netHTTP.Header {
|
||||
headers := make(netHTTP.Header)
|
||||
headers.Add("Remote-User", "alfred")
|
||||
headers.Add("Remote-Email", "alfrednooo")
|
||||
return headers
|
||||
}(),
|
||||
StatusCode: 200,
|
||||
JSONOutput: gin.H{"login": "default", "name": "Default User"},
|
||||
}, {
|
||||
Description: "avatar, no user logged in",
|
||||
URL: "/api/v0/console/user/avatar",
|
||||
ContentType: "image/png",
|
||||
StatusCode: 200,
|
||||
}, {
|
||||
Description: "avatar, simple user",
|
||||
URL: "/api/v0/console/user/avatar",
|
||||
Header: func() netHTTP.Header {
|
||||
headers := make(netHTTP.Header)
|
||||
headers.Add("Remote-User", "alfred")
|
||||
return headers
|
||||
}(),
|
||||
ContentType: "image/png",
|
||||
StatusCode: 200,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("no default user", func(t *testing.T) {
|
||||
c.config.DefaultUser.Login = ""
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
Description: "user info, no user logged in",
|
||||
URL: "/api/v0/console/user/info",
|
||||
StatusCode: 401,
|
||||
JSONOutput: gin.H{"message": "No user logged in."},
|
||||
}, {
|
||||
Description: "user info, invalid user logged in",
|
||||
URL: "/api/v0/console/user/info",
|
||||
Header: func() netHTTP.Header {
|
||||
headers := make(netHTTP.Header)
|
||||
headers.Add("Remote-User", "alfred")
|
||||
headers.Add("Remote-Email", "alfrednooo")
|
||||
return headers
|
||||
}(),
|
||||
StatusCode: 401,
|
||||
JSONOutput: gin.H{"message": "No user logged in."},
|
||||
}, {
|
||||
Description: "avatar, no user logged in",
|
||||
URL: "/api/v0/console/user/avatar",
|
||||
StatusCode: 401,
|
||||
JSONOutput: gin.H{"message": "No user logged in."},
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user