2020-11-30 09:37:17 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-06-22 08:29:02 +00:00
|
|
|
const (
|
|
|
|
APIKEY = "apikey"
|
|
|
|
PASSWORD = "password"
|
|
|
|
)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func TestDeleteDecisionRange(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
lapi := SetupLAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create Valid Alert
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_minibulk.json")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// delete by ip wrong
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "DELETE", "/v1/decisions?range=1.2.3.0/24", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
|
|
|
|
|
|
|
|
// delete by range
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions?range=91.121.79.0/24&contains=false", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"2"}`, w.Body.String())
|
|
|
|
|
|
|
|
// delete by range : ensure it was already deleted
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions?range=91.121.79.0/24", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteDecisionFilter(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
lapi := SetupLAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create Valid Alert
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_minibulk.json")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// delete by ip wrong
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "DELETE", "/v1/decisions?ip=1.2.3.4", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
|
|
|
|
|
|
|
|
// delete by ip good
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions?ip=91.121.79.179", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"1"}`, w.Body.String())
|
|
|
|
|
|
|
|
// delete by scope/value
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions?scopes=Ip&value=91.121.79.178", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"1"}`, w.Body.String())
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:37:27 +00:00
|
|
|
func TestDeleteDecisionFilterByScenario(t *testing.T) {
|
|
|
|
lapi := SetupLAPITest(t)
|
|
|
|
|
|
|
|
// Create Valid Alert
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_minibulk.json")
|
2022-10-19 12:37:27 +00:00
|
|
|
|
|
|
|
// delete by wrong scenario
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "DELETE", "/v1/decisions?scenario=crowdsecurity/ssh-bff", emptyBody, PASSWORD)
|
2022-10-19 12:37:27 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
|
|
|
|
|
|
|
|
// delete by scenario good
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions?scenario=crowdsecurity/ssh-bf", emptyBody, PASSWORD)
|
2022-10-19 12:37:27 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, `{"nbDeleted":"2"}`, w.Body.String())
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func TestGetDecisionFilters(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
lapi := SetupLAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create Valid Alert
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_minibulk.json")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Get Decision
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "GET", "/v1/decisions", emptyBody, APIKEY)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
decisions, code := readDecisionsGetResp(t, w)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions, 2)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, "crowdsecurity/ssh-bf", *decisions[0].Scenario)
|
|
|
|
assert.Equal(t, "91.121.79.179", *decisions[0].Value)
|
|
|
|
assert.Equal(t, int64(1), decisions[0].ID)
|
|
|
|
assert.Equal(t, "crowdsecurity/ssh-bf", *decisions[1].Scenario)
|
|
|
|
assert.Equal(t, "91.121.79.178", *decisions[1].Value)
|
|
|
|
assert.Equal(t, int64(2), decisions[1].ID)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Get Decision : type filter
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions?type=ban", emptyBody, APIKEY)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
decisions, code = readDecisionsGetResp(t, w)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions, 2)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, "crowdsecurity/ssh-bf", *decisions[0].Scenario)
|
|
|
|
assert.Equal(t, "91.121.79.179", *decisions[0].Value)
|
|
|
|
assert.Equal(t, int64(1), decisions[0].ID)
|
|
|
|
assert.Equal(t, "crowdsecurity/ssh-bf", *decisions[1].Scenario)
|
|
|
|
assert.Equal(t, "91.121.79.178", *decisions[1].Value)
|
|
|
|
assert.Equal(t, int64(2), decisions[1].ID)
|
|
|
|
|
|
|
|
// assert.Contains(t, w.Body.String(), `"id":1,"origin":"crowdsec","scenario":"crowdsecurity/ssh-bf","scope":"Ip","type":"ban","value":"91.121.79.179"`)
|
|
|
|
// assert.Contains(t, w.Body.String(), `"id":2,"origin":"crowdsec","scenario":"crowdsecurity/ssh-bf","scope":"Ip","type":"ban","value":"91.121.79.178"`)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Get Decision : scope/value
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions?scopes=Ip&value=91.121.79.179", emptyBody, APIKEY)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
decisions, code = readDecisionsGetResp(t, w)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions, 1)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, "crowdsecurity/ssh-bf", *decisions[0].Scenario)
|
|
|
|
assert.Equal(t, "91.121.79.179", *decisions[0].Value)
|
|
|
|
assert.Equal(t, int64(1), decisions[0].ID)
|
|
|
|
|
|
|
|
// assert.Contains(t, w.Body.String(), `"id":1,"origin":"crowdsec","scenario":"crowdsecurity/ssh-bf","scope":"Ip","type":"ban","value":"91.121.79.179"`)
|
|
|
|
// assert.NotContains(t, w.Body.String(), `"id":2,"origin":"crowdsec","scenario":"crowdsecurity/ssh-bf","scope":"Ip","type":"ban","value":"91.121.79.178"`)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Get Decision : ip filter
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions?ip=91.121.79.179", emptyBody, APIKEY)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
decisions, code = readDecisionsGetResp(t, w)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions, 1)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, "crowdsecurity/ssh-bf", *decisions[0].Scenario)
|
|
|
|
assert.Equal(t, "91.121.79.179", *decisions[0].Value)
|
|
|
|
assert.Equal(t, int64(1), decisions[0].ID)
|
|
|
|
|
|
|
|
// assert.Contains(t, w.Body.String(), `"id":1,"origin":"crowdsec","scenario":"crowdsecurity/ssh-bf","scope":"Ip","type":"ban","value":"91.121.79.179"`)
|
|
|
|
// assert.NotContains(t, w.Body.String(), `"id":2,"origin":"crowdsec","scenario":"crowdsecurity/ssh-bf","scope":"Ip","type":"ban","value":"91.121.79.178"`)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Get decision : by range
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions?range=91.121.79.0/24&contains=false", emptyBody, APIKEY)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
decisions, code = readDecisionsGetResp(t, w)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions, 2)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Contains(t, []string{*decisions[0].Value, *decisions[1].Value}, "91.121.79.179")
|
|
|
|
assert.Contains(t, []string{*decisions[0].Value, *decisions[1].Value}, "91.121.79.178")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDecision(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
lapi := SetupLAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create Valid Alert
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_sample.json")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-03-16 13:37:42 +00:00
|
|
|
// Get Decision
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "GET", "/v1/decisions", emptyBody, APIKEY)
|
2022-03-16 13:37:42 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
decisions, code := readDecisionsGetResp(t, w)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions, 3)
|
2022-03-29 12:20:26 +00:00
|
|
|
/*decisions get doesn't perform deduplication*/
|
|
|
|
assert.Equal(t, "crowdsecurity/test", *decisions[0].Scenario)
|
|
|
|
assert.Equal(t, "127.0.0.1", *decisions[0].Value)
|
|
|
|
assert.Equal(t, int64(1), decisions[0].ID)
|
|
|
|
|
|
|
|
assert.Equal(t, "crowdsecurity/test", *decisions[1].Scenario)
|
|
|
|
assert.Equal(t, "127.0.0.1", *decisions[1].Value)
|
|
|
|
assert.Equal(t, int64(2), decisions[1].ID)
|
|
|
|
|
|
|
|
assert.Equal(t, "crowdsecurity/test", *decisions[2].Scenario)
|
|
|
|
assert.Equal(t, "127.0.0.1", *decisions[2].Value)
|
|
|
|
assert.Equal(t, int64(3), decisions[2].ID)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-03-16 13:37:42 +00:00
|
|
|
// Get Decision with invalid filter. It should ignore this filter
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions?test=test", emptyBody, APIKEY)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions, 3)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteDecisionByID(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
lapi := SetupLAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create Valid Alert
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_sample.json")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
//Have one alerts
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "GET", "/v1/decisions/stream?startup=true", emptyBody, APIKEY)
|
|
|
|
decisions, code := readDecisionsStreamResp(t, w)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, decisions["deleted"])
|
|
|
|
assert.Len(t, decisions["new"], 1)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
// Delete alert with Invalid ID
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions/test", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 400, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
errResp, _ := readDecisionsErrorResp(t, w)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Equal(t, "decision_id must be valid integer", errResp["message"])
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Delete alert with ID that not exist
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions/100", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 500, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
errResp, _ = readDecisionsErrorResp(t, w)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Equal(t, "decision with id '100' doesn't exist: unable to delete", errResp["message"])
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
//Have one alerts
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions/stream?startup=true", emptyBody, APIKEY)
|
|
|
|
decisions, code = readDecisionsStreamResp(t, w)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, decisions["deleted"])
|
|
|
|
assert.Len(t, decisions["new"], 1)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Delete alert with valid ID
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions/1", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
resp, _ := readDecisionsDeleteResp(t, w)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Equal(t, "1", resp.NbDeleted)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
//Have one alert (because we delete an alert that has dup targets)
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions/stream?startup=true", emptyBody, APIKEY)
|
|
|
|
decisions, code = readDecisionsStreamResp(t, w)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, decisions["deleted"])
|
|
|
|
assert.Len(t, decisions["new"], 1)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteDecision(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
lapi := SetupLAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create Valid Alert
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_sample.json")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Delete alert with Invalid filter
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "DELETE", "/v1/decisions?test=test", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 500, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
errResp, _ := readDecisionsErrorResp(t, w)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Equal(t, "'test' doesn't exist: invalid filter", errResp["message"])
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
// Delete all alert
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
resp, _ := readDecisionsDeleteResp(t, w)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Equal(t, "3", resp.NbDeleted)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
func TestStreamStartDecisionDedup(t *testing.T) {
|
|
|
|
//Ensure that at stream startup we only get the longest decision
|
|
|
|
lapi := SetupLAPITest(t)
|
|
|
|
|
|
|
|
// Create Valid Alert : 3 decisions for 127.0.0.1, longest has id=3
|
2024-01-04 16:10:36 +00:00
|
|
|
lapi.InsertAlertFromFile(t, "./tests/alert_sample.json")
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
// Get Stream, we only get one decision (the longest one)
|
2024-01-04 16:10:36 +00:00
|
|
|
w := lapi.RecordResponse(t, "GET", "/v1/decisions/stream?startup=true", emptyBody, APIKEY)
|
|
|
|
decisions, code := readDecisionsStreamResp(t, w)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, decisions["deleted"])
|
|
|
|
assert.Len(t, decisions["new"], 1)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, int64(3), decisions["new"][0].ID)
|
|
|
|
assert.Equal(t, "test", *decisions["new"][0].Origin)
|
|
|
|
assert.Equal(t, "127.0.0.1", *decisions["new"][0].Value)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
// id=3 decision is deleted, this won't affect `deleted`, because there are decisions on the same ip
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions/3", emptyBody, PASSWORD)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
// Get Stream, we only get one decision (the longest one, id=2)
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions/stream?startup=true", emptyBody, APIKEY)
|
|
|
|
decisions, code = readDecisionsStreamResp(t, w)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, decisions["deleted"])
|
|
|
|
assert.Len(t, decisions["new"], 1)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, int64(2), decisions["new"][0].ID)
|
|
|
|
assert.Equal(t, "test", *decisions["new"][0].Origin)
|
|
|
|
assert.Equal(t, "127.0.0.1", *decisions["new"][0].Value)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
// We delete another decision, yet don't receive it in stream, since there's another decision on same IP
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions/2", emptyBody, PASSWORD)
|
2022-03-17 13:12:13 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
// And get the remaining decision (1)
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions/stream?startup=true", emptyBody, APIKEY)
|
|
|
|
decisions, code = readDecisionsStreamResp(t, w)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, decisions["deleted"])
|
|
|
|
assert.Len(t, decisions["new"], 1)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, int64(1), decisions["new"][0].ID)
|
|
|
|
assert.Equal(t, "test", *decisions["new"][0].Origin)
|
|
|
|
assert.Equal(t, "127.0.0.1", *decisions["new"][0].Value)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
// We delete the last decision, we receive the delete order
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "DELETE", "/v1/decisions/1", emptyBody, PASSWORD)
|
2022-03-17 13:12:13 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
//and now we only get a deleted decision
|
2024-01-04 16:10:36 +00:00
|
|
|
w = lapi.RecordResponse(t, "GET", "/v1/decisions/stream?startup=true", emptyBody, APIKEY)
|
|
|
|
decisions, code = readDecisionsStreamResp(t, w)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, 200, code)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Len(t, decisions["deleted"], 1)
|
2022-06-22 08:29:02 +00:00
|
|
|
assert.Equal(t, int64(1), decisions["deleted"][0].ID)
|
|
|
|
assert.Equal(t, "test", *decisions["deleted"][0].Origin)
|
|
|
|
assert.Equal(t, "127.0.0.1", *decisions["deleted"][0].Value)
|
2023-12-14 13:54:11 +00:00
|
|
|
assert.Empty(t, decisions["new"])
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
2022-03-17 13:12:13 +00:00
|
|
|
|
2022-06-22 08:29:02 +00:00
|
|
|
type DecisionCheck struct {
|
|
|
|
ID int64
|
|
|
|
Origin string
|
|
|
|
Scenario string
|
|
|
|
Value string
|
|
|
|
Duration string
|
|
|
|
Type string
|
2022-03-17 13:12:13 +00:00
|
|
|
}
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2022-06-22 08:29:02 +00:00
|
|
|
type DecisionTest struct {
|
|
|
|
TestName string
|
|
|
|
Method string
|
|
|
|
Route string
|
|
|
|
CheckCodeOnly bool
|
|
|
|
Code int
|
|
|
|
LenNew int
|
|
|
|
LenDeleted int
|
|
|
|
NewChecks []DecisionCheck
|
|
|
|
DelChecks []DecisionCheck
|
|
|
|
AuthType string
|
|
|
|
}
|