forgejo-curl/forgejo-curl-test.sh
Twenty Panda b15b880a95
All checks were successful
/ test (map[image:codeberg.org/forgejo-experimental/forgejo version:1.21.0-6-rc2]) (pull_request) Successful in 1m10s
/ test (map[image:codeberg.org/forgejo/forgejo version:1.19]) (pull_request) Successful in 1m9s
/ test (map[image:codeberg.org/forgejo/forgejo version:1.20]) (pull_request) Successful in 1m15s
/ cascade (pull_request) Successful in 11s
introduce DOT_FORGEJO_CURL instead of DOT
to avoid potential variable name conflicts
2024-03-15 11:59:24 +07:00

126 lines
3.4 KiB
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: MIT
set -e
TMPDIR=$(mktemp -d)
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function test_fixture_repository() {
local user="$1" password="$2" host_port="$3" repo="$4"
rm -fr $TMPDIR/fixture
mkdir -p $TMPDIR/fixture
(
cd $TMPDIR/fixture
git init
git checkout -b main
git remote add origin http://${user}:${password}@${host_port}/${user}/${repo}
git config user.email root@example.com
git config user.name username
echo SOMETHING > README
git add README
git commit -m 'initial commit'
git push --set-upstream --force origin main
)
}
function test_usage() {
usage |& grep 'thin curl wrapper'
}
function test_must_fail() {
if "$@" >& $TMPDIR/logs ; then
cat $TMPDIR/logs
return 1
fi
}
function test_logs_grep() {
local pattern="$1"
if ! grep --quiet "$pattern" $TMPDIR/logs ; then
cat $TMPDIR/logs
echo "pattern '$pattern' not found in logs"
return 1
fi
}
function test_login_failure() {
local user="$1" password="$2" token="$3" host_port="$4"
#
# login fails if there are no token or password
#
logout
test_must_fail login "" "" "" "" "http://${host_port}"
test_logs_grep 'permission denied'
#
# login fails if the user:password is invalid
#
logout
test_must_fail login "baduser" "badpassword" "" "" "http://${host_port}"
grep --quiet -i 'username or password is incorrect' $DOT_FORGEJO_CURL/login.html
#
# login fails if the token is invalid
#
logout
test_must_fail login "" "" "badtoken" "" "http://${host_port}"
test_logs_grep 'token is invalid'
}
function test_login_success() {
local user="$1" password="$2" token="$3" host_port="$4"
local repo=testrepo
test_fixture_repository "$user" "$password" "$host_port" "$repo"
#
# login with a token but no user:password
#
logout
login "" "" "${token}" "" "http://${host_port}"
test -f $DOT_FORGEJO_CURL/token
test -f $DOT_FORGEJO_CURL/header-token
! test -f $DOT_FORGEJO_CURL/header-csrf
#
# login with a token in a file and no user:password
#
logout
local tokenpath=$TMPDIR/token
echo -n "${token}" > $tokenpath
login "" "" "@${tokenpath}" "" "http://${host_port}"
test -f $DOT_FORGEJO_CURL/token
test "$(cat $DOT_FORGEJO_CURL/token)" == "${token}"
test -f $DOT_FORGEJO_CURL/header-token
! test -f $DOT_FORGEJO_CURL/header-csrf
#
# login with no token but a user:password obtains a token
#
logout
login "${user}" "${password}" "" "" "http://${host_port}"
test -f $DOT_FORGEJO_CURL/token
test -f $DOT_FORGEJO_CURL/header-token
test -f $DOT_FORGEJO_CURL/header-csrf
local id=$(api_json --data-raw '{"title":"TITLE"}' http://${host_port}/api/v1/repos/${user}/${repo}/issues | jq .id)
local onepixel="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
echo -n $onepixel | base64 --decode > $TMPDIR/attachment.png
api --form name=attachment.png --form attachment=@$TMPDIR/attachment.png http://${host_port}/api/v1/repos/${user}/${repo}/issues/$id/assets
}
function main_test() {
debug
test_usage
test_login_success "$@"
test_login_failure "$@"
}
MAIN=main_test
EXIT_ON_ERROR=false
DOT_FORGEJO_CURL=$TMPDIR/.forgejo-curl
source $SELF_DIR/forgejo-curl.sh "$@"