def test_get_bill(sample_event, sample_bill, sample_participant, sample_user): """Request should return proper bill data.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_bill.participants.add(sample_participant) sample_bill.save() sample_event.bills.add(sample_bill) sample_event.save() response = client.get( reverse( "bills-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_bill.pk} ), format="jason", ) assert response.status_code == status.HTTP_200_OK assert response.data == { "id": sample_bill.id, "url": r"http://testserver{}".format( reverse( "bills-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_bill.pk}, ) ), "title": sample_bill.title, "amount_currency": "PLN", "amount": "0.00", "payer": sample_bill.payer, "participants": [sample_participant.id], }
def test_get_payment(sample_event, sample_payment, sample_participant, sample_user): """Request should return proper payment data.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_payment.issuer = sample_participant sample_payment.save() sample_event.payments.add(sample_payment) sample_event.save() response = client.get( reverse( "payments-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_payment.pk}, ), format="json", ) assert response.status_code == status.HTTP_200_OK assert json.dumps(response.data) == json.dumps( { "id": sample_payment.id, "url": r"http://testserver{}".format( reverse( "payments-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_payment.pk}, ) ), "issuer": sample_payment.issuer.id, "acquirer": sample_payment.acquirer.id, "title": sample_payment.title, "amount_currency": "PLN", "amount": "0.00", } )
def test_get_event_paymaster(sample_event, sample_user, sample_participant): """Response paymaster should be dict with sample_participant.""" sample_participant.event = sample_event sample_participant.save() sample_event.paymaster = sample_participant sample_event.save() client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.data["paymaster"] == { "id": sample_participant.pk, "url": r"http://testserver{}".format( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk }, )), "username": sample_participant.username, }
def test_get_event_bills(sample_event, sample_user, sample_bill): """Bills of event in response should be sample_event's bills.""" sample_event.bills.add(sample_bill) client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.data["bills"] == [{ "id": sample_bill.pk, "url": r"http://testserver{}".format( reverse( "bills-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_bill.pk }, )), "participants": [], "title": sample_bill.title, "amount_currency": "PLN", "amount": "0.00", "event": sample_event.pk, "payer": sample_bill.payer, }]
def test_get_event_payments(sample_event, sample_user, sample_payment): """Payments of event in response should be sample_event's payments.""" sample_event.payments.add(sample_payment) client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.data["payments"] == [{ "id": sample_payment.pk, "url": r"http://testserver{}".format( reverse( "payments-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_payment.pk }, )), "issuer": sample_payment.issuer.pk, "acquirer": sample_payment.acquirer.pk, "title": sample_payment.title, "amount_currency": "PLN", "amount": "0.00", "event": sample_event.pk, }]
def test_patch_participant(sample_event, sample_participant, sample_user): """sample_participant should have a changed username.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.save() changed_participant_data = {"username": "******"} assert sample_participant in Participant.objects.filter( username=sample_participant.username) assert (Participant.objects.filter( username=changed_participant_data["username"]).count() == 0) response = client.patch( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk }, ), changed_participant_data, format="json", ) assert response.status_code == status.HTTP_200_OK assert sample_participant not in Participant.objects.filter( username=sample_participant.username) assert (Participant.objects.filter( username=changed_participant_data["username"]).count() == 1)
def test_get_participant(sample_event, sample_participant, sample_user): """Request should return proper participant data.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.save() response = client.get( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk }, ), format="jason", ) assert response.status_code == status.HTTP_200_OK assert response.data == { "id": sample_participant.id, "url": r"http://testserver{}".format( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk }, )), "username": sample_participant.username, }
def test_get_event_paymaster_none(sample_event, sample_user): """No paymaster set. Response paymaster should be None.""" client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.data["paymaster"] is None
def test_get_event_fail_other_user_event(sample_event_2, sample_user): """User should not have access to other user's events.""" client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event_2.pk}), format="json") assert response.status_code == status.HTTP_404_NOT_FOUND
def test_get_event_id(sample_event, sample_user): """Id of event in response should be sample_event's id.""" client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.data["id"] == sample_event.pk
def test_get_event_payments_url(sample_event, sample_user): """Participants_url of event in response should be sample_event's participants_url.""" client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.data["payments_url"] == "http://testserver{}".format( reverse("payments-list", kwargs={"event_pk": sample_event.pk}))
def test_post_event(sample_user): """New Event object should be created.""" client = auth_client(APIClient(), sample_user.email, "testpassword") assert Event.objects.filter(name="new test event").count() == 0 event_data = {"name": "new test event"} response = client.post(reverse("events-list"), event_data, format="json") assert response.status_code == status.HTTP_201_CREATED assert Event.objects.filter(name="new test event").count() == 1
def test_get_bills_fail_other_user(sample_event, sample_bill, sample_user_2): """User should not have access to other user's event's bills.""" client = auth_client(APIClient(), sample_user_2.email, "testpassword") sample_event.bills.add(sample_bill) sample_event.save() response = client.get( reverse("bills-list", kwargs={"event_pk": sample_event.pk}), format="jason" ) assert response.status_code == status.HTTP_404_NOT_FOUND
def test_delete_event(sample_event, sample_user): """Event object should be deleted""" client = auth_client(APIClient(), sample_user.email, "testpassword") assert sample_event in Event.objects.filter(name=sample_event.name) response = client.delete( reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json", follow=True, ) assert response.status_code == status.HTTP_204_NO_CONTENT assert sample_event not in Event.objects.filter(name=sample_event.name)
def test_get_event(sample_event, sample_participant, sample_bill, sample_payment, sample_user): """Response status code should be 200.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.bills.add(sample_bill) sample_event.payments.add(sample_payment) sample_event.save() response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.status_code == status.HTTP_200_OK
def test_post_participant(sample_event, sample_user): """New Participant object should be created.""" client = auth_client(APIClient(), sample_user.email, "testpassword") assert Participant.objects.filter(username="******").count() == 0 participant_data = {"username": "******"} response = client.post( reverse("participants-list", kwargs={"event_pk": sample_event.pk}), participant_data, format="json", ) assert response.status_code == status.HTTP_201_CREATED assert Participant.objects.filter(username="******").count() == 1
def test_get_bills( sample_event, sample_bill, sample_bill_2, sample_participant, sample_user ): """Request should return all Bill objects data related to sample_event.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_bill.participants.add(sample_participant) sample_bill.save() sample_event.bills.add(sample_bill) sample_event.bills.add(sample_bill_2) sample_event.save() response = client.get( reverse("bills-list", kwargs={"event_pk": sample_event.pk}), format="json" ) assert response.status_code == status.HTTP_200_OK assert json.dumps(response.data) == json.dumps( [ { "id": sample_bill.id, "url": r"http://testserver{}".format( reverse( "bills-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_bill.pk}, ) ), "participants": [sample_participant.id], "title": sample_bill.title, "amount_currency": "PLN", "amount": "0.00", "payer": sample_bill.payer, }, { "id": sample_bill_2.id, "url": r"http://testserver{}".format( reverse( "bills-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_bill_2.pk}, ) ), "participants": [], "title": sample_bill_2.title, "amount_currency": "PLN", "amount": "0.00", "payer": sample_bill_2.payer, }, ] )
def test_put_event(sample_event, sample_user): """sample_event should have a changed name.""" client = auth_client(APIClient(), sample_user.email, "testpassword") changed_event_data = {"name": "new test event"} assert sample_event in Event.objects.filter(name=sample_event.name) assert Event.objects.filter(name=changed_event_data["name"]).count() == 0 response = client.put( reverse("events-detail", kwargs={"pk": sample_event.pk}), changed_event_data, format="json", ) assert response.status_code == status.HTTP_200_OK assert sample_event not in Event.objects.filter(name=sample_event.name) assert Event.objects.filter(name=changed_event_data["name"]).count() == 1
def test_get_participants(sample_event, sample_participant, sample_participant_2, sample_user): """Request should return all Participant objects data related to sample_event""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.participants.add(sample_participant_2) sample_event.save() response = client.get( reverse("participants-list", kwargs={"event_pk": sample_event.pk}), format="json", ) assert response.status_code == status.HTTP_200_OK assert json.dumps(response.data) == json.dumps([ { "id": sample_participant.id, "url": r"http://testserver{}".format( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk, }, )), "username": sample_participant.username, }, { "id": sample_participant_2.id, "url": r"http://testserver{}".format( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant_2.pk, }, )), "username": sample_participant_2.username, }, ])
def test_delete_bill(sample_event, sample_bill, sample_user): """Bill object should be deleted.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.bills.add(sample_bill) sample_event.save() assert sample_bill in Bill.objects.filter(title=sample_bill.title) response = client.delete( reverse( "bills-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_bill.pk} ), format="json", follow=True, ) assert response.status_code == status.HTTP_204_NO_CONTENT assert sample_bill not in Bill.objects.filter(title=sample_bill.title)
def test_get_events( sample_event, sample_event_2, sample_participant, sample_bill, sample_payment, sample_user, ): """ Request should return all related to sample_user Event objects data. Sample_event_2 data should not be returned, as it's not event related to logged user. """ client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.bills.add(sample_bill) sample_event.payments.add(sample_payment) sample_event.save() response = client.get(reverse("events-list"), format="json") assert response.status_code == status.HTTP_200_OK assert response.data == [{ "id": sample_event.pk, "url": r"http://testserver{}".format( reverse("events-detail", kwargs={"pk": sample_event.pk})), "participants_url": r"http://testserver{}".format( reverse("participants-list", kwargs={"event_pk": sample_event.pk})), "bills_url": r"http://testserver{}".format( reverse("bills-list", kwargs={"event_pk": sample_event.pk})), "payments_url": r"http://testserver{}".format( reverse("payments-list", kwargs={"event_pk": sample_event.pk})), "name": sample_event.name, "paymaster": None, }]
def test_post_bill(sample_event, sample_participant, sample_user): """New Bill object should be created.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.save() assert Bill.objects.filter(title="new bill").count() == 0 bill_data = {"title": "new bill", "participants": [sample_participant.id]} response = client.post( reverse("bills-list", kwargs={"event_pk": sample_event.pk}), bill_data, format="json", ) assert response.status_code == status.HTTP_201_CREATED assert ( Bill.objects.filter(title="new bill", participants=sample_participant).count() == 1 )
def test_get_participant_fail_other_user(sample_event, sample_participant, sample_user_2): """User should not have access to other user's event's participants.""" client = auth_client(APIClient(), sample_user_2.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.save() response = client.get( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk }, ), format="jason", ) assert response.status_code == status.HTTP_404_NOT_FOUND
def test_patch_bill(sample_event, sample_bill, sample_user): """sample_bill should have a changed title.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.bills.add(sample_bill) sample_event.save() changed_bill_data = {"title": "new test bill"} assert sample_bill in Bill.objects.filter(title=sample_bill.title) assert Bill.objects.filter(title=changed_bill_data["title"]).count() == 0 response = client.patch( reverse( "bills-detail", kwargs={"event_pk": sample_event.pk, "pk": sample_bill.pk} ), changed_bill_data, format="json", ) assert response.status_code == status.HTTP_200_OK assert sample_bill not in Bill.objects.filter(title=sample_bill.title) assert Bill.objects.filter(title=changed_bill_data["title"]).count() == 1
def test_get_payments_fail_other_user( sample_event, sample_payment, sample_payment_2, sample_participant, sample_participant_2, sample_user_2, ): """User should not have access to other user's event's payments.""" client = auth_client(APIClient(), sample_user_2.email, "testpassword") sample_payment.issuer = sample_participant sample_payment.acquirer = sample_participant_2 sample_payment.save() sample_event.payments.add(sample_payment) sample_event.payments.add(sample_payment_2) sample_event.save() response = client.get( reverse("payments-list", kwargs={"event_pk": sample_event.pk}), format="json" ) assert response.status_code == status.HTTP_404_NOT_FOUND
def test_delete_participant(sample_event, sample_participant, sample_user): """Participant object should be deleted.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant) sample_event.save() assert sample_participant in Participant.objects.filter( username=sample_participant.username) response = client.delete( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk }, ), format="json", follow=True, ) assert response.status_code == status.HTTP_204_NO_CONTENT assert sample_participant not in Participant.objects.filter( username=sample_participant.username)
def test_get_event_participants(sample_event, sample_user, sample_participant): """Participants of event in response should be sample_event's participants.""" sample_event.participants.add(sample_participant) client = auth_client(APIClient(), sample_user.email, "testpassword") response = client.get(reverse("events-detail", kwargs={"pk": sample_event.pk}), format="json") assert response.data["participants"] == [{ "id": sample_participant.pk, "url": r"http://testserver{}".format( reverse( "participants-detail", kwargs={ "event_pk": sample_event.pk, "pk": sample_participant.pk }, )), "username": sample_participant.username, }]
def test_post_payment( sample_event, sample_participant, sample_participant_2, sample_user ): """New Payment object should be created.""" client = auth_client(APIClient(), sample_user.email, "testpassword") sample_event.participants.add(sample_participant, sample_participant_2) sample_event.save() assert Payment.objects.filter(title="new payment").count() == 0 bill_data = { "title": "new payment", "issuer": sample_participant.id, "acquirer": sample_participant_2.id, } response = client.post( reverse("payments-list", kwargs={"event_pk": sample_event.pk}), bill_data, format="json", ) assert response.status_code == status.HTTP_201_CREATED assert Payment.objects.filter(title="new payment").count() == 1 Payment.objects.get(title="new payment").delete()