From 8ddd16d084bd29c87b0bb60e5d4caf512703e4e0 Mon Sep 17 00:00:00 2001 From: jefer94 Date: Mon, 16 Sep 2024 11:55:19 -0500 Subject: [PATCH] update google callback and forward meet url --- .../tests/urls/tests_google_token.py | 9 +- breathecode/authenticate/views.py | 17 ++-- .../tests_meet_slug_service_slug.py | 94 +++++++++++++++++++ breathecode/mentorship/views.py | 3 + 4 files changed, 116 insertions(+), 7 deletions(-) diff --git a/breathecode/authenticate/tests/urls/tests_google_token.py b/breathecode/authenticate/tests/urls/tests_google_token.py index 0ac67b6aa..cc0f84544 100644 --- a/breathecode/authenticate/tests/urls/tests_google_token.py +++ b/breathecode/authenticate/tests/urls/tests_google_token.py @@ -80,7 +80,14 @@ def test_redirect(database: capy.Database, client: capy.Client, token: Any): "client_id": "123456.apps.googleusercontent.com", "redirect_uri": "https://breathecode.herokuapp.com/v1/auth/google/callback", "access_type": "offline", - "scope": "https://www.googleapis.com/auth/calendar.events", + "scope": " ".join( + [ + "https://www.googleapis.com/auth/meetings.space.created", + # "https://www.googleapis.com/auth/meetings.space.readonly", + "https://www.googleapis.com/auth/drive.meet.readonly", + # "https://www.googleapis.com/auth/calendar.events", + ] + ), "state": f"token={model.token.key}&url={callback_url}", } diff --git a/breathecode/authenticate/views.py b/breathecode/authenticate/views.py index 948a44e98..e64a180d2 100644 --- a/breathecode/authenticate/views.py +++ b/breathecode/authenticate/views.py @@ -2104,15 +2104,20 @@ async def save_google_token(request): if "refresh_token" in body: refresh = body["refresh_token"] - await CredentialsGoogle.objects.filter(user__id=user.id).adelete() - - google_credentials = CredentialsGoogle( + google_credentials, created = await CredentialsGoogle.objects.aget_or_create( user=user, - token=body["access_token"], - refresh_token=refresh, expires_at=timezone.now() + timedelta(seconds=body["expires_in"]), + defaults={ + "token": body["access_token"], + "refresh_token": refresh, + }, ) - await google_credentials.asave() + if created is False: + google_credentials.token = body["access_token"] + if refresh: + google_credentials.refresh_token = refresh + + await google_credentials.asave() return HttpResponseRedirect(redirect_to=state["url"][0] + "?token=" + token.key) diff --git a/breathecode/mentorship/tests/urls_shortner/tests_meet_slug_service_slug.py b/breathecode/mentorship/tests/urls_shortner/tests_meet_slug_service_slug.py index 9a2c5f01c..3143e0cd2 100644 --- a/breathecode/mentorship/tests/urls_shortner/tests_meet_slug_service_slug.py +++ b/breathecode/mentorship/tests/urls_shortner/tests_meet_slug_service_slug.py @@ -1363,6 +1363,100 @@ def test_with_mentor_profile__passing_session__passing_mentee__passing_redirect( self.bc.database.delete("auth.Permission") self.bc.database.delete("payments.Service") + """ + 🔽🔽🔽 Google Meet + """ + + @patch("breathecode.mentorship.actions.mentor_is_ready", MagicMock()) + @patch( + "os.getenv", + MagicMock( + side_effect=apply_get_env( + { + "DAILY_API_URL": URL, + "DAILY_API_KEY": API_KEY, + } + ) + ), + ) + @patch( + "requests.request", + apply_requests_request_mock( + [ + ( + 201, + f"{URL}/v1/rooms", + { + "name": ROOM_NAME, + "url": ROOM_URL, + }, + ) + ] + ), + ) + def test_google_meet_redirect(self): + cases = [ + { + "status": x, + "online_meeting_url": self.bc.fake.url(), + "booking_url": self.bc.fake.url(), + } + for x in ["ACTIVE", "UNLISTED"] + ] + service = {"consumer": "JOIN_MENTORSHIP"} + + id = 0 + for mentor_profile in cases: + id += 1 + + user = {"first_name": "", "last_name": ""} + base = self.bc.database.create(user=user, token=1, service=service) + + mentorship_session = {"mentee_id": None, "online_meeting_url": "https://meet.google.com/abc123"} + academy = {"available_as_saas": False} + model = self.bc.database.create( + mentor_profile=mentor_profile, + mentorship_session=mentorship_session, + user=user, + mentorship_service={"language": "en", "video_provider": "GOOGLE_MEET"}, + academy=academy, + ) + + model.mentorship_session.mentee = None + model.mentorship_session.save() + + querystring = self.bc.format.to_querystring( + { + "token": base.token.key, + } + ) + url = ( + reverse_lazy( + "mentorship_shortner:meet_slug_service_slug", + kwargs={"mentor_slug": model.mentor_profile.slug, "service_slug": model.mentorship_service.slug}, + ) + + f"?{querystring}" + ) + response = self.client.get(url) + + self.assertEqual(response.status_code, status.HTTP_302_FOUND) + self.assertEqual(response.url, model.mentorship_session.online_meeting_url) + + self.assertEqual( + self.bc.database.list_of("mentorship.MentorProfile"), + [ + self.bc.format.to_dict(model.mentor_profile), + ], + ) + self.assertEqual(self.bc.database.list_of("payments.Consumable"), []) + self.assertEqual(self.bc.database.list_of("payments.ConsumptionSession"), []) + + # teardown + self.bc.database.delete("mentorship.MentorProfile") + self.bc.database.delete("auth.Permission") + self.bc.database.delete("auth.User") + self.bc.database.delete("payments.Service") + """ 🔽🔽🔽 GET without MentorProfile, good statuses with mentor urls, MentorshipSession without mentee passing session and mentee but mentee does not exist, user without name diff --git a/breathecode/mentorship/views.py b/breathecode/mentorship/views.py index 663040152..bf496635d 100644 --- a/breathecode/mentorship/views.py +++ b/breathecode/mentorship/views.py @@ -434,6 +434,9 @@ def render_start_session(self, session): if "heading" not in obj: obj["heading"] = session.mentor.academy.name + if session.online_meeting_url and "meet.google.com" in session.online_meeting_url: + return HttpResponseRedirect(session.online_meeting_url) + return render( self.request, "message.html",