├── .coveragerc ├── .deepsource.toml ├── .editorconfig ├── .github ├── actions │ └── test_rocketchat │ │ └── action.yml ├── dependabot.yml └── workflows │ ├── lint.yml │ ├── release.yml │ ├── test.yml │ ├── test_latest.yml │ └── test_manually.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.txt ├── README.md ├── bandit.yml ├── docker-compose.yml ├── qodana.yaml ├── requirements.txt ├── rocketchat_API ├── APIExceptions │ ├── RocketExceptions.py │ └── __init__.py ├── APISections │ ├── __init__.py │ ├── assets.py │ ├── banners.py │ ├── base.py │ ├── channels.py │ ├── chat.py │ ├── groups.py │ ├── im.py │ ├── integrations.py │ ├── invites.py │ ├── licenses.py │ ├── livechat.py │ ├── miscellaneous.py │ ├── permissions.py │ ├── roles.py │ ├── rooms.py │ ├── settings.py │ ├── statistics.py │ ├── subscriptions.py │ ├── teams.py │ ├── users.py │ └── video_conferences.py ├── __init__.py └── rocketchat.py ├── setup.cfg ├── setup.py └── tests ├── __init__.py ├── assets ├── avatar.png ├── avatar.svg └── logo.png ├── conftest.py ├── test_assets.py ├── test_banners.py ├── test_channels.py ├── test_chat.py ├── test_groups.py ├── test_ims.py ├── test_integrations.py ├── test_invites.py ├── test_licenses.py ├── test_livechat.py ├── test_permissions.py ├── test_roles.py ├── test_rooms.py ├── test_server.py ├── test_settings.py ├── test_subscriptions.py ├── test_teams.py ├── test_user.py └── test_video_conferences.py /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | branch = True 3 | omit= 4 | tests/tests.py 5 | setup.py 6 | 7 | [report] 8 | exclude_lines = 9 | pragma: no cover 10 | 11 | def __repr__ 12 | if self\.debug 13 | 14 | raise AssertionError 15 | raise NotImplementedError 16 | 17 | if 0: 18 | if __name__ == .__main__.: 19 | -------------------------------------------------------------------------------- /.deepsource.toml: -------------------------------------------------------------------------------- 1 | version = 1 2 | 3 | test_patterns = [ 4 | "tests/**" 5 | ] 6 | 7 | exclude_patterns = [ 8 | 9 | ] 10 | 11 | [[analyzers]] 12 | name = 'python' 13 | enabled = true 14 | runtime_version = '3.x.x' 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.yml] 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/actions/test_rocketchat/action.yml: -------------------------------------------------------------------------------- 1 | name: Test rocket.chat server 2 | description: 'Run tests on the Rocket.Chat server' 3 | 4 | inputs: 5 | python-version: 6 | description: 'The version of Python to use' 7 | required: true 8 | rocket-chat-version: 9 | description: 'The version of Rocket.Chat to use' 10 | required: true 11 | rocket-chat-license: 12 | description: 'The license key for Rocket.Chat' 13 | required: true 14 | codecov-token: 15 | description: 'The token for Codecov' 16 | required: false 17 | 18 | runs: 19 | using: composite 20 | steps: 21 | - name: Start rocket.chat server 22 | uses: isbang/compose-action@v2.2.0 23 | with: 24 | compose-file: docker-compose.yml 25 | env: 26 | ROCKETCHAT_LICENSE: ${{ inputs.rocket-chat-license }} 27 | ROCKET_CHAT_VERSION: ${{ inputs.rocket-chat-version }} 28 | - name: Set up Python 29 | uses: actions/setup-python@v5 30 | with: 31 | python-version: ${{ inputs.python-version }} 32 | - name: Install dependencies 33 | shell: bash 34 | run: pip install pytest-cov requests semver 35 | - name: Wait for rocket.chat server to be online 36 | shell: bash 37 | run: until curl --silent http://localhost:3000/api/info/; do sleep 15; echo "waiting for Rocket.Chat server to start"; done; 38 | - name: Run tests 39 | shell: bash 40 | run: pytest tests rocketchat_API --cov-report xml --cov=./ 41 | env: 42 | PYTEST_ADDOPTS: "--color=yes" 43 | - name: Upload code coverage 44 | if: ${{ inputs.codecov-token }} 45 | uses: codecov/codecov-action@v5 46 | with: 47 | verbose: true 48 | files: coverage.xml 49 | fail_ci_if_error: true 50 | env: 51 | CODECOV_TOKEN: ${{ inputs.codecov-token }} 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "pip" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | 3 | on: 4 | pull_request: 5 | branches: [ master ] 6 | 7 | jobs: 8 | lint: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v4.2.2 13 | with: 14 | fetch-depth: 0 15 | - name: Set up Python 16 | uses: actions/setup-python@v5 17 | with: 18 | python-version: "3.13" 19 | - name: Install black 20 | run: pip install black 21 | - name: Lint with black 22 | run: black --check --diff . 23 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | release: 5 | types: [ created ] 6 | 7 | jobs: 8 | publish: 9 | runs-on: ubuntu-latest 10 | environment: 11 | name: pypi 12 | url: https://pypi.org/p/rocketchat-API 13 | permissions: 14 | id-token: write 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v4.2.2 18 | - name: Set up Python 3.12 19 | uses: actions/setup-python@v5 20 | with: 21 | python-version: '3.12' 22 | - name: Install dependencies 23 | run: | 24 | python -m pip install --upgrade pip 25 | pip install setuptools wheel 26 | - name: Build 27 | run: | 28 | python setup.py sdist bdist_wheel 29 | 30 | - name: Publish package distributions to PyPI 31 | uses: pypa/gh-action-pypi-publish@release/v1 32 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | python-version: [ '3.9', '3.10', '3.11', '3.12', '3.13', ] 16 | rocket-chat-version: [ '7.1.5', '7.2.5', '7.3.4', '7.5.1', '7.6.2', '7.7.0' ] 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v4.2.2 20 | - name: Test 21 | uses: ./.github/actions/test_rocketchat 22 | with: 23 | python-version: ${{ matrix.python-version }} 24 | rocket-chat-version: ${{ matrix.rocket-chat-version }} 25 | rocket-chat-license: ${{ secrets.ROCKETCHAT_LICENSE }} 26 | codecov-token: ${{ secrets.CODECOV_TOKEN }} 27 | -------------------------------------------------------------------------------- /.github/workflows/test_latest.yml: -------------------------------------------------------------------------------- 1 | name: Periodically test against latest Rocket.Chat server 2 | 3 | on: 4 | schedule: 5 | - cron: '0 20 * * 5' 6 | 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v4.2.2 13 | - name: Test 14 | uses: ./.github/actions/test_rocketchat 15 | with: 16 | python-version: '3.13' 17 | rocket-chat-version: 'latest' 18 | rocket-chat-license: ${{ secrets.ROCKETCHAT_LICENSE }} 19 | -------------------------------------------------------------------------------- /.github/workflows/test_manually.yml: -------------------------------------------------------------------------------- 1 | name: Trigger test against a specific version 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | rocket-chat-version: 7 | description: 'The version of Rocket.Chat to use' 8 | required: true 9 | default: 'latest' 10 | python-version: 11 | description: 'The version of Python to use' 12 | required: true 13 | default: '3.13' 14 | 15 | jobs: 16 | test: 17 | runs-on: ubuntu-latest 18 | steps: 19 | - name: Checkout 20 | uses: actions/checkout@v4.2.2 21 | - name: Test 22 | uses: ./.github/actions/test_rocketchat 23 | with: 24 | python-version: ${{ github.event.inputs.python-version }} 25 | rocket-chat-version: ${{ github.event.inputs.rocket-chat-version }} 26 | rocket-chat-license: ${{ secrets.ROCKETCHAT_LICENSE }} 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Python template 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | env/ 14 | build/ 15 | develop-eggs/ 16 | dist/ 17 | downloads/ 18 | eggs/ 19 | .eggs/ 20 | lib/ 21 | lib64/ 22 | parts/ 23 | sdist/ 24 | var/ 25 | wheels/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *,cover 49 | .hypothesis/ 50 | .pytest_cache/ 51 | 52 | # Translations 53 | *.mo 54 | *.pot 55 | 56 | # Django stuff: 57 | *.log 58 | local_settings.py 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # pyenv 77 | .python-version 78 | 79 | # celery beat schedule file 80 | celerybeat-schedule 81 | 82 | # SageMath parsed files 83 | *.sage.py 84 | 85 | # dotenv 86 | .env 87 | 88 | # virtualenv 89 | .venv 90 | venv/ 91 | ENV/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | 96 | # Rope project settings 97 | .ropeproject 98 | .idea/ 99 | rocketchat_API.egg-info/ 100 | .vscode/ 101 | data/ 102 | /.envrc 103 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | You can contribute by creating Pull Requests. (It may take a while to review and merge your code but if it's good it will be merged). 4 | Please, implement tests for all your code and use a [PEP8](https://www.python.org/dev/peps/pep-0008/) compliant code style. 5 | We are using [black](https://github.com/psf/black) for formatting so, probably the fastest way to get it right is just running black. 6 | All Pull Requests are tested against the latest rocket.chat version. Check that your tests pass and you keep the coverage at least as beautiful as it was before your contribution. 7 | Bots and integrations will measure everything for you :-) 8 | 9 | Reporting bugs and asking for features is also contributing ;-) Feel free to help us grow by registering issues. 10 | 11 | Much more important: Have fun and **be nice to each other!** 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jorge Alberto Díaz Orozco (Akiel) (diazorozcoj@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## rocketchat_API 2 | Python API wrapper for [Rocket.Chat](https://developer.rocket.chat/reference/api/rest-api/) 3 | 4 | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/fff725d9a0974c6597c2dd007daaa86e)](https://www.codacy.com/app/jadolg/rocketchat_API?utm_source=github.com&utm_medium=referral&utm_content=jadolg/rocketchat_API&utm_campaign=Badge_Grade) [![Test](https://github.com/jadolg/rocketchat_API/actions/workflows/test.yml/badge.svg)](https://github.com/jadolg/rocketchat_API/actions/workflows/test.yml) [![Test against latest Rocket.Chat version](https://github.com/jadolg/rocketchat_API/actions/workflows/test_latest.yml/badge.svg)](https://github.com/jadolg/rocketchat_API/actions/workflows/test_latest.yml) [![codecov](https://codecov.io/gh/jadolg/rocketchat_API/branch/master/graph/badge.svg)](https://codecov.io/gh/jadolg/rocketchat_API) ![PyPI](https://img.shields.io/pypi/v/rocketchat_API.svg) ![](https://img.shields.io/pypi/dm/rocketchat-api.svg) 5 | 6 | ### Installation 7 | - From pypi: 8 | `pip3 install rocketchat_API` 9 | - From GitHub: 10 | Clone our repository and `python3 setup.py install` 11 | 12 | ### Requirements 13 | - [requests](https://github.com/kennethreitz/requests) 14 | 15 | ### Usage 16 | ```python 17 | from pprint import pprint 18 | from rocketchat_API.rocketchat import RocketChat 19 | 20 | proxy_dict = { 21 | "http" : "http://127.0.0.1:3128", 22 | "https" : "https://127.0.0.1:3128", 23 | } 24 | 25 | rocket = RocketChat('user', 'pass', server_url='https://demo.rocket.chat', proxies=proxy_dict) 26 | pprint(rocket.me().json()) 27 | pprint(rocket.channels_list().json()) 28 | pprint(rocket.chat_post_message('good news everyone!', channel='GENERAL', alias='Farnsworth').json()) 29 | pprint(rocket.channels_history('GENERAL', count=5).json()) 30 | ``` 31 | 32 | *note*: every method returns a [requests](https://github.com/kennethreitz/requests) Response object. 33 | 34 | #### Connection pooling 35 | If you are going to make a couple of request, you can user connection pooling provided by `requests`. This will save significant time by avoiding re-negotiation of TLS (SSL) with the chat server on each call. 36 | 37 | ```python 38 | from requests import sessions 39 | from pprint import pprint 40 | from rocketchat_API.rocketchat import RocketChat 41 | 42 | with sessions.Session() as session: 43 | rocket = RocketChat('user', 'pass', server_url='https://demo.rocket.chat', session=session) 44 | pprint(rocket.me().json()) 45 | pprint(rocket.channels_list().json()) 46 | pprint(rocket.chat_post_message('good news everyone!', channel='GENERAL', alias='Farnsworth').json()) 47 | pprint(rocket.channels_history('GENERAL', count=5).json()) 48 | ``` 49 | 50 | #### Using a token for authentication instead of user and password 51 | 52 | ```python 53 | from pprint import pprint 54 | from rocketchat_API.rocketchat import RocketChat 55 | 56 | rocket = RocketChat(user_id='WPXGmQ64S3BXdCRb6', auth_token='jvNyOYw2f0YKwtiFS06Fk21HBRBBuV7zI43HmkNzI_s', server_url='https://demo.rocket.chat') 57 | pprint(rocket.me().json()) 58 | ``` 59 | 60 | ### Method parameters 61 | Only required parameters are explicit on the RocketChat class but you can still use all other parameters. For a detailed parameters list check the [Rocket chat API](https://developer.rocket.chat/reference/api/rest-api) 62 | 63 | ### API coverage 64 | Most of the API methods are already implemented. If you are interested in a specific call just open an issue or open a pull request. 65 | 66 | ### Tests 67 | We are actively testing :) 68 | 69 | Tests run on a Rocket.Chat Docker container so install Docker and docker-compose. 70 | 1. To start test server do `docker-compose up` and to take test server down `docker-compose down` 71 | 2. To run the tests run `pytest` 72 | 73 | ### Contributing 74 | You can contribute by doing Pull Requests. (It may take a while to merge your code but if it's good it will be merged). Please, try to implement tests for all your code and use a PEP8 compliant code style. 75 | 76 | Reporting bugs and asking for features is also contributing ;) Feel free to help us grow by registering issues. 77 | 78 | We hang out [here](https://open.rocket.chat/channel/python_rocketchat_api) if you want to talk. 79 | 80 | ### Supporters 81 | 82 | [![JetBrains logo.](https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.svg)](https://jb.gg/OpenSourceSupport) 83 | 84 | [JetBrains](https://www.jetbrains.com/) supports this project by providing us with licenses for their fantastic products. 85 | -------------------------------------------------------------------------------- /bandit.yml: -------------------------------------------------------------------------------- 1 | skips: ['B101','B107'] 2 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | rocketchat: 3 | image: rocketchat/rocket.chat:${ROCKET_CHAT_VERSION:-latest} 4 | restart: unless-stopped 5 | volumes: 6 | - ./uploads:/app/uploads 7 | environment: 8 | - PORT=3000 9 | - ROOT_URL=http://localhost:3000 10 | - MONGO_URL=mongodb://mongo:27017/rocketchat?replicaSet=rs0 11 | - MONGO_OPLOG_URL=mongodb://mongo:27017/local?replicaSet=rs0 12 | - OVERWRITE_SETTING_API_Enable_Rate_Limiter=false 13 | - OVERWRITE_SETTING_Accounts_TwoFactorAuthentication_Enforce_Password_Fallback=false 14 | - CREATE_TOKENS_FOR_USERS=true 15 | - ROCKETCHAT_LICENSE=${ROCKETCHAT_LICENSE} 16 | - LICENSE_DEBUG=true 17 | depends_on: 18 | - mongo 19 | ports: 20 | - "3000:3000" 21 | 22 | mongo: 23 | image: docker.io/bitnami/mongodb:6.0 24 | restart: always 25 | environment: 26 | MONGODB_REPLICA_SET_MODE: primary 27 | MONGODB_REPLICA_SET_NAME: rs0 28 | MONGODB_PORT_NUMBER: 27017 29 | MONGODB_INITIAL_PRIMARY_HOST: mongodb 30 | MONGODB_INITIAL_PRIMARY_PORT_NUMBER: 27017 31 | MONGODB_ADVERTISED_HOSTNAME: mongo 32 | MONGODB_ENABLE_JOURNAL: true 33 | ALLOW_EMPTY_PASSWORD: yes 34 | -------------------------------------------------------------------------------- /qodana.yaml: -------------------------------------------------------------------------------- 1 | version: "1.0" 2 | linter: jetbrains/qodana-python:2022.3-eap 3 | include: 4 | - name: CheckDependencyLicenses 5 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.32.4 2 | packaging~=25.0 3 | -------------------------------------------------------------------------------- /rocketchat_API/APIExceptions/RocketExceptions.py: -------------------------------------------------------------------------------- 1 | class RocketException(Exception): 2 | pass 3 | 4 | 5 | class RocketConnectionException(Exception): 6 | pass 7 | 8 | 9 | class RocketAuthenticationException(Exception): 10 | pass 11 | 12 | 13 | class RocketMissingParamException(Exception): 14 | pass 15 | 16 | 17 | class RocketUnsuportedIntegrationType(Exception): 18 | pass 19 | -------------------------------------------------------------------------------- /rocketchat_API/APIExceptions/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadolg/rocketchat_API/3d2680ecccc2d251b673cf3a5e942d6d3f11b157/rocketchat_API/APIExceptions/__init__.py -------------------------------------------------------------------------------- /rocketchat_API/APISections/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadolg/rocketchat_API/3d2680ecccc2d251b673cf3a5e942d6d3f11b157/rocketchat_API/APISections/__init__.py -------------------------------------------------------------------------------- /rocketchat_API/APISections/assets.py: -------------------------------------------------------------------------------- 1 | import mimetypes 2 | 3 | from packaging import version 4 | 5 | from rocketchat_API.APISections.base import RocketChatBase 6 | 7 | 8 | class RocketChatAssets(RocketChatBase): 9 | def assets_set_asset(self, asset_name, file, **kwargs): 10 | """Set an asset image by name.""" 11 | server_info = self.info().json() 12 | content_type = mimetypes.MimeTypes().guess_type(file) 13 | 14 | file_name = asset_name 15 | if version.parse(server_info.get("info").get("version")) >= version.parse( 16 | "5.1" 17 | ): 18 | file_name = "asset" 19 | 20 | files = { 21 | file_name: (file, open(file, "rb"), content_type[0], {"Expires": "0"}), 22 | } 23 | 24 | return self.call_api_post( 25 | "assets.setAsset", 26 | kwargs=kwargs, 27 | assetName=asset_name, 28 | use_json=False, 29 | files=files, 30 | ) 31 | 32 | def assets_unset_asset(self, asset_name): 33 | """Unset an asset by name""" 34 | return self.call_api_post("assets.unsetAsset", assetName=asset_name) 35 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/banners.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatBanners(RocketChatBase): 5 | def banners(self, platform, **kwargs): 6 | """Gets the banner to be shown to the authenticated user""" 7 | return self.call_api_get( 8 | "banners", 9 | platform=platform, 10 | kwargs=kwargs, 11 | ) 12 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/base.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | import requests 4 | 5 | from rocketchat_API.APIExceptions.RocketExceptions import ( 6 | RocketAuthenticationException, 7 | RocketConnectionException, 8 | ) 9 | 10 | 11 | class RocketChatBase: 12 | api_path = "/api/v1/" 13 | 14 | def __init__( 15 | self, 16 | user=None, 17 | password=None, 18 | auth_token=None, 19 | user_id=None, 20 | server_url="http://127.0.0.1:3000", 21 | ssl_verify=True, 22 | proxies=None, 23 | timeout=30, 24 | session=None, 25 | client_certs=None, 26 | ): 27 | """Creates a RocketChat object and does login on the specified server""" 28 | self.headers = {} 29 | self.server_url = server_url 30 | self.proxies = proxies 31 | self.ssl_verify = ssl_verify 32 | self.cert = client_certs 33 | self.timeout = timeout 34 | self.req = session or requests 35 | if user and password: 36 | self.login(user, password) # skipcq: PTC-W1006 37 | if auth_token and user_id: 38 | self.headers["X-Auth-Token"] = auth_token 39 | self.headers["X-User-Id"] = user_id 40 | 41 | @staticmethod 42 | def __reduce_kwargs(kwargs): 43 | if "kwargs" in kwargs: 44 | for arg in kwargs["kwargs"].keys(): 45 | kwargs[arg] = kwargs["kwargs"][arg] 46 | 47 | del kwargs["kwargs"] 48 | return kwargs 49 | 50 | def call_api_delete(self, method): 51 | url = self.server_url + self.api_path + method 52 | 53 | return self.req.delete( 54 | url, 55 | headers=self.headers, 56 | verify=self.ssl_verify, 57 | cert=self.cert, 58 | proxies=self.proxies, 59 | timeout=self.timeout, 60 | ) 61 | 62 | def call_api_get(self, method, api_path=None, **kwargs): 63 | args = self.__reduce_kwargs(kwargs) 64 | if not api_path: 65 | api_path = self.api_path 66 | url = self.server_url + api_path + method 67 | # convert to key[]=val1&key[]=val2 for args like key=[val1, val2], else key=val 68 | params = "&".join( 69 | ( 70 | "&".join(i + "[]=" + j for j in args[i]) 71 | if isinstance(args[i], list) 72 | else i + "=" + str(args[i]) 73 | ) 74 | for i in args 75 | ) 76 | return self.req.get( 77 | "%s?%s" % (url, params), 78 | headers=self.headers, 79 | verify=self.ssl_verify, 80 | cert=self.cert, 81 | proxies=self.proxies, 82 | timeout=self.timeout, 83 | ) 84 | 85 | def call_api_post(self, method, files=None, use_json=None, **kwargs): 86 | reduced_args = self.__reduce_kwargs(kwargs) 87 | # Since pass is a reserved word in Python it has to be injected on the request dict 88 | # Some methods use pass (users.register) and others password (users.create) 89 | if "password" in reduced_args and method != "users.create": 90 | reduced_args["pass"] = reduced_args["password"] 91 | del reduced_args["password"] 92 | if use_json is None: 93 | # see https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests 94 | # > The json parameter is ignored if either data or files is passed. 95 | # If files are sent, json should not be used 96 | use_json = files is None 97 | if use_json: 98 | return self.req.post( 99 | self.server_url + self.api_path + method, 100 | json=reduced_args, 101 | files=files, 102 | headers=self.headers, 103 | verify=self.ssl_verify, 104 | cert=self.cert, 105 | proxies=self.proxies, 106 | timeout=self.timeout, 107 | ) 108 | return self.req.post( 109 | self.server_url + self.api_path + method, 110 | data=reduced_args, 111 | files=files, 112 | headers=self.headers, 113 | verify=self.ssl_verify, 114 | cert=self.cert, 115 | proxies=self.proxies, 116 | timeout=self.timeout, 117 | ) 118 | 119 | def call_api_put(self, method, files=None, use_json=None, **kwargs): 120 | reduced_args = self.__reduce_kwargs(kwargs) 121 | if use_json is None: 122 | # see https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests 123 | # > The json parameter is ignored if either data or files is passed. 124 | # If files are sent, json should not be used 125 | use_json = files is None 126 | if use_json: 127 | return self.req.put( 128 | self.server_url + self.api_path + method, 129 | json=reduced_args, 130 | files=files, 131 | headers=self.headers, 132 | verify=self.ssl_verify, 133 | cert=self.cert, 134 | proxies=self.proxies, 135 | timeout=self.timeout, 136 | ) 137 | return self.req.put( 138 | self.server_url + self.api_path + method, 139 | data=reduced_args, 140 | files=files, 141 | headers=self.headers, 142 | verify=self.ssl_verify, 143 | cert=self.cert, 144 | proxies=self.proxies, 145 | timeout=self.timeout, 146 | ) 147 | 148 | # Authentication 149 | 150 | def login(self, user, password): 151 | request_data = {"password": password} 152 | if re.match( 153 | r"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", 154 | user, 155 | ): 156 | request_data["user"] = user 157 | else: 158 | request_data["username"] = user 159 | login_request = self.req.post( 160 | self.server_url + self.api_path + "login", 161 | json=request_data, 162 | verify=self.ssl_verify, 163 | proxies=self.proxies, 164 | cert=self.cert, 165 | timeout=self.timeout, 166 | ) 167 | if login_request.status_code == 401: 168 | raise RocketAuthenticationException() 169 | 170 | if ( 171 | login_request.status_code == 200 172 | and login_request.json().get("status") == "success" 173 | ): 174 | self.headers["X-Auth-Token"] = ( 175 | login_request.json().get("data").get("authToken") 176 | ) 177 | self.headers["X-User-Id"] = login_request.json().get("data").get("userId") 178 | return login_request 179 | 180 | raise RocketConnectionException() 181 | 182 | def logout(self, **kwargs): 183 | """Invalidate your REST rocketchat_API authentication token.""" 184 | return self.call_api_post("logout", kwargs=kwargs) 185 | 186 | def info(self, **kwargs): 187 | """Information about the Rocket.Chat server.""" 188 | return self.call_api_get("info", api_path="/api/", kwargs=kwargs) 189 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/channels.py: -------------------------------------------------------------------------------- 1 | import json 2 | import logging 3 | 4 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 5 | from rocketchat_API.APISections.base import RocketChatBase 6 | 7 | 8 | class RocketChatChannels(RocketChatBase): 9 | def channels_list(self, **kwargs): 10 | """Retrieves all of the channels from the server.""" 11 | return self.call_api_get("channels.list", kwargs=kwargs) 12 | 13 | def channels_list_joined(self, **kwargs): 14 | """Lists all of the channels the calling user has joined""" 15 | return self.call_api_get("channels.list.joined", kwargs=kwargs) 16 | 17 | def channels_info(self, room_id=None, channel=None, **kwargs): 18 | """Gets a channel's information.""" 19 | if room_id: 20 | return self.call_api_get("channels.info", roomId=room_id, kwargs=kwargs) 21 | if channel: 22 | return self.call_api_get("channels.info", roomName=channel, kwargs=kwargs) 23 | raise RocketMissingParamException("room_id or channel required") 24 | 25 | def channels_history(self, room_id, **kwargs): 26 | """Retrieves the messages from a channel.""" 27 | return self.call_api_get("channels.history", roomId=room_id, kwargs=kwargs) 28 | 29 | def channels_add_all(self, room_id, **kwargs): 30 | """Adds all of the users of the Rocket.Chat server to the channel.""" 31 | return self.call_api_post("channels.addAll", roomId=room_id, kwargs=kwargs) 32 | 33 | def channels_add_moderator(self, room_id, user_id, **kwargs): 34 | """Gives the role of moderator for a user in the current channel.""" 35 | return self.call_api_post( 36 | "channels.addModerator", roomId=room_id, userId=user_id, kwargs=kwargs 37 | ) 38 | 39 | def channels_remove_moderator(self, room_id, user_id, **kwargs): 40 | """Removes the role of moderator from a user in the current channel.""" 41 | return self.call_api_post( 42 | "channels.removeModerator", roomId=room_id, userId=user_id, kwargs=kwargs 43 | ) 44 | 45 | def channels_moderators(self, room_id=None, channel=None, **kwargs): 46 | """Lists all moderators of a channel.""" 47 | if room_id: 48 | return self.call_api_get( 49 | "channels.moderators", roomId=room_id, kwargs=kwargs 50 | ) 51 | if channel: 52 | return self.call_api_get( 53 | "channels.moderators", roomName=channel, kwargs=kwargs 54 | ) 55 | raise RocketMissingParamException("room_id or channel required") 56 | 57 | def channels_add_owner(self, room_id, user_id=None, username=None, **kwargs): 58 | """Gives the role of owner for a user in the current channel.""" 59 | if user_id: 60 | return self.call_api_post( 61 | "channels.addOwner", roomId=room_id, userId=user_id, kwargs=kwargs 62 | ) 63 | if username: 64 | return self.call_api_post( 65 | "channels.addOwner", roomId=room_id, username=username, kwargs=kwargs 66 | ) 67 | raise RocketMissingParamException("userID or username required") 68 | 69 | def channels_remove_owner(self, room_id, user_id, **kwargs): 70 | """Removes the role of owner from a user in the current channel.""" 71 | return self.call_api_post( 72 | "channels.removeOwner", roomId=room_id, userId=user_id, kwargs=kwargs 73 | ) 74 | 75 | def channels_add_leader(self, room_id, user_id, **kwargs): 76 | """Gives the role of Leader for a user in the current channel.""" 77 | return self.call_api_post( 78 | "channels.addLeader", roomId=room_id, userId=user_id, kwargs=kwargs 79 | ) 80 | 81 | def channels_remove_leader(self, room_id, user_id, **kwargs): 82 | """Removes the role of Leader for a user in the current channel.""" 83 | return self.call_api_post( 84 | "channels.removeLeader", roomId=room_id, userId=user_id, kwargs=kwargs 85 | ) 86 | 87 | def channels_archive(self, room_id, **kwargs): 88 | """Archives a channel.""" 89 | return self.call_api_post("channels.archive", roomId=room_id, kwargs=kwargs) 90 | 91 | def channels_unarchive(self, room_id, **kwargs): 92 | """Unarchives a channel.""" 93 | return self.call_api_post("channels.unarchive", roomId=room_id, kwargs=kwargs) 94 | 95 | def channels_close(self, room_id, **kwargs): 96 | """Removes the channel from the user's list of channels.""" 97 | return self.call_api_post("channels.close", roomId=room_id, kwargs=kwargs) 98 | 99 | def channels_open(self, room_id, **kwargs): 100 | """Adds the channel back to the user's list of channels.""" 101 | return self.call_api_post("channels.open", roomId=room_id, kwargs=kwargs) 102 | 103 | def channels_create(self, name, **kwargs): 104 | """Creates a new public channel, optionally including users.""" 105 | return self.call_api_post("channels.create", name=name, kwargs=kwargs) 106 | 107 | def channels_get_integrations(self, room_id, **kwargs): 108 | """Retrieves the integrations which the channel has""" 109 | return self.call_api_get( 110 | "channels.getIntegrations", roomId=room_id, kwargs=kwargs 111 | ) 112 | 113 | def channels_invite(self, room_id, user_id, **kwargs): 114 | """Adds a user to the channel.""" 115 | return self.call_api_post( 116 | "channels.invite", roomId=room_id, userId=user_id, kwargs=kwargs 117 | ) 118 | 119 | def channels_join(self, room_id, join_code, **kwargs): 120 | """Joins yourself to the channel.""" 121 | return self.call_api_post( 122 | "channels.join", roomId=room_id, joinCode=join_code, kwargs=kwargs 123 | ) 124 | 125 | def channels_kick(self, room_id, user_id, **kwargs): 126 | """Removes a user from the channel.""" 127 | return self.call_api_post( 128 | "channels.kick", roomId=room_id, userId=user_id, kwargs=kwargs 129 | ) 130 | 131 | def channels_leave(self, room_id, **kwargs): 132 | """Causes the callee to be removed from the channel.""" 133 | return self.call_api_post("channels.leave", roomId=room_id, kwargs=kwargs) 134 | 135 | def channels_rename(self, room_id, name, **kwargs): 136 | """Changes the name of the channel.""" 137 | return self.call_api_post( 138 | "channels.rename", roomId=room_id, name=name, kwargs=kwargs 139 | ) 140 | 141 | def channels_set_default(self, room_id, default, **kwargs): 142 | """Sets whether the channel is a default channel or not.""" 143 | return self.call_api_post( 144 | "channels.setDefault", 145 | roomId=room_id, 146 | default=default, 147 | kwargs=kwargs, 148 | ) 149 | 150 | def channels_set_description(self, room_id, description, **kwargs): 151 | """Sets the description for the channel.""" 152 | return self.call_api_post( 153 | "channels.setDescription", 154 | roomId=room_id, 155 | description=description, 156 | kwargs=kwargs, 157 | ) 158 | 159 | def channels_set_join_code(self, room_id, join_code, **kwargs): 160 | """Sets the code required to join the channel.""" 161 | return self.call_api_post( 162 | "channels.setJoinCode", roomId=room_id, joinCode=join_code, kwargs=kwargs 163 | ) 164 | 165 | def channels_set_read_only(self, room_id, read_only, **kwargs): 166 | """Sets whether the channel is read only or not.""" 167 | return self.call_api_post( 168 | "channels.setReadOnly", 169 | roomId=room_id, 170 | readOnly=bool(read_only), 171 | kwargs=kwargs, 172 | ) 173 | 174 | def channels_set_topic(self, room_id, topic, **kwargs): 175 | """Sets the topic for the channel.""" 176 | return self.call_api_post( 177 | "channels.setTopic", roomId=room_id, topic=topic, kwargs=kwargs 178 | ) 179 | 180 | def channels_set_type(self, room_id, a_type, **kwargs): 181 | """Sets the type of room this channel should be. The type of room this channel should be, either c or p.""" 182 | return self.call_api_post( 183 | "channels.setType", roomId=room_id, type=a_type, kwargs=kwargs 184 | ) 185 | 186 | def channels_set_announcement(self, room_id, announcement, **kwargs): 187 | """Sets the announcement for the channel.""" 188 | return self.call_api_post( 189 | "channels.setAnnouncement", 190 | roomId=room_id, 191 | announcement=announcement, 192 | kwargs=kwargs, 193 | ) 194 | 195 | def channels_set_custom_fields(self, rid, custom_fields): 196 | """Sets the custom fields for the channel.""" 197 | return self.call_api_post( 198 | "channels.setCustomFields", roomId=rid, customFields=custom_fields 199 | ) 200 | 201 | def channels_delete(self, room_id=None, channel=None, **kwargs): 202 | """Delete a public channel.""" 203 | if room_id: 204 | return self.call_api_post("channels.delete", roomId=room_id, kwargs=kwargs) 205 | if channel: 206 | return self.call_api_post( 207 | "channels.delete", roomName=channel, kwargs=kwargs 208 | ) 209 | raise RocketMissingParamException("room_id or channel required") 210 | 211 | def channels_members(self, room_id=None, channel=None, **kwargs): 212 | """Lists all channel users.""" 213 | if room_id: 214 | return self.call_api_get("channels.members", roomId=room_id, kwargs=kwargs) 215 | if channel: 216 | return self.call_api_get( 217 | "channels.members", roomName=channel, kwargs=kwargs 218 | ) 219 | raise RocketMissingParamException("room_id or channel required") 220 | 221 | def channels_roles(self, room_id=None, room_name=None, **kwargs): 222 | """Lists all user's roles in the channel.""" 223 | if room_id: 224 | return self.call_api_get("channels.roles", roomId=room_id, kwargs=kwargs) 225 | if room_name: 226 | return self.call_api_get( 227 | "channels.roles", roomName=room_name, kwargs=kwargs 228 | ) 229 | raise RocketMissingParamException("room_id or room_name required") 230 | 231 | def channels_files(self, room_id=None, room_name=None, **kwargs): 232 | """Retrieves the files from a channel.""" 233 | if room_id: 234 | return self.call_api_get("channels.files", roomId=room_id, kwargs=kwargs) 235 | if room_name: 236 | return self.call_api_get( 237 | "channels.files", roomName=room_name, kwargs=kwargs 238 | ) 239 | raise RocketMissingParamException("room_id or room_name required") 240 | 241 | def channels_get_all_user_mentions_by_channel(self, room_id, **kwargs): 242 | """Gets all the mentions of a channel.""" 243 | return self.call_api_get( 244 | "channels.getAllUserMentionsByChannel", roomId=room_id, kwargs=kwargs 245 | ) 246 | 247 | def channels_counters(self, room_id=None, room_name=None, **kwargs): 248 | """Gets counters for a channel.""" 249 | if room_id: 250 | return self.call_api_get("channels.counters", roomId=room_id, kwargs=kwargs) 251 | if room_name: 252 | return self.call_api_get( 253 | "channels.counters", roomName=room_name, kwargs=kwargs 254 | ) 255 | raise RocketMissingParamException("room_id or room_name required") 256 | 257 | # TODO: Remove query parameter once version 6 stops being maintained 258 | def channels_online(self, _id=None, query=None): 259 | """Lists all online users of a channel if the channel's id is provided, otherwise it gets all online users of 260 | all channels.""" 261 | if query: 262 | logging.warning( 263 | "The query parameter is deprecated and will be removed in version 7 of Rocket.Chat" 264 | ) 265 | return self.call_api_get("channels.online", query=json.dumps(query)) 266 | if _id: 267 | return self.call_api_get("channels.online", _id=_id) 268 | else: 269 | raise RocketMissingParamException("_id or query required") 270 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/chat.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 2 | from rocketchat_API.APISections.base import RocketChatBase 3 | 4 | 5 | class RocketChatChat(RocketChatBase): 6 | def chat_post_message(self, text, room_id=None, channel=None, **kwargs): 7 | """Posts a new chat message.""" 8 | if room_id: 9 | if text: 10 | return self.call_api_post( 11 | "chat.postMessage", roomId=room_id, text=text, kwargs=kwargs 12 | ) 13 | return self.call_api_post("chat.postMessage", roomId=room_id, kwargs=kwargs) 14 | if channel: 15 | if text: 16 | return self.call_api_post( 17 | "chat.postMessage", channel=channel, text=text, kwargs=kwargs 18 | ) 19 | return self.call_api_post( 20 | "chat.postMessage", channel=channel, kwargs=kwargs 21 | ) 22 | raise RocketMissingParamException("roomId or channel required") 23 | 24 | def chat_send_message(self, message): 25 | if "rid" in message: 26 | return self.call_api_post("chat.sendMessage", message=message) 27 | raise RocketMissingParamException("message.rid required") 28 | 29 | def chat_get_message(self, msg_id, **kwargs): 30 | return self.call_api_get("chat.getMessage", msgId=msg_id, kwargs=kwargs) 31 | 32 | def chat_pin_message(self, msg_id, **kwargs): 33 | return self.call_api_post("chat.pinMessage", messageId=msg_id, kwargs=kwargs) 34 | 35 | def chat_unpin_message(self, msg_id, **kwargs): 36 | return self.call_api_post("chat.unPinMessage", messageId=msg_id, kwargs=kwargs) 37 | 38 | def chat_star_message(self, msg_id, **kwargs): 39 | return self.call_api_post("chat.starMessage", messageId=msg_id, kwargs=kwargs) 40 | 41 | def chat_unstar_message(self, msg_id, **kwargs): 42 | return self.call_api_post("chat.unStarMessage", messageId=msg_id, kwargs=kwargs) 43 | 44 | def chat_delete(self, room_id, msg_id, **kwargs): 45 | """Deletes a chat message.""" 46 | return self.call_api_post( 47 | "chat.delete", roomId=room_id, msgId=msg_id, kwargs=kwargs 48 | ) 49 | 50 | def chat_update(self, room_id, msg_id, text, **kwargs): 51 | """Updates the text of the chat message.""" 52 | return self.call_api_post( 53 | "chat.update", roomId=room_id, msgId=msg_id, text=text, kwargs=kwargs 54 | ) 55 | 56 | def chat_react(self, msg_id, emoji="smile", **kwargs): 57 | """Updates the text of the chat message.""" 58 | return self.call_api_post( 59 | "chat.react", messageId=msg_id, emoji=emoji, kwargs=kwargs 60 | ) 61 | 62 | def chat_search(self, room_id, search_text, **kwargs): 63 | """Search for messages in a channel by id and text message.""" 64 | return self.call_api_get( 65 | "chat.search", roomId=room_id, searchText=search_text, kwargs=kwargs 66 | ) 67 | 68 | def chat_get_message_read_receipts(self, message_id, **kwargs): 69 | """Get Message Read Receipts""" 70 | return self.call_api_get( 71 | "chat.getMessageReadReceipts", messageId=message_id, kwargs=kwargs 72 | ) 73 | 74 | def chat_get_starred_messages(self, room_id, **kwargs): 75 | """Retrieve starred messages.""" 76 | return self.call_api_get( 77 | "chat.getStarredMessages", roomId=room_id, kwargs=kwargs 78 | ) 79 | 80 | def chat_report_message(self, message_id, description, **kwargs): 81 | """Reports a message.""" 82 | return self.call_api_post( 83 | "chat.reportMessage", 84 | messageId=message_id, 85 | description=description, 86 | kwargs=kwargs, 87 | ) 88 | 89 | def chat_follow_message(self, mid, **kwargs): 90 | """Follows a chat message to the message's channel.""" 91 | return self.call_api_post("chat.followMessage", mid=mid, kwargs=kwargs) 92 | 93 | def chat_get_thread_messages(self, thread_msg_id, **kwargs): 94 | """Get thread messages.""" 95 | return self.call_api_get( 96 | "chat.getThreadMessages", 97 | tmid=thread_msg_id, 98 | kwargs=kwargs, 99 | ) 100 | 101 | def chat_get_mentioned_messages(self, room_id, **kwargs): 102 | """Get the messages in which you are mentioned (users are mentioned with the @ symbol).""" 103 | return self.call_api_get( 104 | "chat.getMentionedMessages", 105 | roomId=room_id, 106 | kwargs=kwargs, 107 | ) 108 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/groups.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 2 | from rocketchat_API.APISections.base import RocketChatBase 3 | 4 | 5 | class RocketChatGroups(RocketChatBase): 6 | def groups_list_all(self, **kwargs): 7 | """ 8 | List all the private groups on the server. 9 | The calling user must have the 'view-room-administration' right 10 | """ 11 | return self.call_api_get("groups.listAll", kwargs=kwargs) 12 | 13 | def groups_list(self, **kwargs): 14 | """List the private groups the caller is part of.""" 15 | return self.call_api_get("groups.list", kwargs=kwargs) 16 | 17 | def groups_history(self, room_id, **kwargs): 18 | """Retrieves the messages from a private group.""" 19 | return self.call_api_get("groups.history", roomId=room_id, kwargs=kwargs) 20 | 21 | def groups_add_moderator(self, room_id, user_id, **kwargs): 22 | """Gives the role of moderator for a user in the current groups.""" 23 | return self.call_api_post( 24 | "groups.addModerator", roomId=room_id, userId=user_id, kwargs=kwargs 25 | ) 26 | 27 | def groups_remove_moderator(self, room_id, user_id, **kwargs): 28 | """Removes the role of moderator from a user in the current groups.""" 29 | return self.call_api_post( 30 | "groups.removeModerator", roomId=room_id, userId=user_id, kwargs=kwargs 31 | ) 32 | 33 | def groups_moderators(self, room_id=None, group=None, **kwargs): 34 | """Lists all moderators of a group.""" 35 | if room_id: 36 | return self.call_api_get("groups.moderators", roomId=room_id, kwargs=kwargs) 37 | if group: 38 | return self.call_api_get("groups.moderators", roomName=group, kwargs=kwargs) 39 | raise RocketMissingParamException("room_id or group required") 40 | 41 | def groups_add_owner(self, room_id, user_id, **kwargs): 42 | """Gives the role of owner for a user in the current Group.""" 43 | return self.call_api_post( 44 | "groups.addOwner", roomId=room_id, userId=user_id, kwargs=kwargs 45 | ) 46 | 47 | def groups_remove_owner(self, room_id, user_id, **kwargs): 48 | """Removes the role of owner from a user in the current Group.""" 49 | return self.call_api_post( 50 | "groups.removeOwner", roomId=room_id, userId=user_id, kwargs=kwargs 51 | ) 52 | 53 | def groups_archive(self, room_id, **kwargs): 54 | """Archives a private group, only if you're part of the group.""" 55 | return self.call_api_post("groups.archive", roomId=room_id, kwargs=kwargs) 56 | 57 | def groups_unarchive(self, room_id, **kwargs): 58 | """Unarchives a private group.""" 59 | return self.call_api_post("groups.unarchive", roomId=room_id, kwargs=kwargs) 60 | 61 | def groups_close(self, room_id, **kwargs): 62 | """Removes the private group from the user's list of groups, only if you're part of the group.""" 63 | return self.call_api_post("groups.close", roomId=room_id, kwargs=kwargs) 64 | 65 | def groups_create(self, name, **kwargs): 66 | """Creates a new private group, optionally including users, only if you're part of the group.""" 67 | return self.call_api_post("groups.create", name=name, kwargs=kwargs) 68 | 69 | def groups_get_integrations(self, room_id, **kwargs): 70 | """Retrieves the integrations which the group has""" 71 | return self.call_api_get( 72 | "groups.getIntegrations", roomId=room_id, kwargs=kwargs 73 | ) 74 | 75 | def groups_info(self, room_id=None, room_name=None, **kwargs): 76 | """GRetrieves the information about the private group, only if you're part of the group.""" 77 | if room_id: 78 | return self.call_api_get("groups.info", roomId=room_id, kwargs=kwargs) 79 | if room_name: 80 | return self.call_api_get("groups.info", roomName=room_name, kwargs=kwargs) 81 | raise RocketMissingParamException("room_id or roomName required") 82 | 83 | def groups_invite(self, room_id, user_id, **kwargs): 84 | """Adds a user to the private group.""" 85 | return self.call_api_post( 86 | "groups.invite", roomId=room_id, userId=user_id, kwargs=kwargs 87 | ) 88 | 89 | def groups_kick(self, room_id, user_id, **kwargs): 90 | """Removes a user from the private group.""" 91 | return self.call_api_post( 92 | "groups.kick", roomId=room_id, userId=user_id, kwargs=kwargs 93 | ) 94 | 95 | def groups_leave(self, room_id, **kwargs): 96 | """Causes the callee to be removed from the private group, if they're part of it and are not the last owner.""" 97 | return self.call_api_post("groups.leave", roomId=room_id, kwargs=kwargs) 98 | 99 | def groups_open(self, room_id, **kwargs): 100 | """Adds the private group back to the user's list of private groups.""" 101 | return self.call_api_post("groups.open", roomId=room_id, kwargs=kwargs) 102 | 103 | def groups_rename(self, room_id, name, **kwargs): 104 | """Changes the name of the private group.""" 105 | return self.call_api_post( 106 | "groups.rename", roomId=room_id, name=name, kwargs=kwargs 107 | ) 108 | 109 | def groups_set_announcement(self, room_id, announcement, **kwargs): 110 | """Sets the announcement for the private group.""" 111 | return self.call_api_post( 112 | "groups.setAnnouncement", 113 | roomId=room_id, 114 | announcement=announcement, 115 | kwargs=kwargs, 116 | ) 117 | 118 | def groups_set_description(self, room_id, description, **kwargs): 119 | """Sets the description for the private group.""" 120 | return self.call_api_post( 121 | "groups.setDescription", 122 | roomId=room_id, 123 | description=description, 124 | kwargs=kwargs, 125 | ) 126 | 127 | def groups_set_read_only(self, room_id, read_only, **kwargs): 128 | """Sets whether the group is read only or not.""" 129 | return self.call_api_post( 130 | "groups.setReadOnly", 131 | roomId=room_id, 132 | readOnly=bool(read_only), 133 | kwargs=kwargs, 134 | ) 135 | 136 | def groups_set_topic(self, room_id, topic, **kwargs): 137 | """Sets the topic for the private group.""" 138 | return self.call_api_post( 139 | "groups.setTopic", roomId=room_id, topic=topic, kwargs=kwargs 140 | ) 141 | 142 | def groups_set_type(self, room_id, a_type, **kwargs): 143 | """Sets the type of room this group should be. The type of room this channel should be, either c or p.""" 144 | return self.call_api_post( 145 | "groups.setType", roomId=room_id, type=a_type, kwargs=kwargs 146 | ) 147 | 148 | def groups_set_custom_fields( 149 | self, custom_fields, room_id=None, room_name=None, **kwargs 150 | ): 151 | if room_id: 152 | return self.call_api_post( 153 | "groups.setCustomFields", 154 | roomId=room_id, 155 | customFields=custom_fields, 156 | kwargs=kwargs, 157 | ) 158 | if room_name: 159 | return self.call_api_post( 160 | "groups.setCustomFields", 161 | roomName=room_name, 162 | customFields=custom_fields, 163 | kwargs=kwargs, 164 | ) 165 | raise RocketMissingParamException("room_id or room_name required") 166 | 167 | def groups_delete(self, room_id=None, group=None, **kwargs): 168 | """Delete a private group.""" 169 | if room_id: 170 | return self.call_api_post("groups.delete", roomId=room_id, kwargs=kwargs) 171 | if group: 172 | return self.call_api_post("groups.delete", roomName=group, kwargs=kwargs) 173 | raise RocketMissingParamException("room_id or group required") 174 | 175 | def groups_members(self, room_id=None, group=None, **kwargs): 176 | """Lists all group users.""" 177 | if room_id: 178 | return self.call_api_get("groups.members", roomId=room_id, kwargs=kwargs) 179 | if group: 180 | return self.call_api_get("groups.members", roomName=group, kwargs=kwargs) 181 | raise RocketMissingParamException("room_id or group required") 182 | 183 | def groups_roles(self, room_id=None, room_name=None, **kwargs): 184 | """Lists all user's roles in the private group.""" 185 | if room_id: 186 | return self.call_api_get("groups.roles", roomId=room_id, kwargs=kwargs) 187 | if room_name: 188 | return self.call_api_get("groups.roles", roomName=room_name, kwargs=kwargs) 189 | raise RocketMissingParamException("room_id or room_name required") 190 | 191 | def groups_files(self, room_id=None, room_name=None, **kwargs): 192 | """Retrieves the files from a private group.""" 193 | if room_id: 194 | return self.call_api_get("groups.files", roomId=room_id, kwargs=kwargs) 195 | if room_name: 196 | return self.call_api_get("groups.files", roomName=room_name, kwargs=kwargs) 197 | raise RocketMissingParamException("room_id or room_name required") 198 | 199 | def groups_add_leader(self, room_id, user_id, **kwargs): 200 | """Gives the role of Leader for a user in the current group.""" 201 | return self.call_api_post( 202 | "groups.addLeader", roomId=room_id, userId=user_id, kwargs=kwargs 203 | ) 204 | 205 | def groups_remove_leader(self, room_id, user_id, **kwargs): 206 | """Removes the role of Leader for a user in the current group.""" 207 | return self.call_api_post( 208 | "groups.removeLeader", roomId=room_id, userId=user_id, kwargs=kwargs 209 | ) 210 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/im.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 2 | from rocketchat_API.APISections.base import RocketChatBase 3 | 4 | 5 | class RocketChatIM(RocketChatBase): 6 | def im_list(self, **kwargs): 7 | """List the private im chats for logged user""" 8 | return self.call_api_get("im.list", kwargs=kwargs) 9 | 10 | def im_list_everyone(self, **kwargs): 11 | """List all direct message the caller in the server.""" 12 | return self.call_api_get("im.list.everyone", kwargs=kwargs) 13 | 14 | def im_history(self, room_id, **kwargs): 15 | """Retrieves the history for a private im chat""" 16 | return self.call_api_get("im.history", roomId=room_id, kwargs=kwargs) 17 | 18 | def im_create(self, username, **kwargs): 19 | """Create a direct message session with another user.""" 20 | return self.call_api_post("im.create", username=username, kwargs=kwargs) 21 | 22 | def im_create_multiple(self, usernames, **kwargs): 23 | """Create a direct message session with one or more users.""" 24 | return self.call_api_post( 25 | "im.create", usernames=",".join(usernames), kwargs=kwargs 26 | ) 27 | 28 | def im_open(self, room_id, **kwargs): 29 | """Adds the direct message back to the user's list of direct messages.""" 30 | return self.call_api_post("im.open", roomId=room_id, kwargs=kwargs) 31 | 32 | def im_close(self, room_id, **kwargs): 33 | """Removes the direct message from the user's list of direct messages.""" 34 | return self.call_api_post("im.close", roomId=room_id, kwargs=kwargs) 35 | 36 | def im_members(self, room_id): 37 | """Retrieves members of a direct message.""" 38 | return self.call_api_get("im.members", roomId=room_id) 39 | 40 | def im_messages(self, room_id=None, username=None): 41 | """Retrieves direct messages from the server by username""" 42 | if room_id: 43 | return self.call_api_get("im.messages", roomId=room_id) 44 | 45 | if username: 46 | return self.call_api_get("im.messages", username=username) 47 | 48 | raise RocketMissingParamException("roomId or username required") 49 | 50 | def im_messages_others(self, room_id, **kwargs): 51 | """Retrieves the messages from any direct message in the server""" 52 | return self.call_api_get("im.messages.others", roomId=room_id, kwargs=kwargs) 53 | 54 | def im_set_topic(self, room_id, topic, **kwargs): 55 | """Sets the topic for the direct message""" 56 | return self.call_api_post( 57 | "im.setTopic", roomId=room_id, topic=topic, kwargs=kwargs 58 | ) 59 | 60 | def im_files(self, room_id=None, user_name=None, **kwargs): 61 | """Retrieves the files from a direct message.""" 62 | if room_id: 63 | return self.call_api_get("im.files", roomId=room_id, kwargs=kwargs) 64 | if user_name: 65 | return self.call_api_get("im.files", username=user_name, kwargs=kwargs) 66 | raise RocketMissingParamException("roomId or username required") 67 | 68 | def im_counters(self, room_id, user_name=None): 69 | """Gets counters of direct messages.""" 70 | if user_name: 71 | return self.call_api_get("im.counters", roomId=room_id, username=user_name) 72 | return self.call_api_get("im.counters", roomId=room_id) 73 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/integrations.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APIExceptions.RocketExceptions import ( 2 | RocketUnsuportedIntegrationType, 3 | ) 4 | from rocketchat_API.APISections.base import RocketChatBase 5 | 6 | 7 | class RocketChatIntegrations(RocketChatBase): 8 | # pylint: disable=too-many-arguments 9 | def integrations_create( 10 | self, 11 | integrations_type, 12 | name, 13 | enabled, 14 | username, 15 | channel, 16 | script_enabled, 17 | event=None, 18 | urls=None, 19 | **kwargs 20 | ): 21 | """Creates an integration.""" 22 | if integrations_type == "webhook-outgoing": 23 | return self.call_api_post( 24 | "integrations.create", 25 | type=integrations_type, 26 | name=name, 27 | enabled=enabled, 28 | event=event, 29 | urls=urls, 30 | username=username, 31 | channel=channel, 32 | scriptEnabled=script_enabled, 33 | kwargs=kwargs, 34 | ) 35 | elif integrations_type == "webhook-incoming": 36 | return self.call_api_post( 37 | "integrations.create", 38 | type=integrations_type, 39 | name=name, 40 | enabled=enabled, 41 | username=username, 42 | channel=channel, 43 | scriptEnabled=script_enabled, 44 | kwargs=kwargs, 45 | ) 46 | else: 47 | raise RocketUnsuportedIntegrationType() 48 | 49 | def integrations_get(self, integration_id, **kwargs): 50 | """Retrieves an integration by id.""" 51 | return self.call_api_get( 52 | "integrations.get", integrationId=integration_id, kwargs=kwargs 53 | ) 54 | 55 | def integrations_history(self, integration_id, **kwargs): 56 | """Lists all history of the specified integration.""" 57 | return self.call_api_get( 58 | "integrations.history", id=integration_id, kwargs=kwargs 59 | ) 60 | 61 | def integrations_list(self, **kwargs): 62 | """Lists all of the integrations on the server.""" 63 | return self.call_api_get("integrations.list", kwargs=kwargs) 64 | 65 | def integrations_remove(self, integrations_type, integration_id, **kwargs): 66 | """Removes an integration from the server.""" 67 | return self.call_api_post( 68 | "integrations.remove", 69 | type=integrations_type, 70 | integrationId=integration_id, 71 | kwargs=kwargs, 72 | ) 73 | 74 | def integrations_update( 75 | self, 76 | integrations_type, 77 | name, 78 | enabled, 79 | username, 80 | channel, 81 | script_enabled, 82 | integration_id, 83 | **kwargs 84 | ): 85 | """Updates an existing integration.""" 86 | return self.call_api_put( 87 | "integrations.update", 88 | type=integrations_type, 89 | name=name, 90 | enabled=enabled, 91 | username=username, 92 | channel=channel, 93 | scriptEnabled=script_enabled, 94 | integrationId=integration_id, 95 | kwargs=kwargs, 96 | ) 97 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/invites.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatInvites(RocketChatBase): 5 | def find_or_create_invite(self, rid, days, max_uses): 6 | """ 7 | Creates or return an existing invite with the specified parameters. 8 | Requires the create-invite-links permission 9 | """ 10 | return self.call_api_post( 11 | "findOrCreateInvite", rid=rid, days=days, maxUses=max_uses 12 | ) 13 | 14 | def list_invites(self, **kwargs): 15 | """Lists all of the invites on the server. Requires the create-invite-links permission.""" 16 | return self.call_api_get("listInvites", kwargs=kwargs) 17 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/licenses.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatLicenses(RocketChatBase): 5 | 6 | def licenses_info(self, **kwargs): 7 | """Retrieves a list of all registered licenses in the workspace.""" 8 | return self.call_api_get("licenses.info", kwargs=kwargs) 9 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/livechat.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatLivechat(RocketChatBase): 5 | def livechat_rooms(self, **kwargs): 6 | """Retrieves a list of livechat rooms.""" 7 | return self.call_api_get("livechat/rooms", kwargs=kwargs) 8 | 9 | def livechat_inquiries_list(self, **kwargs): 10 | """Lists all of the open livechat inquiries.""" 11 | return self.call_api_get("livechat/inquiries.list", kwargs=kwargs) 12 | 13 | def livechat_inquiries_take(self, inquiry_id, **kwargs): 14 | """Takes an open inquiry.""" 15 | return self.call_api_post( 16 | "livechat/inquiries.take", inquiryId=inquiry_id, kwargs=kwargs 17 | ) 18 | 19 | def livechat_get_users(self, user_type, **kwargs): 20 | """Get a list of agents or managers.""" 21 | return self.call_api_get("livechat/users/{}".format(user_type), kwargs=kwargs) 22 | 23 | def livechat_create_user(self, user_type, **kwargs): 24 | """Register a new agent or manager.""" 25 | return self.call_api_post("livechat/users/{}".format(user_type), kwargs=kwargs) 26 | 27 | def livechat_get_user(self, user_type, user_id, **kwargs): 28 | """Get info about an agent or manager.""" 29 | return self.call_api_get( 30 | "livechat/users/{}/{}".format(user_type, user_id), kwargs=kwargs 31 | ) 32 | 33 | def livechat_delete_user(self, user_type, user_id): 34 | """Removes an agent or manager.""" 35 | return self.call_api_delete("livechat/users/{}/{}".format(user_type, user_id)) 36 | 37 | def livechat_register_visitor(self, token, **kwargs): 38 | """Register a new Livechat visitor.""" 39 | if "visitor" not in kwargs: 40 | kwargs["visitor"] = {} 41 | kwargs["visitor"]["token"] = token 42 | return self.call_api_post("livechat/visitor", kwargs=kwargs) 43 | 44 | def livechat_get_visitor(self, token): 45 | """Retrieve a visitor data.""" 46 | return self.call_api_get("livechat/visitor/{}".format(token)) 47 | 48 | def livechat_room(self, token, **kwargs): 49 | """Get the Livechat room data or open a new room.""" 50 | return self.call_api_get("livechat/room", token=token, kwargs=kwargs) 51 | 52 | def livechat_message(self, token, rid, msg, **kwargs): 53 | """Send a new Livechat message.""" 54 | return self.call_api_post( 55 | "livechat/message", token=token, rid=rid, msg=msg, kwargs=kwargs 56 | ) 57 | 58 | def livechat_messages_history(self, rid, token, **kwargs): 59 | """Load Livechat messages history.""" 60 | return self.call_api_get( 61 | "livechat/messages.history/{}".format(rid), token=token, kwargs=kwargs 62 | ) 63 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/miscellaneous.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatMiscellaneous(RocketChatBase): 5 | # Miscellaneous information 6 | def directory(self, query, **kwargs): 7 | """Search by users or channels on all server.""" 8 | if isinstance(query, dict): 9 | query = str(query).replace("'", '"') 10 | 11 | return self.call_api_get("directory", query=query, kwargs=kwargs) 12 | 13 | def spotlight(self, query, **kwargs): 14 | """Searches for users or rooms that are visible to the user.""" 15 | return self.call_api_get("spotlight", query=query, kwargs=kwargs) 16 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/permissions.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatPermissions(RocketChatBase): 5 | def permissions_list_all(self, **kwargs): 6 | """Returns all permissions from the server.""" 7 | return self.call_api_get("permissions.listAll", kwargs=kwargs) 8 | 9 | def permissions_update(self, permissions, **kwargs): 10 | """Edits permissions on the server.""" 11 | return self.call_api_post( 12 | "permissions.update", permissions=permissions, kwargs=kwargs 13 | ) 14 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/roles.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatRoles(RocketChatBase): 5 | def roles_list(self, **kwargs): 6 | """Gets all the roles in the system.""" 7 | return self.call_api_get("roles.list", kwargs=kwargs) 8 | 9 | def roles_create(self, name, **kwargs): 10 | """Create a new role in the system.""" 11 | return self.call_api_post("roles.create", name=name, kwargs=kwargs) 12 | 13 | def roles_add_user_to_role(self, role_name, username, **kwargs): 14 | """Assign a role to a user. Optionally, you can set this role to a room.""" 15 | return self.call_api_post( 16 | "roles.addUserToRole", roleName=role_name, username=username, kwargs=kwargs 17 | ) 18 | 19 | def roles_remove_user_from_role(self, role_name, username, **kwargs): 20 | """Remove a role from a user. Optionally, you can unset this role for a specified scope.""" 21 | return self.call_api_post( 22 | "roles.removeUserFromRole", 23 | roleName=role_name, 24 | username=username, 25 | kwargs=kwargs, 26 | ) 27 | 28 | def roles_get_users_in_role(self, role, **kwargs): 29 | """Gets the users that belongs to a role. It supports the Offset and Count Only.""" 30 | return self.call_api_get("roles.getUsersInRole", role=role, kwargs=kwargs) 31 | 32 | def roles_sync(self, updated_since): 33 | """Gets all the roles in the system which are updated after a given date.""" 34 | return self.call_api_get("roles.sync", updatedSince=updated_since) 35 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/rooms.py: -------------------------------------------------------------------------------- 1 | import mimetypes 2 | import os 3 | 4 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 5 | from rocketchat_API.APISections.base import RocketChatBase 6 | 7 | 8 | class RocketChatRooms(RocketChatBase): 9 | def rooms_upload(self, rid, file, **kwargs): 10 | """Post a message with attached file to a dedicated room.""" 11 | files = { 12 | "file": ( 13 | os.path.basename(file), 14 | open(file, "rb"), 15 | mimetypes.guess_type(file)[0], 16 | ), 17 | } 18 | return self.call_api_post( 19 | "rooms.upload/" + rid, kwargs=kwargs, use_json=False, files=files 20 | ) 21 | 22 | def rooms_get(self, **kwargs): 23 | """Get all opened rooms for this user.""" 24 | return self.call_api_get("rooms.get", kwargs=kwargs) 25 | 26 | def rooms_clean_history(self, room_id, latest, oldest, **kwargs): 27 | """Cleans up a room, removing messages from the provided time range.""" 28 | return self.call_api_post( 29 | "rooms.cleanHistory", 30 | roomId=room_id, 31 | latest=latest, 32 | oldest=oldest, 33 | kwargs=kwargs, 34 | ) 35 | 36 | def rooms_favorite(self, room_id=None, room_name=None, favorite=True): 37 | """Favorite or unfavorite room.""" 38 | if room_id is not None: 39 | return self.call_api_post( 40 | "rooms.favorite", roomId=room_id, favorite=favorite 41 | ) 42 | if room_name is not None: 43 | return self.call_api_post( 44 | "rooms.favorite", roomName=room_name, favorite=favorite 45 | ) 46 | raise RocketMissingParamException("room_id or roomName required") 47 | 48 | def rooms_info(self, room_id=None, room_name=None): 49 | """Retrieves the information about the room.""" 50 | if room_id is not None: 51 | return self.call_api_get("rooms.info", roomId=room_id) 52 | if room_name is not None: 53 | return self.call_api_get("rooms.info", roomName=room_name) 54 | raise RocketMissingParamException("room_id or roomName required") 55 | 56 | def rooms_admin_rooms(self, **kwargs): 57 | """Retrieves all rooms (requires the view-room-administration permission).""" 58 | return self.call_api_get("rooms.adminRooms", kwargs=kwargs) 59 | 60 | def rooms_create_discussion(self, prid, t_name, **kwargs): 61 | """ 62 | Creates a new discussion for room. It requires at least one of the 63 | following permissions: start-discussion OR start-discussion-other-user, 64 | AND must be with the following setting enabled: Discussion_enabled. 65 | """ 66 | return self.call_api_post( 67 | "rooms.createDiscussion", prid=prid, t_name=t_name, kwargs=kwargs 68 | ) 69 | 70 | def rooms_leave(self, room_id): 71 | """Causes the request sender to be removed from the room.""" 72 | return self.call_api_post("rooms.leave", roomId=room_id) 73 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/settings.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatSettings(RocketChatBase): 5 | def settings_get(self, _id, **kwargs): 6 | """Gets the setting for the provided _id.""" 7 | return self.call_api_get("settings/" + _id, kwargs=kwargs) 8 | 9 | def settings_update(self, _id, value, **kwargs): 10 | """Updates the setting for the provided _id.""" 11 | return self.call_api_post("settings/" + _id, value=value, kwargs=kwargs) 12 | 13 | def settings(self, **kwargs): 14 | """List all private settings.""" 15 | return self.call_api_get("settings", kwargs=kwargs) 16 | 17 | def settings_public(self, **kwargs): 18 | """List all private settings.""" 19 | return self.call_api_get("settings.public", kwargs=kwargs) 20 | 21 | def settings_oauth(self, **kwargs): 22 | """List all OAuth services.""" 23 | return self.call_api_get("settings.oauth", kwargs=kwargs) 24 | 25 | def settings_addcustomoauth(self, name, **kwargs): 26 | """Add a new custom OAuth service with the provided name.""" 27 | return self.call_api_post("settings.addCustomOAuth", name=name, kwargs=kwargs) 28 | 29 | def service_configurations(self, **kwargs): 30 | """List all service configurations.""" 31 | return self.call_api_get("service.configurations", kwargs=kwargs) 32 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/statistics.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatStatistics(RocketChatBase): 5 | def statistics(self, **kwargs): 6 | """Retrieves the current statistics""" 7 | return self.call_api_get("statistics", kwargs=kwargs) 8 | 9 | def statistics_list(self, **kwargs): 10 | """Selectable statistics about the Rocket.Chat server.""" 11 | return self.call_api_get("statistics.list", kwargs=kwargs) 12 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/subscriptions.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatSubscriptions(RocketChatBase): 5 | def subscriptions_get(self, **kwargs): 6 | """Get all subscriptions.""" 7 | return self.call_api_get("subscriptions.get", kwargs=kwargs) 8 | 9 | def subscriptions_get_one(self, room_id, **kwargs): 10 | """Get the subscription by room id.""" 11 | return self.call_api_get("subscriptions.getOne", roomId=room_id, kwargs=kwargs) 12 | 13 | def subscriptions_unread(self, room_id, **kwargs): 14 | """Mark messages as unread by roomId or from a message""" 15 | return self.call_api_post("subscriptions.unread", roomId=room_id, kwargs=kwargs) 16 | 17 | def subscriptions_read(self, rid, **kwargs): 18 | """Mark room as read""" 19 | return self.call_api_post("subscriptions.read", rid=rid, kwargs=kwargs) 20 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/teams.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 2 | from rocketchat_API.APISections.base import RocketChatBase 3 | 4 | ID_OR_TEAM_NAME_REQUIRED = "team_id or team_name required" 5 | 6 | 7 | class RocketChatTeams(RocketChatBase): 8 | def teams_create(self, name, team_type, **kwargs): 9 | """Creates a new team. Requires create-team permission.""" 10 | return self.call_api_post( 11 | "teams.create", name=name, type=team_type, kwargs=kwargs 12 | ) 13 | 14 | def teams_delete(self, team_id=None, team_name=None, **kwargs): 15 | """Delete a team.""" 16 | if team_id: 17 | return self.call_api_post("teams.delete", teamId=team_id, kwargs=kwargs) 18 | if team_name: 19 | return self.call_api_post("teams.delete", teamName=team_name, kwargs=kwargs) 20 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 21 | 22 | def teams_list_all(self, **kwargs): 23 | """ 24 | List all the teams on the server. 25 | The calling user must have the 'view-all-teams' permission 26 | """ 27 | return self.call_api_get("teams.listAll", kwargs=kwargs) 28 | 29 | def teams_info(self, team_id=None, team_name=None, **kwargs): 30 | """ 31 | Gets a team's information. 32 | If the team is not public, the caller user must be a member of 33 | the team. 34 | """ 35 | if team_id: 36 | return self.call_api_get("teams.info", teamId=team_id, kwargs=kwargs) 37 | if team_name: 38 | return self.call_api_get("teams.info", teamName=team_name, kwargs=kwargs) 39 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 40 | 41 | def teams_members( 42 | self, team_id=None, team_name=None, name="", username="", **kwargs 43 | ): 44 | """Lists the members of a team.""" 45 | if team_id: 46 | return self.call_api_get( 47 | "teams.members", 48 | teamId=team_id, 49 | name=name, 50 | username=username, 51 | kwargs=kwargs, 52 | ) 53 | if team_name: 54 | return self.call_api_get( 55 | "teams.members", 56 | teamName=team_name, 57 | name=name, 58 | username=username, 59 | kwargs=kwargs, 60 | ) 61 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 62 | 63 | def teams_add_members(self, team_id=None, team_name=None, members=None, **kwargs): 64 | """Adds members to the team.""" 65 | if team_id: 66 | return self.call_api_post( 67 | "teams.addMembers", teamId=team_id, members=members, kwargs=kwargs 68 | ) 69 | if team_name: 70 | return self.call_api_post( 71 | "teams.addMembers", teamName=team_name, members=members, kwargs=kwargs 72 | ) 73 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 74 | 75 | def teams_remove_member(self, team_id=None, team_name=None, user_id=None, **kwargs): 76 | """Removes a member from a team. Requires edit-team-member permission.""" 77 | if team_id: 78 | return self.call_api_post( 79 | "teams.removeMember", teamId=team_id, userId=user_id, kwargs=kwargs 80 | ) 81 | if team_name: 82 | return self.call_api_post( 83 | "teams.removeMember", teamName=team_name, userId=user_id, kwargs=kwargs 84 | ) 85 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 86 | 87 | def teams_update_member(self, team_id=None, team_name=None, member=None, **kwargs): 88 | """Updates a team member's roles. Requires edit-team-member permission.""" 89 | if team_id: 90 | return self.call_api_post( 91 | "teams.updateMember", teamId=team_id, member=member, kwargs=kwargs 92 | ) 93 | if team_name: 94 | return self.call_api_post( 95 | "teams.updateMember", teamName=team_name, member=member, kwargs=kwargs 96 | ) 97 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 98 | 99 | def teams_list_rooms( 100 | self, team_id=None, team_name=None, room_type="", name="", **kwargs 101 | ): 102 | """List all rooms of the team.""" 103 | if team_id: 104 | return self.call_api_get( 105 | "teams.listRooms", 106 | teamId=team_id, 107 | type=room_type, 108 | filter=name, 109 | kwargs=kwargs, 110 | ) 111 | if team_name: 112 | return self.call_api_get( 113 | "teams.listRooms", 114 | teamName=team_name, 115 | type=room_type, 116 | filter=name, 117 | kwargs=kwargs, 118 | ) 119 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 120 | 121 | def teams_add_rooms(self, team_id=None, team_name=None, rooms=None, **kwargs): 122 | """Adds rooms to the team. Requires add-team-channel permission.""" 123 | if team_id: 124 | return self.call_api_post( 125 | "teams.addRooms", 126 | teamId=team_id, 127 | rooms=rooms, 128 | kwargs=kwargs, 129 | ) 130 | if team_name: 131 | return self.call_api_post( 132 | "teams.addRooms", 133 | teamName=team_name, 134 | rooms=rooms, 135 | kwargs=kwargs, 136 | ) 137 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 138 | 139 | def teams_remove_room(self, team_id=None, team_name=None, room_id=None, **kwargs): 140 | """Removes a room from a team. Requires remove-team-channel permission.""" 141 | if team_id: 142 | return self.call_api_post( 143 | "teams.removeRoom", 144 | teamId=team_id, 145 | roomId=room_id, 146 | kwargs=kwargs, 147 | ) 148 | if team_name: 149 | return self.call_api_post( 150 | "teams.removeRoom", 151 | teamName=team_name, 152 | roomId=room_id, 153 | kwargs=kwargs, 154 | ) 155 | raise RocketMissingParamException(ID_OR_TEAM_NAME_REQUIRED) 156 | 157 | def teams_update_room(self, room_id, is_default, **kwargs): 158 | """Updates a room from a team. Requires edit-team-channel permission.""" 159 | return self.call_api_post( 160 | "teams.updateRoom", 161 | roomId=room_id, 162 | isDefault=is_default, 163 | kwargs=kwargs, 164 | ) 165 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/users.py: -------------------------------------------------------------------------------- 1 | import mimetypes 2 | import os 3 | 4 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 5 | from rocketchat_API.APISections.base import RocketChatBase 6 | 7 | 8 | class RocketChatUsers(RocketChatBase): 9 | def me(self, **kwargs): 10 | """Displays information about the authenticated user.""" 11 | return self.call_api_get("me", kwargs=kwargs) 12 | 13 | def users_info(self, user_id=None, username=None, **kwargs): 14 | """Gets a user's information, limited to the caller's permissions.""" 15 | if user_id: 16 | return self.call_api_get("users.info", userId=user_id, kwargs=kwargs) 17 | if username: 18 | return self.call_api_get("users.info", username=username, kwargs=kwargs) 19 | raise RocketMissingParamException("userID or username required") 20 | 21 | def users_list(self, **kwargs): 22 | """All of the users and their information, limited to permissions.""" 23 | return self.call_api_get("users.list", kwargs=kwargs) 24 | 25 | def users_get_presence(self, user_id=None, username=None, **kwargs): 26 | """Gets the online presence of the a user.""" 27 | if user_id: 28 | return self.call_api_get("users.getPresence", userId=user_id, kwargs=kwargs) 29 | if username: 30 | return self.call_api_get( 31 | "users.getPresence", username=username, kwargs=kwargs 32 | ) 33 | raise RocketMissingParamException("userID or username required") 34 | 35 | def users_create(self, email, name, password, username, **kwargs): 36 | """Creates a user""" 37 | return self.call_api_post( 38 | "users.create", 39 | email=email, 40 | name=name, 41 | password=password, 42 | username=username, 43 | kwargs=kwargs, 44 | ) 45 | 46 | def users_delete(self, user_id, **kwargs): 47 | """Deletes a user""" 48 | return self.call_api_post("users.delete", userId=user_id, **kwargs) 49 | 50 | def users_register(self, email, name, password, username, **kwargs): 51 | """Register a new user.""" 52 | return self.call_api_post( 53 | "users.register", 54 | email=email, 55 | name=name, 56 | password=password, 57 | username=username, 58 | kwargs=kwargs, 59 | ) 60 | 61 | def users_get_avatar(self, user_id=None, username=None, **kwargs): 62 | """Gets the URL for a user's avatar.""" 63 | if user_id: 64 | response = self.call_api_get( 65 | "users.getAvatar", userId=user_id, kwargs=kwargs 66 | ) 67 | elif username: 68 | response = self.call_api_get( 69 | "users.getAvatar", username=username, kwargs=kwargs 70 | ) 71 | else: 72 | raise RocketMissingParamException("userID or username required") 73 | 74 | # If Accounts_AvatarBlockUnauthorizedAccess is set, we need to provide the Token as cookies 75 | if response.status_code == 403: 76 | return self.req.get( 77 | response.url, 78 | cert=self.cert, 79 | cookies={ 80 | "rc_uid": self.headers.get("X-User-Id"), 81 | "rc_token": self.headers.get("X-Auth-Token"), 82 | }, 83 | ) 84 | return response 85 | 86 | def users_set_avatar(self, avatar_url, **kwargs): 87 | """Set a user's avatar""" 88 | if avatar_url.startswith("http://") or avatar_url.startswith("https://"): 89 | return self.call_api_post( 90 | "users.setAvatar", avatarUrl=avatar_url, kwargs=kwargs 91 | ) 92 | 93 | avatar_file = { 94 | "image": ( 95 | os.path.basename(avatar_url), 96 | open(avatar_url, "rb"), 97 | mimetypes.guess_type(avatar_url)[0], 98 | ), 99 | } 100 | 101 | return self.call_api_post("users.setAvatar", files=avatar_file, kwargs=kwargs) 102 | 103 | def users_reset_avatar(self, user_id=None, username=None, **kwargs): 104 | """Reset a user's avatar""" 105 | if user_id: 106 | return self.call_api_post( 107 | "users.resetAvatar", userId=user_id, kwargs=kwargs 108 | ) 109 | if username: 110 | return self.call_api_post( 111 | "users.resetAvatar", username=username, kwargs=kwargs 112 | ) 113 | raise RocketMissingParamException("userID or username required") 114 | 115 | def users_create_token(self, user_id=None, username=None, **kwargs): 116 | """Create a user authentication token.""" 117 | if user_id: 118 | return self.call_api_post( 119 | "users.createToken", userId=user_id, kwargs=kwargs 120 | ) 121 | if username: 122 | return self.call_api_post( 123 | "users.createToken", username=username, kwargs=kwargs 124 | ) 125 | raise RocketMissingParamException("userID or username required") 126 | 127 | def users_update(self, user_id, **kwargs): 128 | """Update an existing user.""" 129 | return self.call_api_post("users.update", userId=user_id, data=kwargs) 130 | 131 | def users_set_active_status(self, user_id, active_status, **kwargs): 132 | """Update user active status.""" 133 | return self.call_api_post( 134 | "users.setActiveStatus", 135 | userId=user_id, 136 | activeStatus=active_status, 137 | kwargs=kwargs, 138 | ) 139 | 140 | def users_forgot_password(self, email, **kwargs): 141 | """Send email to reset your password.""" 142 | return self.call_api_post("users.forgotPassword", email=email, data=kwargs) 143 | 144 | def users_get_preferences(self, **kwargs): 145 | """Gets all preferences of user.""" 146 | return self.call_api_get("users.getPreferences", kwargs=kwargs) 147 | 148 | def users_set_preferences(self, user_id, data, **kwargs): 149 | """Set user's preferences.""" 150 | return self.call_api_post( 151 | "users.setPreferences", userId=user_id, data=data, kwargs=kwargs 152 | ) 153 | 154 | def users_set_status(self, message, **kwargs): 155 | """Sets a user Status when the status message and state is given.""" 156 | return self.call_api_post("users.setStatus", message=message, kwargs=kwargs) 157 | -------------------------------------------------------------------------------- /rocketchat_API/APISections/video_conferences.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.APISections.base import RocketChatBase 2 | 3 | 4 | class RocketChatVideConferences(RocketChatBase): 5 | def update_jitsi_timeout(self, room_id, **kwargs): 6 | """Updates the timeout of Jitsi video conference in a channel.""" 7 | return self.call_api_post( 8 | "video-conference/jitsi.update-timeout", roomId=room_id, kwargs=kwargs 9 | ) 10 | -------------------------------------------------------------------------------- /rocketchat_API/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadolg/rocketchat_API/3d2680ecccc2d251b673cf3a5e942d6d3f11b157/rocketchat_API/__init__.py -------------------------------------------------------------------------------- /rocketchat_API/rocketchat.py: -------------------------------------------------------------------------------- 1 | # -*-coding:utf-8-*- 2 | 3 | from rocketchat_API.APISections.assets import RocketChatAssets 4 | from rocketchat_API.APISections.banners import RocketChatBanners 5 | from rocketchat_API.APISections.channels import RocketChatChannels 6 | from rocketchat_API.APISections.chat import RocketChatChat 7 | from rocketchat_API.APISections.groups import RocketChatGroups 8 | from rocketchat_API.APISections.im import RocketChatIM 9 | from rocketchat_API.APISections.integrations import RocketChatIntegrations 10 | from rocketchat_API.APISections.invites import RocketChatInvites 11 | from rocketchat_API.APISections.licenses import RocketChatLicenses 12 | from rocketchat_API.APISections.livechat import RocketChatLivechat 13 | from rocketchat_API.APISections.miscellaneous import RocketChatMiscellaneous 14 | from rocketchat_API.APISections.permissions import RocketChatPermissions 15 | from rocketchat_API.APISections.roles import RocketChatRoles 16 | from rocketchat_API.APISections.rooms import RocketChatRooms 17 | from rocketchat_API.APISections.settings import RocketChatSettings 18 | from rocketchat_API.APISections.statistics import RocketChatStatistics 19 | from rocketchat_API.APISections.subscriptions import RocketChatSubscriptions 20 | from rocketchat_API.APISections.teams import RocketChatTeams 21 | from rocketchat_API.APISections.users import RocketChatUsers 22 | from rocketchat_API.APISections.video_conferences import RocketChatVideConferences 23 | 24 | 25 | class RocketChat( 26 | RocketChatUsers, 27 | RocketChatChat, 28 | RocketChatChannels, 29 | RocketChatGroups, 30 | RocketChatIM, 31 | RocketChatIntegrations, 32 | RocketChatStatistics, 33 | RocketChatMiscellaneous, 34 | RocketChatSettings, 35 | RocketChatRooms, 36 | RocketChatSubscriptions, 37 | RocketChatAssets, 38 | RocketChatPermissions, 39 | RocketChatInvites, 40 | RocketChatVideConferences, 41 | RocketChatLivechat, 42 | RocketChatTeams, 43 | RocketChatRoles, 44 | RocketChatBanners, 45 | RocketChatLicenses, 46 | ): 47 | pass 48 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | 4 | [tool:pytest] 5 | python_files = *.py 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*-coding:utf-8-*- 2 | 3 | from setuptools import setup 4 | 5 | setup( 6 | name="rocketchat_API", 7 | version="1.35.1", 8 | packages=[ 9 | "rocketchat_API", 10 | "rocketchat_API.APIExceptions", 11 | "rocketchat_API.APISections", 12 | ], 13 | url="https://github.com/jadolg/rocketchat_API", 14 | license="MIT", 15 | author="Jorge Alberto Díaz Orozco", 16 | author_email="diazorozcoj@gmail.com", 17 | description="Python API wrapper for Rocket.Chat", 18 | long_description=open("README.md", "r").read(), 19 | long_description_content_type="text/markdown", 20 | install_requires=("requests", "packaging"), 21 | ) 22 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadolg/rocketchat_API/3d2680ecccc2d251b673cf3a5e942d6d3f11b157/tests/__init__.py -------------------------------------------------------------------------------- /tests/assets/avatar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadolg/rocketchat_API/3d2680ecccc2d251b673cf3a5e942d6d3f11b157/tests/assets/avatar.png -------------------------------------------------------------------------------- /tests/assets/avatar.svg: -------------------------------------------------------------------------------- 1 | 2 | undefined 3 | -------------------------------------------------------------------------------- /tests/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadolg/rocketchat_API/3d2680ecccc2d251b673cf3a5e942d6d3f11b157/tests/assets/logo.png -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from rocketchat_API.rocketchat import RocketChat 4 | 5 | 6 | @pytest.fixture(scope="session") 7 | def rocket(): 8 | _rocket = RocketChat() 9 | 10 | return _rocket 11 | 12 | 13 | @pytest.fixture(scope="session") 14 | def create_user(rocket, name="user1", email="email@domain.com"): 15 | def _create_user(name=name, password="password", email=email): 16 | # create empty object, because Mock not included to python2 17 | user = type("test", (object,), {})() 18 | 19 | user.name = name 20 | user.password = password 21 | user.email = email 22 | 23 | rocket.users_register( 24 | email=user.email, name=user.name, password=user.password, username=user.name 25 | ) 26 | 27 | return user 28 | 29 | return _create_user 30 | 31 | 32 | @pytest.fixture(scope="session") 33 | def user(create_user): 34 | _user = create_user() 35 | 36 | return _user 37 | 38 | 39 | @pytest.fixture(scope="session") 40 | def logged_rocket(user): 41 | _rocket = RocketChat(user.name, user.password) 42 | 43 | return _rocket 44 | 45 | 46 | @pytest.fixture 47 | def secondary_user(logged_rocket): 48 | testuser = logged_rocket.users_info(username="secondary").json() 49 | if not testuser.get("success"): 50 | testuser = logged_rocket.users_create( 51 | "secondary@domain.com", "secondary", "password", "secondary" 52 | ).json() 53 | 54 | _testuser_id = testuser.get("user").get("_id") 55 | 56 | yield _testuser_id 57 | 58 | logged_rocket.users_delete(_testuser_id) 59 | 60 | 61 | @pytest.fixture 62 | def skip_if_no_license(logged_rocket): 63 | licenses_info = logged_rocket.licenses_info().json() 64 | if not licenses_info.get("success"): 65 | pytest.fail("License endpoint not available") 66 | if "license" in licenses_info and "license" in licenses_info.get("license"): 67 | return 68 | pytest.skip("No license available") 69 | -------------------------------------------------------------------------------- /tests/test_assets.py: -------------------------------------------------------------------------------- 1 | def test_assets_set_asset(logged_rocket): 2 | assets_set_asset = logged_rocket.assets_set_asset( 3 | asset_name="logo", file="tests/assets/logo.png" 4 | ).json() 5 | assert assets_set_asset.get("success") 6 | 7 | 8 | def test_assets_unset_asset(logged_rocket): 9 | assets_unset_asset = logged_rocket.assets_unset_asset(asset_name="logo").json() 10 | assert assets_unset_asset.get("success") 11 | -------------------------------------------------------------------------------- /tests/test_banners.py: -------------------------------------------------------------------------------- 1 | def test_banners(logged_rocket): 2 | banners = logged_rocket.banners(platform="web").json() 3 | assert banners.get("success") 4 | assert "banners" in banners 5 | -------------------------------------------------------------------------------- /tests/test_channels.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | import pytest 4 | from semver import Version 5 | 6 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 7 | 8 | 9 | @pytest.fixture(autouse=True) 10 | def add_owner(logged_rocket, user): 11 | """Add test user as owner in GENERAL channel in every test in this module""" 12 | logged_rocket.channels_add_owner("GENERAL", username=user.name) 13 | 14 | 15 | @pytest.fixture 16 | def testuser_id(logged_rocket): 17 | testuser = logged_rocket.users_info(username="testuser1").json() 18 | if not testuser.get("success"): 19 | testuser = logged_rocket.users_create( 20 | "testuser1@domain.com", "testuser1", "password", "testuser1" 21 | ).json() 22 | 23 | _testuser_id = testuser.get("user").get("_id") 24 | 25 | yield _testuser_id 26 | 27 | logged_rocket.users_delete(_testuser_id) 28 | 29 | 30 | def test_channels_list(logged_rocket): 31 | channels_list = logged_rocket.channels_list().json() 32 | assert channels_list.get("success") 33 | assert "channels" in channels_list 34 | 35 | 36 | def test_channels_list_joined(logged_rocket): 37 | channels_list_joined = logged_rocket.channels_list_joined().json() 38 | assert channels_list_joined.get("success") 39 | assert "channels" in channels_list_joined 40 | 41 | 42 | def test_channels_info(logged_rocket): 43 | channels_info = logged_rocket.channels_info(room_id="GENERAL").json() 44 | assert channels_info.get("success") 45 | assert "channel" in channels_info 46 | assert channels_info.get("channel").get("_id") == "GENERAL" 47 | 48 | channel_name = channels_info.get("channel").get("name") 49 | channels_info = logged_rocket.channels_info(channel=channel_name).json() 50 | assert channels_info.get("success") 51 | assert "channel" in channels_info 52 | assert channels_info.get("channel").get("_id") == "GENERAL" 53 | assert channels_info.get("channel").get("name") == channel_name 54 | 55 | with pytest.raises(RocketMissingParamException): 56 | logged_rocket.channels_info() 57 | 58 | 59 | def test_channels_history(logged_rocket): 60 | channels_history = logged_rocket.channels_history(room_id="GENERAL").json() 61 | assert channels_history.get("success") 62 | assert "messages" in channels_history 63 | 64 | 65 | def test_channels_add_all(logged_rocket): 66 | channels_add_all = logged_rocket.channels_add_all("GENERAL").json() 67 | assert channels_add_all.get("success") 68 | 69 | 70 | def test_channels_add_and_remove_moderator(logged_rocket): 71 | me = logged_rocket.me().json() 72 | channels_add_moderator = logged_rocket.channels_add_moderator( 73 | "GENERAL", me.get("_id") 74 | ).json() 75 | assert channels_add_moderator.get("success") 76 | channels_remove_moderator = logged_rocket.channels_remove_moderator( 77 | "GENERAL", me.get("_id") 78 | ).json() 79 | assert channels_remove_moderator.get("success") 80 | 81 | 82 | def test_channels_list_moderator(logged_rocket): 83 | channels_list_moderator = logged_rocket.channels_moderators( 84 | room_id="GENERAL" 85 | ).json() 86 | assert channels_list_moderator.get("success") 87 | channel_name = ( 88 | logged_rocket.channels_info(room_id="GENERAL").json().get("channel").get("name") 89 | ) 90 | channels_list_moderator_by_name = logged_rocket.channels_moderators( 91 | channel=channel_name 92 | ).json() 93 | assert channels_list_moderator_by_name.get("success") 94 | with pytest.raises(RocketMissingParamException): 95 | logged_rocket.channels_moderators() 96 | 97 | 98 | def test_channels_add_and_remove_owner(logged_rocket, testuser_id): 99 | channels_add_owner = logged_rocket.channels_add_owner( 100 | "GENERAL", user_id=testuser_id 101 | ).json() 102 | assert channels_add_owner.get("success"), channels_add_owner.get("error") 103 | another_owner_id = ( 104 | logged_rocket.users_info(username="user1").json().get("user").get("_id") 105 | ) 106 | logged_rocket.channels_add_owner("GENERAL", user_id=another_owner_id).json() 107 | channels_remove_owner = logged_rocket.channels_remove_owner( 108 | "GENERAL", user_id=testuser_id 109 | ).json() 110 | assert channels_remove_owner.get("success"), channels_remove_owner.get("error") 111 | 112 | with pytest.raises(RocketMissingParamException): 113 | logged_rocket.channels_add_owner(room_id="GENERAL") 114 | 115 | 116 | def test_channels_add_and_remove_leader(logged_rocket, testuser_id): 117 | channels_invite = logged_rocket.channels_invite("GENERAL", testuser_id).json() 118 | assert channels_invite.get("success") 119 | channels_add_leader = logged_rocket.channels_add_leader( 120 | "GENERAL", user_id=testuser_id 121 | ).json() 122 | assert channels_add_leader.get("success"), channels_add_leader.get("error") 123 | channels_remove_leader = logged_rocket.channels_remove_leader( 124 | "GENERAL", user_id=testuser_id 125 | ).json() 126 | assert channels_remove_leader.get("success"), channels_remove_leader.get("error") 127 | 128 | 129 | def test_channels_archive_unarchive(logged_rocket): 130 | channels_archive = logged_rocket.channels_archive("GENERAL").json() 131 | assert channels_archive.get("success") 132 | channels_unarchive = logged_rocket.channels_unarchive("GENERAL").json() 133 | assert channels_unarchive.get("success") 134 | 135 | 136 | def test_channels_close_open(logged_rocket): 137 | channels_close = logged_rocket.channels_close("GENERAL").json() 138 | assert channels_close.get("success") 139 | channels_open = logged_rocket.channels_open("GENERAL").json() 140 | assert channels_open.get("success") 141 | 142 | 143 | def test_channels_create_delete(logged_rocket): 144 | name = str(uuid.uuid1()) 145 | channels_create = logged_rocket.channels_create(name).json() 146 | assert channels_create.get("success") 147 | assert name == channels_create.get("channel").get("name") 148 | channels_delete = logged_rocket.channels_delete(channel=name).json() 149 | assert channels_delete.get("success") 150 | channels_create = logged_rocket.channels_create(name).json() 151 | assert channels_create.get("success") 152 | room_id = channels_create.get("channel").get("_id") 153 | channels_delete = logged_rocket.channels_delete(room_id=room_id).json() 154 | assert channels_delete.get("success") 155 | 156 | with pytest.raises(RocketMissingParamException): 157 | logged_rocket.channels_delete() 158 | 159 | 160 | def test_channels_get_integrations(logged_rocket): 161 | channels_get_integrations = logged_rocket.channels_get_integrations( 162 | room_id="GENERAL" 163 | ).json() 164 | assert channels_get_integrations.get("success") 165 | 166 | 167 | def test_channels_invite(logged_rocket, testuser_id): 168 | channels_invite = logged_rocket.channels_invite("GENERAL", testuser_id).json() 169 | assert channels_invite.get("success") 170 | 171 | 172 | def test_channels_join(logged_rocket, testuser_id): 173 | name = str(uuid.uuid1()) 174 | channels_create = logged_rocket.channels_create(name).json() 175 | assert ( 176 | logged_rocket.channels_invite( 177 | room_id=channels_create.get("channel").get("_id"), user_id=testuser_id 178 | ) 179 | .json() 180 | .get("success") 181 | ) 182 | 183 | assert ( 184 | logged_rocket.channels_add_owner( 185 | channels_create.get("channel").get("_id"), user_id=testuser_id 186 | ) 187 | .json() 188 | .get("success") 189 | ) 190 | 191 | join_code = str(uuid.uuid1()) 192 | channels_set_join_code = logged_rocket.channels_set_join_code( 193 | channels_create.get("channel").get("_id"), join_code 194 | ).json() 195 | assert channels_set_join_code.get("success") 196 | 197 | channels_leave = logged_rocket.channels_leave( 198 | channels_create.get("channel").get("_id") 199 | ).json() 200 | assert channels_leave.get("success") 201 | 202 | channels_join = logged_rocket.channels_join( 203 | channels_create.get("channel").get("_id"), join_code 204 | ).json() 205 | assert channels_join.get("success") 206 | 207 | 208 | def test_channels_kick(logged_rocket, testuser_id): 209 | channels_kick = logged_rocket.channels_kick("GENERAL", testuser_id).json() 210 | assert channels_kick.get("success") 211 | 212 | 213 | def test_channels_leave(logged_rocket, testuser_id): 214 | channels_leave = logged_rocket.channels_leave("GENERAL").json() 215 | assert not channels_leave.get("success") 216 | assert channels_leave.get("errorType") == "error-you-are-last-owner" 217 | 218 | name = str(uuid.uuid1()) 219 | channels_create = logged_rocket.channels_create(name).json() 220 | assert ( 221 | logged_rocket.channels_invite( 222 | room_id=channels_create.get("channel").get("_id"), user_id=testuser_id 223 | ) 224 | .json() 225 | .get("success") 226 | ) 227 | 228 | assert ( 229 | logged_rocket.channels_add_owner( 230 | channels_create.get("channel").get("_id"), user_id=testuser_id 231 | ) 232 | .json() 233 | .get("success") 234 | ) 235 | channels_leave = logged_rocket.channels_leave( 236 | channels_create.get("channel").get("_id") 237 | ).json() 238 | assert channels_leave.get("success") 239 | 240 | 241 | def test_channels_rename(logged_rocket): 242 | name = str(uuid.uuid1()) 243 | name2 = str(uuid.uuid1()) 244 | channels_create = logged_rocket.channels_create(name).json() 245 | channels_rename = logged_rocket.channels_rename( 246 | room_id=channels_create.get("channel").get("_id"), name=name2 247 | ).json() 248 | assert channels_rename.get("success") 249 | assert channels_rename.get("channel").get("name") == name2 250 | 251 | 252 | def test_channels_set_description(logged_rocket): 253 | description = str(uuid.uuid1()) 254 | channels_set_description = logged_rocket.channels_set_description( 255 | "GENERAL", description 256 | ).json() 257 | assert channels_set_description.get("success") 258 | assert ( 259 | channels_set_description.get("description") == description 260 | ), "Description does not match" 261 | 262 | 263 | def test_channels_set_join_code(logged_rocket): 264 | join_code = str(uuid.uuid1()) 265 | channels_set_join_code = logged_rocket.channels_set_join_code( 266 | "GENERAL", join_code 267 | ).json() 268 | assert channels_set_join_code.get("success") 269 | 270 | 271 | def test_channels_set_read_only(logged_rocket): 272 | channels_set_read_only = logged_rocket.channels_set_read_only( 273 | "GENERAL", True 274 | ).json() 275 | assert channels_set_read_only.get("success") 276 | channels_set_read_only = logged_rocket.channels_set_read_only( 277 | "GENERAL", False 278 | ).json() 279 | assert channels_set_read_only.get("success") 280 | 281 | 282 | def test_channels_set_topic(logged_rocket): 283 | topic = str(uuid.uuid1()) 284 | channels_set_topic = logged_rocket.channels_set_topic("GENERAL", topic).json() 285 | assert channels_set_topic.get("success") 286 | assert channels_set_topic.get("topic") == topic, "Topic does not match" 287 | 288 | 289 | def test_channels_set_type(logged_rocket): 290 | name = str(uuid.uuid1()) 291 | channels_create = logged_rocket.channels_create(name).json() 292 | assert channels_create.get("success") 293 | 294 | channels_set_type = logged_rocket.channels_set_type( 295 | channels_create.get("channel").get("_id"), "p" 296 | ).json() 297 | assert channels_set_type.get("success") 298 | assert channels_set_type.get("channel").get("t"), "p" 299 | 300 | channels_set_type = logged_rocket.channels_set_type( 301 | channels_create.get("channel").get("_id"), "c" 302 | ).json() 303 | # should fail because this is no more a channel 304 | assert not channels_set_type.get("success") 305 | 306 | 307 | def test_channels_set_announcement(logged_rocket): 308 | announcement = str(uuid.uuid1()) 309 | channels_set_announcement = logged_rocket.channels_set_announcement( 310 | "GENERAL", announcement 311 | ).json() 312 | assert channels_set_announcement.get("success") 313 | assert ( 314 | channels_set_announcement.get("announcement") == announcement 315 | ), "Topic does not match" 316 | 317 | 318 | def test_channels_set_custom_fields(logged_rocket): 319 | cf = {"key": "value"} 320 | channels_set_custom_fields = logged_rocket.channels_set_custom_fields( 321 | "GENERAL", cf 322 | ).json() 323 | assert channels_set_custom_fields.get("success") 324 | assert cf == channels_set_custom_fields["channel"]["customFields"] 325 | 326 | 327 | def test_channels_members(logged_rocket): 328 | channels_members = logged_rocket.channels_members(room_id="GENERAL").json() 329 | assert channels_members.get("success") 330 | channels_members = logged_rocket.channels_members(channel="general").json() 331 | assert channels_members.get("success") 332 | 333 | with pytest.raises(RocketMissingParamException): 334 | logged_rocket.channels_members() 335 | 336 | 337 | def test_channels_roles(logged_rocket): 338 | channels_roles = logged_rocket.channels_roles(room_id="GENERAL").json() 339 | assert channels_roles.get("success") 340 | assert channels_roles.get("roles") is not None 341 | channels_roles = logged_rocket.channels_roles(room_name="general").json() 342 | assert channels_roles.get("success") 343 | assert channels_roles.get("roles") is not None 344 | 345 | with pytest.raises(RocketMissingParamException): 346 | logged_rocket.channels_roles() 347 | 348 | 349 | def test_channels_files(logged_rocket): 350 | channels_files = logged_rocket.channels_files(room_id="GENERAL").json() 351 | assert channels_files.get("success") 352 | 353 | channels_files = logged_rocket.channels_files(room_name="general").json() 354 | assert channels_files.get("success") 355 | 356 | with pytest.raises(RocketMissingParamException): 357 | logged_rocket.channels_files() 358 | 359 | 360 | def test_channels_get_all_user_mentions_by_channel(logged_rocket): 361 | channels_get_all_user_mentions_by_channel = ( 362 | logged_rocket.channels_get_all_user_mentions_by_channel( 363 | room_id="GENERAL" 364 | ).json() 365 | ) 366 | assert channels_get_all_user_mentions_by_channel.get("success") 367 | 368 | 369 | def test_channels_counters(logged_rocket): 370 | channels_counters = logged_rocket.channels_counters(room_id="GENERAL").json() 371 | assert channels_counters.get("success") 372 | channels_counters_by_name = logged_rocket.channels_counters( 373 | room_name="general" 374 | ).json() 375 | assert channels_counters_by_name.get("success") 376 | with pytest.raises(RocketMissingParamException): 377 | logged_rocket.channels_counters() 378 | 379 | 380 | def test_channels_online(logged_rocket): 381 | version = logged_rocket.info().json().get("info").get("version") 382 | if version and Version.parse(version) >= Version.parse("7.0.0"): 383 | channels_online = logged_rocket.channels_online(_id="GENERAL").json() 384 | else: 385 | channels_online = logged_rocket.channels_online(query={"_id": "GENERAL"}).json() 386 | 387 | assert channels_online.get("success") 388 | assert len(channels_online.get("online")) >= 1 389 | 390 | with pytest.raises(RocketMissingParamException): 391 | logged_rocket.channels_online() 392 | 393 | 394 | def test_channels_set_default(logged_rocket): 395 | channels_set_default = logged_rocket.channels_set_default( 396 | room_id="GENERAL", default=False 397 | ).json() 398 | assert channels_set_default.get("success") 399 | assert channels_set_default.get("channel").get("default") is False 400 | channels_set_default = logged_rocket.channels_set_default( 401 | room_id="GENERAL", default=True 402 | ).json() 403 | assert channels_set_default.get("channel").get("default") 404 | -------------------------------------------------------------------------------- /tests/test_chat.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 4 | from tests.conftest import skip_if_no_license 5 | 6 | 7 | def test_chat_post_notext_message(logged_rocket): 8 | chat_post_message = logged_rocket.chat_post_message(None, channel="GENERAL").json() 9 | assert chat_post_message.get("channel") == "GENERAL" 10 | assert chat_post_message.get("message").get("msg") == "" 11 | assert chat_post_message.get("success") 12 | 13 | 14 | def test_chat_post_update_delete_message(logged_rocket): 15 | chat_post_message = logged_rocket.chat_post_message( 16 | "hello", 17 | channel="GENERAL", 18 | attachments=[ 19 | {"color": "#ff0000", "text": "there"}, 20 | ], 21 | ).json() 22 | assert chat_post_message.get("channel") == "GENERAL" 23 | assert chat_post_message.get("message").get("msg") == "hello" 24 | assert ( 25 | chat_post_message.get("message").get("attachments")[0].get("color") == "#ff0000" 26 | ) 27 | assert chat_post_message.get("message").get("attachments")[0].get("text") == "there" 28 | assert chat_post_message.get("success") 29 | 30 | with pytest.raises(RocketMissingParamException): 31 | logged_rocket.chat_post_message(text="text") 32 | 33 | msg_id = chat_post_message.get("message").get("_id") 34 | chat_get_message = logged_rocket.chat_get_message(msg_id=msg_id).json() 35 | assert chat_get_message.get("message").get("_id") == msg_id 36 | 37 | chat_update = logged_rocket.chat_update( 38 | room_id=chat_post_message.get("channel"), 39 | msg_id=chat_post_message.get("message").get("_id"), 40 | text="hello again", 41 | ).json() 42 | 43 | assert chat_update.get("message").get("msg") == "hello again" 44 | assert chat_update.get("success") 45 | 46 | chat_delete = logged_rocket.chat_delete( 47 | room_id=chat_post_message.get("channel"), 48 | msg_id=chat_post_message.get("message").get("_id"), 49 | ).json() 50 | assert chat_delete.get("success") 51 | 52 | 53 | def test_chat_send_notext_message(logged_rocket): 54 | chat_send_message = logged_rocket.chat_send_message({"rid": "GENERAL"}).json() 55 | assert chat_send_message.get("message").get("rid") == "GENERAL" 56 | assert chat_send_message.get("message").get("msg") == "" 57 | assert chat_send_message.get("success") 58 | with pytest.raises(RocketMissingParamException): 59 | logged_rocket.chat_send_message({"msg": "General Kenobi"}) 60 | 61 | 62 | def test_chat_send_custom_id_delete_message(logged_rocket): 63 | chat_send_message = logged_rocket.chat_send_message( 64 | {"rid": "GENERAL", "msg": "Hello There", "_id": "42"} 65 | ).json() 66 | assert chat_send_message.get("message").get("rid") == "GENERAL" 67 | assert chat_send_message.get("message").get("msg") == "Hello There" 68 | assert chat_send_message.get("message").get("_id") == "42" 69 | assert chat_send_message.get("success") 70 | chat_delete = logged_rocket.chat_delete( 71 | room_id=chat_send_message.get("message").get("rid"), 72 | msg_id=chat_send_message.get("message").get("_id"), 73 | ).json() 74 | assert chat_delete.get("success") 75 | 76 | 77 | def test_chat_post_react(logged_rocket): 78 | message_id = ( 79 | logged_rocket.chat_post_message("hello", channel="GENERAL") 80 | .json() 81 | .get("message") 82 | .get("_id") 83 | ) 84 | chat_react = logged_rocket.chat_react(msg_id=message_id).json() 85 | assert chat_react.get("success") 86 | 87 | 88 | def test_post_pin_unpin(logged_rocket): 89 | message_id = ( 90 | logged_rocket.chat_post_message("hello", channel="GENERAL") 91 | .json() 92 | .get("message") 93 | .get("_id") 94 | ) 95 | chat_pin_message = logged_rocket.chat_pin_message(message_id).json() 96 | assert chat_pin_message.get("success") 97 | assert chat_pin_message.get("message").get("t") == "message_pinned" 98 | 99 | chat_unpin_message = logged_rocket.chat_unpin_message(message_id).json() 100 | assert chat_unpin_message.get("success") 101 | 102 | 103 | def test_post_star_unstar_get_starred_messages(logged_rocket): 104 | message_id = ( 105 | logged_rocket.chat_post_message("hello", channel="GENERAL") 106 | .json() 107 | .get("message") 108 | .get("_id") 109 | ) 110 | 111 | chat_get_starred_messages = logged_rocket.chat_get_starred_messages( 112 | room_id="GENERAL" 113 | ).json() 114 | assert chat_get_starred_messages.get("success") 115 | assert chat_get_starred_messages.get("count") == 0 116 | assert chat_get_starred_messages.get("messages") == [] 117 | 118 | chat_star_message = logged_rocket.chat_star_message(message_id).json() 119 | assert chat_star_message.get("success") 120 | 121 | chat_get_starred_messages = logged_rocket.chat_get_starred_messages( 122 | room_id="GENERAL" 123 | ).json() 124 | assert chat_get_starred_messages.get("success") 125 | assert chat_get_starred_messages.get("count") == 1 126 | assert chat_get_starred_messages.get("messages") != [] 127 | 128 | chat_unstar_message = logged_rocket.chat_unstar_message(message_id).json() 129 | assert chat_unstar_message.get("success") 130 | 131 | 132 | def test_chat_search(logged_rocket): 133 | chat_search = logged_rocket.chat_search( 134 | room_id="GENERAL", search_text="hello" 135 | ).json() 136 | assert chat_search.get("success") 137 | 138 | 139 | def test_chat_get_message_read_receipts(logged_rocket, skip_if_no_license): 140 | message_id = ( 141 | logged_rocket.chat_post_message("hello", channel="GENERAL") 142 | .json() 143 | .get("message") 144 | .get("_id") 145 | ) 146 | chat_get_message_read_receipts = logged_rocket.chat_get_message_read_receipts( 147 | message_id=message_id 148 | ).json() 149 | assert chat_get_message_read_receipts.get("success") 150 | assert "receipts" in chat_get_message_read_receipts 151 | 152 | 153 | def test_chat_report_message(logged_rocket): 154 | message_id = ( 155 | logged_rocket.chat_post_message("hello", channel="GENERAL") 156 | .json() 157 | .get("message") 158 | .get("_id") 159 | ) 160 | chat_get_message_report_message = logged_rocket.chat_report_message( 161 | message_id=message_id, description="this makes me angry" 162 | ).json() 163 | assert chat_get_message_report_message.get("success") 164 | 165 | 166 | def test_chat_follow_message(logged_rocket): 167 | message_id = ( 168 | logged_rocket.chat_post_message("hello", channel="GENERAL") 169 | .json() 170 | .get("message") 171 | .get("_id") 172 | ) 173 | chat_get_message_follow_message = logged_rocket.chat_follow_message( 174 | mid=message_id 175 | ).json() 176 | assert chat_get_message_follow_message.get("success") 177 | 178 | 179 | # TODO: Rollback the room_id change if https://github.com/RocketChat/Rocket.Chat/issues/35923 works out 180 | def test_chat_get_thread_messages(logged_rocket): 181 | chat_post_message1 = logged_rocket.chat_post_message( 182 | "text1", 183 | channel="GENERAL", 184 | ).json() 185 | msg1_id = chat_post_message1.get("message").get("_id") 186 | 187 | chat_post_message2 = logged_rocket.chat_post_message( 188 | "text2", 189 | room_id="GENERAL", 190 | tmid=msg1_id, 191 | ).json() 192 | 193 | chat_post_message3 = logged_rocket.chat_post_message( 194 | "text3", 195 | room_id="GENERAL", 196 | tmid=msg1_id, 197 | ).json() 198 | 199 | chat_get_thread_messages = logged_rocket.chat_get_thread_messages( 200 | thread_msg_id=msg1_id, 201 | ).json() 202 | 203 | # check that correct number of messages is returned 204 | assert chat_get_thread_messages.get("count") == 2 205 | 206 | # check that text of messages are correct and messages are returned in order 207 | assert chat_get_thread_messages.get("messages")[0].get( 208 | "msg" 209 | ) == chat_post_message2.get("message").get("msg") 210 | assert chat_get_thread_messages.get("messages")[1].get( 211 | "msg" 212 | ) == chat_post_message3.get("message").get("msg") 213 | assert chat_get_thread_messages.get("success") 214 | 215 | 216 | def test_chat_get_mentioned_messages(logged_rocket): 217 | chat_post_message_with_mention = logged_rocket.chat_post_message( 218 | "hello @user1", 219 | channel="GENERAL", 220 | ).json() 221 | assert chat_post_message_with_mention.get("success") 222 | 223 | chat_get_mentioned_messages = logged_rocket.chat_get_mentioned_messages( 224 | room_id="GENERAL" 225 | ).json() 226 | assert chat_get_mentioned_messages.get("success") 227 | assert "messages" in chat_get_mentioned_messages 228 | assert len(chat_get_mentioned_messages.get("messages")) > 0 229 | assert chat_get_mentioned_messages.get("messages")[0].get("msg") == "hello @user1" 230 | -------------------------------------------------------------------------------- /tests/test_groups.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | import pytest 4 | 5 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 6 | 7 | 8 | @pytest.fixture 9 | def testuser_id(logged_rocket): 10 | testuser = logged_rocket.users_info(username="testuser1").json() 11 | if not testuser.get("success"): 12 | testuser = logged_rocket.users_create( 13 | "testuser1@domain.com", "testuser1", "password", "testuser1" 14 | ).json() 15 | 16 | _testuser_id = testuser.get("user").get("_id") 17 | 18 | yield _testuser_id 19 | 20 | logged_rocket.users_delete(_testuser_id) 21 | 22 | 23 | @pytest.fixture 24 | def test_group_name(): 25 | return str(uuid.uuid1()) 26 | 27 | 28 | @pytest.fixture 29 | def test_group_id(test_group_name, logged_rocket): 30 | _test_group_id = ( 31 | logged_rocket.groups_create(test_group_name).json().get("group").get("_id") 32 | ) 33 | 34 | return _test_group_id 35 | 36 | 37 | def test_groups_list_all(logged_rocket): 38 | groups_list = logged_rocket.groups_list_all().json() 39 | assert groups_list.get("success") 40 | assert "groups" in groups_list 41 | 42 | 43 | def test_groups_list(logged_rocket): 44 | groups_list = logged_rocket.groups_list().json() 45 | assert groups_list.get("success") 46 | assert "groups" in groups_list 47 | 48 | 49 | def test_groups_info(logged_rocket, test_group_name, test_group_id): 50 | groups_info_by_id = logged_rocket.groups_info(room_id=test_group_id).json() 51 | assert groups_info_by_id.get("success") 52 | assert "group" in groups_info_by_id 53 | assert groups_info_by_id.get("group").get("_id") == test_group_id 54 | 55 | groups_info_by_name = logged_rocket.groups_info(room_name=test_group_name).json() 56 | assert groups_info_by_name.get("success") 57 | assert "group" in groups_info_by_name 58 | assert groups_info_by_name.get("group").get("_id") == test_group_id 59 | 60 | with pytest.raises(RocketMissingParamException): 61 | logged_rocket.groups_info() 62 | 63 | 64 | def test_groups_history(logged_rocket, test_group_id): 65 | groups_history = logged_rocket.groups_history(room_id=test_group_id).json() 66 | assert groups_history.get("success") 67 | assert "messages" in groups_history 68 | 69 | 70 | def test_groups_add_and_remove_moderator(logged_rocket, test_group_id): 71 | me = logged_rocket.me().json() 72 | groups_add_moderator = logged_rocket.groups_add_moderator( 73 | test_group_id, me.get("_id") 74 | ).json() 75 | assert groups_add_moderator.get("success") 76 | groups_remove_moderator = logged_rocket.groups_remove_moderator( 77 | test_group_id, me.get("_id") 78 | ).json() 79 | assert groups_remove_moderator.get("success") 80 | 81 | 82 | def test_groups_add_and_remove_leader(logged_rocket, test_group_id): 83 | me = logged_rocket.me().json() 84 | groups_add_leader = logged_rocket.groups_add_leader( 85 | test_group_id, me.get("_id") 86 | ).json() 87 | assert groups_add_leader.get("success") 88 | groups_remove_leader = logged_rocket.groups_remove_leader( 89 | test_group_id, me.get("_id") 90 | ).json() 91 | assert groups_remove_leader.get("success") 92 | 93 | 94 | def test_groups_list_moderator(logged_rocket, test_group_name, test_group_id): 95 | groups_list_moderator = logged_rocket.groups_moderators( 96 | room_id=test_group_id 97 | ).json() 98 | assert groups_list_moderator.get("success") 99 | groups_list_moderator_by_name = logged_rocket.groups_moderators( 100 | group=test_group_name 101 | ).json() 102 | assert groups_list_moderator_by_name.get("success") 103 | with pytest.raises(RocketMissingParamException): 104 | logged_rocket.groups_moderators() 105 | 106 | 107 | def test_groups_add_and_remove_owner(logged_rocket, testuser_id, test_group_id): 108 | logged_rocket.groups_invite(test_group_id, testuser_id) 109 | groups_add_owner = logged_rocket.groups_add_owner( 110 | test_group_id, user_id=testuser_id 111 | ).json() 112 | assert groups_add_owner.get("success"), groups_add_owner.get("error") 113 | 114 | groups_remove_owner = logged_rocket.groups_remove_owner( 115 | test_group_id, user_id=testuser_id 116 | ).json() 117 | assert groups_remove_owner.get("success"), groups_remove_owner.get("error") 118 | 119 | 120 | def test_groups_archive_unarchive(logged_rocket, test_group_id): 121 | groups_archive = logged_rocket.groups_archive(test_group_id).json() 122 | assert groups_archive.get("success") 123 | groups_unarchive = logged_rocket.groups_unarchive(test_group_id).json() 124 | assert groups_unarchive.get("success") 125 | 126 | 127 | def test_groups_close_open(logged_rocket, test_group_id): 128 | groups_close = logged_rocket.groups_close(test_group_id).json() 129 | assert groups_close.get("success") 130 | groups_open = logged_rocket.groups_open(test_group_id).json() 131 | assert groups_open.get("success") 132 | 133 | 134 | def test_groups_create_delete(logged_rocket): 135 | name = str(uuid.uuid1()) 136 | groups_create = logged_rocket.groups_create(name).json() 137 | assert groups_create.get("success") 138 | assert name == groups_create.get("group").get("name") 139 | groups_delete = logged_rocket.groups_delete(group=name).json() 140 | assert groups_delete.get("success") 141 | groups_create = logged_rocket.groups_create(name).json() 142 | assert groups_create.get("success") 143 | room_id = groups_create.get("group").get("_id") 144 | groups_delete = logged_rocket.groups_delete(room_id=room_id).json() 145 | assert groups_delete.get("success") 146 | 147 | with pytest.raises(RocketMissingParamException): 148 | logged_rocket.groups_delete() 149 | 150 | 151 | def test_groups_get_integrations(logged_rocket, test_group_id): 152 | groups_get_integrations = logged_rocket.groups_get_integrations( 153 | room_id=test_group_id 154 | ).json() 155 | assert groups_get_integrations.get("success") 156 | 157 | 158 | def test_groups_invite(logged_rocket, testuser_id, test_group_id): 159 | groups_invite = logged_rocket.groups_invite(test_group_id, testuser_id).json() 160 | assert groups_invite.get("success") 161 | 162 | 163 | def test_groups_kick(logged_rocket, testuser_id): 164 | id_group_created = ( 165 | logged_rocket.groups_create(str(uuid.uuid1())).json().get("group").get("_id") 166 | ) 167 | groups_invite = logged_rocket.groups_invite(id_group_created, testuser_id).json() 168 | assert groups_invite.get("success") 169 | groups_kick = logged_rocket.groups_kick(id_group_created, testuser_id).json() 170 | assert groups_kick.get("success") 171 | 172 | 173 | def test_groups_leave(logged_rocket, test_group_id, testuser_id): 174 | groups_leave = logged_rocket.groups_leave(test_group_id).json() 175 | assert not groups_leave.get("success") 176 | assert groups_leave.get("errorType") == "error-you-are-last-owner" 177 | 178 | name = str(uuid.uuid1()) 179 | groups_create = logged_rocket.groups_create(name).json() 180 | logged_rocket.groups_invite( 181 | room_id=groups_create.get("group").get("_id"), user_id=testuser_id 182 | ) 183 | logged_rocket.groups_add_owner( 184 | groups_create.get("group").get("_id"), user_id=testuser_id 185 | ).json() 186 | groups_leave = logged_rocket.groups_leave( 187 | groups_create.get("group").get("_id") 188 | ).json() 189 | assert groups_leave.get("success") 190 | 191 | 192 | def test_groups_rename(logged_rocket): 193 | name = str(uuid.uuid1()) 194 | name2 = str(uuid.uuid1()) 195 | groups_create = logged_rocket.groups_create(name).json() 196 | groups_rename = logged_rocket.groups_rename( 197 | room_id=groups_create.get("group").get("_id"), name=name2 198 | ).json() 199 | assert groups_rename.get("success") 200 | assert groups_rename.get("group").get("name") == name2 201 | 202 | 203 | def test_groups_set_announcement(logged_rocket, test_group_id): 204 | announcement = str(uuid.uuid1()) 205 | groups_set_announcement = logged_rocket.groups_set_announcement( 206 | test_group_id, announcement 207 | ).json() 208 | assert groups_set_announcement.get("success") 209 | assert ( 210 | groups_set_announcement.get("announcement") == announcement 211 | ), "Announcement does not match" 212 | 213 | 214 | def test_groups_set_description(logged_rocket, test_group_id): 215 | description = str(uuid.uuid1()) 216 | groups_set_description = logged_rocket.groups_set_description( 217 | test_group_id, description 218 | ).json() 219 | assert groups_set_description.get("success") 220 | assert ( 221 | groups_set_description.get("description") == description 222 | ), "Description does not match" 223 | 224 | 225 | def test_groups_set_read_only(logged_rocket, test_group_id): 226 | groups_set_read_only = logged_rocket.groups_set_read_only( 227 | test_group_id, True 228 | ).json() 229 | assert groups_set_read_only.get("success") 230 | groups_set_read_only = logged_rocket.groups_set_read_only( 231 | test_group_id, False 232 | ).json() 233 | assert groups_set_read_only.get("success") 234 | 235 | 236 | def test_groups_set_topic(logged_rocket, test_group_id): 237 | topic = str(uuid.uuid1()) 238 | groups_set_topic = logged_rocket.groups_set_topic(test_group_id, topic).json() 239 | assert groups_set_topic.get("success") 240 | assert groups_set_topic.get("topic") == topic, "Topic does not match" 241 | 242 | 243 | def test_groups_set_type(logged_rocket): 244 | name = str(uuid.uuid1()) 245 | groups_create = logged_rocket.groups_create(name).json() 246 | assert groups_create.get("success") 247 | 248 | groups_set_type = logged_rocket.groups_set_type( 249 | groups_create.get("group").get("_id"), "c" 250 | ).json() 251 | assert groups_set_type.get("success") 252 | assert groups_set_type.get("group").get("t"), "p" 253 | 254 | groups_set_type = logged_rocket.groups_set_type( 255 | groups_create.get("group").get("_id"), "p" 256 | ).json() 257 | # should fail because this is no more a group 258 | assert not groups_set_type.get("success") 259 | 260 | 261 | def test_groups_set_custom_fields( 262 | logged_rocket, 263 | test_group_id, 264 | test_group_name, 265 | ): 266 | field_name = str(uuid.uuid1()) 267 | field_value = str(uuid.uuid1()) 268 | custom_fields = {field_name: field_value} 269 | 270 | groups_set_custom_fields = logged_rocket.groups_set_custom_fields( 271 | custom_fields, room_id=test_group_id 272 | ).json() 273 | assert groups_set_custom_fields.get("success") 274 | assert ( 275 | groups_set_custom_fields.get("group").get("customFields") == custom_fields 276 | ), "Custom fields do not match" 277 | 278 | groups_set_custom_fields = logged_rocket.groups_set_custom_fields( 279 | custom_fields, room_name=test_group_name 280 | ).json() 281 | assert groups_set_custom_fields.get("success") 282 | assert ( 283 | groups_set_custom_fields.get("group").get("customFields") == custom_fields 284 | ), "Custom fields do not match" 285 | 286 | with pytest.raises(RocketMissingParamException): 287 | logged_rocket.groups_set_custom_fields(custom_fields) 288 | 289 | 290 | def test_groups_members(logged_rocket, test_group_name, test_group_id): 291 | groups_members = logged_rocket.groups_members(room_id=test_group_id).json() 292 | assert groups_members.get("success") 293 | groups_members = logged_rocket.groups_members(group=test_group_name).json() 294 | assert groups_members.get("success") 295 | 296 | with pytest.raises(RocketMissingParamException): 297 | logged_rocket.groups_members() 298 | 299 | 300 | def test_groups_roles(logged_rocket): 301 | name = str(uuid.uuid1()) 302 | groups_create = logged_rocket.groups_create(name).json() 303 | assert groups_create.get("success") 304 | 305 | groups_roles = logged_rocket.groups_roles( 306 | room_id=groups_create.get("group").get("_id") 307 | ).json() 308 | assert groups_roles.get("success") 309 | assert groups_roles.get("roles") is not None 310 | 311 | groups_roles = logged_rocket.groups_roles(room_name=name).json() 312 | assert groups_roles.get("success") 313 | assert groups_roles.get("roles") is not None 314 | 315 | with pytest.raises(RocketMissingParamException): 316 | logged_rocket.groups_roles() 317 | 318 | 319 | def test_groups_files(logged_rocket): 320 | name = str(uuid.uuid1()) 321 | groups_create = logged_rocket.groups_create(name).json() 322 | assert groups_create.get("success") 323 | 324 | groups_files = logged_rocket.groups_files( 325 | room_id=groups_create.get("group").get("_id") 326 | ).json() 327 | assert groups_files.get("success") 328 | 329 | groups_files = logged_rocket.groups_files(room_name=name).json() 330 | assert groups_files.get("success") 331 | 332 | with pytest.raises(RocketMissingParamException): 333 | logged_rocket.groups_files() 334 | -------------------------------------------------------------------------------- /tests/test_ims.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 4 | 5 | 6 | @pytest.fixture(scope="module") 7 | def recipient_user(create_user): 8 | _recipient = create_user(name="user1") 9 | 10 | return _recipient.name 11 | 12 | 13 | @pytest.fixture(scope="module") 14 | def recipient_user3(create_user): 15 | _recipient = create_user(name="user3", email="anotherone@domain.com") 16 | 17 | return _recipient.name 18 | 19 | 20 | @pytest.fixture(scope="module") 21 | def recipient_user2(create_user): 22 | _recipient = create_user(name="user2", email="another@domain.com") 23 | 24 | return _recipient.name 25 | 26 | 27 | def test_im_create_multiple(logged_rocket, recipient_user3, recipient_user2): 28 | im_create = logged_rocket.im_create_multiple( 29 | [recipient_user3, recipient_user2] 30 | ).json() 31 | assert im_create.get("success") 32 | room_id = im_create.get("room").get("_id") 33 | im_send = logged_rocket.chat_post_message( 34 | room_id=room_id, text="Das ist eine Testnachricht" 35 | ).json() 36 | assert im_send.get("success") 37 | 38 | 39 | def test_im_create(logged_rocket, recipient_user): 40 | im_create = logged_rocket.im_create(recipient_user).json() 41 | assert im_create.get("success") 42 | 43 | 44 | def test_im_send(logged_rocket, recipient_user): 45 | im_create = logged_rocket.im_create(recipient_user).json() 46 | room_id = im_create.get("room").get("_id") 47 | im_send = logged_rocket.chat_post_message(room_id=room_id, text="test").json() 48 | assert im_send.get("success") 49 | 50 | 51 | def test_im_send_no_text(logged_rocket, recipient_user): 52 | im_create = logged_rocket.im_create(recipient_user).json() 53 | room_id = im_create.get("room").get("_id") 54 | im_send = logged_rocket.chat_post_message(room_id=room_id, text=None).json() 55 | assert im_send.get("success") 56 | 57 | 58 | def test_im_list(logged_rocket, recipient_user): 59 | logged_rocket.im_create(recipient_user) 60 | im_list = logged_rocket.im_list().json() 61 | assert im_list.get("success") 62 | 63 | 64 | def test_im_close_open(logged_rocket, recipient_user): 65 | im_create = logged_rocket.im_create(recipient_user).json() 66 | room_id = im_create.get("room").get("_id") 67 | im_close = logged_rocket.im_close(room_id).json() 68 | assert im_close.get("success") 69 | im_open = logged_rocket.im_open(room_id).json() 70 | assert im_open.get("success") 71 | 72 | 73 | def test_im_set_topic(logged_rocket, recipient_user): 74 | topic = "this is my new topic" 75 | im_create = logged_rocket.im_create(recipient_user).json() 76 | room_id = im_create.get("room").get("_id") 77 | im_set_topic = logged_rocket.im_set_topic(room_id, topic).json() 78 | assert im_set_topic.get("success") 79 | assert im_set_topic.get("topic") == topic, "Topic set does not match topic returned" 80 | 81 | 82 | def test_im_list_everyone(logged_rocket): 83 | im_list_everyone = logged_rocket.im_list_everyone().json() 84 | assert im_list_everyone.get("success") 85 | 86 | 87 | def test_im_history(logged_rocket, recipient_user): 88 | im_create = logged_rocket.im_create(recipient_user).json() 89 | room_id = im_create.get("room").get("_id") 90 | im_history = logged_rocket.im_history(room_id).json() 91 | assert im_history.get("success") 92 | 93 | 94 | def test_im_members(logged_rocket, recipient_user): 95 | im_create = logged_rocket.im_create(recipient_user).json() 96 | room_id = im_create.get("room").get("_id") 97 | im_members = logged_rocket.im_members(room_id=room_id).json() 98 | assert im_members.get("success") 99 | assert im_members.get("members")[0].get("name") == "user1" 100 | 101 | 102 | def test_im_messages(logged_rocket, recipient_user): 103 | im_message = logged_rocket.im_messages(username=recipient_user).json() 104 | assert im_message.get("success") 105 | im_create = logged_rocket.im_create(recipient_user).json() 106 | room_id = im_create.get("room").get("_id") 107 | im_message = logged_rocket.im_messages(room_id=room_id).json() 108 | assert im_message.get("success") 109 | 110 | with pytest.raises(RocketMissingParamException): 111 | logged_rocket.im_messages() 112 | 113 | 114 | def test_im_messages_others(logged_rocket, recipient_user): 115 | # ToDo: Try changing the access configuration so endpoint can be successfully tested 116 | im_create = logged_rocket.im_create(recipient_user).json() 117 | room_id = im_create.get("room").get("_id") 118 | im_messages_others = logged_rocket.im_messages_others(room_id).json() 119 | assert not im_messages_others.get("success") 120 | 121 | 122 | def test_im_files(logged_rocket, recipient_user): 123 | im_create = logged_rocket.im_create(recipient_user).json() 124 | assert im_create.get("success") 125 | 126 | im_files = logged_rocket.im_files(room_id=im_create.get("room").get("_id")).json() 127 | assert im_files.get("success") 128 | 129 | im_files = logged_rocket.im_files(user_name=recipient_user).json() 130 | assert im_files.get("success") 131 | 132 | with pytest.raises(RocketMissingParamException): 133 | logged_rocket.im_files() 134 | 135 | 136 | def test_im_counters(logged_rocket, recipient_user): 137 | im_create = logged_rocket.im_create(recipient_user).json() 138 | assert im_create.get("success") 139 | 140 | im_counters = logged_rocket.im_counters( 141 | room_id=im_create.get("room").get("_id") 142 | ).json() 143 | assert im_counters.get("success") 144 | 145 | im_counters = logged_rocket.im_counters( 146 | room_id=im_create.get("room").get("_id"), 147 | user_name=logged_rocket.me().json().get("_id"), 148 | ).json() 149 | assert im_counters.get("success") 150 | -------------------------------------------------------------------------------- /tests/test_integrations.py: -------------------------------------------------------------------------------- 1 | from uuid import uuid1 2 | 3 | import pytest 4 | 5 | from rocketchat_API.APIExceptions.RocketExceptions import ( 6 | RocketUnsuportedIntegrationType, 7 | ) 8 | 9 | GENERAL_CHANNEL = "#general" 10 | 11 | 12 | @pytest.fixture(autouse=True) 13 | def integrations_create_webhook_incoming(logged_rocket): 14 | return logged_rocket.integrations_create( 15 | integrations_type="webhook-incoming", 16 | name=str(uuid1()), 17 | enabled=True, 18 | username=logged_rocket.me().json().get("username"), 19 | channel=GENERAL_CHANNEL, 20 | script_enabled=False, 21 | ).json() 22 | 23 | 24 | def test_integrations_create(integrations_create_webhook_incoming, logged_rocket): 25 | integration_webhook_incoming = integrations_create_webhook_incoming 26 | assert integration_webhook_incoming.get("success") 27 | 28 | integration_webhook_outgoing = logged_rocket.integrations_create( 29 | integrations_type="webhook-outgoing", 30 | name=str(uuid1()), 31 | enabled=True, 32 | event="sendMessage", 33 | username=logged_rocket.me().json().get("username"), 34 | urls=["https://example.com/fake"], 35 | channel=GENERAL_CHANNEL, 36 | script_enabled=False, 37 | ).json() 38 | assert integration_webhook_outgoing.get("success") 39 | 40 | 41 | def test_integrations_get(integrations_create_webhook_incoming, logged_rocket): 42 | integration_id = integrations_create_webhook_incoming.get("integration").get("_id") 43 | assert logged_rocket.integrations_get(integration_id).json().get("success") 44 | 45 | 46 | def test_integrations_history(integrations_create_webhook_incoming, logged_rocket): 47 | integration_id = integrations_create_webhook_incoming.get("integration").get("_id") 48 | assert logged_rocket.integrations_history(integration_id).json().get("success") 49 | 50 | 51 | def test_integrations_list(logged_rocket): 52 | assert logged_rocket.integrations_list().json().get("success") 53 | 54 | 55 | def test_integrations_remove(integrations_create_webhook_incoming, logged_rocket): 56 | integration = integrations_create_webhook_incoming.get("integration") 57 | assert ( 58 | logged_rocket.integrations_remove( 59 | integration.get("type"), integration.get("_id") 60 | ) 61 | .json() 62 | .get("success") 63 | ) 64 | 65 | 66 | def test_integrations_update(integrations_create_webhook_incoming, logged_rocket): 67 | integration_id = integrations_create_webhook_incoming.get("integration").get("_id") 68 | assert ( 69 | logged_rocket.integrations_update( 70 | integrations_type="webhook-incoming", 71 | name=str(uuid1()), 72 | enabled=False, 73 | username=logged_rocket.me().json().get("username"), 74 | channel=GENERAL_CHANNEL, 75 | script_enabled=False, 76 | integration_id=integration_id, 77 | ) 78 | .json() 79 | .get("success") 80 | ) 81 | 82 | 83 | def test_integration_invalid_type(logged_rocket): 84 | with pytest.raises(RocketUnsuportedIntegrationType): 85 | logged_rocket.integrations_create( 86 | integrations_type="not-valid-type", 87 | name=str(uuid1()), 88 | enabled=True, 89 | username=logged_rocket.me().json().get("username"), 90 | channel=GENERAL_CHANNEL, 91 | script_enabled=False, 92 | ) 93 | -------------------------------------------------------------------------------- /tests/test_invites.py: -------------------------------------------------------------------------------- 1 | def test_find_or_create_invite(logged_rocket): 2 | rid = "GENERAL" 3 | find_or_create_invite = logged_rocket.find_or_create_invite( 4 | rid=rid, days=7, max_uses=5 5 | ).json() 6 | assert find_or_create_invite.get("success") 7 | assert find_or_create_invite.get("days") == 7 8 | assert find_or_create_invite.get("maxUses") == 5 9 | 10 | 11 | def test_list_invites(logged_rocket): 12 | list_invites = logged_rocket.list_invites().json() 13 | assert isinstance(list_invites, list) 14 | -------------------------------------------------------------------------------- /tests/test_licenses.py: -------------------------------------------------------------------------------- 1 | def test_licenses_info(logged_rocket): 2 | licenses_info = logged_rocket.licenses_info().json() 3 | assert licenses_info.get("success") 4 | -------------------------------------------------------------------------------- /tests/test_livechat.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | 4 | def test_livechat_rooms(logged_rocket): 5 | livechat_rooms = logged_rocket.livechat_rooms().json() 6 | assert livechat_rooms.get("success") 7 | assert "rooms" in livechat_rooms 8 | 9 | 10 | def test_livechat_inquiries_list(logged_rocket): 11 | livechat_inquiries_list = logged_rocket.livechat_inquiries_list().json() 12 | assert livechat_inquiries_list.get("success") 13 | assert "inquiries" in livechat_inquiries_list 14 | 15 | 16 | def test_livechat_inquiries_take(logged_rocket): 17 | # TODO: Find a way of creating an inquiry to be able to properly test this method 18 | livechat_inquiries_take = logged_rocket.livechat_inquiries_take( 19 | inquiry_id="NotARealThing" 20 | ).json() 21 | assert not livechat_inquiries_take.get("success") 22 | assert "Inquiry not found [error-not-found]" == livechat_inquiries_take.get("error") 23 | 24 | 25 | def test_livechat_users(logged_rocket): 26 | livechat_users = logged_rocket.livechat_get_users(user_type="agent").json() 27 | assert livechat_users.get("success") 28 | assert "users" in livechat_users 29 | assert len(livechat_users.get("users")) == 0 30 | 31 | username = logged_rocket.me().json().get("username") 32 | new_user = logged_rocket.livechat_create_user( 33 | user_type="agent", username=username 34 | ).json() 35 | assert new_user.get("success") 36 | assert "user" in new_user 37 | assert new_user.get("user").get("username") == username 38 | 39 | livechat_users = logged_rocket.livechat_get_users(user_type="agent").json() 40 | assert len(livechat_users.get("users")) == 1 41 | 42 | livechat_get_user = logged_rocket.livechat_get_user( 43 | user_type="agent", user_id=new_user.get("user").get("_id") 44 | ).json() 45 | assert livechat_get_user.get("success") 46 | assert new_user.get("user").get("username") == username 47 | 48 | livechat_delete_user = logged_rocket.livechat_delete_user( 49 | user_type="agent", user_id=new_user.get("user").get("_id") 50 | ).json() 51 | assert livechat_delete_user.get("success") 52 | 53 | livechat_users = logged_rocket.livechat_get_users(user_type="agent").json() 54 | assert len(livechat_users.get("users")) == 0 55 | 56 | 57 | def test_livechat_visitor_minimal(logged_rocket): 58 | token = str(uuid.uuid1()) 59 | new_visitor = logged_rocket.livechat_register_visitor(token=token).json() 60 | assert new_visitor.get("success") 61 | assert "visitor" in new_visitor 62 | 63 | 64 | def test_livechat_visitor_room_and_message(logged_rocket): 65 | token = str(uuid.uuid1()) 66 | name = str(uuid.uuid1()) 67 | new_visitor = logged_rocket.livechat_register_visitor( 68 | token=token, visitor={"username": name} 69 | ).json() 70 | assert new_visitor.get("success") 71 | assert "visitor" in new_visitor 72 | 73 | get_visitor = logged_rocket.livechat_get_visitor(token=token).json() 74 | assert get_visitor.get("success") 75 | assert name == get_visitor.get("visitor").get("username") 76 | 77 | # We need to create a livechat agent and set the user online in order to be able to get a room 78 | username = logged_rocket.me().json().get("username") 79 | new_user = logged_rocket.livechat_create_user( 80 | user_type="agent", username=username 81 | ).json() 82 | assert new_user.get("success") 83 | users_set_status = logged_rocket.users_set_status( 84 | message="working on it", status="online" 85 | ).json() 86 | assert users_set_status.get("success") 87 | 88 | livechat_room = logged_rocket.livechat_room(token=token).json() 89 | assert livechat_room.get("success") 90 | assert "room" in livechat_room 91 | 92 | livechat_message = logged_rocket.livechat_message( 93 | token=token, 94 | rid=livechat_room.get("room").get("_id"), 95 | msg="may the 4th be with you", 96 | ).json() 97 | 98 | assert livechat_message.get("success") 99 | assert "message" in livechat_message 100 | assert livechat_message.get("message").get("msg") == "may the 4th be with you" 101 | 102 | livechat_messages_history = logged_rocket.livechat_messages_history( 103 | rid=livechat_room.get("room").get("_id"), token=token 104 | ).json() 105 | assert livechat_messages_history.get("success") 106 | assert "messages" in livechat_messages_history 107 | 108 | livechat_delete_user = logged_rocket.livechat_delete_user( 109 | user_type="agent", user_id=new_user.get("user").get("_id") 110 | ).json() 111 | assert livechat_delete_user.get("success") 112 | -------------------------------------------------------------------------------- /tests/test_permissions.py: -------------------------------------------------------------------------------- 1 | def test_permissions_list_all(logged_rocket): 2 | permissions_list_all = logged_rocket.permissions_list_all().json() 3 | assert permissions_list_all.get("success") 4 | assert "update" in permissions_list_all 5 | assert "remove" in permissions_list_all 6 | 7 | 8 | # pylint: disable=invalid-name 9 | def test_permissions_list_all_with_updatedSince(logged_rocket): 10 | permissions_list_all = logged_rocket.permissions_list_all( 11 | updatedSince="2017-11-25T15:08:17.248Z" 12 | ).json() 13 | assert permissions_list_all.get("success") 14 | assert "update" in permissions_list_all 15 | assert "remove" in permissions_list_all 16 | 17 | 18 | def test_permissions_update(logged_rocket): 19 | def get_updated_roles(permission, permissions_update): 20 | for permission_update in permissions_update: 21 | if permission.get("_id", "A") == permission_update.get("_id", "B"): 22 | return permission_update.get("roles") 23 | 24 | # Permission not found 25 | raise ValueError 26 | 27 | def check_update(permissions, permissions_update): 28 | for permission in permissions: 29 | try: 30 | if permission.get("roles") != get_updated_roles( 31 | permission, permissions_update 32 | ): 33 | return False 34 | except ValueError: 35 | return False 36 | 37 | return True 38 | 39 | permissions = [ 40 | {"_id": "access-permissions", "roles": ["admin", "bot"]}, 41 | {"_id": "add-user-to-any-c-room", "roles": ["admin"]}, 42 | ] 43 | permissions_update = logged_rocket.permissions_update( 44 | permissions=permissions 45 | ).json() 46 | assert permissions_update.get("success") 47 | assert check_update(permissions, permissions_update.get("permissions", [])) 48 | 49 | permissions_wrong_id = [ 50 | {"_id": "wrong-id", "roles": ["admin", "bot"]}, 51 | ] 52 | assert ( 53 | check_update(permissions_wrong_id, permissions_update.get("permissions", [])) 54 | is False 55 | ) 56 | 57 | permissions_wrong_roles = [ 58 | {"_id": "access-permissions", "roles": ["bot"]}, 59 | ] 60 | assert ( 61 | check_update(permissions_wrong_roles, permissions_update.get("permissions", [])) 62 | is False 63 | ) 64 | -------------------------------------------------------------------------------- /tests/test_roles.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | 4 | def test_roles_list(logged_rocket): 5 | roles_list = logged_rocket.roles_list().json() 6 | assert roles_list.get("success") 7 | assert len(roles_list.get("roles")) > 0 8 | 9 | 10 | def test_roles_create(logged_rocket, skip_if_no_license): 11 | name = str(uuid.uuid1()) 12 | roles_create = logged_rocket.roles_create( 13 | name=name, scope="Subscriptions", description="a test role" 14 | ).json() 15 | assert roles_create.get("success") 16 | assert roles_create.get("role").get("name") == name 17 | assert roles_create.get("role").get("scope") == "Subscriptions" 18 | assert roles_create.get("role").get("description") == "a test role" 19 | 20 | 21 | def test_roles_add_remove_user_to_from_role(logged_rocket): 22 | me = logged_rocket.me().json() 23 | roles_add_user_to_role = logged_rocket.roles_add_user_to_role( 24 | role_name="auditor-log", username=me.get("username") 25 | ).json() 26 | assert roles_add_user_to_role.get("success") 27 | assert roles_add_user_to_role.get("role").get("name") == "auditor-log" 28 | 29 | roles_remove_user_from_role = logged_rocket.roles_remove_user_from_role( 30 | role_name="auditor-log", username=me.get("username") 31 | ).json() 32 | 33 | assert roles_remove_user_from_role.get("success") 34 | 35 | 36 | def test_roles_get_users_in_role(logged_rocket): 37 | roles_get_users_in_role = logged_rocket.roles_get_users_in_role( 38 | role="owner", roomId="GENERAL" 39 | ).json() 40 | 41 | assert roles_get_users_in_role.get("success") 42 | assert roles_get_users_in_role.get("users")[0].get("name") == "user1" 43 | 44 | 45 | def test_roles_sync(logged_rocket): 46 | roles_sync = logged_rocket.roles_sync( 47 | updated_since="2017-11-25T15:08:17.248Z" 48 | ).json() 49 | assert roles_sync.get("success") 50 | assert len(roles_sync.get("roles")) > 0 51 | -------------------------------------------------------------------------------- /tests/test_rooms.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | import pytest 4 | 5 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 6 | 7 | 8 | def test_rooms_upload(logged_rocket): 9 | rooms_upload = logged_rocket.rooms_upload( 10 | "GENERAL", file="tests/assets/avatar.png", description="hey there" 11 | ).json() 12 | assert rooms_upload.get("success") 13 | 14 | 15 | def test_rooms_get(logged_rocket): 16 | rooms_get = logged_rocket.rooms_get().json() 17 | assert rooms_get.get("success") 18 | 19 | 20 | def test_rooms_clean_history(logged_rocket): 21 | rooms_clean_history = logged_rocket.rooms_clean_history( 22 | room_id="GENERAL", 23 | latest="2016-09-30T13:42:25.304Z", 24 | oldest="2016-05-30T13:42:25.304Z", 25 | ).json() 26 | assert rooms_clean_history.get("success") 27 | 28 | 29 | def test_rooms_favorite(logged_rocket): 30 | rooms_favorite = logged_rocket.rooms_favorite( 31 | room_id="GENERAL", favorite=True 32 | ).json() 33 | assert rooms_favorite.get("success") 34 | 35 | rooms_favorite = logged_rocket.rooms_favorite( 36 | room_name="general", favorite=True 37 | ).json() 38 | assert rooms_favorite.get("success") 39 | 40 | rooms_favorite = logged_rocket.rooms_favorite( 41 | room_id="unexisting_channel", favorite=True 42 | ).json() 43 | assert not rooms_favorite.get("success") 44 | 45 | with pytest.raises(RocketMissingParamException): 46 | logged_rocket.rooms_favorite() 47 | 48 | 49 | def test_rooms_info(logged_rocket): 50 | rooms_infoby_name = logged_rocket.rooms_info(room_name="general").json() 51 | assert rooms_infoby_name.get("success") 52 | assert rooms_infoby_name.get("room").get("_id") == "GENERAL" 53 | rooms_info_by_id = logged_rocket.rooms_info(room_id="GENERAL").json() 54 | assert rooms_info_by_id.get("success") 55 | assert rooms_info_by_id.get("room").get("_id") == "GENERAL" 56 | with pytest.raises(RocketMissingParamException): 57 | logged_rocket.rooms_info() 58 | 59 | 60 | def test_rooms_create_discussion(logged_rocket): 61 | discussion_name = "this is a discussion" 62 | rooms_create_discussion = logged_rocket.rooms_create_discussion( 63 | prid="GENERAL", 64 | t_name=discussion_name, 65 | ).json() 66 | assert rooms_create_discussion.get("success") 67 | assert "discussion" in rooms_create_discussion 68 | assert rooms_create_discussion.get("discussion").get("fname") == discussion_name 69 | 70 | 71 | def test_rooms_admin_rooms(logged_rocket): 72 | rooms_simple = logged_rocket.rooms_admin_rooms().json() 73 | assert rooms_simple.get("success") 74 | 75 | # Using a room type filter does not seem to work 76 | offset = actual_count = 0 77 | res = {} 78 | while res.get("total") is None or res.get("total") > offset: 79 | res = logged_rocket.rooms_admin_rooms( 80 | **{ 81 | "types": [ 82 | "c", 83 | ], 84 | "offset": offset, 85 | } 86 | ).json() 87 | assert res.get("success") 88 | offset += res.get("count") 89 | actual_count += len(list(filter(lambda x: "c" in x["t"], res.get("rooms")))) 90 | assert res.get("total") == actual_count 91 | 92 | rooms_with_filter = logged_rocket.rooms_admin_rooms(**{"filter": "general"}).json() 93 | assert rooms_with_filter.get("success") 94 | assert rooms_with_filter.get("rooms")[0].get("_id") == "GENERAL" 95 | 96 | 97 | def test_rooms_leave(logged_rocket, secondary_user): 98 | rooms_leave = logged_rocket.rooms_leave("GENERAL").json() 99 | assert not rooms_leave.get("success") 100 | assert rooms_leave.get("errorType") == "error-you-are-last-owner" 101 | 102 | name = str(uuid.uuid1()) 103 | channels_create = logged_rocket.channels_create(name).json() 104 | assert ( 105 | logged_rocket.channels_invite( 106 | room_id=channels_create.get("channel").get("_id"), user_id=secondary_user 107 | ) 108 | .json() 109 | .get("success") 110 | ) 111 | 112 | assert ( 113 | logged_rocket.channels_add_owner( 114 | channels_create.get("channel").get("_id"), user_id=secondary_user 115 | ) 116 | .json() 117 | .get("success") 118 | ) 119 | rooms_leave = logged_rocket.rooms_leave( 120 | channels_create.get("channel").get("_id") 121 | ).json() 122 | assert rooms_leave.get("success") 123 | -------------------------------------------------------------------------------- /tests/test_server.py: -------------------------------------------------------------------------------- 1 | from rocketchat_API.rocketchat import RocketChat 2 | 3 | 4 | def test_info(logged_rocket): 5 | info = logged_rocket.info().json() 6 | assert "info" in info 7 | assert info.get("success") 8 | 9 | 10 | def test_statistics(logged_rocket): 11 | statistics = logged_rocket.statistics().json() 12 | assert statistics.get("success") 13 | 14 | 15 | def test_statistics_list(logged_rocket): 16 | statistics_list = logged_rocket.statistics_list().json() 17 | assert statistics_list.get("success") 18 | 19 | 20 | def test_directory(logged_rocket): 21 | directory = logged_rocket.directory( 22 | query={"text": "rocket", "type": "users"} 23 | ).json() 24 | assert directory.get("success") 25 | 26 | 27 | def test_spotlight(logged_rocket): 28 | spotlight = logged_rocket.spotlight(query="user1").json() 29 | assert spotlight.get("success") 30 | assert spotlight.get("users") is not None, "No users list found" 31 | assert spotlight.get("rooms") is not None, "No rooms list found" 32 | 33 | 34 | def test_login_token(logged_rocket): 35 | user_id = logged_rocket.headers["X-User-Id"] 36 | auth_token = logged_rocket.headers["X-Auth-Token"] 37 | 38 | another_rocket = RocketChat(user_id=user_id, auth_token=auth_token) 39 | logged_user = another_rocket.me().json() 40 | 41 | assert logged_user.get("_id") == user_id 42 | -------------------------------------------------------------------------------- /tests/test_settings.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | import pytest 4 | 5 | 6 | def test_settings(logged_rocket): 7 | settings = logged_rocket.settings().json() 8 | assert settings.get("success") 9 | settings_get = logged_rocket.settings_get(_id="API_Allow_Infinite_Count").json() 10 | assert settings_get.get("success") 11 | assert settings_get.get("value") 12 | settings_update = logged_rocket.settings_update( 13 | _id="API_Allow_Infinite_Count", value=True 14 | ).json() 15 | assert settings_update.get("success") 16 | 17 | 18 | def test_settings_public(rocket): 19 | settings_public = rocket.settings_public().json() 20 | assert settings_public.get("success") 21 | assert "settings" in settings_public 22 | 23 | 24 | @pytest.mark.skip( 25 | reason="Broken in 5.0 https://github.com/jadolg/rocketchat_API/issues/168" 26 | ) 27 | def test_settings_oauth(logged_rocket): 28 | # refresh is not done with any API call ever, so we need to call it manually here 29 | response = logged_rocket.call_api_post( 30 | "method.call/refreshOAuthService", 31 | message='{"method": "refreshOAuthService", "params": []}', 32 | ) 33 | assert response.ok 34 | oauth_get = logged_rocket.settings_oauth().json() 35 | assert oauth_get.get("success") 36 | if oauth_get.get("services"): 37 | # remove the OAuth app Test beforehand, when this is not the first test run (for reproducibility) 38 | response = logged_rocket.call_api_post( 39 | "method.call/removeOAuthService", 40 | message='{"method": "removeOAuthService", "params": ["Test"]}', 41 | ) 42 | assert response.ok 43 | oauth_get = logged_rocket.settings_oauth().json() 44 | assert not oauth_get.get("services") 45 | 46 | oauth_set = logged_rocket.settings_addcustomoauth("Test").json() 47 | assert oauth_set.get("success") 48 | oauth_set = logged_rocket.settings_update("Accounts_OAuth_Custom-Test", True).json() 49 | time.sleep(3) 50 | assert oauth_set.get("success") 51 | oauth_get = logged_rocket.settings_oauth().json() 52 | assert oauth_get.get("success") 53 | assert oauth_get.get("services")[0].get("service") == "test" 54 | 55 | 56 | def test_service_configurations(rocket): 57 | service_configurations = rocket.service_configurations().json() 58 | assert service_configurations.get("success") 59 | assert "configurations" in service_configurations 60 | -------------------------------------------------------------------------------- /tests/test_subscriptions.py: -------------------------------------------------------------------------------- 1 | def test_subscriptions_get(logged_rocket): 2 | subscriptions_get = logged_rocket.subscriptions_get().json() 3 | assert subscriptions_get.get("success") 4 | assert "update" in subscriptions_get 5 | 6 | 7 | def test_subscriptions_get_one(logged_rocket): 8 | subscriptions_get_one = logged_rocket.subscriptions_get_one( 9 | room_id="GENERAL" 10 | ).json() 11 | assert subscriptions_get_one.get("success") 12 | assert "subscription" in subscriptions_get_one 13 | 14 | 15 | def test_subscriptions_unread(logged_rocket): 16 | subscriptions_unread = logged_rocket.subscriptions_unread(room_id="GENERAL").json() 17 | assert subscriptions_unread.get("success"), "Call did not succeed" 18 | 19 | 20 | def test_subscriptions_read(logged_rocket): 21 | subscriptions_read = logged_rocket.subscriptions_read(rid="GENERAL").json() 22 | assert subscriptions_read.get("success"), "Call did not succeed" 23 | -------------------------------------------------------------------------------- /tests/test_teams.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | import pytest 4 | 5 | from rocketchat_API.APIExceptions.RocketExceptions import RocketMissingParamException 6 | 7 | 8 | @pytest.fixture 9 | def test_team_name(): 10 | return str(uuid.uuid1()) 11 | 12 | 13 | @pytest.fixture 14 | def test_team_id(test_team_name, logged_rocket): 15 | _test_team_id = ( 16 | logged_rocket.teams_create(name=test_team_name, team_type=1) 17 | .json() 18 | .get("team") 19 | .get("_id") 20 | ) 21 | return _test_team_id 22 | 23 | 24 | @pytest.fixture 25 | def testuser_id(logged_rocket): 26 | testuser = logged_rocket.users_info(username="testuser1").json() 27 | 28 | _testuser_id = testuser.get("user").get("_id") 29 | 30 | yield _testuser_id 31 | 32 | logged_rocket.users_delete(_testuser_id) 33 | 34 | 35 | @pytest.fixture 36 | def test_group_name(): 37 | return str(uuid.uuid1()) 38 | 39 | 40 | @pytest.fixture 41 | def test_group_id(test_group_name, logged_rocket): 42 | _test_group_id = ( 43 | logged_rocket.groups_create(test_group_name).json().get("group").get("_id") 44 | ) 45 | return _test_group_id 46 | 47 | 48 | def test_teams_create_delete(logged_rocket): 49 | name = str(uuid.uuid1()) 50 | teams_create = logged_rocket.teams_create(name=name, team_type=1).json() 51 | assert teams_create.get("success") 52 | assert name == teams_create.get("team").get("name") 53 | teams_delete = logged_rocket.teams_delete(team_name=name).json() 54 | assert teams_delete.get("success") 55 | teams_create = logged_rocket.teams_create(name=name, team_type=1).json() 56 | assert teams_create.get("success") 57 | team_id = teams_create.get("team").get("_id") 58 | teams_delete = logged_rocket.teams_delete(team_id=team_id).json() 59 | assert teams_delete.get("success") 60 | 61 | with pytest.raises(RocketMissingParamException): 62 | logged_rocket.teams_delete() 63 | 64 | 65 | def test_teams_list_all(logged_rocket): 66 | teams_list = logged_rocket.teams_list_all().json() 67 | assert teams_list.get("success") 68 | assert "teams" in teams_list 69 | 70 | 71 | def test_teams_info(logged_rocket, test_team_name, test_team_id): 72 | teams_info_by_id = logged_rocket.teams_info(team_id=test_team_id).json() 73 | assert teams_info_by_id.get("success") 74 | assert "teamInfo" in teams_info_by_id 75 | assert teams_info_by_id.get("teamInfo").get("_id") == test_team_id 76 | 77 | teams_info_by_name = logged_rocket.teams_info(team_name=test_team_name).json() 78 | assert teams_info_by_name.get("success") 79 | assert "teamInfo" in teams_info_by_name 80 | assert teams_info_by_name.get("teamInfo").get("_id") == test_team_id 81 | 82 | with pytest.raises(RocketMissingParamException): 83 | logged_rocket.teams_info() 84 | 85 | 86 | def test_teams_members(logged_rocket, test_team_name, test_team_id): 87 | teams_members = logged_rocket.teams_members(team_id=test_team_id).json() 88 | assert teams_members.get("success") 89 | teams_members = logged_rocket.teams_members(team_name=test_team_name).json() 90 | assert teams_members.get("success") 91 | 92 | with pytest.raises(RocketMissingParamException): 93 | logged_rocket.teams_members() 94 | 95 | 96 | def test_teams_add_update_remove_members(logged_rocket, test_team_id, testuser_id): 97 | teams_add_members = logged_rocket.teams_add_members( 98 | team_id=test_team_id, 99 | members=[ 100 | { 101 | "userId": testuser_id, 102 | "roles": [ 103 | "member", 104 | ], 105 | } 106 | ], 107 | ).json() 108 | assert teams_add_members.get("success"), teams_add_members.get("error") 109 | 110 | teams_members = logged_rocket.teams_members(team_id=test_team_id).json() 111 | assert teams_members.get("success") 112 | assert "members" in teams_members 113 | assert len(teams_members.get("members")) == 2 114 | user_ids = [ 115 | member.get("user").get("_id") for member in teams_members.get("members") 116 | ] 117 | assert testuser_id in user_ids 118 | 119 | # Make testuser owner 120 | teams_update_member = logged_rocket.teams_update_member( 121 | team_id=test_team_id, member={"userId": testuser_id, "roles": ["owner"]} 122 | ).json() 123 | assert teams_update_member.get("success"), teams_update_member.get("error") 124 | 125 | teams_members = logged_rocket.teams_members(team_id=test_team_id).json() 126 | testuser_member = list( 127 | filter( 128 | lambda member: member.get("user").get("_id") == testuser_id, 129 | teams_members.get("members"), 130 | ) 131 | )[0] 132 | assert "owner" in testuser_member.get("roles") 133 | 134 | teams_remove_member = logged_rocket.teams_remove_member( 135 | team_id=test_team_id, user_id=testuser_id 136 | ).json() 137 | assert teams_remove_member.get("success"), teams_remove_member.get("error") 138 | teams_members = logged_rocket.teams_members(team_id=test_team_id).json() 139 | assert "members" in teams_members 140 | assert len(teams_members.get("members")) == 1 141 | 142 | with pytest.raises(RocketMissingParamException): 143 | logged_rocket.teams_add_members() 144 | 145 | with pytest.raises(RocketMissingParamException): 146 | logged_rocket.teams_update_member() 147 | 148 | with pytest.raises(RocketMissingParamException): 149 | logged_rocket.teams_remove_member() 150 | 151 | 152 | def test_teams_add_update_remove_members_team_name( 153 | logged_rocket, test_team_name, test_team_id, testuser_id 154 | ): 155 | teams_add_members = logged_rocket.teams_add_members( 156 | team_name=test_team_name, 157 | members=[ 158 | { 159 | "userId": testuser_id, 160 | "roles": [ 161 | "member", 162 | ], 163 | } 164 | ], 165 | ).json() 166 | assert teams_add_members.get("success"), teams_add_members.get("error") 167 | 168 | teams_members = logged_rocket.teams_members(team_name=test_team_name).json() 169 | assert teams_members.get("success") 170 | assert "members" in teams_members 171 | assert len(teams_members.get("members")) == 2 172 | user_ids = [ 173 | member.get("user").get("_id") for member in teams_members.get("members") 174 | ] 175 | assert testuser_id in user_ids 176 | 177 | # Make testuser owner 178 | teams_update_member = logged_rocket.teams_update_member( 179 | team_name=test_team_name, member={"userId": testuser_id, "roles": ["owner"]} 180 | ).json() 181 | assert teams_update_member.get("success"), teams_update_member.get("error") 182 | 183 | teams_members = logged_rocket.teams_members(team_name=test_team_name).json() 184 | testuser_member = list( 185 | filter( 186 | lambda member: member.get("user").get("_id") == testuser_id, 187 | teams_members.get("members"), 188 | ) 189 | )[0] 190 | assert "owner" in testuser_member.get("roles") 191 | 192 | teams_remove_member = logged_rocket.teams_remove_member( 193 | team_name=test_team_name, user_id=testuser_id 194 | ).json() 195 | assert teams_remove_member.get("success"), teams_remove_member.get("error") 196 | teams_members = logged_rocket.teams_members(team_name=test_team_name).json() 197 | assert "members" in teams_members 198 | assert len(teams_members.get("members")) == 1 199 | 200 | 201 | def test_teams_list_rooms(logged_rocket, test_team_name, test_team_id): 202 | teams_rooms = logged_rocket.teams_list_rooms( 203 | team_id=test_team_id, room_type=1 204 | ).json() 205 | assert teams_rooms.get("success") 206 | assert "rooms" in teams_rooms 207 | 208 | teams_rooms_name = logged_rocket.teams_list_rooms( 209 | team_name=test_team_name, room_type=1 210 | ).json() 211 | assert teams_rooms_name.get("success") 212 | assert "rooms" in teams_rooms_name 213 | 214 | with pytest.raises(RocketMissingParamException): 215 | logged_rocket.teams_list_rooms() 216 | 217 | 218 | def test_teams_add_update_remove_rooms(logged_rocket, test_team_id, test_group_id): 219 | created_room = logged_rocket.teams_add_rooms( 220 | team_id=test_team_id, rooms=[test_group_id] 221 | ).json() 222 | assert created_room.get("success"), created_room.get("error") 223 | assert "rooms" in created_room 224 | assert created_room.get("rooms")[0]["_id"] == test_group_id 225 | 226 | teams_rooms = logged_rocket.teams_list_rooms(team_id=test_team_id).json() 227 | assert teams_rooms.get("success"), teams_rooms.get("error") 228 | assert len(teams_rooms.get("rooms")) == 1 229 | assert teams_rooms.get("rooms")[0]["_id"] == test_group_id 230 | 231 | teams_update_room = logged_rocket.teams_update_room( 232 | test_group_id, is_default=True 233 | ).json() 234 | assert teams_update_room.get("success"), teams_update_room.get("success") 235 | assert teams_update_room.get("room")["_id"] == test_group_id 236 | assert teams_update_room.get("room")["teamDefault"] 237 | 238 | teams_rooms = logged_rocket.teams_list_rooms(team_id=test_team_id).json() 239 | assert teams_rooms.get("success"), teams_rooms.get("error") 240 | assert len(teams_rooms.get("rooms")) == 1 241 | assert teams_rooms.get("rooms")[0]["_id"] == test_group_id 242 | assert teams_rooms.get("rooms")[0]["teamDefault"] 243 | 244 | teams_remove_room = logged_rocket.teams_remove_room( 245 | team_id=test_team_id, room_id=test_group_id 246 | ).json() 247 | assert teams_remove_room.get("success"), teams_remove_room.get("error") 248 | 249 | teams_rooms = logged_rocket.teams_list_rooms(team_id=test_team_id).json() 250 | assert teams_rooms.get("success"), teams_rooms.get("error") 251 | assert len(teams_rooms.get("rooms")) == 0 252 | 253 | 254 | def test_teams_add_update_remove_rooms_name( 255 | logged_rocket, test_team_name, test_team_id, test_group_id 256 | ): 257 | created_room = logged_rocket.teams_add_rooms( 258 | team_name=test_team_name, rooms=[test_group_id] 259 | ).json() 260 | assert created_room.get("success"), created_room.get("error") 261 | assert "rooms" in created_room 262 | assert created_room.get("rooms")[0]["_id"] == test_group_id 263 | 264 | teams_rooms = logged_rocket.teams_list_rooms(team_name=test_team_name).json() 265 | assert teams_rooms.get("success"), teams_rooms.get("error") 266 | assert len(teams_rooms.get("rooms")) == 1 267 | assert teams_rooms.get("rooms")[0]["_id"] == test_group_id 268 | 269 | teams_update_room = logged_rocket.teams_update_room( 270 | test_group_id, is_default=True 271 | ).json() 272 | assert teams_update_room.get("success"), teams_update_room.get("success") 273 | assert teams_update_room.get("room")["_id"] == test_group_id 274 | assert teams_update_room.get("room")["teamDefault"] 275 | 276 | teams_rooms = logged_rocket.teams_list_rooms(team_name=test_team_name).json() 277 | assert teams_rooms.get("success"), teams_rooms.get("error") 278 | assert len(teams_rooms.get("rooms")) == 1 279 | assert teams_rooms.get("rooms")[0]["_id"] == test_group_id 280 | assert teams_rooms.get("rooms")[0]["teamDefault"] 281 | 282 | teams_remove_room = logged_rocket.teams_remove_room( 283 | team_name=test_team_name, room_id=test_group_id 284 | ).json() 285 | assert teams_remove_room.get("success"), teams_remove_room.get("error") 286 | 287 | teams_rooms = logged_rocket.teams_list_rooms(team_name=test_team_name).json() 288 | assert teams_rooms.get("success"), teams_rooms.get("error") 289 | assert len(teams_rooms.get("rooms")) == 0 290 | 291 | with pytest.raises(RocketMissingParamException): 292 | logged_rocket.teams_add_rooms() 293 | 294 | with pytest.raises(RocketMissingParamException): 295 | logged_rocket.teams_remove_room() 296 | -------------------------------------------------------------------------------- /tests/test_user.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | import pytest 4 | 5 | from rocketchat_API.APIExceptions.RocketExceptions import ( 6 | RocketAuthenticationException, 7 | RocketMissingParamException, 8 | ) 9 | 10 | 11 | @pytest.fixture 12 | def delete_user2(logged_rocket): 13 | user2_exists = logged_rocket.users_info(username="user2").json().get("success") 14 | if user2_exists: 15 | user_id = ( 16 | logged_rocket.users_info(username="user2").json().get("user").get("_id") 17 | ) 18 | logged_rocket.users_delete(user_id) 19 | 20 | 21 | def test_login(logged_rocket, user): 22 | login = logged_rocket.login(user.name, user.password).json() 23 | 24 | with pytest.raises(RocketAuthenticationException): 25 | logged_rocket.login(user.name, "bad_password") 26 | 27 | assert login.get("status") == "success" 28 | assert "authToken" in login.get("data") 29 | assert "userId" in login.get("data") 30 | 31 | 32 | def test_login_email(logged_rocket, user): 33 | login = logged_rocket.login(user.email, user.password).json() 34 | 35 | with pytest.raises(RocketAuthenticationException): 36 | logged_rocket.login(user.name, "bad_password") 37 | 38 | assert login.get("status") == "success" 39 | assert "authToken" in login.get("data") 40 | assert "userId" in login.get("data") 41 | 42 | 43 | def test_me(logged_rocket, user): 44 | me = logged_rocket.me().json() 45 | assert me.get("success") 46 | assert me.get("username") == user.name 47 | assert me.get("name") == user.name 48 | 49 | 50 | def test_logout(logged_rocket, user): 51 | logged_rocket.login(user.name, user.password).json() 52 | logout = logged_rocket.logout() 53 | assert logout.status_code == 200 54 | # TODO: Enable this check back once https://github.com/RocketChat/Rocket.Chat/issues/35922 is fixed 55 | # assert logout.get("status") == "success" 56 | 57 | 58 | def test_users_info(logged_rocket, user): 59 | login = logged_rocket.login(user.name, user.password).json() 60 | user_id = login.get("data").get("userId") 61 | 62 | users_info_by_id = logged_rocket.users_info(user_id=user_id).json() 63 | assert users_info_by_id.get("success") 64 | assert users_info_by_id.get("user").get("_id") == user_id 65 | 66 | users_info_by_name = logged_rocket.users_info(username=user.name).json() 67 | assert users_info_by_name.get("success") 68 | assert users_info_by_name.get("user").get("name") == user.name 69 | 70 | with pytest.raises(RocketMissingParamException): 71 | logged_rocket.users_info() 72 | 73 | 74 | def test_users_get_presence(logged_rocket, user): 75 | login = logged_rocket.login(user.name, user.password).json() 76 | users_get_presence = logged_rocket.users_get_presence( 77 | user_id=login.get("data").get("userId") 78 | ).json() 79 | assert users_get_presence.get("success") 80 | 81 | users_get_presence_by_name = logged_rocket.users_get_presence( 82 | username=user.name 83 | ).json() 84 | assert users_get_presence_by_name.get("success") 85 | 86 | with pytest.raises(RocketMissingParamException): 87 | logged_rocket.users_get_presence() 88 | 89 | 90 | def test_users_get_avatar(logged_rocket, user): 91 | login = logged_rocket.login(user.name, user.password).json() 92 | 93 | users_get_avatar = logged_rocket.users_get_avatar(user_id="no_user") 94 | assert users_get_avatar.status_code == 400 95 | 96 | users_get_avatar = logged_rocket.users_get_avatar( 97 | user_id=login.get("data").get("userId") 98 | ) 99 | assert users_get_avatar.text 100 | 101 | users_get_avatar = logged_rocket.users_get_avatar(username=user.name) 102 | assert users_get_avatar.text 103 | 104 | with pytest.raises(RocketMissingParamException): 105 | logged_rocket.users_get_avatar() 106 | 107 | 108 | def test_users_reset_avatar(logged_rocket, user): 109 | login = logged_rocket.login(user.name, user.password).json() 110 | users_reset_avatar = logged_rocket.users_reset_avatar( 111 | user_id=login.get("data").get("userId") 112 | ).json() 113 | assert users_reset_avatar.get("success") 114 | 115 | users_reset_avatar = logged_rocket.users_reset_avatar(username=user.name).json() 116 | assert users_reset_avatar.get("success") 117 | 118 | with pytest.raises(RocketMissingParamException): 119 | logged_rocket.users_reset_avatar() 120 | 121 | 122 | def test_users_create_token(logged_rocket, user): 123 | login = logged_rocket.login(user.name, user.password).json() 124 | users_create_token = logged_rocket.users_create_token( 125 | user_id=login.get("data").get("userId") 126 | ).json() 127 | assert users_create_token.get("success") 128 | assert "authToken" in users_create_token.get("data") 129 | assert "userId" in users_create_token.get("data") 130 | 131 | users_create_token = logged_rocket.users_create_token( 132 | username=login.get("data").get("me").get("name") 133 | ).json() 134 | assert users_create_token.get("success") 135 | assert "authToken" in users_create_token.get("data") 136 | assert "userId" in users_create_token.get("data") 137 | 138 | with pytest.raises(RocketMissingParamException): 139 | logged_rocket.users_create_token() 140 | 141 | 142 | def test_users_create_update_delete(logged_rocket, user): 143 | name = str(uuid.uuid1()) 144 | users_create = logged_rocket.users_create( 145 | email="{}@domain.com".format(name), 146 | name=name, 147 | password=user.password, 148 | username=name, 149 | ).json() 150 | assert users_create.get("success"), users_create.get("error") 151 | 152 | user_id = users_create.get("user").get("_id") 153 | users_update = logged_rocket.users_update( 154 | user_id, 155 | name="newname", 156 | password=user.password, 157 | username="newusername", 158 | ).json() 159 | assert users_update.get("success"), "Can not update user" 160 | assert users_update.get("user").get("name") == "newname" 161 | assert users_update.get("user").get("username") == "newusername" 162 | 163 | users_delete = logged_rocket.users_delete(user_id).json() 164 | assert users_delete.get("success") 165 | 166 | 167 | def test_users_set_active_status(logged_rocket, user): 168 | login = logged_rocket.login(user.name, user.password).json() 169 | user_id = login.get("data").get("userId") 170 | user_set_status = logged_rocket.users_set_active_status( 171 | user_id, active_status=True 172 | ).json() 173 | assert user_set_status.get("success"), user_set_status.get("error") 174 | 175 | 176 | def test_users_set_avatar_from_file(logged_rocket): 177 | users_set_avatar = logged_rocket.users_set_avatar( 178 | avatar_url="tests/assets/avatar.svg" 179 | ).json() 180 | assert users_set_avatar.get("success"), users_set_avatar.get("error") 181 | 182 | 183 | def test_users_set_avatar_from_url(logged_rocket): 184 | # ToDo: Modify this test so it can run while offline 185 | users_set_avatar = logged_rocket.users_set_avatar( 186 | avatar_url="https://upload.wikimedia.org/wikipedia/commons/7/77/Wikipedia_svg_logo.svg" 187 | ).json() 188 | assert users_set_avatar.get("success"), users_set_avatar.get("error") 189 | 190 | 191 | def test_users_forgot_password(logged_rocket): 192 | users_forgot_password = logged_rocket.users_forgot_password( 193 | email="email@domain.com" 194 | ).json() 195 | assert users_forgot_password.get("success") 196 | 197 | 198 | def test_users_set_get_preferences(logged_rocket): 199 | users_set_preferences = logged_rocket.users_set_preferences( 200 | user_id=logged_rocket.me().json().get("_id"), data={"useEmojis": False} 201 | ).json() 202 | assert users_set_preferences.get("success") 203 | users_get_preferences = logged_rocket.users_get_preferences().json() 204 | assert users_get_preferences.get("success") 205 | 206 | 207 | def test_users_list(logged_rocket): 208 | users_list = logged_rocket.users_list().json() 209 | assert users_list.get("success") 210 | 211 | 212 | def test_users_set_status(logged_rocket): 213 | users_set_status = logged_rocket.users_set_status( 214 | message="working on it", status="online" 215 | ).json() 216 | assert users_set_status.get("success") 217 | -------------------------------------------------------------------------------- /tests/test_video_conferences.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | 4 | # TODO: Go back to this test once the ticket has being answered 5 | @pytest.mark.skip( 6 | reason="Broken in 5.0. https://github.com/RocketChat/Rocket.Chat/issues/26520" 7 | ) 8 | def test_update_jitsi_timeout(logged_rocket): 9 | update_jitsi_timeout = logged_rocket.update_jitsi_timeout(room_id="GENERAL").json() 10 | assert update_jitsi_timeout.get("success") 11 | --------------------------------------------------------------------------------