wsvincent / djangoforprofessionals

Source code for Django for Professionals 4.0
https://djangoforprofessionals.com/
MIT License
641 stars 266 forks source link

Accounts test fail after following steps for adding Social in Chapter 8 #310

Open Mr-Ison opened 1 year ago

Mr-Ison commented 1 year ago

The accounts test start to fail with a "allauth.socialaccount.models.socialapp.doesnotexist" error. I was using github as a provider and the sign up works just fine on the localhost. The issue starts at the SignUpPagesTests inside the the def setUp method. url = reverse("account_signup") will eventually lead to the social app doesn't exist error. I did actually find a solution for it after getting a little understanding of the issue. The test database has no record of the social app since the testing databases are empty and the social app was added on the localhost.

A solution I found was creating a fixture that contained a dump of the socialapp from the database. I did that by following a guide on this site: https://www.marinamele.com/user-authentication-with-google-using-django-allauth

Make sure to add the fixture to your gitignore as it contains secret keys to the provider and other sensitive information. I deleted all the other fixture data that wasn't related to the social app accounts so I was left with the sistes.site and the social account model. When I got to the command for dumping, I had to add -primary to the --natural command to avoid an error. Adding the Fixtures directory in settings.py and putting fixtures = ["allauth_fixture"] underneath the email line in SignUpTestPages fixed the issue.

jefftriplett commented 1 year ago

I recently ran into these issues on Django News Jobs and some client projects who use OKTA because of what I assume are some default allauth behavior changes.

We either need to load the GitHub (or whichever third-party app) credentials into the database to avoid the template tags exception being raised.

Or, we need to add a few settings to work around it:

SOCIALACCOUNT_PROVIDERS = {
    "github": {
        "APP": {
            "client_id": "",
            "secret": "",
            "key": "",  # I think this one can remain blank
        }
    }
}

If you use a library like environs[django] or any of the read from the ENV libraries, I use this to get around it:

SOCIALACCOUNT_PROVIDERS = {
    "github": {
        "APP": {
            "client_id": env("GITHUB_CLIENT_ID", default=""),
            "secret": env("GITHUB_SECRET", default=""),
            "key": env("GITHUB_KEY", default=""),
        }
    }
}

I hope this help someone else out too.