Skip to content

Commit

Permalink
build: Merge branch 'fix/complete-user-profile' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Mar 11, 2024
2 parents a6eddfc + a2fd250 commit 426091f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
8 changes: 6 additions & 2 deletions terraso_backend/apps/auth/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ def create_test_access_token(self, user):
raise ValueError("User is not a test user")
return self.create_token(user, None, {"test": True, "access": True})

def create_access_token(self, user):
return self.create_token(user, self.JWT_ACCESS_EXP_DELTA_SECONDS, {"access": True})
def create_access_token(self, user, created_with_service=None):
return self.create_token(
user,
self.JWT_ACCESS_EXP_DELTA_SECONDS,
{"access": True, "createdWithService": created_with_service},
)

def verify_access_token(self, token):
decoded = self._verify_token(token)
Expand Down
6 changes: 3 additions & 3 deletions terraso_backend/apps/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def process_callback(self, req):

try:
user, created_with_service = self.process_signup()
access_token, refresh_token = terraso_login(self.request, user)
access_token, refresh_token = terraso_login(self.request, user, created_with_service)
except Exception as exc:
logger.exception("Error attempting create access and refresh tokens")
return HttpResponse(f"Error: {exc}", status=400)
Expand Down Expand Up @@ -244,8 +244,8 @@ def post(self, request, *args, **kwargs):
return HttpResponse("OK", status=200)


def terraso_login(request, user):
access_token = jwt_service.create_access_token(user)
def terraso_login(request, user, created_with_service=None):
access_token = jwt_service.create_access_token(user, created_with_service)
refresh_token = jwt_service.create_refresh_token(user)
dj_login(request, user, backend="django.contrib.auth.backends.ModelBackend")

Expand Down
16 changes: 16 additions & 0 deletions terraso_backend/tests/auth/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from oauth2_provider.views import UserInfoView

from apps.auth.providers import AppleProvider, GoogleProvider
from apps.auth.services import JWTService
from apps.core.models import User

pytestmark = pytest.mark.django_db
Expand Down Expand Up @@ -259,3 +260,18 @@ def test_userinfo_claims(rf, bearer_header, user):
assert resp.status_code == 200
resp_json = json.loads(resp.content)
assert resp_json["email"] == user.email


@mock_aws
def test_sign_up_created_with_service(client, access_tokens_google, respx_mock):
respx_mock.post(GoogleProvider.GOOGLE_TOKEN_URI).mock(
return_value=Response(200, json=access_tokens_google)
)
url = reverse("terraso_auth:google-callback")
response = client.get(url, {"code": "testing-code-google-auth"})

token = response.cookies.get("atoken")

decoded_payload = JWTService().verify_access_token(token.value)

assert decoded_payload["createdWithService"] == "google"

0 comments on commit 426091f

Please sign in to comment.