Skip to content

Commit

Permalink
update google callback and forward meet url
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Sep 16, 2024
1 parent 87dc60b commit 8ddd16d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
9 changes: 8 additions & 1 deletion breathecode/authenticate/tests/urls/tests_google_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
}

Expand Down
17 changes: 11 additions & 6 deletions breathecode/authenticate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions breathecode/mentorship/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8ddd16d

Please sign in to comment.