Set the project license in config.env; AGPL-3.0 default only for a public GitHub project
- PROJECT_LICENSE (a Gitea license key, or none) is read, checked and never asked - Without it AGPL-3.0 applies only when GitHub is chosen and the project is public - The license is applied on Gitea with or without GitHub, and checked against the server first - Plan and summary name the license and where it came from - MIL-006 accepted; README and config.env.example document the key - Tests: new test-license.sh; existing tests use a public project where they expect AGPL-3.0 Task: MIL-006#1 Task: MIL-006#2 Task: MIL-006#3 Task: MIL-006#4 Refs #44 Refs #45 Refs #46 Refs #50
This commit is contained in:
@@ -21,6 +21,16 @@ readonly FAKE_GITHUB_PAT="ghpFAKEtoken1234567890"
|
||||
# Answers to the prompts: name, description, visibility, Gitea owner, GitHub
|
||||
# yes or no, GitHub owner, directory, plan gate.
|
||||
readonly ANSWERS_GITHUB=$'my-app\nA test app\n\nTirSystem\ny\nacme-org\n\nn\n'
|
||||
# The same, for a public project (the AGPL-3.0 default applies with GitHub).
|
||||
readonly ANSWERS_GITHUB_PUBLIC=$'my-app
|
||||
A test app
|
||||
public
|
||||
TirSystem
|
||||
y
|
||||
acme-org
|
||||
|
||||
n
|
||||
'
|
||||
readonly ANSWERS_GITEA_ONLY=$'my-app\n\n\nTirSystem\nn\n\nn\n'
|
||||
|
||||
SHARED_REMOTES=""
|
||||
|
||||
+22
-8
@@ -31,7 +31,7 @@ test_dry_run_prints_the_plan_and_only_reads() {
|
||||
setup_hosts
|
||||
run_dry "$ANSWERS_GITHUB"
|
||||
assert_status "dry run" 0 "$STATUS"
|
||||
assert_contains "Gitea plan" "$OUT" "Gitea repository : create (private) with the AGPL-3.0 license https://git.example.test/TirSystem/my-app"
|
||||
assert_contains "Gitea plan" "$OUT" "Gitea repository : create (private), empty https://git.example.test/TirSystem/my-app"
|
||||
assert_contains "GitHub plan" "$OUT" "GitHub repository : create (private), empty https://github.com/acme-org/my-app"
|
||||
assert_contains "mirror plan" "$OUT" "Push mirror : Gitea -> GitHub every 10m0s"
|
||||
assert_contains "origin plan" "$OUT" "Local origin : will use SSH (the SSH test passed)"
|
||||
@@ -148,14 +148,18 @@ test_github_owner_must_be_a_member() {
|
||||
assert_contains "pending message" "$ERR" "membership of the GitHub organization 'acme-org' is not active"
|
||||
}
|
||||
|
||||
test_license_must_be_offered_when_github_is_chosen() {
|
||||
test_license_must_be_offered_when_a_public_project_has_github() {
|
||||
setup_hosts
|
||||
prepend_route 'GET|/api/v1/licenses|200|[{"key":"MIT","name":"MIT"}]'
|
||||
run_apply "$ANSWERS_GITHUB"
|
||||
run_apply "$ANSWERS_GITHUB_PUBLIC"
|
||||
assert_status "no AGPL" 1 "$STATUS"
|
||||
assert_contains "message" "$ERR" "does not offer the AGPL-3.0 license"
|
||||
assert_not_contains "nothing created" "$(calls)" "POST"
|
||||
# Without GitHub no license is needed, so the same server is fine.
|
||||
# Without GitHub, or for a private project, no license is needed, so the same server is fine.
|
||||
setup_hosts
|
||||
prepend_route 'GET|/api/v1/licenses|200|[{"key":"MIT","name":"MIT"}]'
|
||||
run_dry "$ANSWERS_GITHUB"
|
||||
assert_status "private with GitHub" 0 "$STATUS"
|
||||
setup_hosts
|
||||
prepend_route 'GET|/api/v1/licenses|200|[{"key":"MIT","name":"MIT"}]'
|
||||
printf 'GITEA_TOKEN=%s\n' "$FAKE_GITEA_TOKEN" >"$WORK/.env"
|
||||
@@ -215,8 +219,7 @@ test_apply_sends_the_right_request_bodies() {
|
||||
bodies="$(cat "$WORK/curl.bodies")"
|
||||
assert_contains "GitHub name" "$bodies" '"name":"my-app"'
|
||||
assert_contains "GitHub is created empty" "$bodies" '"private":true,"auto_init":false}'
|
||||
assert_contains "Gitea gets the license" "$bodies" '"license":"AGPL-3.0"'
|
||||
assert_contains "Gitea is initialised with it" "$bodies" '"auto_init":true'
|
||||
assert_not_contains "a private project gets no license" "$bodies" '"license"'
|
||||
assert_contains "default branch" "$bodies" '"default_branch":"main"'
|
||||
assert_contains "description" "$bodies" '"description":"A test app"'
|
||||
assert_contains "mirror target without credentials" "$bodies" '"remote_address":"https://github.com/acme-org/my-app.git"'
|
||||
@@ -239,6 +242,17 @@ test_apply_never_prints_or_passes_a_token() {
|
||||
assert_contains "GitHub token in the private config" "$(cat "$WORK/curl.config")" "Authorization: Bearer $FAKE_GITHUB_PAT"
|
||||
}
|
||||
|
||||
test_apply_sends_the_license_for_a_public_project_with_github() {
|
||||
setup_hosts
|
||||
run_apply "$ANSWERS_GITHUB_PUBLIC"$'y\n'
|
||||
local bodies
|
||||
bodies="$(cat "$WORK/curl.bodies")"
|
||||
assert_contains "Gitea gets the license" "$bodies" '"license":"AGPL-3.0"'
|
||||
assert_contains "Gitea is initialised with it" "$bodies" '"auto_init":true'
|
||||
assert_contains "public" "$bodies" '"private":false'
|
||||
assert_contains "plan names the rule" "$OUT" "with the AGPL-3.0 license (default: GitHub and a public project)"
|
||||
}
|
||||
|
||||
test_apply_with_gitea_only() {
|
||||
setup_hosts
|
||||
printf 'GITEA_TOKEN=%s\n' "$FAKE_GITEA_TOKEN" >"$WORK/.env"
|
||||
@@ -337,7 +351,7 @@ test_gitea_repository_with_only_the_license_can_be_reused() {
|
||||
setup_hosts
|
||||
prepend_route 'GET|/api/v1/repos/TirSystem/my-app|200|{"empty":false}'
|
||||
prepend_route 'GET|/api/v1/repos/TirSystem/my-app/contents|200|[{"name":"LICENSE","type":"file","path":"LICENSE"}]'
|
||||
run_apply "$ANSWERS_GITHUB"$'y\ny\n'
|
||||
run_apply "$ANSWERS_GITHUB_PUBLIC"$'y\ny\n'
|
||||
assert_status "license only" 0 "$STATUS"
|
||||
assert_not_contains "Gitea repository not created again" "$(calls)" "$GITEA_REPO_CALL"
|
||||
assert_eq "mirror created once" "1" "$(calls | grep -c -x -F "$MIRROR_CALL")"
|
||||
@@ -355,7 +369,7 @@ test_gitea_repository_with_only_the_license_can_be_reused() {
|
||||
test_reusing_an_empty_gitea_repository_warns_about_the_license() {
|
||||
setup_hosts
|
||||
prepend_route 'GET|/api/v1/repos/TirSystem/my-app|200|{"empty":true}'
|
||||
run_apply "$ANSWERS_GITHUB"$'y\ny\n'
|
||||
run_apply "$ANSWERS_GITHUB_PUBLIC"$'y\ny\n'
|
||||
assert_status "reuse empty Gitea repository" 0 "$STATUS"
|
||||
assert_contains "warning" "$ERR" "the AGPL-3.0 license is not added to it"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
#!/usr/bin/env bash
|
||||
# test-license.sh - tests for the project license (MIL-006): PROJECT_LICENSE
|
||||
# in config.env, the default (AGPL-3.0 only for a public project with GitHub),
|
||||
# "none", invalid values and a license the server does not offer. Sourced by
|
||||
# run-tests.sh.
|
||||
|
||||
# shellcheck disable=SC2016 # snippet and fixture text is literal on purpose
|
||||
|
||||
readonly ANSWERS_GITEA_ONLY_PUBLIC=$'my-app\n\npublic\nTirSystem\nn\n\nn\n'
|
||||
readonly MIT_ROUTE='GET|/api/v1/licenses|200|[{"key":"MIT","name":"MIT"},{"key":"AGPL-3.0","name":"AGPL-3.0"}]'
|
||||
|
||||
# use_license LINE: add a line to config.env and make the server offer MIT.
|
||||
use_license() {
|
||||
printf '%s\n' "$1" >>"$WORK/config.env"
|
||||
prepend_route "$MIT_ROUTE"
|
||||
}
|
||||
|
||||
# gitea_only_env: a Gitea-only run needs no GitHub credentials.
|
||||
gitea_only_env() {
|
||||
printf 'GITEA_TOKEN=%s\n' "$FAKE_GITEA_TOKEN" >"$WORK/.env"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------- key is set
|
||||
|
||||
test_a_license_in_config_is_used_with_github_for_a_private_project() {
|
||||
setup_hosts
|
||||
use_license "PROJECT_LICENSE=MIT"
|
||||
run_apply "$ANSWERS_GITHUB"$'y\n'
|
||||
assert_status "apply" 0 "$STATUS"
|
||||
assert_contains "Gitea gets MIT" "$(cat "$WORK/curl.bodies")" '"license":"MIT"'
|
||||
assert_not_contains "no AGPL" "$(cat "$WORK/curl.bodies")" "AGPL"
|
||||
assert_contains "plan" "$OUT" "create (private) with the MIT license (from config.env)"
|
||||
assert_contains "summary" "$OUT" "License : MIT (from config.env)"
|
||||
}
|
||||
|
||||
test_a_license_in_config_is_used_without_github() {
|
||||
setup_hosts
|
||||
gitea_only_env
|
||||
use_license "PROJECT_LICENSE=MIT"
|
||||
run_apply "$ANSWERS_GITEA_ONLY"$'y\n'
|
||||
assert_status "apply" 0 "$STATUS"
|
||||
assert_contains "Gitea gets MIT" "$(cat "$WORK/curl.bodies")" '"license":"MIT"'
|
||||
assert_contains "initialised with it" "$(cat "$WORK/curl.bodies")" '"auto_init":true'
|
||||
assert_not_contains "no GitHub call" "$(calls)" "api.github.com"
|
||||
assert_contains "the server is asked" "$(calls)" "/licenses"
|
||||
}
|
||||
|
||||
test_the_license_key_is_taken_in_any_case_for_none() {
|
||||
setup_hosts
|
||||
use_license "PROJECT_LICENSE=NONE"
|
||||
run_apply "$ANSWERS_GITHUB_PUBLIC"$'y\n'
|
||||
assert_status "apply" 0 "$STATUS"
|
||||
assert_not_contains "no license" "$(cat "$WORK/curl.bodies")" '"license"'
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------- none
|
||||
|
||||
test_none_gives_no_license_even_for_a_public_project_with_github() {
|
||||
setup_hosts
|
||||
use_license "PROJECT_LICENSE=none"
|
||||
run_apply "$ANSWERS_GITHUB_PUBLIC"$'y\n'
|
||||
assert_status "apply" 0 "$STATUS"
|
||||
assert_not_contains "no license" "$(cat "$WORK/curl.bodies")" '"license"'
|
||||
assert_not_contains "no license lookup" "$(calls)" "/licenses"
|
||||
assert_contains "plan" "$OUT" "create (public), empty"
|
||||
assert_contains "summary" "$OUT" "License : none (from config.env)"
|
||||
}
|
||||
|
||||
# ----------------------------------------------------------------- absent
|
||||
|
||||
test_without_the_key_a_public_project_with_github_gets_agpl() {
|
||||
setup_hosts
|
||||
run_dry "$ANSWERS_GITHUB_PUBLIC"
|
||||
assert_status "dry run" 0 "$STATUS"
|
||||
assert_contains "plan" "$OUT" "with the AGPL-3.0 license (default: GitHub and a public project)"
|
||||
assert_contains "summary" "$OUT" "License : AGPL-3.0 (default: GitHub and a public project)"
|
||||
}
|
||||
|
||||
test_without_the_key_a_private_project_with_github_gets_no_license() {
|
||||
setup_hosts
|
||||
run_dry "$ANSWERS_GITHUB"
|
||||
assert_status "dry run" 0 "$STATUS"
|
||||
assert_contains "summary" "$OUT" "License : none (no default for a private project)"
|
||||
assert_not_contains "no license lookup" "$(calls)" "/licenses"
|
||||
}
|
||||
|
||||
test_without_the_key_a_project_without_github_gets_no_license() {
|
||||
setup_hosts
|
||||
gitea_only_env
|
||||
run_dry "$ANSWERS_GITEA_ONLY_PUBLIC"
|
||||
assert_status "dry run" 0 "$STATUS"
|
||||
assert_contains "summary" "$OUT" "License : none"
|
||||
assert_not_contains "no marker" "$OUT" "License : none ("
|
||||
assert_not_contains "no license lookup" "$(calls)" "/licenses"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------- empty and invalid
|
||||
|
||||
test_an_empty_license_stops_before_any_request() {
|
||||
setup_hosts
|
||||
printf 'PROJECT_LICENSE=\n' >>"$WORK/config.env"
|
||||
run_dry "$ANSWERS_GITHUB"
|
||||
assert_status "empty" 1 "$STATUS"
|
||||
assert_contains "key named" "$ERR" "PROJECT_LICENSE in"
|
||||
assert_contains "says empty" "$ERR" "is empty"
|
||||
assert_eq "no request to any host" "" "$(calls)"
|
||||
}
|
||||
|
||||
test_an_invalid_license_stops_before_any_request_and_is_never_asked() {
|
||||
local value
|
||||
for value in 'bad license' 'MIT/2' 'MIT;rm' "$(printf 'a%.0s' {1..65})"; do
|
||||
remove_workdir
|
||||
new_workdir
|
||||
setup_hosts
|
||||
printf 'PROJECT_LICENSE=%s\n' "$value" >>"$WORK/config.env"
|
||||
run_dry "$ANSWERS_GITHUB"
|
||||
assert_status "invalid '$value'" 1 "$STATUS"
|
||||
assert_contains "key named" "$ERR" "PROJECT_LICENSE in"
|
||||
assert_eq "no request to any host" "" "$(calls)"
|
||||
assert_not_contains "never asked" "$ERR" "License ("
|
||||
done
|
||||
}
|
||||
|
||||
# ------------------------------------------------------ not offered
|
||||
|
||||
test_a_license_the_server_does_not_offer_stops_before_anything_is_created() {
|
||||
setup_hosts
|
||||
use_license "PROJECT_LICENSE=Zlib"
|
||||
run_apply "$ANSWERS_GITHUB"$'y\n'
|
||||
assert_status "not offered" 1 "$STATUS"
|
||||
assert_contains "license named" "$ERR" "does not offer the Zlib license"
|
||||
assert_not_contains "nothing created" "$(calls)" "POST"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------- never asked
|
||||
|
||||
test_the_license_is_never_asked() {
|
||||
setup_hosts
|
||||
run_dry "$ANSWERS_GITHUB_PUBLIC"
|
||||
assert_not_contains "no license prompt" "$ERR" "icense ("
|
||||
assert_not_contains "no license prompt, any case" "$ERR" "License:"
|
||||
remove_workdir
|
||||
new_workdir
|
||||
setup_hosts
|
||||
use_license "PROJECT_LICENSE=MIT"
|
||||
run_dry "$ANSWERS_GITHUB"
|
||||
assert_not_contains "no license prompt with the key" "$ERR" "icense ("
|
||||
}
|
||||
|
||||
# ------------------------------------------- mirror and local history
|
||||
|
||||
test_the_license_file_reaches_the_local_project_without_github() {
|
||||
setup_hosts
|
||||
gitea_only_env
|
||||
use_license "PROJECT_LICENSE=MIT"
|
||||
local dir="$WORK/project" answers
|
||||
printf -v answers 'my-app\n\n\nTirSystem\nn\n%s\nn\n' "$dir"
|
||||
run_apply "$answers"$'y\n'
|
||||
assert_status "apply" 0 "$STATUS"
|
||||
assert_eq "the Gitea license commit is the whole history" "1" "$(GIT_CONFIG_GLOBAL="$WORK/gitconfig" GIT_CONFIG_NOSYSTEM=1 git -C "$dir" rev-list --count HEAD)"
|
||||
assert_file_exists "LICENSE from Gitea" "$dir/LICENSE"
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
# LOCAL_ANSWERS (kept in a variable because $(...) would drop the last newline).
|
||||
local_answers() {
|
||||
if [[ $2 == y ]]; then
|
||||
printf -v LOCAL_ANSWERS 'my-app\nA test app\n\nTirSystem\ny\nacme-org\n%s\n%s\n' "$1" "$3"
|
||||
printf -v LOCAL_ANSWERS 'my-app\nA test app\npublic\nTirSystem\ny\nacme-org\n%s\n%s\n' "$1" "$3"
|
||||
else
|
||||
printf -v LOCAL_ANSWERS 'my-app\n\n\nTirSystem\nn\n%s\n%s\n' "$1" "$3"
|
||||
fi
|
||||
|
||||
@@ -205,7 +205,7 @@ test_summary_marks_the_values_from_config_env() {
|
||||
assert_contains "name" "$OUT" "Repository : my-app (from config.env) (private (from config.env))"
|
||||
assert_contains "description" "$OUT" "Description : A test app (from config.env)"
|
||||
assert_contains "Gitea" "$OUT" "/TirSystem/my-app (from config.env)"
|
||||
assert_contains "GitHub" "$OUT" "(AGPL license applied) (from config.env)"
|
||||
assert_contains "GitHub" "$OUT" "/acme-org/my-app (from config.env)"
|
||||
assert_contains "directory" "$OUT" "Directory : ./my-app (from config.env)"
|
||||
assert_contains "plan gate" "$OUT" "Plan gate : no (from config.env)"
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ test_full_run_with_github() {
|
||||
after="$(listing)"
|
||||
assert_status "full run" 0 "$STATUS"
|
||||
assert_contains "dry run" "$OUT" "Dry run: nothing was created"
|
||||
assert_contains "plan" "$OUT" "create (private) with the AGPL-3.0 license"
|
||||
assert_contains "plan" "$OUT" "create (private), empty"
|
||||
assert_contains "Gitea link derived from config" "$OUT" "https://git.example.test/TirSystem/my-app"
|
||||
assert_contains "GitHub link uses the chosen organization" "$OUT" "https://github.com/acme-org/my-app"
|
||||
assert_contains "AGPL noted" "$OUT" "AGPL license applied"
|
||||
assert_contains "no default license for a private project" "$OUT" "License : none (no default for a private project)"
|
||||
assert_contains "credential state" "$OUT" "GITEA_TOKEN set, GITHUB_PAT set"
|
||||
assert_not_contains "no Gitea token in output" "$OUT$ERR" "$FAKE_GITEA_TOKEN"
|
||||
assert_not_contains "no GitHub token in output" "$OUT$ERR" "$FAKE_GITHUB_PAT"
|
||||
|
||||
Reference in New Issue
Block a user