Tests for unauthorized silenced api access
@@ -13,8 +13,6 @@ import (
"github.com/stretchr/testify/mock"
)
-// TODO: add some authorization testing
-
func TestHttpApiSilencedGet(t *testing.T) {
store := &mockstore.MockStore{}
@@ -41,6 +39,34 @@ func TestHttpApiSilencedGet(t *testing.T) {
assert.EqualValues(t, silenced, returnedSilencedEntries)
}
+func TestHttpApiSilencedGetError(t *testing.T) {
+ store := &mockstore.MockStore{}
+
+ c := &SilencedController{
+ Store: store,
+ }
+
+ var nilSilenced []*types.Silenced
+ store.On("GetSilencedEntries", mock.Anything).Return(nilSilenced, errors.New("error"))
+ req := newRequest("GET", "/silenced", nil)
+ res := processRequest(c, req)
+
+ body := res.Body.Bytes()
+
+ assert.Equal(t, http.StatusInternalServerError, res.Code)
+ assert.Equal(t, "error\n", string(body))
+}
+
+func TestHttpApiSilencedGetUnauthorized(t *testing.T) {
+ controller := SilencedController{}
+
+ req := newRequest("GET", "/silenced", nil)
+ req = requestWithNoAccess(req)
+
+ res := processRequest(&controller, req)
+ assert.Equal(t, http.StatusUnauthorized, res.Code)
+}
+
func TestHttpApiSilencedPost(t *testing.T) {
store := &mockstore.MockStore{}
@@ -108,6 +134,22 @@ func TestHttpApiSilencedClear(t *testing.T) {
}
+func TestHttpApiSilencedDeleteUnauthorized(t *testing.T) {
+ controller := SilencedController{}
+
+ silenced := types.FixtureSilenced("check1")
+ silenced.ID = "test-subcription:test-check"
+ silenced.Subscription = "test-subscription"
+ silenced.CheckName = "test-check"
+
+ encoded, _ := json.Marshal(silenced)
+ req := newRequest("POST", "/silenced/clear", bytes.NewBuffer(encoded))
+ req = requestWithNoAccess(req)
+
+ res := processRequest(&controller, req)
+ assert.Equal(t, http.StatusUnauthorized, res.Code)
+}
+
func TestHttpApiSilencedGetByCheckName(t *testing.T) {
store := &mockstore.MockStore{}
