github.com/crowdsecurity/crowdsec@v1.6.1/test/bats/70_plugin_http.bats (about) 1 #!/usr/bin/env bats 2 # vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si: 3 4 set -u 5 6 setup_file() { 7 load "../lib/setup_file.sh" 8 # eval "$(debug)" 9 ./instance-data load 10 11 MOCK_OUT="${LOG_DIR}/mock-http.out" 12 export MOCK_OUT 13 MOCK_PORT="9999" 14 MOCK_URL="http://localhost:${MOCK_PORT}" 15 export MOCK_URL 16 PLUGIN_DIR=$(config_get '.config_paths.plugin_dir') 17 # could have a trailing slash 18 PLUGIN_DIR=$(realpath "${PLUGIN_DIR}") 19 export PLUGIN_DIR 20 21 # https://mikefarah.gitbook.io/yq/operators/env-variable-operators 22 config_set "$(config_get '.config_paths.notification_dir')/http.yaml" ' 23 .url=strenv(MOCK_URL) | 24 .group_wait="5s" | 25 .group_threshold=2 26 ' 27 28 config_set "$(config_get '.api.server.profiles_path')" ' 29 .notifications=["http_default"] | 30 .filters=["Alert.GetScope() == \"Ip\""] 31 ' 32 33 config_set ' 34 .plugin_config.user="" | 35 .plugin_config.group="" 36 ' 37 38 rm -f -- "${MOCK_OUT}" 39 40 ./instance-crowdsec start 41 ./instance-mock-http start "${MOCK_PORT}" 42 } 43 44 teardown_file() { 45 load "../lib/teardown_file.sh" 46 ./instance-crowdsec stop 47 ./instance-mock-http stop 48 } 49 50 setup() { 51 load "../lib/setup.sh" 52 } 53 54 #---------- 55 56 @test "add two bans" { 57 rune -0 cscli decisions add --ip 1.2.3.4 --duration 30s 58 assert_stderr --partial 'Decision successfully added' 59 60 rune -0 cscli decisions add --ip 1.2.3.5 --duration 30s 61 assert_stderr --partial 'Decision successfully added' 62 sleep 5 63 } 64 65 @test "expected 1 log line from http server" { 66 rune -0 wc -l <"${MOCK_OUT}" 67 # wc can pad with spaces on some platforms 68 rune -0 tr -d ' ' < <(output) 69 assert_output 1 70 } 71 72 @test "expected to receive 2 alerts in the request body from plugin" { 73 rune -0 jq -r '.request_body' <"${MOCK_OUT}" 74 rune -0 jq -r 'length' <(output) 75 assert_output 2 76 } 77 78 @test "expected to receive IP 1.2.3.4 as value of first decision" { 79 rune -0 jq -r '.request_body[0].decisions[0].value' <"${MOCK_OUT}" 80 assert_output 1.2.3.4 81 } 82 83 @test "expected to receive IP 1.2.3.5 as value of second decision" { 84 rune -0 jq -r '.request_body[1].decisions[0].value' <"${MOCK_OUT}" 85 assert_output 1.2.3.5 86 }