<commit_before>import json
from apps.cowry.factory import _adapter_for_payment_method
from apps.cowry.models import PaymentStatuses
from apps.cowry_docdata.adapters import default_payment_methods
from apps.cowry_docdata.tests import run_docdata_tests
from django.test import TestCase
from django.test.client import Client
from django.utils import unittest
from django.utils import timezone
from django.test.utils import override_settings
from apps.bluebottle_utils.tests import UserTestsMixin
from apps.projects.tests import ProjectTestsMixin
from apps.projects.models import Project
from rest_framework import status
from ..models import Order, OrderStatuses, DonationStatuses, RecurringDirectDebitPayment


class CartApiIntegrationTest(ProjectTestsMixin, UserTestsMixin, TestCase):
    """
    Integration tests for the adding Donations to an Order (a cart in this case).
    """
    def setUp(self):
        self.some_project = self.create_project(money_asked=50000)
        self.another_project = self.create_project(money_asked=75000)

        self.some_user = self.create_user()
        self.another_user = self.create_user()

        self.current_donations_url = '/i18n/api/fund/orders/current/donations/'
        self.current_order_url = '/i18n/api/fund/orders/current'
        self.payment_profile_current_url = '/i18n/api/fund/paymentprofiles/current'
        self.payment_url_base = '/i18n/api/fund/payments/'
        self.order_url_base = '/i18n/api/fund/orders/'

        self.some_profile = {'first_name': 'Nijntje',
                             'last_name': '<NAME>',
                             'email': '<EMAIL>',
                             'address': 'Dam',
                             'postal_code': '1001AM',
                             'city': 'Amsterdam',
                             'country': 'NL'}

    def test_current_order_donation_crud(self):
        """
        Tests for creating, retrieving, updating and deleting a donation to shopping cart.
        """
        # First make sure we have a current order
        self.client.login(username=self.some_user.email, password='password')
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertEqual(response.data['status'], 'current')

        # Create a Donation
        response = self.client.post(self.current_donations_url, {'project': self.some_project.slug, 'amount': 35})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
        self.assertEqual(response.data['amount'], '35.00')
        self.assertEqual(response.data['project'], self.some_project.slug)
        self.assertEqual(response.data['status'], 'new')

        # Retrieve the created Donation
        donation_detail_url = "{0}{1}".format(self.current_donations_url, response.data['id'])
        response = self.client.get(donation_detail_url)
        self.assertEqual(response.data['amount'], '35.00')
        self.assertEqual(response.data['project'], self.some_project.slug)

        # Retrieve the list with all donations in this cart should return one
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['amount'], '35.00')
        self.assertEqual(response.data['results'][0]['project'], self.some_project.slug)

        # Create another Donation
        response = self.client.post(self.current_donations_url,
                                    {'project': self.another_project.slug, 'amount': '12.50'})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
        self.assertEqual(response.data['amount'], '12.50')
        self.assertEqual(response.data['project'], self.another_project.slug)

        # Create a Donation under 5 should fail
        response = self.client.post(self.current_donations_url,
                                    {'project': self.another_project.slug, 'amount': '2.50'})
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data)

        # Retrieve the list with all donations in this cart should return one
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(response.data['results'][0]['amount'], '35.00')
        self.assertEqual(response.data['results'][1]['amount'], '12.50')
        self.assertEqual(response.data['results'][0]['project'], self.some_project.slug)

        # Update the created Donation by owner.
        response = self.client.put(donation_detail_url, json.dumps({'amount': 150, 'project': self.some_project.slug}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertEqual(response.data['amount'], '150.00')

        # Update with amount under 5 should fail.
        response = self.client.put(donation_detail_url, json.dumps({'amount': 3, 'project': self.some_project.slug}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data)

        # Update the status of the created Donation by owner should be ignored
        response = self.client.put(donation_detail_url,json.dumps({'amount': 150, 'project': self.some_project.slug, 'status': 'paid'}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertEqual(response.data['amount'], '150.00')
        self.assertEqual(response.data['status'], 'new')

        # Delete a donation should work the list should have one donation now
        response = self.client.delete(donation_detail_url)
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.data)
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 1)

        # Another user should not see the cart of the first user
        self.client.logout()
        self.client.login(username=self.another_user.email, password='password')
        # make a cart for this another user
        self.client.get(self.current_order_url)
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 0)

        # Another user should not be able to view a donation in the cart of the first user
        response = self.client.get(donation_detail_url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, response.data)

        # Now let's get anonymous and create a donation
        self.client.logout()
        # make a cart for this anonymous user
        self.client.get(self.current_order_url)
        response = self.client.post(self.current_donations_url, {'project': self.some_project.slug, 'amount': 71})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
        self.assertEqual(response.data['amount'], '71.00')
        self.assertEqual(response.data['project'], self.some_project.slug)
        self.assertEqual(response.data['status'], 'new')
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 1)

        # Login and out again... The anonymous cart should NOT be returned
        self.client.login(username=self.some_user.email, password='password')
        self.client.logout()
        self.client.get(self.current_order_url)
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 0)

        # Login as the first user and cart should still have one donation
        self.client.login(username=self.some_user.email, password='password')
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['amount'], '12.50')
        self.assertEqual(response.data['results'][0]['project'], self.another_project.slug)
        self.client.logout()

    def test_current_order_monthly(self):
        # Test setting a recurring order as logged in user.
        self.client.login(username=self.some_user.email, password='password')
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.data['recurring'], False)
        response = self.client.put(self.current_order_url, json.dumps({'recurring': True}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['recurring'], True)

        # Test that setting a recurring order as anonymous user fails.
        self.client.logout()
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.data['recurring'], False)
        response = self.client.put(self.current_order_url, json.dumps({'recurring': True}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.data['recurring'], False)

    @override_settings(COWRY_PAYMENT_METHODS=default_payment_methods)
    @unittest.skipUnless(run_docdata_tests, 'DocData credentials not set or not online')<commit_msg>Fix problem with order test.<commit_after>import json
from apps.cowry.factory import _adapter_for_payment_method
from apps.cowry.models import PaymentStatuses
from apps.cowry_docdata.adapters import default_payment_methods
from apps.cowry_docdata.tests import run_docdata_tests
from django.test import TestCase
from django.test.client import Client
from django.utils import unittest
from django.utils import timezone
from django.test.utils import override_settings
from apps.bluebottle_utils.tests import UserTestsMixin
from apps.projects.tests import ProjectTestsMixin
from apps.projects.models import Project
from rest_framework import status
from ..models import Order, OrderStatuses, DonationStatuses, RecurringDirectDebitPayment


class CartApiIntegrationTest(ProjectTestsMixin, UserTestsMixin, TestCase):
    """
    Integration tests for the adding Donations to an Order (a cart in this case).
    """
    def setUp(self):
        self.some_project = self.create_project(money_asked=50000)
        self.another_project = self.create_project(money_asked=75000)

        self.some_user = self.create_user()
        self.another_user = self.create_user()

        self.current_donations_url = '/i18n/api/fund/orders/current/donations/'
        self.current_order_url = '/i18n/api/fund/orders/current'
        self.payment_profile_current_url = '/i18n/api/fund/paymentprofiles/current'
        self.payment_url_base = '/i18n/api/fund/payments/'
        self.order_url_base = '/i18n/api/fund/orders/'

        self.some_profile = {'first_name': 'Nijntje',
                             'last_name': '<NAME>',
                             'email': '<EMAIL>',
                             'address': 'Dam',
                             'postal_code': '1001AM',
                             'city': 'Amsterdam',
                             'country': 'NL'}

    def test_current_order_donation_crud(self):
        """
        Tests for creating, retrieving, updating and deleting a donation to shopping cart.
        """
        # First make sure we have a current order
        self.client.login(username=self.some_user.email, password='password')
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertEqual(response.data['status'], 'current')

        # Create a Donation
        response = self.client.post(self.current_donations_url, {'project': self.some_project.slug, 'amount': 35})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
        self.assertEqual(response.data['amount'], '35.00')
        self.assertEqual(response.data['project'], self.some_project.slug)
        self.assertEqual(response.data['status'], 'new')

        # Retrieve the created Donation
        donation_detail_url = "{0}{1}".format(self.current_donations_url, response.data['id'])
        response = self.client.get(donation_detail_url)
        self.assertEqual(response.data['amount'], '35.00')
        self.assertEqual(response.data['project'], self.some_project.slug)

        # Retrieve the list with all donations in this cart should return one
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['amount'], '35.00')
        self.assertEqual(response.data['results'][0]['project'], self.some_project.slug)

        # Create another Donation
        response = self.client.post(self.current_donations_url,
                                    {'project': self.another_project.slug, 'amount': '12.50'})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
        self.assertEqual(response.data['amount'], '12.50')
        self.assertEqual(response.data['project'], self.another_project.slug)

        # Create a Donation under 5 should fail
        response = self.client.post(self.current_donations_url,
                                    {'project': self.another_project.slug, 'amount': '2.50'})
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data)

        # Retrieve the list with all donations in this cart should return one
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(response.data['results'][0]['amount'], '35.00')
        self.assertEqual(response.data['results'][1]['amount'], '12.50')
        self.assertEqual(response.data['results'][0]['project'], self.some_project.slug)

        # Update the created Donation by owner.
        response = self.client.put(donation_detail_url, json.dumps({'amount': 150, 'project': self.some_project.slug}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertEqual(response.data['amount'], '150.00')

        # Update with amount under 5 should fail.
        response = self.client.put(donation_detail_url, json.dumps({'amount': 3, 'project': self.some_project.slug}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data)

        # Update the status of the created Donation by owner should be ignored
        response = self.client.put(donation_detail_url,json.dumps({'amount': 150, 'project': self.some_project.slug, 'status': 'paid'}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertEqual(response.data['amount'], '150.00')
        self.assertEqual(response.data['status'], 'new')

        # Delete a donation should work the list should have one donation now
        response = self.client.delete(donation_detail_url)
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.data)
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 1)

        # Another user should not see the cart of the first user
        self.client.logout()
        self.client.login(username=self.another_user.email, password='password')
        # make a cart for this another user
        self.client.get(self.current_order_url)
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 0)

        # Another user should not be able to view a donation in the cart of the first user
        response = self.client.get(donation_detail_url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, response.data)

        # Now let's get anonymous and create a donation
        self.client.logout()
        # make a cart for this anonymous user
        self.client.get(self.current_order_url)
        response = self.client.post(self.current_donations_url, {'project': self.some_project.slug, 'amount': 71})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
        self.assertEqual(response.data['amount'], '71.00')
        self.assertEqual(response.data['project'], self.some_project.slug)
        self.assertEqual(response.data['status'], 'new')
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 1)

        # Login and out again... The anonymous cart should NOT be returned
        self.client.login(username=self.some_user.email, password='password')
        self.client.logout()
        self.client.get(self.current_order_url)
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 0)

        # Login as the first user and cart should 2 donations - one that was there and the one from
        # the anonymous cart that was automatically added.
        self.client.login(username=self.some_user.email, password='password')
        response = self.client.get(self.current_donations_url)
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(response.data['results'][0]['amount'], '12.50')
        self.assertEqual(response.data['results'][0]['project'], self.another_project.slug)
        self.assertEqual(response.data['results'][1]['amount'], '71.00')
        self.assertEqual(response.data['results'][1]['project'], self.some_project.slug)
        self.client.logout()

    def test_current_order_monthly(self):
        # Test setting a recurring order as logged in user.
        self.client.login(username=self.some_user.email, password='password')
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.data['recurring'], False)
        response = self.client.put(self.current_order_url, json.dumps({'recurring': True}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['recurring'], True)

        # Test that setting a recurring order as anonymous user fails.
        self.client.logout()
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.data['recurring'], False)
        response = self.client.put(self.current_order_url, json.dumps({'recurring': True}), 'application/json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        response = self.client.get(self.current_order_url)
        self.assertEqual(response.data['recurring'], False)

    @override_settings(COWRY_PAYMENT_METHODS=default_payment_methods)
    @unittest.skipUnless(run_docdata_tests, 'DocData credentials not set or not online')
{'fraction_non_alphanumeric': 0.11580006353912951, 'fraction_numerical': 0.014984644710367468, 'mean_word_length': 3.374015748031496, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2239579', 'n_tokens_mistral': 5499, 'n_tokens_neox': 5284, 'n_words': 1060}