2020-04-27 22:41:31 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
import responses
|
2020-04-27 22:41:31 +02:00
|
|
|
from django.http import HttpResponseRedirect
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-12-28 20:45:54 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2019-11-16 09:26:28 +01:00
|
|
|
|
2018-12-28 20:45:54 +01:00
|
|
|
|
2020-01-18 08:57:08 +01:00
|
|
|
class TestVideoCall(ZulipTestCase):
|
2018-12-28 20:45:54 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2019-11-16 09:26:28 +01:00
|
|
|
self.user = self.example_user("hamlet")
|
|
|
|
self.login_user(self.user)
|
|
|
|
|
|
|
|
def test_register_video_request_no_settings(self) -> None:
|
|
|
|
with self.settings(VIDEO_ZOOM_CLIENT_ID=None):
|
|
|
|
response = self.client_get("/calls/zoom/register")
|
|
|
|
self.assert_json_error(
|
2021-02-12 08:19:30 +01:00
|
|
|
response,
|
|
|
|
"Zoom credentials have not been configured",
|
2019-11-16 09:26:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_register_video_request(self) -> None:
|
|
|
|
response = self.client_get("/calls/zoom/register")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2018-12-28 20:45:54 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
@responses.activate
|
2018-12-28 20:45:54 +01:00
|
|
|
def test_create_video_request_success(self) -> None:
|
2019-11-16 09:26:28 +01:00
|
|
|
responses.add(
|
|
|
|
responses.POST,
|
|
|
|
"https://zoom.us/oauth/token",
|
|
|
|
json={"access_token": "oldtoken", "expires_in": -60},
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client_get(
|
|
|
|
"/calls/zoom/complete",
|
|
|
|
{"code": "code", "state": '{"realm":"zulip","sid":""}'},
|
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
responses.replace(
|
|
|
|
responses.POST,
|
|
|
|
"https://zoom.us/oauth/token",
|
|
|
|
json={"access_token": "newtoken", "expires_in": 60},
|
|
|
|
)
|
|
|
|
|
|
|
|
responses.add(
|
|
|
|
responses.POST,
|
|
|
|
"https://api.zoom.us/v2/users/me/meetings",
|
|
|
|
json={"join_url": "example.com"},
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client_post("/json/calls/zoom/create")
|
|
|
|
self.assertEqual(
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.calls[-1].request.url,
|
|
|
|
"https://api.zoom.us/v2/users/me/meetings",
|
2019-11-16 09:26:28 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.calls[-1].request.headers["Authorization"],
|
|
|
|
"Bearer newtoken",
|
2019-11-16 09:26:28 +01:00
|
|
|
)
|
|
|
|
json = self.assert_json_success(response)
|
|
|
|
self.assertEqual(json["url"], "example.com")
|
|
|
|
|
|
|
|
self.logout()
|
|
|
|
self.login_user(self.user)
|
|
|
|
|
|
|
|
response = self.client_post("/json/calls/zoom/create")
|
|
|
|
self.assert_json_error(response, "Invalid Zoom access token")
|
|
|
|
|
|
|
|
def test_create_video_realm_redirect(self) -> None:
|
|
|
|
response = self.client_get(
|
|
|
|
"/calls/zoom/complete",
|
|
|
|
{"code": "code", "state": '{"realm":"zephyr","sid":"somesid"}'},
|
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertIn("http://zephyr.testserver/", response.url)
|
|
|
|
self.assertIn("somesid", response.url)
|
|
|
|
|
|
|
|
def test_create_video_sid_error(self) -> None:
|
|
|
|
response = self.client_get(
|
|
|
|
"/calls/zoom/complete",
|
|
|
|
{"code": "code", "state": '{"realm":"zulip","sid":"bad"}'},
|
|
|
|
)
|
|
|
|
self.assert_json_error(response, "Invalid Zoom session identifier")
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_create_video_credential_error(self) -> None:
|
|
|
|
responses.add(responses.POST, "https://zoom.us/oauth/token", status=400)
|
|
|
|
|
|
|
|
response = self.client_get(
|
|
|
|
"/calls/zoom/complete",
|
|
|
|
{"code": "code", "state": '{"realm":"zulip","sid":""}'},
|
|
|
|
)
|
|
|
|
self.assert_json_error(response, "Invalid Zoom credentials")
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_create_video_refresh_error(self) -> None:
|
|
|
|
responses.add(
|
|
|
|
responses.POST,
|
|
|
|
"https://zoom.us/oauth/token",
|
|
|
|
json={"access_token": "token", "expires_in": -60},
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client_get(
|
|
|
|
"/calls/zoom/complete",
|
|
|
|
{"code": "code", "state": '{"realm":"zulip","sid":""}'},
|
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
responses.replace(responses.POST, "https://zoom.us/oauth/token", status=400)
|
|
|
|
|
|
|
|
response = self.client_post("/json/calls/zoom/create")
|
|
|
|
self.assert_json_error(response, "Invalid Zoom access token")
|
2018-12-28 20:45:54 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
@responses.activate
|
|
|
|
def test_create_video_request_error(self) -> None:
|
|
|
|
responses.add(
|
|
|
|
responses.POST,
|
|
|
|
"https://zoom.us/oauth/token",
|
|
|
|
json={"access_token": "token"},
|
|
|
|
)
|
2018-12-28 20:45:54 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
responses.add(
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.POST,
|
|
|
|
"https://api.zoom.us/v2/users/me/meetings",
|
|
|
|
status=400,
|
2019-11-16 09:26:28 +01:00
|
|
|
)
|
2019-01-17 00:25:22 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
response = self.client_get(
|
|
|
|
"/calls/zoom/complete",
|
|
|
|
{"code": "code", "state": '{"realm":"zulip","sid":""}'},
|
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-01-17 00:54:01 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
response = self.client_post("/json/calls/zoom/create")
|
|
|
|
self.assert_json_error(response, "Failed to create Zoom call")
|
2019-01-17 00:54:01 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
responses.replace(
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.POST,
|
|
|
|
"https://api.zoom.us/v2/users/me/meetings",
|
|
|
|
status=401,
|
2019-11-16 09:26:28 +01:00
|
|
|
)
|
2019-01-17 00:54:01 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
response = self.client_post("/json/calls/zoom/create")
|
|
|
|
self.assert_json_error(response, "Invalid Zoom access token")
|
2019-01-17 00:54:01 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
@responses.activate
|
|
|
|
def test_deauthorize_zoom_user(self) -> None:
|
|
|
|
responses.add(responses.POST, "https://api.zoom.us/oauth/data/compliance")
|
2018-12-28 20:45:54 +01:00
|
|
|
|
2019-11-16 09:26:28 +01:00
|
|
|
response = self.client_post(
|
|
|
|
"/calls/zoom/deauthorize",
|
|
|
|
"""\
|
|
|
|
{
|
|
|
|
"event": "app_deauthorized",
|
|
|
|
"payload": {
|
|
|
|
"user_data_retention": "false",
|
|
|
|
"account_id": "EabCDEFghiLHMA",
|
|
|
|
"user_id": "z9jkdsfsdfjhdkfjQ",
|
|
|
|
"signature": "827edc3452044f0bc86bdd5684afb7d1e6becfa1a767f24df1b287853cf73000",
|
|
|
|
"deauthorization_time": "2019-06-17T13:52:28.632Z",
|
|
|
|
"client_id": "ADZ9k9bTWmGUoUbECUKU_a"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
self.assert_json_success(response)
|
2020-04-27 22:41:31 +02:00
|
|
|
|
|
|
|
def test_create_bigbluebutton_link(self) -> None:
|
|
|
|
with mock.patch('zerver.views.video_calls.random.randint', return_value="1"), mock.patch(
|
2021-02-12 08:19:30 +01:00
|
|
|
'secrets.token_bytes', return_value=b"\x00" * 7
|
|
|
|
):
|
2020-04-27 22:41:31 +02:00
|
|
|
response = self.client_get("/json/calls/bigbluebutton/create")
|
|
|
|
self.assert_json_success(response)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
response.json()['url'],
|
|
|
|
"/calls/bigbluebutton/join?meeting_id=%22zulip-1%22&password=%22AAAAAAAAAA%22"
|
|
|
|
"&checksum=%22697939301a52d3a2f0b3c3338895c1a5ab528933%22",
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_join_bigbluebutton_redirect(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://bbb.example.com/bigbluebutton/api/create?meetingID=zulip-1&moderatorPW=a&attendeePW=aa&checksum=check",
|
|
|
|
"<response><returncode>SUCCESS</returncode><messageKey/></response>",
|
|
|
|
)
|
|
|
|
response = self.client_get(
|
|
|
|
"/calls/bigbluebutton/join",
|
|
|
|
{"meeting_id": '"zulip-1"', "password": '"a"', "checksum": '"check"'},
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(isinstance(response, HttpResponseRedirect), True)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
response.url,
|
|
|
|
"https://bbb.example.com/bigbluebutton/api/join?meetingID=zulip-1&password=a"
|
|
|
|
"&fullName=King%20Hamlet&checksum=7ddbb4e7e5aa57cb8c58db12003f3b5b040ff530",
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_join_bigbluebutton_redirect_wrong_check(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://bbb.example.com/bigbluebutton/api/create?meetingID=zulip-1&moderatorPW=a&attendeePW=aa&checksum=check",
|
|
|
|
"<response><returncode>FAILED</returncode><messageKey>checksumError</messageKey>"
|
|
|
|
"<message>You did not pass the checksum security check</message></response>",
|
|
|
|
)
|
|
|
|
response = self.client_get(
|
|
|
|
"/calls/bigbluebutton/join",
|
|
|
|
{"meeting_id": '"zulip-1"', "password": '"a"', "checksum": '"check"'},
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
self.assert_json_error(response, "Error authenticating to the Big Blue Button server.")
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_join_bigbluebutton_redirect_server_error(self) -> None:
|
|
|
|
# Simulate bbb server error
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://bbb.example.com/bigbluebutton/api/create?meetingID=zulip-1&moderatorPW=a&attendeePW=aa&checksum=check",
|
|
|
|
"",
|
|
|
|
status=500,
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
response = self.client_get(
|
2021-02-12 08:19:30 +01:00
|
|
|
"/calls/bigbluebutton/join",
|
|
|
|
{"meeting_id": '"zulip-1"', "password": '"a"', "checksum": '"check"'},
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
self.assert_json_error(response, "Error connecting to the Big Blue Button server.")
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_join_bigbluebutton_redirect_error_by_server(self) -> None:
|
|
|
|
# Simulate bbb server error
|
2021-02-12 08:19:30 +01:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://bbb.example.com/bigbluebutton/api/create?meetingID=zulip-1&moderatorPW=a&attendeePW=aa&checksum=check",
|
|
|
|
"<response><returncode>FAILURE</returncode><messageKey>otherFailure</messageKey></response>",
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
response = self.client_get(
|
2021-02-12 08:19:30 +01:00
|
|
|
"/calls/bigbluebutton/join",
|
|
|
|
{"meeting_id": '"zulip-1"', "password": '"a"', "checksum": '"check"'},
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
self.assert_json_error(response, "Big Blue Button server returned an unexpected error.")
|
|
|
|
|
|
|
|
def test_join_bigbluebutton_redirect_not_configured(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
with self.settings(BIG_BLUE_BUTTON_SECRET=None, BIG_BLUE_BUTTON_URL=None):
|
2020-04-27 22:41:31 +02:00
|
|
|
response = self.client_get(
|
2021-02-12 08:19:30 +01:00
|
|
|
"/calls/bigbluebutton/join",
|
|
|
|
{"meeting_id": '"zulip-1"', "password": '"a"', "checksum": '"check"'},
|
|
|
|
)
|
2020-04-27 22:41:31 +02:00
|
|
|
self.assert_json_error(response, "Big Blue Button is not configured.")
|