Skip to content

Commit 518ce25

Browse files
authored
test: Run integration tests on CI (#726)
Closes: SDK-2088
1 parent d3a6324 commit 518ce25

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Integration tests
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
tests:
12+
name: Integration tests
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
- name: Set up Python 3.6
18+
uses: actions/setup-python@v2
19+
with:
20+
python-version: '3.6'
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
python -m pip install -r requirements-dev.txt
25+
- name: Run integration tests
26+
run: |
27+
tox -e integration-tests
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
JWT_CONFIG_BASE_64: ${{ secrets.JWT_CONFIG_BASE_64 }}

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ addopts = --strict --showlocals -r a --tb=long --ignore=test/integration_new/
33
xfail_strict = True
44
junit_suite_name = boxsdk
55
testpaths = test/
6+
python_files=*.py
7+
68

test/integration_new/README.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1-
#Integration tests
1+
# Integration Tests
22

3-
To run integration tests locally you have to use a Box account with all possible scopes enabled.
4-
For integration tests we use JWT authentication method. To use it download jwt settings file from
5-
Developer Console and provide path to your config file in `integration_tests.cfg`.
3+
## Running integration tests locally
64

7-
To launch all integration tests locally run:
8-
`pytest test/integration_new/object/*`
5+
### Create Custom Application
6+
To run integration tests locally you will need a `Custom App` created at https://cloud.app.box.com/developers/console
7+
with `Server Authentication (with JWT)` selected as authentication method.
8+
Once created you can edit properties of the application:
9+
- In section `App Access Level` select `App + Enterprise Access`. You can enable all `Application Scopes`.
10+
- In section `Advanced Features` enable `Make API calls using the as-user header` and `Generate user access tokens`.
11+
12+
Now select `Authorization` and submit application to be reviewed by account admin.
13+
14+
15+
### Export configuration
16+
17+
There are two ways to set up JWT configuration:
18+
1. First method:
19+
- Select `Configuration` tab and in the bottom in the section `App Settings`
20+
download your app configuration settings as JSON.
21+
- Specify the path to the JWT config file in `integration_tests.cfg`, e.g. `ConfigFilePath = /Users/me/jwt-config.json`
22+
2. Alternatively:
23+
- Select `Configuration` tab and in the bottom in the section `App Settings`
24+
download your app configuration settings as JSON.
25+
- Encode configuration file to Base64, e.g. using command: `base64 -i path_to_json_file`
26+
- Set environment variable: `JWT_CONFIG_BASE_64` with base64 encoded jwt configuration file
27+
28+
### Running Tests
29+
30+
You can run all tests (unit in all supported python environments and integration) using command:
31+
```bash
32+
tox
33+
```
34+
35+
To run only integration tests, you can run:
36+
```bash
37+
tox -e integration-tests
38+
```

test/integration_new/__init__.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
1+
import base64
12
import configparser
23
import os
4+
import json
35
from pathlib import Path
6+
from typing import Optional
47

58
from boxsdk.auth.jwt_auth import JWTAuth
69
from boxsdk.client import Client
710

811

9-
def read_jwt_path_from_config(config_path: str):
12+
JWT_CONFIG_ENV_VAR_NAME = 'JWT_CONFIG_BASE_64'
13+
CURRENT_DIR_PATH = str(Path(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))))
14+
CONFIG_PATH = f'{CURRENT_DIR_PATH}/integration_tests.cfg'
15+
16+
17+
def get_jwt_config() -> JWTAuth:
18+
jwt_config = read_jwt_config_from_env_var() or read_jwt_config_from_file()
19+
20+
if not jwt_config:
21+
raise RuntimeError(
22+
f'JWT config cannot be loaded. Missing environment variable: {JWT_CONFIG_ENV_VAR_NAME} or JWT config path.'
23+
)
24+
return jwt_config
25+
26+
27+
def read_jwt_config_from_env_var() -> Optional[JWTAuth]:
28+
29+
jwt_config_base64 = os.getenv(JWT_CONFIG_ENV_VAR_NAME)
30+
if not jwt_config_base64:
31+
return None
32+
jwt_config_str = base64.b64decode(jwt_config_base64)
33+
jwt_config_json = json.loads(jwt_config_str)
34+
return JWTAuth.from_settings_dictionary(jwt_config_json)
35+
36+
37+
def read_jwt_config_from_file() -> Optional[JWTAuth]:
1038
config_parser = configparser.ConfigParser()
11-
config_parser.read(config_path)
12-
return config_parser["JWT"].get('SettingsFilePath')
39+
config_parser.read(CONFIG_PATH)
40+
jwt_config_file_path = config_parser["JWT"].get('ConfigFilePath')
41+
if not jwt_config_file_path:
42+
return None
43+
return JWTAuth.from_settings_file(jwt_config_file_path)
1344

1445

15-
CURRENT_DIR_PATH = str(Path(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))))
16-
jwt_config_path = read_jwt_path_from_config(config_path=f'{CURRENT_DIR_PATH}/integration_tests.cfg')
17-
jwt_config = JWTAuth.from_settings_file(jwt_config_path)
18-
CLIENT = Client(jwt_config)
46+
CLIENT = Client(get_jwt_config())
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[JWT]
2-
SettingsFilePath = PATH_TO_YOUR_JWT_CONFIG_FILE
2+
ConfigFilePath =

tox.ini

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ envlist =
1616
py39,
1717
py310,
1818
docs,
19-
coverage
19+
coverage,
20+
integration-tests
2021

2122
[gh-actions]
2223
python =
@@ -89,3 +90,9 @@ sitepackages = False
8990
recreate = True
9091
deps =
9192
twine
93+
94+
[testenv:integration-tests]
95+
passenv = JWT_CONFIG_BASE_64
96+
commands =
97+
pytest {toxinidir}/test/integration_new {posargs} --disable-pytest-warnings
98+
deps = -rrequirements-dev.txt

0 commit comments

Comments
 (0)