Change mock responses to be a single list of mocked response values
@@ -143,7 +143,16 @@ class TestClient(unittest.TestCase):
def test_authenticate(self):
"""A client is authenticated."""
response = mock.MagicMock(status_code=200)
- response.json.return_value = {"metadata": {"auth": "untrusted"}}
+ response.json.side_effect = [
+ {"metadata": {"auth": "untrusted"}},
+ {
+ "metadata": {
+ "type": "client",
+ "fingerprint": "eaf55b72fc23aa516d709271df9b0116064bf8cfa009cf34c67c33ad32c2320c",
+ }
+ },
+ {"metadata": {"auth": "trusted"}},
+ ]
self.get.return_value = response
certs = (
@@ -152,24 +161,6 @@ class TestClient(unittest.TestCase):
)
an_client = client.Client("https://lxd", cert=certs)
- get_count = []
-
- def _get(*args, **kwargs):
- if len(get_count) == 0:
- get_count.append(None)
- return {
- "metadata": {
- "type": "client",
- "fingerprint": "eaf55b72fc23aa516d709271df9b0116064bf8cfa009cf34c67c33ad32c2320c",
- }
- }
- else:
- return {"metadata": {"auth": "trusted"}}
-
- response = mock.MagicMock(status_code=200)
- response.json.side_effect = _get
- self.get.return_value = response
-
an_client.authenticate("test-password")
self.assertTrue(an_client.trusted)
@@ -230,43 +221,34 @@ class TestClient(unittest.TestCase):
default one is requested.
"""
response = mock.MagicMock(status_code=200)
- response.json.return_value = {
+ response.json.side_effect = [
+ {
"metadata": {
"auth": "untrusted",
"api_extensions": [],
}
- }
- self.get.return_value = response
-
- certs = (
- os.path.join(os.path.dirname(__file__), "lxd.crt"),
- os.path.join(os.path.dirname(__file__), "lxd.key"),
- )
- an_client = client.Client("https://lxd", cert=certs, project="default")
-
- get_count = []
-
- def _get(*args, **kwargs):
- if len(get_count) == 0:
- get_count.append(None)
- return {
+ },
+ {
"metadata": {
"type": "client",
"fingerprint": "eaf55b72fc23aa516d709271df9b0116064bf8cfa009cf34c67c33ad32c2320c",
}
- }
- else:
- return {
+ },
+ {
"metadata": {
"auth": "trusted",
"environment": {},
}
- }
-
- response = mock.MagicMock(status_code=200)
- response.json.side_effect = _get
+ },
+ ]
self.get.return_value = response
+ certs = (
+ os.path.join(os.path.dirname(__file__), "lxd.crt"),
+ os.path.join(os.path.dirname(__file__), "lxd.key"),
+ )
+ an_client = client.Client("https://lxd", cert=certs, project="default")
+
an_client.authenticate("test-password")
self.assertTrue(an_client.trusted)
