Skip to content

Commit c25891b

Browse files
committed
Add Acceptance QA Data Injectors
1 parent 5b05e7e commit c25891b

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

injectors/data_injector.sh

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/bin/bash
2+
3+
if [ -z "${SERVER_URL}" ]; then
4+
echo "Error: SERVER_URL must not be empty."
5+
exit 1
6+
fi
7+
8+
if [ -z "${COUNT}" ] || [ "${COUNT}" -lt 1 ]; then
9+
echo "Error: COUNT must be greater than 0."
10+
exit 1
11+
fi
12+
13+
if [ ! -z "${COUNT}" ] && [ "${COUNT}" -gt 3000 ]; then
14+
echo "Error: Only 3000 is allowed to create users/spaces."
15+
exit 1
16+
fi
17+
18+
if [ -z "${START_FROM}" ] || [ "${START_FROM}" -lt 0 ]; then
19+
echo "Error: START_FROM must be positive."
20+
exit 1
21+
fi
22+
23+
if [[ ! "${TYPE}" =~ ^(spaces|users)$ ]]; then
24+
echo "Error: TYPE must be spaces or users."
25+
exit 1
26+
fi
27+
28+
if [ -z "${ADMIN_USERNAME}" ]; then
29+
echo "Using \"root\" as default admin username"
30+
fi
31+
32+
if [ -z "${ADMIN_PASSWORD}" ]; then
33+
echo "Using \"PASSWORD\" as default admin password"
34+
fi
35+
36+
if [ -z "${USE_AVATARS}" ]; then
37+
USE_AVATARS="false"
38+
fi
39+
40+
if [ -z "${USE_FORMALNAMES}" ]; then
41+
USE_FORMALNAMES="false"
42+
fi
43+
44+
if [ -z "${USE_TRUEUSERNAMES}" ]; then
45+
USE_TRUEUSERNAMES="false"
46+
fi
47+
48+
if [ -z "${USERS_PASSWORD}" ]; then
49+
USERS_PASSWORD="123456"
50+
fi
51+
52+
set -e
53+
if wget --spider "https://${SERVER_URL}" &>/dev/null; then
54+
baseurl="https://${SERVER_URL}"
55+
else
56+
baseurl="http://${SERVER_URL}"
57+
fi
58+
url="$baseurl/rest/private/v1/social/users"
59+
auth="${ADMIN_USERNAME}:${ADMIN_PASSWORD}"
60+
line='--------------------------------------------------------------------'
61+
if [ "${TYPE}" = "users" ]; then
62+
if [ -z "${PREFIX}" ]; then
63+
PREFIX="user"
64+
fi
65+
maxIndex=$((${COUNT} + ${START_FROM} - 1))
66+
counter=1
67+
userIndex=${START_FROM}
68+
until [ $userIndex -gt $maxIndex ]; do
69+
if ${USE_FORMALNAMES}; then
70+
trycount=1
71+
personJson=""
72+
set +e
73+
while [ -z "$personJson" ] && [ $trycount -le 3 ]; do
74+
personJson=$(wget -qO- https://randomuser.me/api/)
75+
if [ -z "$personJson" ]; then
76+
echo "Warning: Could not get random user details! Retry ($trycount/3)"
77+
fi
78+
((trycount++))
79+
done
80+
set -e
81+
[ -z "$personJson" ] && echo "Error: Failed to get user details from Random User Rest Api" && return
82+
firstname=$(echo $personJson | jq '.results[0].name.first' | tr -d '"')
83+
lastname=$(echo $personJson | jq '.results[0].name.last' | tr -d '"')
84+
echo $firstname | grep -qP "^[a-zA-Z éèçà]+$" || continue
85+
echo $lastname | grep -qP "^[a-zA-Z éèçà]+$" || continue
86+
else
87+
firstname=$(head -c 500 /dev/urandom | tr -dc "a-z" | fold -w 6 | head -n 1)
88+
lastname=$(head -c 500 /dev/urandom | tr -dc "a-z" | fold -w 6 | head -n 1)
89+
fi
90+
data="{\"id\": \"$userIndex\","
91+
username="${PREFIX}$userIndex"
92+
$USE_TRUEUSERNAMES && username="$(echo $firstname.$lastname | sed 's/./\L&/g' | sed -E 's/\s+/./g' | sed -e 's/ç/c/g' -e 's/à/a/g' -e 's/é/e/g' -e 's/è/e/g')"
93+
data+="\"username\": \"$username\","
94+
data+="\"lastname\": \"$lastname\","
95+
data+="\"firstname\": \"$firstname\","
96+
data+="\"fullname\": \"$username\","
97+
data+="\"password\": \"$USERS_PASSWORD\","
98+
data+="\"email\": \"$username@exomail.org\"}"
99+
curlCmd="curl -s -L -w '%{response_code}' -X POST -u "$auth" -H \"Content-Type: application/json\" --data '$data' $url | grep -o '[1-4][0-9][0-9]'"
100+
outputmsg="$(printf '%-6s' $counter/${COUNT}:) ID=\"$username\", Full Name=\"$firstname $lastname\" "
101+
printf "%-80s" "$outputmsg"
102+
httprs=$(eval $curlCmd)
103+
if [[ "$httprs" =~ "200" ]]; then echo "[ OK ]"; else echo "[ Fail ]"; fi
104+
if [[ "$httprs" =~ "200" ]] && ${USE_AVATARS}; then
105+
printf "Avatar..."
106+
uploadId=$(date +"%s")
107+
curl -s -o /tmp/$uploadId.jpg $(echo $personJson | jq '.results[0].picture.large' | tr -d '"')
108+
uploadCMD="curl -s -L -w '%{response_code}' -X POST '$baseurl/portal/upload?uploadId=$uploadId&action=upload' -F upload=@/tmp/$uploadId.jpg | grep -o '[1-4][0-9][0-9]'"
109+
uploadHTTPRS=$(eval $uploadCMD)
110+
if [[ "$uploadHTTPRS" =~ "200" ]]; then
111+
printf "[ Uploaded ]..."
112+
else
113+
echo "[ Fail ]"
114+
continue
115+
fi
116+
updateCMD="curl -s -L -w '%{response_code}' -XPATCH -u '$username:$USERS_PASSWORD' '$baseurl/rest/private/v1/social/users/$username' --data 'name=avatar&value=$uploadId' | grep -o '[1-4][0-9][0-9]'"
117+
updateHTTPRS=$(eval $updateCMD)
118+
if [[ "$updateHTTPRS" =~ "204" ]]; then
119+
echo "[ Updated ]..."
120+
else
121+
echo "[ Fail ]"
122+
fi
123+
set +e
124+
rm /tmp/$uploadId.jpg &>/dev/null
125+
set -e
126+
fi
127+
userIndex=$(($userIndex + 1))
128+
((counter++))
129+
done
130+
elif [ "${TYPE}" = "spaces" ]; then
131+
if [ -z "${PREFIX}" ]; then
132+
PREFIX="space"
133+
fi
134+
maxIndex=$((${COUNT} + ${START_FROM} - 1))
135+
counter=1
136+
spaceIndex=${START_FROM}
137+
url="${baseurl}/rest/private/v1/social/spaces"
138+
until [ $spaceIndex -gt $maxIndex ]; do
139+
displayName=$(head -c 500 /dev/urandom | tr -dc "a-z" | fold -w 6 | head -n 1)
140+
data="{\"displayName\": \"$displayName\","
141+
data+="\"description\": \"${PREFIX}$spaceIndex\","
142+
data+="\"visibility\": \"public\","
143+
data+="\"subscription\": \"open\"}"
144+
curlCmd="curl -s -L -w '%{response_code}' -X POST -u "$auth" -H \"Content-Type: application/json\" --data '$data' $url | grep -o '[1-9][0-9][0-9]'"
145+
httprs=$(eval $curlCmd)
146+
outputmsg="$(printf '%-6s' $counter/${COUNT}:) Display Name=\"$displayName\" "
147+
printf "%-80s" "$outputmsg"
148+
if [[ "$httprs" =~ "200" ]]; then echo "[ OK ]"; else echo "[ Fail ]"; fi
149+
spaceIndex=$(($spaceIndex + 1))
150+
((counter++))
151+
done
152+
fi

0 commit comments

Comments
 (0)