Skip to content

Commit

Permalink
add changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Jan 21, 2025
1 parent 1191956 commit 080dec9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
18 changes: 11 additions & 7 deletions breathecode/authenticate/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,23 @@ class UserAdmin(UserAdmin):
actions = [clean_all_tokens, clean_expired_tokens, send_reset_password, clear_user_password]

def get_queryset(self, request):
self.callback_url = "https://4geeks.com"
self.callback_url = str(base64.urlsafe_b64encode(self.callback_url.encode("utf-8")), "utf-8")

self.github_callback = "https://4geeks.com"
self.github_callback = str(base64.urlsafe_b64encode(self.github_callback.encode("utf-8")), "utf-8")
return super(UserAdmin, self).get_queryset(request)

def github_login(self, obj):

return format_html(
f"<a rel='noopener noreferrer' target='_blank' href='/v1/auth/github/?user={obj.id}&url={self.github_callback}'>connect github</a>"
f"<a rel='noopener noreferrer' target='_blank' href='/v1/auth/github/?user={obj.id}&url={self.callback_url}'>connect github</a>"
)

def google_login(self, obj):
self.github_callback = "https://4geeks.com"
self.github_callback = str(base64.urlsafe_b64encode(self.github_callback.encode("utf-8")), "utf-8")

return format_html(
"<a rel='noopener noreferrer' target='_blank' href='/v1/auth/academy/google'>connect google</a>"
f"<a rel='noopener noreferrer' target='_blank' href='/v1/auth/academy/google?academysettings=set&url={self.callback_url}'>connect google</a>"
)


Expand Down Expand Up @@ -494,8 +498,8 @@ class AcademyAuthSettingsAdmin(admin.ModelAdmin):
def get_queryset(self, request):
self.admin_request = request

self.github_callback = "https://4geeks.com"
self.github_callback = str(base64.urlsafe_b64encode(self.github_callback.encode("utf-8")), "utf-8")
self.callback_url = "https://4geeks.com"
self.callback_url = str(base64.urlsafe_b64encode(self.callback_url.encode("utf-8")), "utf-8")
return super(AcademyAuthSettingsAdmin, self).get_queryset(request)

def github_errors(self, obj):
Expand All @@ -511,7 +515,7 @@ def authenticate_github(self, obj):

scopes = str(base64.urlsafe_b64encode(b"user repo admin:org"), "utf-8")
return format_html(
f"<a href='/v1/auth/github?user={obj.github_owner.id}&url={self.github_callback}&scope={scopes}'>connect github</a>"
f"<a href='/v1/auth/github?user={obj.github_owner.id}&url={self.callback_url}&scope={scopes}'>connect github</a>"
)

def authenticate_google(self, obj):
Expand Down
13 changes: 6 additions & 7 deletions breathecode/authenticate/tests/urls/tests_academy_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from rest_framework import status
from rest_framework.test import APIClient

now = timezone.now()
callback = str(base64.urlsafe_b64encode(b"https://potato.io"), "utf-8")


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -165,13 +165,13 @@ def test_no_callback_url(database: capy.Database, client: APIClient, format: cap
def test_redirect_to_google(database: capy.Database, client: APIClient, format: capy.Format, utc_now: datetime):
model = database.create(token=1, user=1)
print(utc_now)
url = reverse_lazy("authenticate:academy_google") + f"?token={model.token.key}&url=https://potato.io"
url = reverse_lazy("authenticate:academy_google") + f"?token={model.token.key}&url={callback}"
response = client.get(url, headers={"Academy": 1})

assert response.status_code == status.HTTP_302_FOUND

query_params = {
"url": "https://potato.io",
"url": callback,
}
query_string = urlencode(query_params)

Expand All @@ -197,7 +197,7 @@ def test_no_capability_with_academy_settings(
print(utc_now)
url = (
reverse_lazy("authenticate:academy_google")
+ f"?token={model.token.key}&url=https://potato.io&academysettings={academy_settings}"
+ f"?token={model.token.key}&url={callback}&academysettings={academy_settings}"
)
response = client.get(url, headers={"Academy": 1})

Expand Down Expand Up @@ -231,18 +231,17 @@ def test_redirect_to_google_with_academy_settings(
city=1,
country=1,
)
print(utc_now)
url = (
reverse_lazy("authenticate:academy_google")
+ f"?token={model.token.key}&url=https://potato.io&academysettings={academy_settings}"
+ f"?token={model.token.key}&url={callback}&academysettings={academy_settings}"
)
response = client.get(url, headers={"Academy": 1})

assert response.status_code == status.HTTP_302_FOUND

query_params = {
"academysettings": academy_settings,
"url": "https://potato.io",
"url": callback,
}
query_string = urlencode(query_params)

Expand Down

0 comments on commit 080dec9

Please sign in to comment.