Skip to content

Commit

Permalink
fix(account): Store email vs no USER_MODEL_EMAIL_FIELD
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Nov 5, 2023
1 parent 3a6046d commit a86cdaf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
5 changes: 3 additions & 2 deletions allauth/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,15 @@ def clean(self):
return self.cleaned_data

def save(self, request):
email = self.cleaned_data.get("email")
if self.account_already_exists:
raise ValueError(self.cleaned_data.get("email"))
raise ValueError(email)
adapter = get_adapter()
user = adapter.new_user(request)
adapter.save_user(request, user, self)
self.custom_signup(request, user)
# TODO: Move into adapter `save_user` ?
setup_user_email(request, user, [])
setup_user_email(request, user, [EmailAddress(email=email)] if email else [])
return user


Expand Down
21 changes: 21 additions & 0 deletions allauth/account/tests/test_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,24 @@ def test_get_initial_with_valid_email():
with context.request_context(request):
view = signup(request)
assert view.context_data["view"].get_initial()["email"] == "[email protected]"


def test_signup_user_model_no_email(settings, client, password_factory, db):
settings.ACCOUNT_USERNAME_REQUIRED = False
settings.ACCOUNT_EMAIL_REQUIRED = True
settings.ACCOUNT_EMAIL_VERIFICATION = app_settings.EmailVerificationMethod.MANDATORY
settings.ACCOUNT_USER_MODEL_EMAIL_FIELD = None
password = password_factory()
email = "[email protected]"
resp = client.post(
reverse("account_signup"),
{
"email": email,
"password1": password,
"password2": password,
},
)
assert resp.status_code == 302
email = EmailAddress.objects.get(email=email)
assert email.primary
assert not email.verified
2 changes: 1 addition & 1 deletion allauth/account/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def cleanup_email_addresses(request, addresses):
primary_address = primary_addresses[0]
elif e2a:
# Pick the first
primary_address = e2a.keys()[0]
primary_address = list(e2a.values())[0]
else:
# Empty
primary_address = None
Expand Down
12 changes: 10 additions & 2 deletions allauth/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ def auth_client(client, user):


@pytest.fixture
def user_password():
return str(uuid.uuid4())
def password_factory():
def f():
return str(uuid.uuid4())

return f


@pytest.fixture
def user_password(password_factory):
return password_factory()


@pytest.fixture
Expand Down

0 comments on commit a86cdaf

Please sign in to comment.