105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAccountScope_NoUserID(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(AccountScope())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"ok": true})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(t, 401, w.Code)
|
|
}
|
|
|
|
func TestAccountScope_NoAccountID(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(func(c *gin.Context) { c.Set("user_id", uint(1)); c.Next() })
|
|
r.Use(AccountScope())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"ok": true})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(t, 400, w.Code)
|
|
}
|
|
|
|
func TestAccountScope_FromHeader(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(func(c *gin.Context) { c.Set("user_id", uint(1)); c.Next() })
|
|
r.Use(AccountScope())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
accountID, _ := c.Get("account_id")
|
|
c.JSON(200, gin.H{"account_id": accountID})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
req.Header.Set("X-Account-ID", "5")
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(t, 200, w.Code)
|
|
}
|
|
|
|
func TestAccountScope_FromContext(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(func(c *gin.Context) { c.Set("user_id", uint(1)); c.Set("account_id", uint(2)); c.Next() })
|
|
r.Use(AccountScope())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
accountID, _ := c.Get("account_id")
|
|
c.JSON(200, gin.H{"account_id": accountID})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(t, 200, w.Code)
|
|
}
|
|
|
|
func TestAccountScope_HeaderOverridesContext(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(func(c *gin.Context) { c.Set("user_id", uint(1)); c.Set("account_id", uint(2)); c.Next() })
|
|
r.Use(AccountScope())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
accountID, _ := c.Get("account_id")
|
|
c.JSON(200, gin.H{"account_id": accountID})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
req.Header.Set("X-Account-ID", "10")
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(t, 200, w.Code)
|
|
}
|
|
|
|
func TestAccountScope_InvalidHeaderAccountID(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(func(c *gin.Context) { c.Set("user_id", uint(1)); c.Next() })
|
|
r.Use(AccountScope())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"ok": true})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
req.Header.Set("X-Account-ID", "abc")
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(t, 400, w.Code)
|
|
} |