1010from django .contrib .auth import get_user_model
1111
1212from thebook .bookkeeping .models import BankAccount , Category , Transaction
13+ from thebook .webhooks .models import OpenPixWebhookPayload
1314from thebook .webhooks .openpix .services import fetch_transactions
1415
1516SAMPLE_PAYLOADS_DIR = Path (__file__ ).parent / "sample_payloads"
@@ -47,6 +48,23 @@ def openpix__transactions():
4748 return payload .read ()
4849
4950
51+ @pytest .fixture
52+ def openpix_webhook__one_transaction ():
53+ with open (
54+ Path (SAMPLE_PAYLOADS_DIR / "openpix_webhook__one_transaction.json" ), "r"
55+ ) as payload :
56+ return payload .read ()
57+
58+
59+ @pytest .fixture
60+ def openpix_fetch_transactions__one_transaction ():
61+ with open (
62+ Path (SAMPLE_PAYLOADS_DIR / "openpix_fetch_transactions__one_transaction.json" ),
63+ "r" ,
64+ ) as payload :
65+ return payload .read ()
66+
67+
5068@responses .activate
5169def test_fetch_multiple_transactions (
5270 db ,
@@ -71,15 +89,15 @@ def test_fetch_multiple_transactions(
7189
7290 assert len (transactions ) == 5
7391
74- assert transactions [0 ].reference == "E54843980984242151500EqEkgXhBQPb "
92+ assert transactions [0 ].reference == "01KH7WTFXGGFDSJKHFKJSDHGDG "
7593 assert transactions [0 ].date == datetime .date (2026 , 2 , 15 )
7694 assert transactions [0 ].description == "NED LUDD - 12345678910"
7795 assert transactions [0 ].amount == Decimal ("110" )
7896 assert transactions [0 ].bank_account == openpix_bank_account
7997 assert transactions [0 ].category is None
8098 assert transactions [0 ].created_by == user
8199
82- assert transactions [1 ].reference == "E54843980984242151500EqEkgXhBQPb -T"
100+ assert transactions [1 ].reference == "01KH7WTFXGGFDSJKHFKJSDHGDG -T"
83101 assert transactions [1 ].date == datetime .date (2026 , 2 , 15 )
84102 assert transactions [1 ].description == "Taxa OpenPix - NED LUDD - 12345678910"
85103 assert transactions [1 ].amount == Decimal ("-0.88" )
@@ -98,15 +116,15 @@ def test_fetch_multiple_transactions(
98116 assert transactions [2 ].category == bank_account_transfer_category
99117 assert transactions [2 ].created_by == user
100118
101- assert transactions [3 ].reference == "E00416968202602281512zy3bbOcj3IJ "
119+ assert transactions [3 ].reference == "MTBQL "
102120 assert transactions [3 ].date == datetime .date (2026 , 2 , 28 )
103121 assert transactions [3 ].description == "LUIZ ANTONIO - 12345678910"
104122 assert transactions [3 ].amount == Decimal ("42" )
105123 assert transactions [3 ].bank_account == openpix_bank_account
106124 assert transactions [3 ].category == None
107125 assert transactions [3 ].created_by == user
108126
109- assert transactions [4 ].reference == "E00416968202602281512zy3bbOcj3IJ -T"
127+ assert transactions [4 ].reference == "MTBQL -T"
110128 assert transactions [4 ].date == datetime .date (2026 , 2 , 28 )
111129 assert transactions [4 ].description == "Taxa OpenPix - LUIZ ANTONIO - 12345678910"
112130 assert transactions [4 ].amount == Decimal ("-0.50" )
@@ -126,9 +144,9 @@ def test_fetch_already_existing_transactions(
126144 body = openpix__transactions ,
127145 content_type = "application/json" ,
128146 )
129- baker .make (Transaction , reference = "E54843980984242151500EqEkgXhBQPb " )
147+ baker .make (Transaction , reference = "01KH7WTFXGGFDSJKHFKJSDHGDG " )
130148 baker .make (Transaction , reference = "E54811417202602091301r0gglTvTqLN" )
131- baker .make (Transaction , reference = "E00416968202602281512zy3bbOcj3IJ " )
149+ baker .make (Transaction , reference = "MTBQL " )
132150
133151 transactions = fetch_transactions (
134152 start_date = datetime .date (2026 , 2 , 1 ), end_date = datetime .date (2026 , 2 , 28 )
@@ -148,7 +166,7 @@ def test_only_fetch_new_transactions(
148166 body = openpix__transactions ,
149167 content_type = "application/json" ,
150168 )
151- baker .make (Transaction , reference = "E54843980984242151500EqEkgXhBQPb " )
169+ baker .make (Transaction , reference = "01KH7WTFXGGFDSJKHFKJSDHGDG " )
152170 baker .make (Transaction , reference = "E54811417202602091301r0gglTvTqLN" )
153171
154172 transactions = fetch_transactions (
@@ -157,5 +175,27 @@ def test_only_fetch_new_transactions(
157175
158176 assert len (transactions ) == 2
159177
160- assert transactions [0 ].reference == "E00416968202602281512zy3bbOcj3IJ"
161- assert transactions [1 ].reference == "E00416968202602281512zy3bbOcj3IJ-T"
178+ assert transactions [0 ].reference == "MTBQL"
179+ assert transactions [1 ].reference == "MTBQL-T"
180+
181+
182+ @responses .activate
183+ def test_do_not_return_webhook_processed_transactions (
184+ db , openpix_webhook__one_transaction , openpix_fetch_transactions__one_transaction
185+ ):
186+ responses .add (
187+ responses .GET ,
188+ f"{ settings .OPENPIX_API_BASE_URL } /api/v1/transaction" ,
189+ body = openpix_fetch_transactions__one_transaction ,
190+ content_type = "application/json" ,
191+ )
192+ openpix_webhook_payload = baker .make (
193+ OpenPixWebhookPayload , payload = openpix_webhook__one_transaction
194+ )
195+ openpix_webhook_payload .process ()
196+
197+ transactions = fetch_transactions (
198+ start_date = datetime .date (2026 , 2 , 1 ), end_date = datetime .date (2026 , 2 , 28 )
199+ )
200+
201+ assert len (transactions ) == 0
0 commit comments