From c29c8b816e41ed8e1da598ff09bceb5bb2e4b024 Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Thu, 27 Aug 2020 23:23:38 +0200 Subject: [PATCH 1/2] Add Acceptance QA Data Injectors --- injectors/data_injector.sh | 152 +++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100755 injectors/data_injector.sh diff --git a/injectors/data_injector.sh b/injectors/data_injector.sh new file mode 100755 index 00000000..7b191be5 --- /dev/null +++ b/injectors/data_injector.sh @@ -0,0 +1,152 @@ +#!/bin/bash + +if [ -z "${SERVER_URL}" ]; then + echo "Error: SERVER_URL must not be empty." + exit 1 +fi + +if [ -z "${COUNT}" ] || [ "${COUNT}" -lt 1 ]; then + echo "Error: COUNT must be greater than 0." + exit 1 +fi + +if [ ! -z "${COUNT}" ] && [ "${COUNT}" -gt 3000 ]; then + echo "Error: Only 3000 is allowed to create users/spaces." + exit 1 +fi + +if [ -z "${START_FROM}" ] || [ "${START_FROM}" -lt 0 ]; then + echo "Error: START_FROM must be positive." + exit 1 +fi + +if [[ ! "${TYPE}" =~ ^(spaces|users)$ ]]; then + echo "Error: TYPE must be spaces or users." + exit 1 +fi + +if [ -z "${ADMIN_USERNAME}" ]; then + echo "Using \"root\" as default admin username" +fi + +if [ -z "${ADMIN_PASSWORD}" ]; then + echo "Using \"PASSWORD\" as default admin password" +fi + +if [ -z "${USE_AVATARS}" ]; then + USE_AVATARS="false" +fi + +if [ -z "${USE_FORMALNAMES}" ]; then + USE_FORMALNAMES="false" +fi + +if [ -z "${USE_TRUEUSERNAMES}" ]; then + USE_TRUEUSERNAMES="false" +fi + +if [ -z "${USERS_PASSWORD}" ]; then + USERS_PASSWORD="123456" +fi + +set -e +if wget --spider "https://${SERVER_URL}" &>/dev/null; then + baseurl="https://${SERVER_URL}" +else + baseurl="http://${SERVER_URL}" +fi +url="$baseurl/rest/private/v1/social/users" +auth="${ADMIN_USERNAME}:${ADMIN_PASSWORD}" +line='--------------------------------------------------------------------' +if [ "${TYPE}" = "users" ]; then + if [ -z "${PREFIX}" ]; then + PREFIX="user" + fi + maxIndex=$((${COUNT} + ${START_FROM} - 1)) + counter=1 + userIndex=${START_FROM} + until [ $userIndex -gt $maxIndex ]; do + if ${USE_FORMALNAMES}; then + trycount=1 + personJson="" + set +e + while [ -z "$personJson" ] && [ $trycount -le 3 ]; do + personJson=$(wget -qO- https://randomuser.me/api/) + if [ -z "$personJson" ]; then + echo "Warning: Could not get random user details! Retry ($trycount/3)" + fi + ((trycount++)) + done + set -e + [ -z "$personJson" ] && echo "Error: Failed to get user details from Random User Rest Api" && return + firstname=$(echo $personJson | jq '.results[0].name.first' | tr -d '"') + lastname=$(echo $personJson | jq '.results[0].name.last' | tr -d '"') + echo $firstname | grep -qP "^[a-zA-Z éèçà]+$" || continue + echo $lastname | grep -qP "^[a-zA-Z éèçà]+$" || continue + else + firstname=$(head -c 500 /dev/urandom | tr -dc "a-z" | fold -w 6 | head -n 1) + lastname=$(head -c 500 /dev/urandom | tr -dc "a-z" | fold -w 6 | head -n 1) + fi + data="{\"id\": \"$userIndex\"," + username="${PREFIX}$userIndex" + $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')" + data+="\"username\": \"$username\"," + data+="\"lastname\": \"$lastname\"," + data+="\"firstname\": \"$firstname\"," + data+="\"fullname\": \"$username\"," + data+="\"password\": \"$USERS_PASSWORD\"," + data+="\"email\": \"$username@exomail.org\"}" + 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]'" + outputmsg="$(printf '%-6s' $counter/${COUNT}:) ID=\"$username\", Full Name=\"$firstname $lastname\" " + printf "%-80s" "$outputmsg" + httprs=$(eval $curlCmd) + if [[ "$httprs" =~ "200" ]]; then echo "[ OK ]"; else echo "[ Fail ]"; fi + if [[ "$httprs" =~ "200" ]] && ${USE_AVATARS}; then + printf "Avatar..." + uploadId=$(date +"%s") + curl -s -o /tmp/$uploadId.jpg $(echo $personJson | jq '.results[0].picture.large' | tr -d '"') + 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]'" + uploadHTTPRS=$(eval $uploadCMD) + if [[ "$uploadHTTPRS" =~ "200" ]]; then + printf "[ Uploaded ]..." + else + echo "[ Fail ]" + continue + fi + 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]'" + updateHTTPRS=$(eval $updateCMD) + if [[ "$updateHTTPRS" =~ "204" ]]; then + echo "[ Updated ]..." + else + echo "[ Fail ]" + fi + set +e + rm /tmp/$uploadId.jpg &>/dev/null + set -e + fi + userIndex=$(($userIndex + 1)) + ((counter++)) + done +elif [ "${TYPE}" = "spaces" ]; then + if [ -z "${PREFIX}" ]; then + PREFIX="space" + fi + maxIndex=$((${COUNT} + ${START_FROM} - 1)) + counter=1 + spaceIndex=${START_FROM} + url="${baseurl}/rest/private/v1/social/spaces" + until [ $spaceIndex -gt $maxIndex ]; do + displayName=$(head -c 500 /dev/urandom | tr -dc "a-z" | fold -w 6 | head -n 1) + data="{\"displayName\": \"$displayName\"," + data+="\"description\": \"${PREFIX}$spaceIndex\"," + data+="\"visibility\": \"public\"," + data+="\"subscription\": \"open\"}" + 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]'" + httprs=$(eval $curlCmd) + outputmsg="$(printf '%-6s' $counter/${COUNT}:) Display Name=\"$displayName\" " + printf "%-80s" "$outputmsg" + if [[ "$httprs" =~ "200" ]]; then echo "[ OK ]"; else echo "[ Fail ]"; fi + spaceIndex=$(($spaceIndex + 1)) + ((counter++)) + done +fi From 4fb5eff31ba6e00de23b9f63789e5a1489e4b9fa Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Sun, 7 Feb 2021 20:08:21 +0100 Subject: [PATCH 2/2] Add Banner, city, country extra infor --- injectors/data_injector.sh | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/injectors/data_injector.sh b/injectors/data_injector.sh index 7b191be5..012f1583 100755 --- a/injectors/data_injector.sh +++ b/injectors/data_injector.sh @@ -37,6 +37,10 @@ if [ -z "${USE_AVATARS}" ]; then USE_AVATARS="false" fi +if [ -z "${USE_BANNERS}" ]; then + USE_BANNERS="false" +fi + if [ -z "${USE_FORMALNAMES}" ]; then USE_FORMALNAMES="false" fi @@ -55,6 +59,7 @@ if wget --spider "https://${SERVER_URL}" &>/dev/null; then else baseurl="http://${SERVER_URL}" fi +jobs=($(curl -s https://gist.githubusercontent.com/wsc/1083459/raw/d8d0aa8737a36912e6c119a172c8367276b76260/gistfile1.txt | sed "s/ /%20/g" | tr "\n" " ")) url="$baseurl/rest/private/v1/social/users" auth="${ADMIN_USERNAME}:${ADMIN_PASSWORD}" line='--------------------------------------------------------------------' @@ -101,6 +106,15 @@ if [ "${TYPE}" = "users" ]; then printf "%-80s" "$outputmsg" httprs=$(eval $curlCmd) if [[ "$httprs" =~ "200" ]]; then echo "[ OK ]"; else echo "[ Fail ]"; fi + if [ ! -z "$personJson" ]; then + # Extra Info + country=$(echo $personJson | jq '.results[0].location.country' | tr -d '"' | sed "s/ /%20/g") + curl -s -XPATCH -u "$username:$USERS_PASSWORD" "$baseurl/rest/private/v1/social/users/$username" --data "name=country&value=$country" + city=$(echo $personJson | jq '.results[0].location.city' | tr -d '"' | sed "s/ /%20/g") + curl -s -XPATCH -u "$username:$USERS_PASSWORD" "$baseurl/rest/private/v1/social/users/$username" --data "name=city&value=$city" + rand=$(($RANDOM % ${#jobs[@]})) + curl -s -XPATCH -u "$username:$USERS_PASSWORD" "$baseurl/rest/private/v1/social/users/$username" --data "name=position&value=${jobs[$rand]}" + fi if [[ "$httprs" =~ "200" ]] && ${USE_AVATARS}; then printf "Avatar..." uploadId=$(date +"%s") @@ -124,6 +138,38 @@ if [ "${TYPE}" = "users" ]; then rm /tmp/$uploadId.jpg &>/dev/null set -e fi + if [[ "$httprs" =~ "200" ]] && ${USE_BANNERS}; then + printf "Banner..." + uploadId=$(date +"%s") + categories=("architecture" "network" "nature" "minimal" "sea" "sky" "city" "flower" "butterfly" "building" "buisness" "rail" "rain" "colors" "paint" "happiness" "work" "mojave" "montain" "camping") + rand=$(($RANDOM % ${#categories[@]})) + bannerLinks=($(curl -s -H "Authorization: Client-ID ${UNSPLASH_TOKEN:-}" "https://api.unsplash.com/search/photos?page=${RANDOM:0:2}&query=${categories[$rand]}" | jq '.results[].urls.regular' | tr -d '"')) + if [ -z "${bannerLinks}" ]; then + echo "[ Fail ]" + echo "[ERROR] Banners Gathering api rate limit may be reached. Please try again within an hour." + continue + fi + rand=$(($RANDOM % ${#bannerLinks[@]})) + curl -s -o /tmp/$uploadId.jpg "${bannerLinks[$rand]}" + uploadCMD="curl -s -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]'" + uploadHTTPRS=$(eval $uploadCMD) + if [[ "$uploadHTTPRS" =~ "200" ]]; then + printf "[ Uploaded ]..." + else + echo "[ Fail ]" + continue + fi + updateCMD="curl -s -w '%{response_code}' -XPATCH -u '$username:$USERS_PASSWORD' '$baseurl/rest/private/v1/social/users/$username' --data 'name=banner&value=$uploadId' | grep -o '[1-4][0-9][0-9]'" + updateHTTPRS=$(eval $updateCMD) + if [[ "$updateHTTPRS" =~ "204" ]]; then + echo "[ Updated ]..." + else + echo "[ Fail ]" + fi + set +e + rm /tmp/$uploadId.jpg &>/dev/null + set -e + fi userIndex=$(($userIndex + 1)) ((counter++)) done