github.com/crowdsecurity/crowdsec@v1.6.1/test/bats/73_plugin_formatting.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      is_package_testing && return
     9  
    10      ./instance-data load
    11  
    12      tempfile=$(TMPDIR="${BATS_FILE_TMPDIR}" mktemp)
    13      export tempfile
    14  
    15      DUMMY_YAML="$(config_get '.config_paths.notification_dir')/dummy.yaml"
    16  
    17      # we test the template that is suggested in the email notification
    18      # the $alert is not a shell variable
    19      # shellcheck disable=SC2016
    20      config_set "${DUMMY_YAML}" '
    21         .group_wait="5s" |
    22         .group_threshold=2 |
    23         .output_file=strenv(tempfile) |
    24         .format="<html><body> {{range . -}} {{$alert := . -}} {{range .Decisions -}} <p><a href=\"https://www.whois.com/whois/{{.Value}}\">{{.Value}}</a> will get <b>{{.Type}}</b> for next <b>{{.Duration}}</b> for triggering <b>{{.Scenario}}</b> on machine <b>{{$alert.MachineID}}</b>.</p> <p><a href=\"https://app.crowdsec.net/cti/{{.Value}}\">CrowdSec CTI</a></p> {{end -}} {{end -}} </body></html>"
    25      '
    26  
    27      config_set "$(config_get '.api.server.profiles_path')" '
    28         .notifications=["dummy_default"] |
    29         .filters=["Alert.GetScope() == \"Ip\""]
    30      '
    31  
    32      config_set '
    33         .plugin_config.user="" |
    34         .plugin_config.group=""
    35      '
    36  
    37      ./instance-crowdsec start
    38  }
    39  
    40  teardown_file() {
    41      load "../lib/teardown_file.sh"
    42  }
    43  
    44  setup() {
    45      is_package_testing && skip
    46      load "../lib/setup.sh"
    47  }
    48  
    49  #----------
    50  
    51  @test "add two bans" {
    52      rune -0 cscli decisions add --ip 1.2.3.4 --duration 30s
    53      assert_stderr --partial 'Decision successfully added'
    54  
    55      rune -0 cscli decisions add --ip 1.2.3.5 --duration 30s
    56      assert_stderr --partial 'Decision successfully added'
    57      sleep 2
    58  }
    59  
    60  @test "expected 1 notification" {
    61      rune -0 cat "${tempfile}"
    62      assert_output - <<-EOT
    63  	<html><body> <p><a href="https://www.whois.com/whois/1.2.3.4">1.2.3.4</a> will get <b>ban</b> for next <b>30s</b> for triggering <b>manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX'</b> on machine <b>githubciXXXXXXXXXXXXXXXXXXXXXXXX</b>.</p> <p><a href="https://app.crowdsec.net/cti/1.2.3.4">CrowdSec CTI</a></p> <p><a href="https://www.whois.com/whois/1.2.3.5">1.2.3.5</a> will get <b>ban</b> for next <b>30s</b> for triggering <b>manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX'</b> on machine <b>githubciXXXXXXXXXXXXXXXXXXXXXXXX</b>.</p> <p><a href="https://app.crowdsec.net/cti/1.2.3.5">CrowdSec CTI</a></p> </body></html>
    64  	EOT
    65  }