2020-11-30 09:37:17 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIKey(t *testing.T) {
|
2023-05-25 13:32:32 +00:00
|
|
|
router, config, err := NewAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to run local API: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
APIKey, err := CreateTestBouncer(config.API.Server.DbConfig)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
// Login with empty token
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader(""))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 403, w.Code)
|
|
|
|
assert.Equal(t, "{\"message\":\"access forbidden\"}", w.Body.String())
|
|
|
|
|
|
|
|
// Login with invalid token
|
|
|
|
w = httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader(""))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
req.Header.Add("X-Api-Key", "a1b2c3d4e5f6")
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 403, w.Code)
|
|
|
|
assert.Equal(t, "{\"message\":\"access forbidden\"}", w.Body.String())
|
|
|
|
|
|
|
|
// Login with valid token
|
|
|
|
w = httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, "/v1/decisions", strings.NewReader(""))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
req.Header.Add("X-Api-Key", APIKey)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, "null", w.Body.String())
|
|
|
|
|
|
|
|
}
|