44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from aare.gui.auth import auth
|
|
|
|
|
|
def test_auth_success(mocker):
|
|
mock_run = mocker.patch("aare.gui.auth.subprocess.run")
|
|
mock_run.return_value = mocker.Mock(
|
|
returncode=0, stdout=json.dumps({"access_token": "fake_token_abc.123.xyz"}), stderr=""
|
|
)
|
|
|
|
token = auth("http://test-server", "/tmp/test-cert.pem")
|
|
|
|
assert token == "fake_token_abc.123.xyz"
|
|
mock_run.assert_called_once()
|
|
args, _kwargs = mock_run.call_args
|
|
assert "curl" in args[0]
|
|
assert "--cacert" in args[0]
|
|
assert "/tmp/test-cert.pem" in args[0]
|
|
assert "http://test-server/token" in args[0]
|
|
|
|
|
|
def test_auth_network_failure(mocker):
|
|
mock_run = mocker.patch("aare.gui.auth.subprocess.run")
|
|
mock_run.side_effect = OSError("Connection refused")
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
auth("http://test-server", "/tmp/test-cert.pem")
|
|
|
|
assert "Cannot reach AareDAQ server" in str(excinfo.value)
|
|
|
|
|
|
def test_auth_no_url_returns_dummy_jwt(mocker):
|
|
mock_run = mocker.patch("aare.gui.auth.subprocess.run")
|
|
mocker.patch("aare.gui.auth.get_user", return_value="testuser")
|
|
|
|
token = auth(None, None)
|
|
|
|
assert isinstance(token, str)
|
|
assert token.count(".") == 2
|
|
mock_run.assert_not_called()
|