specs/tests/web/gitea.bats

55 lines
1.7 KiB
Plaintext
Raw Normal View History

2020-09-29 10:37:55 +00:00
#! /bin/bash
function setup {
# Load helper functions
load ../helper
2020-09-29 10:37:55 +00:00
# Which forgehook implementation to use?
if [ -z "$FORGEHOOK" ]; then FORGEHOOK="forgehook"; fi
port=$(find_free_port)
[ ! -z "$FORGEHOOKENDPOINT" ]
2022-02-20 16:33:36 +00:00
if [ -z "$WHCK" ]; then export WHCK="whck"; fi
export WHCK_DIR="$(mktemp -d)"
2020-09-29 10:37:55 +00:00
# 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
2022-02-20 16:33:36 +00:00
if [ -d $WHCK_DIR ]; then rm -r $WHCK_DIR; fi
2020-09-29 10:37:55 +00:00
}
2022-02-20 16:33:36 +00:00
@test "gitea: correct signature works" {
2020-09-29 10:37:55 +00:00
repo="https://tildegit.org/forge/hook.sh"
2022-02-20 16:33:36 +00:00
id="$(echo -n "$repo" | base64)"
webhook="$(gen_webhook ../gitea.json "$repo")"
secret="$($FORGEHOOK secret $repo)"
echo -n "$secret" > "$WHCK_DIR"/"$id"
sig="$(hash_hmac sha256 "$webhook" "$secret")"
2020-09-29 10:37:55 +00:00
run send_webhook "${FORGEHOOKSRV}?action=gitea" "$webhook" "$sig" "X-Gitea-Signature"
2022-02-20 16:33:36 +00:00
echo "$output"
2020-09-29 10:37:55 +00:00
[ $status -eq 0 ]
2022-02-20 16:33:36 +00:00
[[ "$output" = "" ]]
2020-09-29 10:37:55 +00:00
}
2022-02-20 16:33:36 +00:00
@test "gitea: incorrect signature fails" {
2020-09-29 10:37:55 +00:00
repo="https://tildegit.org/forge/hook.sh"
2022-02-20 16:33:36 +00:00
id="$(echo -n "$repo" | base64)"
webhook="$(gen_webhook ../gitea.json "$repo")"
2020-09-29 10:37:55 +00:00
# Calculate wrong signature
2022-02-20 16:33:36 +00:00
secret="$($FORGEHOOK secret $repo)"
echo -n "$secret" > "$WHCK_DIR"/"$id"
sig="$(hash_hmac sha256 "EXTRA$webhook" "$secret")"
2020-09-29 10:37:55 +00:00
run send_webhook "${FORGEHOOKSRV}?action=gitea" "$webhook" "$sig" "X-Gitea-Signature"
[ "$status" -eq 2 ]
2022-02-20 16:33:36 +00:00
[[ "$output" != "" ]]
2020-09-29 10:37:55 +00:00
}