generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: setup a Google Meet class to handle Hangouts * feat: update link creation with new Google Meet
- Loading branch information
Showing
4 changed files
with
209 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from googleapiclient.discovery import Resource # type: ignore | ||
from integrations.google_next.service import ( | ||
execute_google_api_call, | ||
handle_google_api_errors, | ||
get_google_service, | ||
GOOGLE_DELEGATED_ADMIN_EMAIL, | ||
) | ||
|
||
|
||
class GoogleMeet: | ||
""" | ||
A class to simplify the use of various Google Meet API operations across modules. | ||
This class provides methods to interact with the Google Workspace Meet API, including creating new meetings, updating existing meetings, and retrieving meeting details, and includes error handling for Google API errors. | ||
While this class aims to simplify the usage of the Google Meet API, it is always possible | ||
to use the Google API Python client directly as per the official documentation: | ||
(https://googleapis.github.io/google-api-python-client/docs/) | ||
Attributes: | ||
scopes (list): The list of scopes to request. | ||
delegated_email (str): The email address of the user to impersonate. | ||
service (Resource): Optional - An authenticated Google service resource. If provided, the service will be used instead of creating a new one. | ||
""" | ||
|
||
def __init__( | ||
self, scopes=None, delegated_email=None, service: Resource | None = None | ||
): | ||
if not scopes and not service: | ||
raise ValueError("Either scopes or a service must be provided.") | ||
if not delegated_email and not service: | ||
delegated_email = GOOGLE_DELEGATED_ADMIN_EMAIL | ||
self.scopes = scopes | ||
self.delegated_email = delegated_email | ||
self.service = service if service else self._get_google_service() | ||
|
||
def _get_google_service(self) -> Resource: | ||
"""Get authenticated directory service for Google Workspace.""" | ||
return get_google_service("meet", "v2", self.scopes, self.delegated_email) | ||
|
||
@handle_google_api_errors | ||
def create_space(self): | ||
"""Creates a new and empty space in Google Meet.""" | ||
config = {"accessType": "TRUSTED", "entryPointAccess": "ALL"} | ||
return execute_google_api_call( | ||
self.service, "spaces", "create", body={"config": config} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""""Unit tests for the Google Meet API.""" | ||
|
||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from integrations.google_next.meet import GoogleMeet | ||
|
||
|
||
@pytest.fixture(scope="class") | ||
@patch("integrations.google_next.meet.get_google_service") | ||
def google_meet(mock_get_google_service) -> GoogleMeet: | ||
scopes = ["https://www.googleapis.com/auth/meetings.space.created"] | ||
delegated_email = "[email protected]" | ||
mock_get_google_service.return_value = MagicMock() | ||
return GoogleMeet(scopes, delegated_email) | ||
|
||
|
||
class TestGoogleMeet: | ||
@pytest.fixture(autouse=True) | ||
def setup(self, google_meet: GoogleMeet): | ||
self.google_meet = google_meet | ||
|
||
def test_init_without_scopes_and_service(self): | ||
"""Test initialization without scopes and service raises ValueError.""" | ||
with pytest.raises( | ||
ValueError, match="Either scopes or a service must be provided." | ||
): | ||
GoogleMeet(delegated_email="[email protected]") | ||
|
||
@patch( | ||
"integrations.google_next.meet.GOOGLE_DELEGATED_ADMIN_EMAIL", | ||
new="[email protected]", | ||
) | ||
@patch("integrations.google_next.meet.get_google_service") | ||
def test_init_without_delegated_email_and_service(self, mock_get_google_service): | ||
"""Test initialization without delegated email and service uses default email.""" | ||
mock_get_google_service.return_value = MagicMock() | ||
google_meet = GoogleMeet( | ||
scopes=["https://www.googleapis.com/auth/meetings.space.created"] | ||
) | ||
assert google_meet.scopes == [ | ||
"https://www.googleapis.com/auth/meetings.space.created" | ||
] | ||
assert google_meet.delegated_email == "[email protected]" | ||
|
||
@patch("integrations.google_next.meet.get_google_service") | ||
def test_get_google_service(self, mock_get_google_service): | ||
"""Test get_docs_service returns a service.""" | ||
mock_get_google_service.return_value = MagicMock() | ||
service = self.google_meet._get_google_service() | ||
assert service is not None | ||
mock_get_google_service.assert_called_once_with( | ||
"meet", | ||
"v2", | ||
self.google_meet.scopes, | ||
self.google_meet.delegated_email, | ||
) | ||
|
||
@patch("integrations.google_next.meet.execute_google_api_call") | ||
def test_create_space(self, mock_execute_google_api_call): | ||
"""Test create_space returns a response.""" | ||
mock_execute_google_api_call.return_value = { | ||
"name": "spaces/asdfasdf", | ||
"meetingUri": "https://meet.google.com/aaa-bbbb-ccc", | ||
"meetingCode": "aaa-bbbb-ccc", | ||
"config": {"accessType": "TRUSTED", "entryPointAccess": "ALL"}, | ||
} | ||
response = self.google_meet.create_space() | ||
assert response is not None | ||
mock_execute_google_api_call.assert_called_once_with( | ||
self.google_meet.service, | ||
"spaces", | ||
"create", | ||
body={"config": {"accessType": "TRUSTED", "entryPointAccess": "ALL"}}, | ||
) |
Oops, something went wrong.