diff options
-rwxr-xr-x | tools/build_test_env.sh | 45 | ||||
-rwxr-xr-x | tools/generate_token.py | 67 | ||||
-rw-r--r-- | tools/python_test_v3.py | 6 | ||||
-rw-r--r-- | tools/python_test_v4.py | 6 |
4 files changed, 83 insertions, 41 deletions
diff --git a/tools/build_test_env.sh b/tools/build_test_env.sh index 31651b3..7e149f6 100755 --- a/tools/build_test_env.sh +++ b/tools/build_test_env.sh @@ -94,6 +94,21 @@ testcase() { OK } +if [ -z "$NOVENV" ]; then + log "Creating Python virtualenv..." + try "$VENV_CMD" "$VENV" + . "$VENV"/bin/activate || fatal "failed to activate Python virtual environment" + + log "Installing dependencies into virtualenv..." + try pip install -rrequirements.txt + + log "Installing into virtualenv..." + try pip install -e . + + # to run generate_token.py + pip install bs4 lxml +fi + log "Waiting for gitlab to come online... " I=0 while :; do @@ -107,23 +122,7 @@ while :; do done # Get the token -log "Getting GitLab token..." -I=0 -while :; do - sleep 1 - TOKEN_JSON=$( - try curl -s http://localhost:8080/api/v3/session \ - -X POST \ - --data "login=$LOGIN&password=$PASSWORD" - ) >/dev/null 2>&1 || true - TOKEN=$( - pecho "${TOKEN_JSON}" | - try python -c \ - 'import sys, json; print(json.load(sys.stdin)["private_token"])' - ) >/dev/null 2>&1 && break - I=$((I+1)) - [ "$I" -lt 20 ] || fatal "timed out" -done +TOKEN=$($(dirname $0)/generate_token.py) cat > $CONFIG << EOF [global] @@ -139,18 +138,6 @@ EOF log "Config file content ($CONFIG):" log <$CONFIG -if [ -z "$NOVENV" ]; then - log "Creating Python virtualenv..." - try "$VENV_CMD" "$VENV" - . "$VENV"/bin/activate || fatal "failed to activate Python virtual environment" - - log "Installing dependencies into virtualenv..." - try pip install -rrequirements.txt - - log "Installing into virtualenv..." - try pip install -e . -fi - log "Pausing to give GitLab some time to finish starting up..." sleep 30 diff --git a/tools/generate_token.py b/tools/generate_token.py new file mode 100755 index 0000000..ab14188 --- /dev/null +++ b/tools/generate_token.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +import sys +try: + from urllib.parse import urljoin +except ImportError: + from urlparse import urljoin + +from bs4 import BeautifulSoup +import requests + +endpoint = "http://localhost:8080" +root_route = urljoin(endpoint, "/") +sign_in_route = urljoin(endpoint, "/users/sign_in") +pat_route = urljoin(endpoint, "/profile/personal_access_tokens") + +login = "root" +password = "5iveL!fe" + + +def find_csrf_token(text): + soup = BeautifulSoup(text, "lxml") + token = soup.find(attrs={"name": "csrf-token"}) + param = soup.find(attrs={"name": "csrf-param"}) + data = {param.get("content"): token.get("content")} + return data + + +def obtain_csrf_token(): + r = requests.get(root_route) + token = find_csrf_token(r.text) + return token, r.cookies + + +def sign_in(csrf, cookies): + data = { + "user[login]": login, + "user[password]": password, + } + data.update(csrf) + r = requests.post(sign_in_route, data=data, cookies=cookies) + token = find_csrf_token(r.text) + return token, r.history[0].cookies + + +def obtain_personal_access_token(name, csrf, cookies): + data = { + "personal_access_token[name]": name, + "personal_access_token[scopes][]": ["api", "sudo"], + } + data.update(csrf) + r = requests.post(pat_route, data=data, cookies=cookies) + soup = BeautifulSoup(r.text, "lxml") + token = soup.find('input', id='created-personal-access-token').get('value') + return token + + +def main(): + csrf1, cookies1 = obtain_csrf_token() + csrf2, cookies2 = sign_in(csrf1, cookies1) + + token = obtain_personal_access_token('default', csrf2, cookies2) + print(token) + + +if __name__ == "__main__": + main() diff --git a/tools/python_test_v3.py b/tools/python_test_v3.py index 00faccc..a05e6a4 100644 --- a/tools/python_test_v3.py +++ b/tools/python_test_v3.py @@ -21,14 +21,8 @@ DEPLOY_KEY = ("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFdRyjJQh+1niBpXqE2I8dzjG" "rke9IepE7SPBT41C+YtUX4dfDZDmczM1cE0YL/krdUCfuZHMa4ZS2YyNd6slufc" "vn bar@foo") -# login/password authentication -gl = gitlab.Gitlab('http://localhost:8080', email=LOGIN, password=PASSWORD) -gl.auth() -token_from_auth = gl.private_token - # token authentication from config file gl = gitlab.Gitlab.from_config(config_files=['/tmp/python-gitlab.cfg']) -assert(token_from_auth == gl.private_token) gl.auth() assert(isinstance(gl.user, gitlab.v3.objects.CurrentUser)) diff --git a/tools/python_test_v4.py b/tools/python_test_v4.py index ce3c796..a306f48 100644 --- a/tools/python_test_v4.py +++ b/tools/python_test_v4.py @@ -51,14 +51,8 @@ qG2ZdhHHmSK2LaQLFiSprUkikStNU9BqSQ== -----END PGP PUBLIC KEY BLOCK-----''' -# login/password authentication -gl = gitlab.Gitlab('http://localhost:8080', email=LOGIN, password=PASSWORD) -gl.auth() -token_from_auth = gl.private_token - # token authentication from config file gl = gitlab.Gitlab.from_config(config_files=['/tmp/python-gitlab.cfg']) -assert(token_from_auth == gl.private_token) gl.auth() assert(isinstance(gl.user, gitlab.v4.objects.CurrentUser)) |