44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestAdminOnlyAcceptsSuperAdminAmongMultipleRoles(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
router.GET("/", func(c *gin.Context) {
|
|
c.Set(RolesKey, []string{"purchaser", "super_admin"})
|
|
c.Next()
|
|
}, AdminOnly(), func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if response.Code != http.StatusNoContent {
|
|
t.Fatalf("expected super admin access, got status %d", response.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminOnlyRejectsBusinessRoles(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
router.GET("/", func(c *gin.Context) {
|
|
c.Set(RolesKey, []string{"warehouse_admin", "purchaser"})
|
|
c.Next()
|
|
}, AdminOnly(), func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if response.Code != http.StatusForbidden {
|
|
t.Fatalf("expected forbidden status, got %d", response.Code)
|
|
}
|
|
}
|