test_open_collective.py 4.27 KB
Newer Older
Agate's avatar
Agate committed
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
from contributions.sources import opencollective

import datetime


def test_retrieve_transactions(requests_mock):
    expected_query = opencollective.TRANSACTIONS_QUERY.copy()
    expected_query["variables"] = {"CollectiveId": 42, "limit": 5, "offset": 3}
    graphql = requests_mock.post(
        opencollective.ENDPOINT, json={"data": {"allTransactions": {"hello": "world"}}}
    )
    result = opencollective.retrieve_transactions(collective_id=42, limit=5, offset=3)
    request = graphql.request_history[0]

    assert result == {"hello": "world"}
    assert request.json() == expected_query


def test_import_transaction_as_contribution(now, db):
    transaction_payload = {
        "__typename": "Order",
        "amount": 100,
        "createdAt": "Mon Oct 15 2018 22:49:53 GMT+0000 (UTC)",
        "currency": "EUR",
        "description": "Monthly donation to Funkwhale (Backer)",
        "fromCollective": {
            "__typename": "User",
            "id": 22538,
            "image": "https://www.gravatar.com/avatar/1bf34eec3e0e8a1b6a818103ee6619e1?default=404",
            "name": "Alice",
            "path": "/alice",
            "slug": "alice",
        },
        "hostCurrency": "EUR",
        "hostCurrencyFxRate": 1,
        "hostFeeInHostCurrency": -5,
        "id": 125_616,
        "netAmountInCollectiveCurrency": 64,
        "paymentProcessorFeeInHostCurrency": -26,
        "platformFeeInHostCurrency": -5,
        "subscription": {"__typename": "Subscription", "interval": "month"},
        "type": "CREDIT",
    }

    contribution = opencollective.import_transaction_as_contribution(
        transaction_payload, collective="funkwhale"
    )

    contributor = contribution.contributor

    author_metadata = transaction_payload["fromCollective"].copy()
    author_metadata["web_url"] = f"https://opencollective.com/{author_metadata['slug']}"

    assert contributor.name == transaction_payload["fromCollective"]["name"]
    assert contributor.username == transaction_payload["fromCollective"]["slug"]
    assert contributor.metadata == {"opencollective": author_metadata}

    assert contribution.summary == transaction_payload["description"]
    assert contribution.type == "donation"
    assert contribution.creation_date == datetime.datetime.strptime(
        transaction_payload["createdAt"], opencollective.DATE_FORMAT
    )
    assert contribution.import_date == now
    assert contribution.is_visible is True
    assert contribution.external_id == transaction_payload["id"]
    assert contribution.url is None

    expected_metadata = transaction_payload.copy()
    expected_metadata.update(
        {
            "id": transaction_payload["id"],
            "amount": transaction_payload["amount"],
            "currency": transaction_payload["currency"],
            "collective": "funkwhale",
            "project_url": f"https://opencollective.com/funkwhale",
            "subscription": transaction_payload["subscription"],
        }
    )
    assert contribution.metadata == expected_metadata


def test_import_contributor_create(db):
    payload = {
        "__typename": "User",
        "id": 22538,
        "image": "https://www.gravatar.com/avatar/1bf34eec3e0e8a1b6a818103ee6619e1?default=404",
        "name": "Alice",
        "path": "/alice",
        "slug": "alice",
    }

    contributor = opencollective.import_contributor(payload)

    payload["web_url"] = f"https://opencollective.com/{payload['slug']}"

    assert contributor.name == payload["name"]
    assert contributor.username == payload["slug"]
    assert contributor.metadata == {"opencollective": payload}


def test_import_contributor_update_metadata(factories):
    existing = factories.Contributor(username="Alice42", metadata={"hello": "world"})
    payload = {
        "__typename": "User",
        "id": 22538,
        "image": "https://www.gravatar.com/avatar/1bf34eec3e0e8a1b6a818103ee6619e1?default=404",
        "name": "Alice",
        "path": "/alice",
        "slug": "alice42",
    }

    contributor = opencollective.import_contributor(payload)
    payload["web_url"] = f"https://opencollective.com/{payload['slug']}"

    assert contributor == existing
    assert contributor.name == existing.name
    assert contributor.username == existing.username
    assert contributor.metadata == {"opencollective": payload, "hello": "world"}