Skip to content

Commit b360c9a

Browse files
committed
Add unit tests for omero_user_token reading APIs
Add support for envvar allowing to override default token directory Add pytests covering assert_and_get_token_dir, assert_and_get_token_path, get_token, login and getter APIs
1 parent 6e6fb06 commit b360c9a

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

.github/workflows/workflow.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ jobs:
3131
with:
3232
python-version: ${{ matrix.python-version }}
3333
- name: Install dependencies
34-
run: python -mpip install -U wheel flake8 virtualenv
34+
run: |
35+
python -mpip install -U wheel pip
36+
pip install -r requirements-dev.txt
3537
- name: Run tests
3638
run: |
3739
git fetch --prune --unshallow --tags --force
3840
git describe
3941
flake8
40-
python setup.py build
42+
python setup.py install
43+
pytest -v
4144
4245
# https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
4346
publish-pypi:

omero_user_token/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121

2222
def assert_and_get_token_dir():
23-
home = os.path.expanduser('~')
24-
token_dir = os.path.join(home, '.omero_user_token')
23+
token_dir = os.getenv('OMERO_USER_TOKEN_DIR', default=None)
24+
if token_dir is None:
25+
home = os.path.expanduser('~')
26+
token_dir = os.path.join(home, '.omero_user_token')
2527
if not os.path.exists(token_dir):
2628
os.makedirs(token_dir)
2729
os.chmod(token_dir, stat.S_IRWXU)

requirements-dev.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
https://github.com/glencoesoftware/zeroc-ice-py-linux-x86_64/releases/download/20221003/zeroc_ice-3.6.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl; platform_system=="Linux" and python_version=="3.8"
2+
https://github.com/glencoesoftware/zeroc-ice-py-linux-x86_64/releases/download/20221003/zeroc_ice-3.6.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl; platform_system=="Linux" and python_version=="3.9"
3+
https://github.com/glencoesoftware/zeroc-ice-py-linux-x86_64/releases/download/20221003/zeroc_ice-3.6.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl; platform_system=="Linux" and python_version=="3.10"
4+
https://github.com/glencoesoftware/zeroc-ice-py-macos-x86_64/releases/download/20220722/zeroc_ice-3.6.5-cp38-cp38-macosx_10_15_x86_64.whl; platform_system!="Windows" and platform_system!="Linux" and python_version=="3.8"
5+
https://github.com/glencoesoftware/zeroc-ice-py-macos-x86_64/releases/download/20220722/zeroc_ice-3.6.5-cp39-cp39-macosx_10_15_x86_64.whl; platform_system!="Windows" and platform_system!="Linux" and python_version=="3.9"
6+
https://github.com/glencoesoftware/zeroc-ice-py-macos-x86_64/releases/download/20220722/zeroc_ice-3.6.5-cp310-cp310-macosx_10_15_x86_64.whl; platform_system!="Windows" and platform_system!="Linux" and python_version=="3.10"
7+
flake8
8+
pytest

tests/test_token.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2023 Glencoe Software, Inc. All rights reserved.
5+
#
6+
# This software is distributed under the terms described by the LICENSE.txt
7+
# file you can find at the root of the distribution bundle. If the file is
8+
# missing please request a copy by contacting [email protected]
9+
10+
11+
from omero_user_token import assert_and_get_token_dir
12+
from omero_user_token import assert_and_get_token_path
13+
from omero_user_token import get_token
14+
from omero_user_token import getter
15+
from omero_user_token import login
16+
17+
import omero
18+
import os
19+
import pytest
20+
import uuid
21+
22+
23+
class TestUserToken:
24+
25+
@pytest.fixture(autouse=True)
26+
def set_up_token_dir(self, monkeypatch, tmpdir):
27+
self.token_dir = str(tmpdir / ".omero_user_token")
28+
monkeypatch.setenv("OMERO_USER_TOKEN_DIR", self.token_dir)
29+
self.token_path = str(tmpdir / ".omero_user_token" / "token")
30+
31+
def write_token(self, token):
32+
os.makedirs(self.token_dir)
33+
with open(self.token_path, 'w') as f:
34+
f.write(token)
35+
36+
def test_assert_and_get_token_dir(self):
37+
token_dir = assert_and_get_token_dir()
38+
assert token_dir == self.token_dir
39+
assert os.path.exists(token_dir)
40+
assert os.path.isdir(token_dir)
41+
42+
def test_assert_and_get_token_path(self):
43+
token_path = assert_and_get_token_path()
44+
assert token_path == self.token_path
45+
assert os.path.exists(os.path.dirname(token_path))
46+
47+
def test_get_token(self):
48+
token = "%s@localhost:4064" % uuid.uuid4()
49+
self.write_token(token)
50+
assert get_token() == token
51+
52+
def test_get_token_file_not_found(self):
53+
with pytest.raises(FileNotFoundError):
54+
get_token()
55+
56+
def test_login(self):
57+
token = "%s@localhost:4064" % uuid.uuid4()
58+
self.write_token(token)
59+
client = login(get_token())
60+
assert isinstance(client, omero.clients.BaseClient)
61+
62+
@pytest.mark.parametrize("invalidtoken", (
63+
"%s", "%s@", "%s@localhost"))
64+
def test_login_invalid_token(self, invalidtoken):
65+
token = invalidtoken % uuid.uuid4()
66+
self.write_token(token)
67+
with pytest.raises(ValueError):
68+
login(get_token())
69+
70+
def test_getter(self, monkeypatch):
71+
token = "%s@localhost:4064" % uuid.uuid4()
72+
self.write_token(token)
73+
monkeypatch.setattr(
74+
omero.clients.BaseClient, "getSession", lambda x: True)
75+
assert getter() == token
76+
77+
def test_getter_invalid_session(self):
78+
token = "%s@localhost:4064" % uuid.uuid4()
79+
self.write_token(token)
80+
with pytest.raises(SystemExit):
81+
getter()

0 commit comments

Comments
 (0)