Skip to content

Commit

Permalink
save user with federation oauth app
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrindt committed Nov 8, 2023
1 parent 6564ce4 commit db0cf3f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
25 changes: 16 additions & 9 deletions ephios/plugins/federation/views/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def get_form_kwargs(self):
@transaction.atomic
def _setup_federated_host(self, data):
oauth_application = Application(
user=self.request.user,
client_type=Application.CLIENT_CONFIDENTIAL,
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
redirect_uris=urljoin(data["host_url"], reverse("federation:oauth_callback")),
Expand Down Expand Up @@ -205,19 +206,25 @@ def _setup_federated_host(self, data):
def form_valid(self, form):
try:
self._setup_federated_host(form.code_data)
except (KeyError, HTTPError, ReadTimeout):
except (KeyError, JSONDecodeError):
messages.error(
self.request,
_("Invalid code data. Please try again with a new invite code."),
)
except (HTTPError, ReadTimeout):
messages.error(
self.request,
_("Could not connect to host instance. Please try again later."),
)
return self.form_invalid(form)
messages.success(
self.request,
_(
"Invite code redeemded successfully. You are now receiving events from this instance."
),
)
return redirect(reverse("federation:settings"))
else:
messages.success(
self.request,
_(
"Invite code redeemded successfully. You are now receiving events from this instance."
),
)
return redirect(reverse("federation:settings"))
return self.form_invalid(form)


class FederatedGuestDeleteView(StaffRequiredMixin, SuccessMessageMixin, DeleteView):
Expand Down
9 changes: 9 additions & 0 deletions tests/plugins/federation/test_federation_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def test_create_invitecode(django_app, superuser):
assert InviteCode.objects.get().url == "https://example.com"


def test_federation_oauth_detail_view(django_app, superuser, federation):
host, guest = federation
django_app.get(
reverse("api:settings-oauth-app-detail", args=[host.oauth_application.pk]),
user=superuser,
status=200,
)


@patch("ephios.plugins.federation.views.frontend.requests")
def test_redeem_invitecode_frontend(mock_requests, django_app, superuser, invite_code):
mock_requests.post.return_value.json.return_value = {
Expand Down
8 changes: 0 additions & 8 deletions tests/plugins/federation/test_federation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ def test_federation_get_shared_events(django_app, volunteer, federation, federat
assert federated_event.title in response.text


def test_federation_oauth_detail_view(django_app, superuser, federation):
host, guest = federation
response = django_app.get(
reverse("api:settings-oauth-app-detail", args=[host.oauth_application.pk]),
user=superuser,
)


@patch("ephios.plugins.federation.views.frontend.requests")
def test_federation_shared_event_list(
mock_requests, django_app, volunteer, federation, federated_event, settings
Expand Down

0 comments on commit db0cf3f

Please sign in to comment.