#!/usr/bin/env bash # Copyright © 2023 OpenIM. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # OpenIM RPC Service Test Control Script # # This control script is designed to conduct various tests on the OpenIM RPC services. # It includes functions to perform smoke tests, API tests, and comprehensive service tests. # The script is intended to be used in a Linux environment with appropriate permissions and # environmental variables set. # # It provides robust error handling and logging to facilitate debugging and service monitoring. # Functions within the script can be called directly or passed as arguments to perform # systematic testing, ensuring the integrity of the RPC services. # # Test Functions: # - openim::test::smoke: Runs basic tests to ensure the fundamental functionality of the service. # - openim::test::api: Executes a series of API tests covering authentication, user, friend, # group, and message functionalities. # - openim::test::test: Performs a complete test suite, invoking utility checks and all defined # test cases, and reports on their success. # # The root of the build/dist directory OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/../.. [[ -z ${COMMON_SOURCED} ]] && source ${OPENIM_ROOT}/scripts/install/common.sh # API Server API Address:Port INSECURE_OPENIMAPI="http://${OPENIM_API_HOST}:${API_OPENIM_PORT}" INSECURE_OPENIMAUTO=${OPENIM_RPC_AUTH_HOST}:${OPENIM_AUTH_PORT} CCURL="curl -f -s -XPOST" # Create UCURL="curl -f -s -XPUT" # Update RCURL="curl -f -s -XGET" # Retrieve DCURL="curl -f -s -XDELETE" # Delete openim::test::check_error() { local response=$1 local err_code=$(echo "$response" | jq '.errCode') openim::log::status "Response from user registration: $response" if [[ "$err_code" != "0" ]]; then openim::log::error_exit "Error occurred: $response, You can read the error code in the API documentation https://docs.openim.io/restapi/errcode" else openim::log::success "Operation was successful." fi } # The `openim::test::auth` function serves as a test suite for authentication-related operations. function openim::test::auth() { # 1. Retrieve and set the authentication token. openim::test::get_token # 2. Force logout the test user from a specific platform. openim::test::force_logout # Log the completion of the auth test suite. openim::log::success "Auth test suite completed successfully." } #################################### Auth Module #################################### # Define a function to get a token for a specific user openim::test::get_token() { local user_id="${1:-imAdmin}" # Default user ID if not provided token_response=$( ${CCURL} "${OperationID}" "${Header}" ${INSECURE_OPENIMAPI}/auth/user_token \ -d'{"secret": "'"$SECRET"'","platformID": 1,"userID": "'$user_id'"}' ) token=$(echo $token_response | grep -Po 'token[" :]+\K[^"]+') echo "$token" } Header="-HContent-Type: application/json" OperationID="-HoperationID: 1646445464564" Token="-Htoken: $(openim::test::get_token)" # Forces a user to log out from the specified platform by user ID. openim::test::force_logout() { local request_body=$( cat <