summaryrefslogtreecommitdiff
path: root/talerdonations/tests.py
blob: 48ef764c17a8b23e74a6b9952a278f9a9d7dbddb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

import unittest
from mock import patch, MagicMock
from talerdonations.donations import donations
from talerdonations.talerconfig import TalerConfig
from datetime import datetime

tc = TalerConfig.from_env()
CURRENCY = tc["taler"]["currency"].value_string(required=True)

class DonationsTestCase(unittest.TestCase):
    def setUp(self):
        donations.app.testing = True
        self.app = donations.app.test_client()

    @patch("requests.post")
    @patch("random.randint")
    @patch("datetime.datetime")
    def test_proposal_creation(self, mocked_datetime,
                               mocked_random, mocked_post):
        mocked_datetime.today.return_value = datetime.today()
        mocked_random.return_value = 333
        order_id = "donation-%s-%X-%s" % \
            ("Tor", mocked_random(), mocked_datetime.today().strftime("%H_%M_%S")) 
        ret_post = MagicMock()
        ret_post.status_code = 200
        ret_post.json.return_value = {}
        mocked_post.return_value = ret_post
        self.app.get(
            "/generate-contract?nonce=44&donation_receiver=Tor&donation_amount=1.0")
        mocked_post.assert_called_with(
            "http://backend.test.taler.net/proposal", json={
            "order": {
                "summary": "Donation!",
                "order_id": order_id,
                "nonce": "44",
                "amount": {
                    "value": 1,
                    "fraction": 0,
                    "currency": CURRENCY},
                "max_fee": {
                    "value": 1,
                    "fraction": 0,
                    "currency": CURRENCY},
                "order_id": order_id,
                "products": [{
                    "description": "Donation to Tor",
                    "quantity": 1,
                    "product_id": 0,
                    "price": {
                        "value": 1,
                        "fraction": 0,
                        "currency": CURRENCY}}],
                "fulfillment_url":
                    "http://localhost/fulfillment?order_id=%s" % order_id,
                "pay_url": "http://localhost/pay",
                "merchant": {
                    "instance": "Tor",
                    "address": "nowhere",
                    "name": "Kudos Inc.",
                    "jurisdiction": "none"}
        }})

if "__main__" == __name__:
    unittest.main()