Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
4.6 kB
2
Indexable
Never
    def test_list_referral_campaigns__no_country_filter(self, app_context):
        factory = YoyoAPIRequestFactory()

        retailer = RetailerFactory()
        voucher_template_1 = AdHocVoucherTemplateFactory(retailer_id=retailer.id)
        referee_validation_reward_1 = YoyoReward.objects.create(
            quantity=2, scheme=voucher_template_1
        )
        campaign_1 = ReferralCampaignFactory(
            retailer=retailer,
            referee_validation_reward_id=referee_validation_reward_1.id,
        )

        # Retailer 1 has an RBA
        retailer_app_name = 'my_app'
        RetailerApiClientNameFactory(name=retailer_app_name, retailer_id=retailer.id)

        voucher_template_2 = AdHocVoucherTemplateFactory(retailer_id=retailer.id)
        referee_completion_reward_2 = YoyoReward.objects.create(
            quantity=2, scheme=voucher_template_2
        )
        campaign_2 = ReferralCampaignFactory(
            retailer=retailer,
            referee_completion_reward_id=referee_completion_reward_2.id,
            countries='PL',
        )

        session = login_factories.ActiveUserSessionFactory()
        request = factory.get('/?all_campaigns=1')
        force_authenticate(request, session=session)

        view = viewsets.UserViewSet.as_view(actions={'get': 'referral_campaigns'})

        app_context.api_client_name = retailer_app_name

        response = view(request, pk=session.user.pk)

        print(response.data)

        assert response.status_code == HTTP_200_OK
        assert sorted(
            with_nested_ordered_dicts_as_dicts(
                response.data['data']['referral_campaigns']
            ),
            key=lambda x: x['retailer_id'],
        ) == sorted([
                   {
                       'campaign_id': campaign_1.id,
                       'retailer_name': campaign_1.retailer.external_name,
                       'retailer_id': campaign_1.retailer_id,
                       'referee_reward': {
                           'type': 'voucher',
                           'name': voucher_template_1.name,
                           'quantity': 2,
                           'reward_immediately': True,
                       },
                       'description': campaign_1.description,
                       'title': campaign_1.title,
                       'subtitle': campaign_1.subtitle,
                       'image_url': None,
                       'imageURL': None,
                       'rewards': {
                           'referee': {
                               'validation_reward': mock.ANY,
                               'completion_reward': mock.ANY,
                           },
                           'referrer': {
                               'validation_reward': mock.ANY,
                               'completion_reward': mock.ANY,
                           },
                       },
                       'start_date': datetime_to_str(campaign_1.start_date),
                       'end_date': datetime_to_str(campaign_1.end_date),
                       'user_progress': mock.ANY,
                   },
                   {
                       'campaign_id': campaign_2.id,
                       'retailer_name': campaign_2.retailer.external_name,
                       'retailer_id': campaign_2.retailer_id,
                       'referee_reward': {
                           'type': 'voucher',
                           'name': voucher_template_2.name,
                           'quantity': 2,
                           'reward_immediately': False,
                        },
                       'description': campaign_2.description,
                       'title': campaign_2.title,
                       'subtitle': campaign_2.subtitle,
                       'image_url': None,
                       'imageURL': None,
                       'rewards': {
                            'referee': {
                                'validation_reward': mock.ANY,
                                'completion_reward': mock.ANY,
                            },
                            'referrer': {
                                'validation_reward': mock.ANY,
                                'completion_reward': mock.ANY,
                            },
                        },
                       'start_date': datetime_to_str(campaign_2.start_date),
                       'end_date': datetime_to_str(campaign_2.end_date),
                       'user_progress': mock.ANY,
                   }
               ],
            key=lambda x: x['retailer_id'],
        )