Skip to content

Commit

Permalink
@W-14870747 - Dependecy updates and disabling Socalapp in db
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbharath committed Feb 6, 2024
1 parent f60024a commit 6c4b58b
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 242 deletions.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def safe_key() -> str:
MIDDLEWARE = [
"metecho.logging_middleware.LoggingMiddleware",
"sfdo_template_helpers.admin.middleware.AdminRestrictMiddleware",
"allauth.account.middleware.AccountMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand Down
34 changes: 21 additions & 13 deletions docs/api/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,11 @@ components:
- Review
- Merged
type: string
description: |-
* `Planned` - Planned
* `In progress` - In Progress
* `Review` - Review
* `Merged` - Merged
FullUser:
type: object
properties:
Expand Down Expand Up @@ -1940,8 +1945,6 @@ components:
self_guided_tour_enabled:
type: boolean
self_guided_tour_state:
type: object
additionalProperties: {}
nullable: true
organizations:
type: array
Expand Down Expand Up @@ -2078,9 +2081,7 @@ components:
properties:
enabled:
type: boolean
state:
type: object
additionalProperties: {}
state: {}
MinimalUser:
type: object
properties:
Expand Down Expand Up @@ -2118,6 +2119,10 @@ components:
- QA
- Playground
type: string
description: |-
* `Dev` - Dev
* `QA` - QA
* `Playground` - Playground
PaginatedEpicList:
type: object
properties:
Expand Down Expand Up @@ -2277,8 +2282,6 @@ components:
description:
type: string
ignored_changes_write:
type: object
additionalProperties: {}
writeOnly: true
org_config_name:
type: string
Expand Down Expand Up @@ -2481,11 +2484,17 @@ components:
- Approved
- Changes requested
type: string
description: |-
* `Approved` - Approved
* `Changes requested` - Changes Requested
RoleEnum:
enum:
- assigned_qa
- assigned_dev
type: string
description: |-
* `assigned_qa` - assigned_qa
* `assigned_dev` - assigned_dev
ScratchOrg:
type: object
properties:
Expand Down Expand Up @@ -2612,8 +2621,6 @@ components:
type: boolean
readOnly: true
installed_packages:
type: object
additionalProperties: {}
readOnly: true
is_omnistudio_installed:
type: boolean
Expand Down Expand Up @@ -2672,8 +2679,6 @@ components:
description:
type: string
ignored_changes_write:
type: object
additionalProperties: {}
writeOnly: true
org_config_name:
type: string
Expand Down Expand Up @@ -2773,8 +2778,6 @@ components:
format: uri
readOnly: true
commits:
type: object
additionalProperties: {}
readOnly: true
origin_sha:
type: string
Expand Down Expand Up @@ -2909,6 +2912,11 @@ components:
- Completed
- Canceled
type: string
description: |-
* `Planned` - Planned
* `In progress` - In Progress
* `Completed` - Completed
* `Canceled` - Canceled
securitySchemes:
cookieAuth:
type: apiKey
Expand Down
3 changes: 0 additions & 3 deletions locales_dev/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"Complete a Task": "Complete a Task",
"Completed item": "Completed item",
"Confirm": "Confirm",
"Confirm Changing Developer and Deleting Dev Org": "Confirm Changing Developer and Deleting Dev Org",
"Confirm Deleting Account": "Confirm Deleting Account",
"Confirm Deleting Epic": "Confirm Deleting Epic",
"Confirm Deleting Org With Unretrieved Changes": "Confirm Deleting Org With Unretrieved Changes",
Expand Down Expand Up @@ -174,7 +173,6 @@
"GitHub Repository Name": "GitHub Repository Name",
"Go Back": "Go Back",
"Heading": "Heading",
"Health Check": "Health Check",
"Hello! What can Metecho help you do today?": "Hello! What can Metecho help you do today?",
"Help Walkthrough": "Help Walkthrough",
"Home": "Home",
Expand Down Expand Up @@ -397,7 +395,6 @@
"Tester": "Tester",
"Tester & Test Org": "Tester & Test Org",
"The current scratch org cannot be transferred to the selected GitHub user. Remove the scratch org before transferring this task or correct the following issues: {{issueDescription}}": "The current scratch org cannot be transferred to the selected GitHub user. Remove the scratch org before transferring this task or correct the following issues: {{issueDescription}}",
"The existing Dev Org for this Task has unretrieved changes. Changing the assigned Developer will also delete the Org, and any changes will be lost. Are you sure you want to do that?": "The existing Dev Org for this Task has unretrieved changes. Changing the assigned Developer will also delete the Org, and any changes will be lost. Are you sure you want to do that?",
"The existing Dev Org for this Task has unretrieved changes. Removing the assigned Developer will also delete the Org, and any changes will be lost. Are you sure you want to do that?": "The existing Dev Org for this Task has unretrieved changes. Removing the assigned Developer will also delete the Org, and any changes will be lost. Are you sure you want to do that?",
"The last line of the log is “{{message}}” If you need support, your scratch org id is {{orgId}}.": "The last line of the log is “{{message}}” If you need support, your scratch org id is {{orgId}}.",
"There are no available Epic Collaborators.": "There are no available Epic Collaborators.",
Expand Down
10 changes: 8 additions & 2 deletions metecho/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import UserManager as BaseUserManager
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
from django.core.exceptions import ValidationError, MultipleObjectsReturned
from django.core.mail import send_mail
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models, transaction
Expand Down Expand Up @@ -240,6 +240,10 @@ def avatar_url(self) -> Optional[str]:
return self.github_account.get_avatar_url()
except (AttributeError, KeyError, TypeError):
return None
# if social app exists in both db and settings retrun sample url
except (MultipleObjectsReturned):
return "https://example.com/avatar/"


@property
def org_id(self) -> Optional[str]:
Expand Down Expand Up @@ -716,8 +720,10 @@ def __str__(self):
return self.name

def save(self, *args, **kwargs):
if not self.id:
super().save(*args, **kwargs)
self.update_status()
return super().save(*args, **kwargs)
return super().save()

def subscribable_by(self, user): # pragma: nocover
return True
Expand Down
2 changes: 1 addition & 1 deletion metecho/oauth2/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
def authentication_error(self, *args, **kwargs):
"""Make sure that auth errors get logged"""
logger.error(f"Social Account authentication error: {args}, {kwargs}")
return super().authentication_error(*args, **kwargs)
return super().on_authentication_error(*args, **kwargs)
2 changes: 1 addition & 1 deletion metecho/oauth2/github/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestGitHubOAuth2Adapter:
@pytest.mark.django_db
def test_complete_login(self, mocker, rf):
mocker.patch("metecho.oauth2.github.views.GitHubOAuth2Adapter.complete_login")
token = mock.MagicMock(app=SocialApp(provider="github"))
token = mock.MagicMock()
request = rf.get("/")
adapter = CustomGitHubOAuth2Adapter(request)
adapter.complete_login(request, None, token)
Expand Down
3 changes: 0 additions & 3 deletions metecho/oauth2/github/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
from ..views import (
LoggingOAuth2CallbackView,
LoggingOAuth2LoginView,
ensure_socialapp_in_db,
)


class CustomGitHubOAuth2Adapter(GitHubOAuth2Adapter):
"""GitHub adapter that can handle the app being configured in settings"""

def complete_login(self, request, app, token, **kwargs):
# make sure token is attached to a SocialApp in the db
ensure_socialapp_in_db(token)
return super().complete_login(request, app, token, **kwargs)


Expand Down
18 changes: 14 additions & 4 deletions metecho/oauth2/salesforce/tests/provider.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import pytest
from ..provider import CustomSalesforceProvider


def test_get_auth_params(rf):
@pytest.mark.django_db
def test_get_auth_params(rf, social_app_factory):
request = rf.get("/")
result = CustomSalesforceProvider(request).get_auth_params(request, None)
app = social_app_factory(
provider="salesforce",
)
provider = CustomSalesforceProvider(request, app)
result = provider.get_auth_params(request, None)
assert "prompt" in result and result["prompt"] == "login"


def test_extract_uid(rf):
@pytest.mark.django_db
def test_extract_uid(rf, social_app_factory):
request = rf.get("/")
provider = CustomSalesforceProvider(request)
app = social_app_factory(
provider="salesforce",
)
provider = CustomSalesforceProvider(request, app)
result = provider.extract_uid({"organization_id": "ORG", "user_id": "USER"})
assert result == "ORG/USER"
3 changes: 0 additions & 3 deletions metecho/oauth2/salesforce/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from ..views import (
LoggingOAuth2CallbackView,
LoggingOAuth2LoginView,
ensure_socialapp_in_db,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -66,8 +65,6 @@ def get_org_details(self, extra_data, token):
return resp.json()

def complete_login(self, request, app, token, **kwargs):
# make sure token is attached to a SocialApp in the db
ensure_socialapp_in_db(token)

token = fernet_decrypt(token.token)
headers = {"Authorization": f"Bearer {token}"}
Expand Down
2 changes: 1 addition & 1 deletion metecho/oauth2/tests/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def test_authentication_error_logs(mocker):
mocker.patch(
"allauth.socialaccount.adapter.DefaultSocialAccountAdapter.authentication_error"
"allauth.socialaccount.adapter.DefaultSocialAccountAdapter.on_authentication_error"
) # noqa
error = mocker.patch("metecho.oauth2.adapter.logger.error")
adapter = CustomSocialAccountAdapter()
Expand Down
19 changes: 0 additions & 19 deletions metecho/oauth2/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging

from allauth.socialaccount import providers
from allauth.socialaccount.models import SocialApp
from allauth.socialaccount.providers.oauth2.views import (
OAuth2CallbackView,
OAuth2LoginView,
Expand All @@ -11,23 +9,6 @@
logger = logging.getLogger(__name__)


def ensure_socialapp_in_db(token):
"""Make sure that token is attached to a SocialApp in the db.
Since we are using SocialApps constructed from settings,
there are none in the db for tokens to be related to
unless we create them here.
"""
if token.app.pk is None:
provider = providers.registry.by_id(token.app.provider)
app, created = SocialApp.objects.get_or_create(
provider=provider.id,
name=provider.name,
client_id="-",
)
token.app = app


class LoggingOAuth2LoginView(OAuth2LoginView):
def dispatch(self, request, *args, **kwargs):
ret = super().dispatch(request, *args, **kwargs)
Expand Down
Loading

0 comments on commit 6c4b58b

Please sign in to comment.