#! /bin/bash function setup { # Load helper functions load helper # Which forgehook implementation to use? if [ -z "$FORGEHOOK" ]; then FORGEHOOK="forgehook"; fi port=$(find_free_port) [ ! -z "$FORGEHOOKENDPOINT" ] TMPFILE=$(mktemp) # Need 3>&- so bats doesn't hang because of background task $FORGEHOOKENDPOINT $port 3>&- & export FORGEHOOKPID="$!" [[ $? = 0 ]] export FORGEHOOKSRV="http://localhost:$port" } function teardown { # If setup fails, $FORGEHOOKPID will be empty so nothing to clean if [ ! -z "$FORGEHOOKPID" ]; then # Also kill the PID's children processes kill $(ps -o pid= --ppid $FORGEHOOKPID) fi if [ -f $TMPFILE ]; then rm $TMPFILE; fi } @test "correct signature works" { repo="https://tildegit.org/forge/hook.sh" webhook="$(gen_webhook tests/gitea.json "$repo")" sig="$(hash_hmac sha256 "$webhook" "$($FORGEHOOK secret $repo)")" run send_webhook "${FORGEHOOKSRV}?action=gitea" "$webhook" "$sig" "X-Gitea-Signature" [ $status -eq 0 ] [[ "$output" -eq "200" ]] } @test "incorrect signature fails" { repo="https://tildegit.org/forge/hook.sh" webhook="$(gen_webhook tests/gitea.json "$repo")" # Calculate wrong signature sig="$(hash_hmac sha256 "EXTRA$webhook" "$($FORGEHOOK secret $repo)")" run send_webhook "${FORGEHOOKSRV}?action=gitea" "$webhook" "$sig" "X-Gitea-Signature" [ "$status" -eq 2 ] [[ "$output" = "403" ]] }