70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
import os
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
import numpy as np
|
|
|
|
os.environ["JWT_AAREDAQ_KEY"] = "test_key_for_unit_testing"
|
|
|
|
|
|
def test_meta_error_codes(client):
|
|
response = client.get("/meta/error-codes")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "AuthErrorCode" in data
|
|
|
|
|
|
def test_status(api, daq_status_factory):
|
|
from aare.common.models import BeamlineStateEnum, SessionsStateEnum
|
|
|
|
api.daq.status = daq_status_factory(
|
|
state=BeamlineStateEnum.Maintenance,
|
|
current_pgroup="p12345",
|
|
staff=True,
|
|
)
|
|
api.daq.status.sample = None
|
|
api.daq.status.box = None
|
|
api.daq.status.last_best_res = None
|
|
api.daq.status.last_best_b_factor = None
|
|
api.daq.status.crystal_size = SimpleNamespace(x=0, y=0, z=0)
|
|
api.daq.status.open_guis = []
|
|
|
|
api.cfg.pgroup = "p12345"
|
|
api.cfg.session_state.return_value = SessionsStateEnum.OwnedByYou
|
|
api.cfg.get_open_gui_sessions.return_value = []
|
|
|
|
response = api.client.get("/status", headers={"Authorization": "Bearer fake-token"})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["state"] == BeamlineStateEnum.Maintenance.value
|
|
assert data["session"]["current_pgroup"] == "p12345"
|
|
assert data["session"]["session"] == SessionsStateEnum.OwnedByYou.value
|
|
|
|
def test_omega_put(client):
|
|
with patch("aare.daq.auth.check_jwt_rw"), patch("aare.daq.server.daq") as mock_daq:
|
|
response = client.put("/beamline/omega?val=10.5", headers={"Authorization": "Bearer fake-token"})
|
|
assert response.status_code == 200
|
|
assert response.json() == "OK"
|
|
assert mock_daq.omega == 10.5
|
|
|
|
|
|
def test_login_success(client):
|
|
with patch("aare.daq.auth.authenticate_from_proxy_header", return_value="user"), \
|
|
patch("aare.daq.auth.authenticate_user", return_value="fake-access-token"):
|
|
response = client.post(
|
|
"/token",
|
|
data={"username": "user", "password": "pwd"},
|
|
headers={"X-Remote-User": "user"},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json() == {"access_token": "fake-access-token", "token_type": "bearer"}
|
|
|
|
|
|
def test_get_image(client, mock_backend):
|
|
mock_daq = mock_backend["daq"]
|
|
mock_daq.camera_image = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
|
|
response = client.get("/beamline/image", headers={"Authorization": "Bearer fake-token"})
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"] == "image/jpeg"
|
|
assert len(response.content) > 0 |