|
| 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