def post(self, request, *args, **kwargs): current_url = self.request.get_full_path() social_application = current_url.split('/')[6] for account in request.user.socialaccount_set.all(): if account.provider == social_application: social_account = account branding = account.get_provider().name # we do not need to use validate_disconnect, because accounts are not # associated with being able to log in try: social_account.delete() social_account_removed.send(sender=SocialAccount, request=request, socialaccount=social_account) message = "%s has been successfully disconnected." % branding messages.add_message(self.request, messages.SUCCESS, message) # if multiple posts went in, there won't be any 'social_account' or 'branding' # basically, that means it's already gone and it already works except UnboundLocalError: get_account_adapter().add_message(self.request, messages.SUCCESS, 'socialaccount/messages/' 'account_disconnected.txt') return HttpResponseRedirect(self.get_success_url())
def delete_socialaccount(request): account = SocialAccount.objects.filter(id=request.POST["socialaccount"], user=request.user).first() if not account: return JsonResponse({'msg': 'Unknown account'}, status=404) account.delete() social_account_removed.send(sender=SocialAccount, request=request, socialaccount=account) return JsonResponse({'msg': 'Deleted account'}, status=200)
def test_social_removed(self): """Test the social-account-removed Allauth signal handling.""" mock_obj = mock.Mock() with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj): AllauthSignalListener() # Don't attempt to remove groups if the user doesn't have a linked Discord account social_account_removed.send(SocialLogin, socialaccount=self.social_user_github) mock_obj.assert_not_called() # Don't attempt to remove groups if the social account doesn't map to a Django user social_account_removed.send(SocialLogin, socialaccount=self.social_unmapped) mock_obj.assert_not_called() # Attempt to remove groups if everything checks out social_account_removed.send(SocialLogin, socialaccount=self.social_user) mock_obj.assert_called_with(self.discord_user, self.social_user, deletion=True)