|
| 1 | +import os |
| 2 | +import requests |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from DIRAC import S_ERROR, S_OK |
| 6 | + |
| 7 | + |
| 8 | +# Assuming 'arc' is imported at the module level of ARCComputingElement |
| 9 | +@patch.dict("sys.modules", {"arc": MagicMock()}) |
| 10 | +def test__checkSession(mocker): |
| 11 | + """Test checkSession""" |
| 12 | + from DIRAC.Resources.Computing.AREXComputingElement import AREXComputingElement |
| 13 | + |
| 14 | + arex = AREXComputingElement("test") |
| 15 | + |
| 16 | + # 1. The session has not been initialized: it shoud return an error |
| 17 | + result = arex._checkSession() |
| 18 | + |
| 19 | + assert not result["OK"], result |
| 20 | + assert not arex.session |
| 21 | + assert "Authorization" not in arex.headers, arex.headers |
| 22 | + |
| 23 | + # 2. This time the session is initialized but there is no proxy nor token |
| 24 | + # It should return an error |
| 25 | + arex.session = requests.Session() |
| 26 | + result = arex._checkSession() |
| 27 | + |
| 28 | + assert not result["OK"], result |
| 29 | + assert arex.session |
| 30 | + assert not arex.session.cert |
| 31 | + assert "Authorization" not in arex.headers, arex.headers |
| 32 | + |
| 33 | + # 3. We set a malformed proxy, but not a token: it should return an error |
| 34 | + arex.proxy = "fake proxy" |
| 35 | + mocker.patch( |
| 36 | + "DIRAC.Resources.Computing.AREXComputingElement.AREXComputingElement._prepareProxy", return_value=S_ERROR() |
| 37 | + ) |
| 38 | + |
| 39 | + result = arex._checkSession() |
| 40 | + assert not result["OK"], result |
| 41 | + assert arex.session |
| 42 | + assert not arex.session.cert |
| 43 | + assert "Authorization" not in arex.headers, arex.headers |
| 44 | + |
| 45 | + # 4. We set a proxy, but not a token: the session should include the proxy, but not the token |
| 46 | + arex.proxy = "fake proxy" |
| 47 | + |
| 48 | + def side_effect(): |
| 49 | + os.environ["X509_USER_PROXY"] = arex.proxy |
| 50 | + return S_OK() |
| 51 | + |
| 52 | + mocker.patch( |
| 53 | + "DIRAC.Resources.Computing.AREXComputingElement.AREXComputingElement._prepareProxy", side_effect=side_effect |
| 54 | + ) |
| 55 | + |
| 56 | + result = arex._checkSession() |
| 57 | + assert result["OK"], result |
| 58 | + assert arex.session |
| 59 | + assert arex.session.cert |
| 60 | + assert "Authorization" not in arex.headers, arex.headers |
| 61 | + |
| 62 | + # 5. We set a proxy and a token: the session should just include |
| 63 | + # the token because the proxy is not mandatory |
| 64 | + arex.proxy = "fake proxy" |
| 65 | + arex.token = {"access_token": "fake token"} |
| 66 | + |
| 67 | + result = arex._checkSession() |
| 68 | + assert result["OK"], result |
| 69 | + assert arex.session |
| 70 | + assert not arex.session.cert |
| 71 | + assert "Authorization" in arex.headers, arex.headers |
| 72 | + |
| 73 | + # 6. We make the proxy mandatory: the session should include both the proxy and the token |
| 74 | + result = arex._checkSession(mandatoryProxy=True) |
| 75 | + assert result["OK"], result |
| 76 | + assert arex.session |
| 77 | + assert arex.session.cert |
| 78 | + assert "Authorization" in arex.headers, arex.headers |
| 79 | + |
| 80 | + # 7. Now we just include the token, the proxy is not mandatory: |
| 81 | + # the session should only include the token |
| 82 | + arex.proxy = None |
| 83 | + result = arex._checkSession() |
| 84 | + assert result["OK"], result |
| 85 | + assert arex.session |
| 86 | + assert not arex.session.cert |
| 87 | + assert "Authorization" in arex.headers, arex.headers |
| 88 | + |
| 89 | + # 8. Now we just include the token, but the proxy is mandatory: it should return an error |
| 90 | + result = arex._checkSession(mandatoryProxy=True) |
| 91 | + assert not result["OK"], result |
| 92 | + assert arex.session |
| 93 | + assert not arex.session.cert |
| 94 | + assert "Authorization" not in arex.headers, arex.headers |
0 commit comments