Skip to content

Commit 8ad4a76

Browse files
author
Gauvain Pocentek
committed
Update testing tools for /session removal
1 parent 5a5cd74 commit 8ad4a76

File tree

4 files changed

+83
-41
lines changed

4 files changed

+83
-41
lines changed

tools/build_test_env.sh

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ testcase() {
9494
OK
9595
}
9696

97+
if [ -z "$NOVENV" ]; then
98+
log "Creating Python virtualenv..."
99+
try "$VENV_CMD" "$VENV"
100+
. "$VENV"/bin/activate || fatal "failed to activate Python virtual environment"
101+
102+
log "Installing dependencies into virtualenv..."
103+
try pip install -rrequirements.txt
104+
105+
log "Installing into virtualenv..."
106+
try pip install -e .
107+
108+
# to run generate_token.py
109+
pip install bs4 lxml
110+
fi
111+
97112
log "Waiting for gitlab to come online... "
98113
I=0
99114
while :; do
@@ -107,23 +122,7 @@ while :; do
107122
done
108123

109124
# Get the token
110-
log "Getting GitLab token..."
111-
I=0
112-
while :; do
113-
sleep 1
114-
TOKEN_JSON=$(
115-
try curl -s http://localhost:8080/api/v3/session \
116-
-X POST \
117-
--data "login=$LOGIN&password=$PASSWORD"
118-
) >/dev/null 2>&1 || true
119-
TOKEN=$(
120-
pecho "${TOKEN_JSON}" |
121-
try python -c \
122-
'import sys, json; print(json.load(sys.stdin)["private_token"])'
123-
) >/dev/null 2>&1 && break
124-
I=$((I+1))
125-
[ "$I" -lt 20 ] || fatal "timed out"
126-
done
125+
TOKEN=$($(dirname $0)/generate_token.py)
127126

128127
cat > $CONFIG << EOF
129128
[global]
@@ -139,18 +138,6 @@ EOF
139138
log "Config file content ($CONFIG):"
140139
log <$CONFIG
141140

142-
if [ -z "$NOVENV" ]; then
143-
log "Creating Python virtualenv..."
144-
try "$VENV_CMD" "$VENV"
145-
. "$VENV"/bin/activate || fatal "failed to activate Python virtual environment"
146-
147-
log "Installing dependencies into virtualenv..."
148-
try pip install -rrequirements.txt
149-
150-
log "Installing into virtualenv..."
151-
try pip install -e .
152-
fi
153-
154141
log "Pausing to give GitLab some time to finish starting up..."
155142
sleep 30
156143

tools/generate_token.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
try:
5+
from urllib.parse import urljoin
6+
except ImportError:
7+
from urlparse import urljoin
8+
9+
from bs4 import BeautifulSoup
10+
import requests
11+
12+
endpoint = "http://localhost:8080"
13+
root_route = urljoin(endpoint, "/")
14+
sign_in_route = urljoin(endpoint, "/users/sign_in")
15+
pat_route = urljoin(endpoint, "/profile/personal_access_tokens")
16+
17+
login = "root"
18+
password = "5iveL!fe"
19+
20+
21+
def find_csrf_token(text):
22+
soup = BeautifulSoup(text, "lxml")
23+
token = soup.find(attrs={"name": "csrf-token"})
24+
param = soup.find(attrs={"name": "csrf-param"})
25+
data = {param.get("content"): token.get("content")}
26+
return data
27+
28+
29+
def obtain_csrf_token():
30+
r = requests.get(root_route)
31+
token = find_csrf_token(r.text)
32+
return token, r.cookies
33+
34+
35+
def sign_in(csrf, cookies):
36+
data = {
37+
"user[login]": login,
38+
"user[password]": password,
39+
}
40+
data.update(csrf)
41+
r = requests.post(sign_in_route, data=data, cookies=cookies)
42+
token = find_csrf_token(r.text)
43+
return token, r.history[0].cookies
44+
45+
46+
def obtain_personal_access_token(name, csrf, cookies):
47+
data = {
48+
"personal_access_token[name]": name,
49+
"personal_access_token[scopes][]": ["api", "sudo"],
50+
}
51+
data.update(csrf)
52+
r = requests.post(pat_route, data=data, cookies=cookies)
53+
soup = BeautifulSoup(r.text, "lxml")
54+
token = soup.find('input', id='created-personal-access-token').get('value')
55+
return token
56+
57+
58+
def main():
59+
csrf1, cookies1 = obtain_csrf_token()
60+
csrf2, cookies2 = sign_in(csrf1, cookies1)
61+
62+
token = obtain_personal_access_token('default', csrf2, cookies2)
63+
print(token)
64+
65+
66+
if __name__ == "__main__":
67+
main()

tools/python_test_v3.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@
2121
"rke9IepE7SPBT41C+YtUX4dfDZDmczM1cE0YL/krdUCfuZHMa4ZS2YyNd6slufc"
2222
"vn bar@foo")
2323

24-
# login/password authentication
25-
gl = gitlab.Gitlab('http://localhost:8080', email=LOGIN, password=PASSWORD)
26-
gl.auth()
27-
token_from_auth = gl.private_token
28-
2924
# token authentication from config file
3025
gl = gitlab.Gitlab.from_config(config_files=['/tmp/python-gitlab.cfg'])
31-
assert(token_from_auth == gl.private_token)
3226
gl.auth()
3327
assert(isinstance(gl.user, gitlab.v3.objects.CurrentUser))
3428

tools/python_test_v4.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,8 @@
5151
-----END PGP PUBLIC KEY BLOCK-----'''
5252

5353

54-
# login/password authentication
55-
gl = gitlab.Gitlab('http://localhost:8080', email=LOGIN, password=PASSWORD)
56-
gl.auth()
57-
token_from_auth = gl.private_token
58-
5954
# token authentication from config file
6055
gl = gitlab.Gitlab.from_config(config_files=['/tmp/python-gitlab.cfg'])
61-
assert(token_from_auth == gl.private_token)
6256
gl.auth()
6357
assert(isinstance(gl.user, gitlab.v4.objects.CurrentUser))
6458

0 commit comments

Comments
 (0)