2020-10-09 03:32:00 +02:00
|
|
|
import subprocess
|
2022-06-08 04:52:09 +02:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest.mock import patch
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2017-03-08 12:03:39 +01:00
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-08-01 10:53:40 +02:00
|
|
|
from zerver.lib.users import get_api_key
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import get_realm
|
|
|
|
from zerver.models.users import get_user
|
2017-05-23 20:57:59 +02:00
|
|
|
|
2022-06-08 04:52:09 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from django.test.client import _MonkeyPatchedWSGIResponse as TestHttpResponse
|
|
|
|
|
2017-03-08 12:03:39 +01:00
|
|
|
|
|
|
|
class ZephyrTest(ZulipTestCase):
|
2017-11-19 04:02:03 +01:00
|
|
|
def test_webathena_kerberos_login(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2022-06-08 04:52:09 +02:00
|
|
|
def post(subdomain: Any, **kwargs: Any) -> "TestHttpResponse":
|
2020-08-07 01:09:47 +02:00
|
|
|
params = {k: orjson.dumps(v).decode() for k, v in kwargs.items()}
|
2021-02-12 08:19:30 +01:00
|
|
|
return self.client_post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/accounts/webathena_kerberos_login/", params, subdomain=subdomain
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zulip")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Could not find Kerberos credential")
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
result = post("zulip", cred="whatever")
|
|
|
|
self.assert_json_error(result, "Webathena login not enabled")
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2017-05-24 21:21:35 +02:00
|
|
|
email = str(self.mit_email("starnine"))
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zephyr")
|
2018-08-01 10:53:40 +02:00
|
|
|
user = get_user(email, realm)
|
|
|
|
api_key = get_api_key(user)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def ccache_mock(**kwargs: Any) -> Any:
|
2021-02-12 08:20:45 +01:00
|
|
|
return patch("zerver.views.zephyr.make_ccache", **kwargs)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def ssh_mock(**kwargs: Any) -> Any:
|
2021-02-12 08:20:45 +01:00
|
|
|
return patch("zerver.views.zephyr.subprocess.check_call", **kwargs)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def mirror_mock() -> Any:
|
2021-02-12 08:20:45 +01:00
|
|
|
return self.settings(PERSONAL_ZMIRROR_SERVER="server")
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
cred = dict(cname=dict(nameString=["starnine"]))
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with ccache_mock(side_effect=KeyError("foo")):
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zephyr", cred=cred)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Invalid Kerberos cache")
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with ccache_mock(return_value=b"1234"), ssh_mock(
|
2021-02-12 08:19:30 +01:00
|
|
|
side_effect=subprocess.CalledProcessError(1, [])
|
2022-07-28 21:16:40 +02:00
|
|
|
), mirror_mock(), self.assertLogs(level="ERROR") as log:
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zephyr", cred=cred)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
docs: Add missing space to compound verbs “log in”, “set up”, etc.
Noun: backup, checkout, cleanup, login, logout, setup, shutdown, signup,
timeout.
Verb: back up, check out, clean up, log in, log out, set up, shut
down, sign up, time out.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-25 23:05:38 +02:00
|
|
|
self.assert_json_error(result, "We were unable to set up mirroring for you")
|
2020-12-23 21:45:16 +01:00
|
|
|
self.assertIn("Error updating the user's ccache", log.output[0])
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2022-07-28 21:16:40 +02:00
|
|
|
with ccache_mock(return_value=b"1234"), self.assertLogs(level="ERROR") as log:
|
|
|
|
result = post("zephyr", cred=cred)
|
|
|
|
|
|
|
|
self.assert_json_error(result, "We were unable to set up mirroring for you")
|
|
|
|
self.assertIn("PERSONAL_ZMIRROR_SERVER is not properly configured", log.output[0])
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with ccache_mock(return_value=b"1234"), mirror_mock(), ssh_mock() as ssh:
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zephyr", cred=cred)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:19:30 +01:00
|
|
|
ssh.assert_called_with(
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
"ssh",
|
|
|
|
"server",
|
|
|
|
"--",
|
|
|
|
f"/home/zulip/python-zulip-api/zulip/integrations/zephyr/process_ccache starnine {api_key} MTIzNA==",
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
|
|
|
)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
|
|
|
# Accounts whose Kerberos usernames are known not to match their
|
|
|
|
# zephyr accounts are hardcoded, and should be handled properly.
|
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def kerberos_alter_egos_mock() -> Any:
|
2017-03-08 12:03:39 +01:00
|
|
|
return patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"zerver.views.zephyr.kerberos_alter_egos", {"kerberos_alter_ego": "starnine"}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
cred = dict(cname=dict(nameString=["kerberos_alter_ego"]))
|
2021-02-12 08:19:30 +01:00
|
|
|
with ccache_mock(
|
2021-02-12 08:20:45 +01:00
|
|
|
return_value=b"1234"
|
2021-02-12 08:19:30 +01:00
|
|
|
), mirror_mock(), ssh_mock() as ssh, kerberos_alter_egos_mock():
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zephyr", cred=cred)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:19:30 +01:00
|
|
|
ssh.assert_called_with(
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
"ssh",
|
|
|
|
"server",
|
|
|
|
"--",
|
|
|
|
f"/home/zulip/python-zulip-api/zulip/integrations/zephyr/process_ccache starnine {api_key} MTIzNA==",
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
|
|
|
)
|