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

Fix API for member edition #1261

Open
wants to merge 4 commits into
base: master
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
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ You can get all the members with a `GET` on
[{"weight": 1, "activated": true, "id": 31, "name": "Arnaud"},
{"weight": 1, "activated": true, "id": 32, "name": "Alexis"},
{"weight": 1, "activated": true, "id": 33, "name": "Olivier"},
{"weight": 1, "activated": true, "id": 34, "name": "Fred"}]
{"weight": 1, "activated": true, "id": 34, "name": "Jeanne"}]

Add a member with a `POST` request on `/api/projects/<id>/members`:

Expand Down Expand Up @@ -244,7 +244,7 @@ You can get some project stats with a `GET` on
"balance": 10.5
},
{
"member": {"activated": true, "id": 2, "name": "fred", "weight": 1.0},
"member": {"activated": true, "id": 2, "name": "jeanne", "weight": 1.0},
"paid": 5,
"spent": 15.5,
"balance": -10.5
Expand Down
11 changes: 7 additions & 4 deletions ihatemoney/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ class APIMemberForm(MemberForm):
But we want Member.enabled to be togglable via the API.
"""

activated = BooleanField(false_values=("false", "", "False"))
activated = BooleanField(false_values=("false", "False"))

def save(self, project, person):
person.activated = self.activated.data
# Check for raw data, otherwise no value will make it default to False
if self.activated.raw_data:
person.activated = self.activated.data
return super(APIMemberForm, self).save(project, person)


Expand Down Expand Up @@ -136,9 +138,10 @@ def get(self, project, member_id):
return member

def put(self, project, member_id):
form = APIMemberForm(project, meta={"csrf": False}, edit=True)
member = Person.query.get(member_id, project)
form = APIMemberForm(project, obj=member, meta={"csrf": False}, edit=True)

if form.validate():
member = Person.query.get(member_id, project)
form.save(project, member)
db.session.commit()
return member
Expand Down
76 changes: 56 additions & 20 deletions ihatemoney/tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ def api_create(
)

def api_add_member(self, project, name, weight=1):
self.client.post(
resp = self.client.post(
f"/api/projects/{project}/members",
data={"name": name, "weight": weight},
headers=self.get_auth(project),
)
return resp.json

def get_auth(self, username, password=None):
password = password or username
Expand Down Expand Up @@ -314,7 +315,7 @@ def test_member(self):
# edit the participant
req = self.client.put(
"/api/projects/raclette/members/1",
data={"name": "Fred", "weight": 2},
data={"name": "Jeanne", "weight": 2},
headers=self.get_auth("raclette"),
)

Expand All @@ -326,14 +327,14 @@ def test_member(self):
)

self.assertStatus(200, req)
assert "Fred" == json.loads(req.data.decode("utf-8"))["name"]
assert "Jeanne" == json.loads(req.data.decode("utf-8"))["name"]
assert 2 == json.loads(req.data.decode("utf-8"))["weight"]

# edit this member with same information
# (test PUT idemopotence)
# (test PUT idempotence)
req = self.client.put(
"/api/projects/raclette/members/1",
data={"name": "Fred"},
data={"name": "Jeanne"},
headers=self.get_auth("raclette"),
)

Expand All @@ -342,7 +343,7 @@ def test_member(self):
# de-activate the participant
req = self.client.put(
"/api/projects/raclette/members/1",
data={"name": "Fred", "activated": False},
data={"name": "Jeanne", "activated": False},
headers=self.get_auth("raclette"),
)
self.assertStatus(200, req)
Expand All @@ -356,7 +357,7 @@ def test_member(self):
# re-activate the participant
req = self.client.put(
"/api/projects/raclette/members/1",
data={"name": "Fred", "activated": True},
data={"name": "Jeanne", "activated": True},
headers=self.get_auth("raclette"),
)

Expand All @@ -382,13 +383,48 @@ def test_member(self):
self.assertStatus(200, req)
assert "[]\n" == req.data.decode("utf-8")

def test_member_edition_keep_data(self):
# create a project
self.api_create("raclette")
zorg_id = self.api_add_member("raclette", "zorglub", weight=2)
self.api_add_member("raclette", "jeanne")

resp = self.client.get(
f"/api/projects/raclette/members/{zorg_id}",
headers=self.get_auth("raclette"),
)
assert resp.json == {
"activated": True,
"id": 1,
"name": "zorglub",
"weight": 2.0,
}

self.client.put(
f"/api/projects/raclette/members/{zorg_id}",
data={"name": "zorglub"},
headers=self.get_auth("raclette"),
)

resp = self.client.get(
f"/api/projects/raclette/members/{zorg_id}",
headers=self.get_auth("raclette"),
)
# The user should still be activated and weight 2
assert resp.json == {
"activated": True,
"id": 1,
"name": "zorglub",
"weight": 2.0,
}

def test_bills(self):
# create a project
self.api_create("raclette")

# add participants
self.api_add_member("raclette", "zorglub")
self.api_add_member("raclette", "fred")
self.api_add_member("raclette", "jeanne")
self.api_add_member("raclette", "quentin")

# get the list of bills (should be empty)
Expand Down Expand Up @@ -429,7 +465,7 @@ def test_bills(self):
"payer_id": 1,
"owers": [
{"activated": True, "id": 1, "name": "zorglub", "weight": 1},
{"activated": True, "id": 2, "name": "fred", "weight": 1},
{"activated": True, "id": 2, "name": "jeanne", "weight": 1},
],
"amount": 25.0,
"date": "2011-08-10",
Expand Down Expand Up @@ -498,7 +534,7 @@ def test_bills(self):
"payer_id": 2,
"owers": [
{"activated": True, "id": 1, "name": "zorglub", "weight": 1},
{"activated": True, "id": 2, "name": "fred", "weight": 1},
{"activated": True, "id": 2, "name": "jeanne", "weight": 1},
],
"amount": 25.0,
"date": "2011-09-10",
Expand Down Expand Up @@ -534,7 +570,7 @@ def test_bills_with_calculation(self):

# add participants
self.api_add_member("raclette", "zorglub")
self.api_add_member("raclette", "fred")
self.api_add_member("raclette", "jeanne")

# valid amounts
input_expected = [
Expand Down Expand Up @@ -576,7 +612,7 @@ def test_bills_with_calculation(self):
"payer_id": 1,
"owers": [
{"activated": True, "id": 1, "name": "zorglub", "weight": 1},
{"activated": True, "id": 2, "name": "fred", "weight": 1},
{"activated": True, "id": 2, "name": "jeanne", "weight": 1},
],
"amount": expected_amount,
"date": "2011-08-10",
Expand Down Expand Up @@ -647,7 +683,7 @@ def test_currencies(self):

# Add participants
self.api_add_member("raclette", "zorglub")
self.api_add_member("raclette", "fred")
self.api_add_member("raclette", "jeanne")
self.api_add_member("raclette", "quentin")

# Add a bill without explicit currency
Expand Down Expand Up @@ -680,7 +716,7 @@ def test_currencies(self):
"payer_id": 1,
"owers": [
{"activated": True, "id": 1, "name": "zorglub", "weight": 1},
{"activated": True, "id": 2, "name": "fred", "weight": 1},
{"activated": True, "id": 2, "name": "jeanne", "weight": 1},
],
"amount": 25.0,
"date": "2011-08-10",
Expand Down Expand Up @@ -725,7 +761,7 @@ def test_currencies(self):
"payer_id": 1,
"owers": [
{"activated": True, "id": 1, "name": "zorglub", "weight": 1.0},
{"activated": True, "id": 2, "name": "fred", "weight": 1.0},
{"activated": True, "id": 2, "name": "jeanne", "weight": 1.0},
],
"amount": 30.0,
"date": "2011-08-10",
Expand Down Expand Up @@ -781,7 +817,7 @@ def test_statistics(self):

# add participants
self.api_add_member("raclette", "zorglub")
self.api_add_member("raclette", "fred")
self.api_add_member("raclette", "jeanne")

# add a bill
req = self.client.post(
Expand Down Expand Up @@ -818,7 +854,7 @@ def test_statistics(self):
"member": {
"activated": True,
"id": 2,
"name": "fred",
"name": "jeanne",
"weight": 1.0,
},
"paid": 0,
Expand All @@ -844,7 +880,7 @@ def test_weighted_bills(self):

# add participants
self.api_add_member("raclette", "zorglub")
self.api_add_member("raclette", "freddy familly", 4)
self.api_add_member("raclette", "jeannedy familly", 4)
self.api_add_member("raclette", "quentin")

# add a bill
Expand Down Expand Up @@ -875,7 +911,7 @@ def test_weighted_bills(self):
"payer_id": 1,
"owers": [
{"activated": True, "id": 1, "name": "zorglub", "weight": 1},
{"activated": True, "id": 2, "name": "freddy familly", "weight": 4},
{"activated": True, "id": 2, "name": "jeannedy familly", "weight": 4},
],
"amount": 25.0,
"date": "2011-08-10",
Expand Down Expand Up @@ -909,7 +945,7 @@ def test_weighted_bills(self):
{
"activated": True,
"id": 2,
"name": "freddy familly",
"name": "jeannedy familly",
"weight": 4.0,
"balance": -20.0,
},
Expand Down
Loading