2017-03-08 12:03:39 +01:00
|
|
|
import ujson
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from mock import patch
|
2019-02-02 23:53:44 +01:00
|
|
|
from typing import Any
|
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
|
2017-05-23 20:57:59 +02:00
|
|
|
from zerver.models import get_user, get_realm
|
|
|
|
|
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:
|
2017-05-23 20:57:59 +02:00
|
|
|
email = self.example_email('hamlet')
|
2017-03-08 12:03:39 +01:00
|
|
|
self.login(email)
|
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def post(subdomain: Any, **kwargs: Any) -> HttpResponse:
|
2017-03-08 12:03:39 +01:00
|
|
|
params = {k: ujson.dumps(v) for k, v in kwargs.items()}
|
2017-08-26 00:21:54 +02:00
|
|
|
return self.client_post('/accounts/webathena_kerberos_login/', params,
|
|
|
|
subdomain=subdomain)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zulip")
|
2017-03-08 12:03:39 +01:00
|
|
|
self.assert_json_error(result, 'Could not find Kerberos credential')
|
|
|
|
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zulip", cred='whatever')
|
2017-03-08 12:03:39 +01:00
|
|
|
self.assert_json_error(result, 'Webathena login not enabled')
|
|
|
|
|
2017-05-24 21:21:35 +02:00
|
|
|
email = str(self.mit_email("starnine"))
|
2017-05-23 20:57:59 +02:00
|
|
|
realm = get_realm('zephyr')
|
2018-08-01 10:53:40 +02:00
|
|
|
user = get_user(email, realm)
|
|
|
|
api_key = get_api_key(user)
|
2017-11-18 00:11:24 +01:00
|
|
|
self.login(email, realm=realm)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def ccache_mock(**kwargs: Any) -> Any:
|
2017-03-08 12:03:39 +01:00
|
|
|
return patch('zerver.views.zephyr.make_ccache', **kwargs)
|
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def ssh_mock(**kwargs: Any) -> Any:
|
2017-03-08 12:03:39 +01:00
|
|
|
return patch('zerver.views.zephyr.subprocess.check_call', **kwargs)
|
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def mirror_mock() -> Any:
|
2017-03-08 12:03:39 +01:00
|
|
|
return self.settings(PERSONAL_ZMIRROR_SERVER='server')
|
|
|
|
|
2017-11-19 04:02:03 +01:00
|
|
|
def logging_mock() -> Any:
|
2017-03-08 12:03:39 +01:00
|
|
|
return patch('logging.exception')
|
|
|
|
|
|
|
|
cred = dict(cname=dict(nameString=['starnine']))
|
|
|
|
|
|
|
|
with ccache_mock(side_effect=KeyError('foo')):
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zephyr", cred=cred)
|
2017-03-08 12:03:39 +01:00
|
|
|
self.assert_json_error(result, 'Invalid Kerberos cache')
|
|
|
|
|
|
|
|
with \
|
|
|
|
ccache_mock(return_value=b'1234'), \
|
|
|
|
ssh_mock(side_effect=KeyError('foo')), \
|
|
|
|
logging_mock() as log:
|
2017-08-26 00:21:54 +02:00
|
|
|
result = post("zephyr", cred=cred)
|
2017-03-08 12:03:39 +01:00
|
|
|
|
|
|
|
self.assert_json_error(result, 'We were unable to setup mirroring for you')
|
|
|
|
log.assert_called_with("Error updating the user's ccache")
|
|
|
|
|
|
|
|
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)
|
|
|
|
ssh.assert_called_with([
|
|
|
|
'ssh',
|
|
|
|
'server',
|
|
|
|
'--',
|
2017-10-05 21:26:54 +02:00
|
|
|
'/home/zulip/python-zulip-api/zulip/integrations/zephyr/process_ccache',
|
2017-03-08 12:03:39 +01:00
|
|
|
'starnine',
|
2018-08-01 10:53:40 +02:00
|
|
|
api_key,
|
2017-03-08 12:03:39 +01:00
|
|
|
'MTIzNA=='])
|
|
|
|
|
|
|
|
# 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(
|
|
|
|
'zerver.views.zephyr.kerberos_alter_egos',
|
|
|
|
{'kerberos_alter_ego': 'starnine'})
|
|
|
|
|
|
|
|
cred = dict(cname=dict(nameString=['kerberos_alter_ego']))
|
|
|
|
with \
|
|
|
|
ccache_mock(return_value=b'1234'), \
|
|
|
|
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)
|
|
|
|
ssh.assert_called_with([
|
|
|
|
'ssh',
|
|
|
|
'server',
|
|
|
|
'--',
|
2017-10-05 21:26:54 +02:00
|
|
|
'/home/zulip/python-zulip-api/zulip/integrations/zephyr/process_ccache',
|
2017-03-08 12:03:39 +01:00
|
|
|
'starnine',
|
2018-08-01 10:53:40 +02:00
|
|
|
api_key,
|
2017-03-08 12:03:39 +01:00
|
|
|
'MTIzNA=='])
|