Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated checks for validate_name() in MemberForm() #1106

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ihatemoney/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ def validate_name(self, field):
and Person.query.filter(
Person.name == field.data,
Person.project == self.project,
Person.activated,
).all()
): # NOQA
raise ValidationError(_("This project already have this participant"))
Expand Down
60 changes: 58 additions & 2 deletions ihatemoney/tests/budget_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ def test_membership(self):
result = self.client.get("/raclette/add")
assert "jeanne" not in result.data.decode("utf-8")

# adding him again should reactivate him
self.client.post("/raclette/members/add", data={"name": "jeanne"})
# it should be possible to reactivate him
self.client.post(f"/raclette/members/{jeanne_id}/reactivate")
assert len(self.get_project("raclette").active_members) == 2

# adding an user with the same name as another user from a different
Expand Down Expand Up @@ -1846,6 +1846,62 @@ def test_amount_too_high(self):
# No bills, the previous one was not added
assert "No bills" in resp.data.decode("utf-8")

def test_add_duplicate_user(self):
"""
Adding a user with the same name as a deactivated user with 0 balance
and no associated bills should succeed
"""
self.post_project("raclette")
self.login("raclette")

# adds a member to this project and delete it right after.
self.client.post("/raclette/members/add", data={"name": "zorglub"})
self.client.post("/raclette/members/1/delete")
assert len(self.get_project("raclette").active_members) == 0
assert len(self.get_project("raclette").members) == 0

# try to add this deleted user should be successful
response = self.client.post("/raclette/members/add", data={"name": "zorglub"})
assert len(self.get_project("raclette").members) == 1

def test_add_duplicate_user_with_balance(self):
"""
Adding a user with same name as a deactivated user with non-zero balance
and associated bills should fail
"""
self.post_project("raclette")

# add two participants
self.client.post("/raclette/members/add", data={"name": "Alice"})
self.client.post("/raclette/members/add", data={"name": "Bob"})

members_ids = [m.id for m in self.get_project("raclette").members]

# create one bill
self.client.post(
"/raclette/add",
data={
"date": "2011-08-10",
"what": "fromage à raclette",
"payer": members_ids[0],
"payed_for": members_ids,
"amount": "100",
},
)

# deactivate Bob
self.client.post(
"/raclette/members/%s/delete" % self.get_project("raclette").members[-1].id
)

assert len(self.get_project("raclette").members) == 2
self.client.post("/raclette/members/add", data={"name": "Bob"})

# adding a user with the same name should fail
assert len(self.get_project("raclette").members) == 2
# The only active_member is Alice, this means adding a new Bob failed
assert len(self.get_project("raclette").active_members) == 1

def test_session_projects_migration_to_list(self):
"""In https://github.com/spiral-project/ihatemoney/pull/1082, session["projects"]
was migrated from a list to a dict. We need to handle this.
Expand Down
Loading