specs/tests/gitlab.bats

45 lines
1.3 KiB
Bash

#! /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 token works" {
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook tests/gitlab.json "$repo")"
run send_webhook "${FORGEHOOKSRV}?action=gitlab" "$webhook" "$($FORGEHOOK secret $repo)" "X-Gitlab-Token"
[ $status -eq 0 ]
[[ "$output" = "200" ]]
}
@test "incorrect token fails" {
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook tests/gitlab.json "$repo")"
# Send FAKE token
run send_webhook "${FORGEHOOKSRV}?action=gitlab" "$webhook" "FAKE" "X-Gitlab-Token"
[ "$status" -eq 2 ]
[[ "$output" = "403" ]]
}