Skip to content

Instantly share code, notes, and snippets.

Created August 13, 2020 17:33
What would you like to do?
Mattermost Nextcloud plugin testing script
# Configure shell
set -euo pipefail
# Configuration
readonly SITE_URL='http://localhost:8065'
readonly API_URI='/api/v4'
readonly PLUGIN_URI='/plugins/com.github.salatfreak.mattermost-plugin-nextcloud'
readonly TOKEN='<admin personal access token>'
readonly SECRET='<secret from plugin settings>'
readonly BROWSER='firefox'
# Main function
main() {
# Parse arguments
local args; args="$(getopt -q -o bh -l browser,help -- "$@")" ||
abort "Invalid arguments"
eval set -- "$args"
# Get options and parameters
local use_browser=false
while [[ "$1" != "--" ]]; do
case "$1" in
-b | --browser ) use_browser=true;;
-h | --help ) usage; exit;;
(( $# == 0 )) || abort "Unexpected parameter \"$1\""
# Set user name
local username="<user name>"
# Get user id
local user_id; user_id="$(
request_api '/users/usernames' -d '["'"$username"'"]' | jq -re '.[0]["id"]'
)" || abort "Getting user id failed"
# Create user access token
local user_token; user_token="$(
request_api "/users/$user_id/tokens" \
-d '{"description": "Nextcloud login"}' |
jq -re '.["token"]'
)" || abort "Creating user access token failed"
# Create login token
local login_token; login_token="$(
request_plugin '/login' -X PUT -d '{
"secret": "'"$SECRET"'",
"auth_token": "'"$user_token"'",
"user_id": "'"$user_id"'"
}' | jq -re '.["token"]'
)" || abort "Creating login token failed"
# Request login page or open it in browser
if $use_browser; then
$BROWSER "$SITE_URL$PLUGIN_URI/login/$login_token"
request_plugin "/login/$login_token" -i
# Make HTTP request to mattermost
request() { local uri="$1" params=("${@:2}")
curl -s "$SITE_URL$uri" "${params[@]}"
# Make HTTP request to mattermost API
request_api() { local uri="$1" params=("${@:2}")
request "$API_URI$uri" -H "Authorization: Bearer $TOKEN" "${params[@]}"
# Make HTTP request to plugin
request_plugin() { local uri="$1" params=("${@:2}")
request "$PLUGIN_URI$uri" "${params[@]}"
# Print usage info
usage() {
cat <<END
Usage: [-b] [-h]
-b --browser Open login page in browser
-h --help Show this help and exit
# Abort with error
abort() {
echo "[ERROR] $1" >&2
exit 1
# Call main function
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment