Esempio n. 1
0
    def test_snippet_update(self):
        ''' Test the encoding campaign endpoint '''
        shorts = ShortenedUrl.objects.values_list('url', flat=True)
        self.assertEqual(shorts.count(), 0)

        response = self.client.get(
            reverse('targetadmin:snippet-update', args=[self.test_client.pk]),
            {'campaign': self.campaign.pk, 'content': self.content.pk},
        )
        self.assertStatusCode(response, 200)

        data = json.loads(response.content)
        self.assertEqual(
            data['slug'],
            encryptedslug.make_slug(self.campaign, self.content)
        )
        oauth_url = data['fb_oauth_url']
        parsed = urlparse.urlparse(oauth_url)
        query = urlparse.parse_qs(parsed.query)
        redirect_uri = query['redirect_uri'][0]
        self.assertEqual(
            redirect_uri,
            incoming_redirect(False, 'testserver', self.campaign, self.content)
        )
        self.assertEqual(shorts.get(), oauth_url)
Esempio n. 2
0
def incoming_redirect(is_secure, host, campaign=None, content=None, slug=None):
    if slug is None:
        if campaign is None or content is None:
            raise TypeError("Either slug or campaign and content required")
        slug = encryptedslug.make_slug(campaign, content)
    path = reverse('targetshare:incoming-encoded', args=(slug,))
    protocol = 'https://' if is_secure else 'http://'
    return protocol + host + path
Esempio n. 3
0
def faces_url(client_faces_url, campaign, content, *multi, **extra):
    """Construct the complete faces frame URL for the given campaign's base URL."""
    url = urlparse.urlparse(client_faces_url)
    query = http.QueryDict(url.query, mutable=True)
    slug = encryptedslug.make_slug(campaign, content)
    extra.update(efcmpgslug=slug)
    query.update(*multi, **extra)
    full_url = url._replace(query=query.urlencode())
    return full_url.geturl()
Esempio n. 4
0
def build_urls(incoming_host, campaign, content, incoming_secure=settings.INCOMING_REQUEST_SECURE):
    client = campaign.client
    slug = encryptedslug.make_slug(campaign, content)
    incoming_url = incoming_redirect(incoming_secure, incoming_host, slug=slug)
    oauth_url = fb_oauth_url(client.fb_app, incoming_url)
    shortened_url = chapo.utils.shorten(oauth_url, prefix=client.codename, campaign=campaign)
    return {
        'slug': slug,
        'incoming_url': incoming_url,
        'fb_oauth_url': oauth_url,
        'initial_url': shortened_url,
    }
Esempio n. 5
0
 def test_incoming_url_fb_auth_permitted(self, task_mock):
     async_result = task_mock.delay.return_value
     async_result.id = async_result.task_id = 'OAUTH_TOKEN_TASK-1'
     campaign = models.Campaign.objects.get(pk=1)
     path = reverse('targetshare:incoming-encoded', args=[encryptedslug.make_slug(campaign, 1)])
     response = self.client.get(path, {'code': 'PIEZ'})
     self.assertStatusCode(response, 302)
     self.assertNotIn('code=', response['Location'])
     session = self.client.session
     self.assertEqual(session['oauth_task'], 'OAUTH_TOKEN_TASK-1')
     visit_id = models.Visit.objects.only('visit_id').get(session_id=session.session_key).visit_id
     task_mock.delay.assert_called_once_with('PIEZ', 'http://testserver' + path, Decimal('1.0'),
                                             client_id=1, visit_id=visit_id, campaign_id=1, content_id=1)
Esempio n. 6
0
 def test_incoming_url_redirect(self, task_mock):
     campaign = models.Campaign.objects.get(pk=1)
     response = self.client.get(
         reverse('targetshare:incoming-encoded', args=[encryptedslug.make_slug(campaign, 1)])
     )
     self.assertStatusCode(response, 302)
     self.assertTrue(
         models.Event.objects.filter(event_type='incoming_redirect').exists()
     )
     self.assertEqual(
         response['Location'],
         'http://local.edgeflip.com:8080/mocks/guncontrol_share?efcmpgslug=uV8JNec7DxI'
     )
     self.assertEqual(encryptedslug.get_params('uV8JNec7DxI'), (1, 1, 1))
     self.assertFalse(task_mock.delay.called)
Esempio n. 7
0
def client_faces_url(campaign, content):
    slug = encryptedslug.make_slug(campaign, content)
    if settings.ENV == 'development':
        netloc = 'http://local.edgeflip.com:8080'
        path = reverse('targetshare:frame-faces-encoded', args=(slug,))
    else:
        netloc = 'https://apps.facebook.com'
        true_path = reverse('targetshare-canvas:frame-faces-encoded', args=(slug,))
        # Remove the true path base from FB vanity URL path:
        (rel_path, subs) = re.subn(r'^/canvas/', '', true_path, 1)
        if subs != 1:
            LOG.error("Failed to trim Facebook canvas base path (/canvas/) "
                      "from true request path (%s)", true_path,
                      extra={'stack': True})
        path = '/{}/{}'.format(campaign.client.fb_app.name, rel_path)
    return netloc + path
Esempio n. 8
0
    def test_incoming_url_redirect_fb_auth_declined(self):
        auth_fails = models.Event.objects.filter(event_type='auth_fail')
        redirects = models.Event.objects.filter(event_type='incoming_redirect')
        self.assertFalse(auth_fails.exists())
        self.assertFalse(redirects.exists())

        campaign = models.Campaign.objects.get(pk=1)
        response = self.client.get(
            reverse('targetshare:incoming-encoded', args=[encryptedslug.make_slug(campaign, 1)]),
            {'error': 'access_denied', 'error_reason': 'user_denied'}
        )
        self.assertStatusCode(response, 302)

        campaign_props = campaign.campaignproperties.get()
        outgoing_path = reverse('targetshare:outgoing', args=[
            campaign_props.campaign.client.fb_app_id,
            campaign_props.client_error_url]
        )
        outgoing_url = "{}?{}".format(outgoing_path, urllib.urlencode({
            'campaignid': campaign_props.campaign.pk,
        }))
        expected_url = 'http://testserver' + outgoing_url
        self.assertEqual(response['Location'], expected_url)

        session_id = self.client.cookies['sessionid'].value
        visit = models.Visit.objects.get(
            session_id=session_id,
            app_id=campaign_props.campaign.client.fb_app_id,
        )

        auth_fail = auth_fails.get()
        self.assertEqual(auth_fail.content, 'oauth')
        self.assertEqual(auth_fail.visit_id, visit.visit_id)
        self.assertEqual(auth_fail.campaign_id, 1)
        self.assertEqual(auth_fail.client_content_id, 1)

        incoming = redirects.get()
        self.assertEqual(incoming.content, outgoing_url)
        self.assertEqual(incoming.visit_id, visit.visit_id)
        self.assertEqual(incoming.campaign_id, 1)
        self.assertEqual(incoming.client_content_id, 1)
Esempio n. 9
0
    def test_edit_campaign_wizard(self):
        app = relational.FBApp.objects.create(
            appid=1,
            name='testing',
            secret='sekret',
        )
        new_client = app.clients.create(name='Test Client')
        self.assertFalse(new_client.campaigns.exists())
        response = self.client.post(
            reverse('targetadmin:campaign-wizard', args=[new_client.pk]), {
                # Campaign Details
                'name': 'Test Campaign',
                'error_url': 'http://www.error.com',
                'thanks_url': 'http://www.thanks.com',
                'content_url': 'http://www.content.com',
                'include_empty_fallback': False,
                'enabled-filters-1': '"state.eq.California"',
                # FB Object
                'og_title': 'Test Title',
                'org_name': 'Test Organization',
                'msg1_pre': 'Hey, ',
                'msg1_post': ' How goes it?',
                'msg2_pre': 'Hey 2, ',
                'msg2_post': ' How goes it 2?',
                'og_image': 'http://imgur.com/VsiPr',
                'sharing_prompt': 'SHARE IT',
                'sharing_button': 'Show Your Support!',
                'og_description': 'Description of FB stuff'
            }
        )
        self.assertStatusCode(response, 302)
        campaign = new_client.campaigns.get()
        content0 = new_client.clientcontent.get()
        props = campaign.campaignproperties.all()
        encoded = encryptedslug.make_slug(campaign, content0)
        self.assertEqual(props.values_list('client_faces_url', flat=True).get(),
                         'https://apps.facebook.com/{}/share/{}'.format(new_client.fb_app.name, encoded))
        self.assertIsNone(props.get().fallback_campaign)

        response = self.client.post(
            reverse('targetadmin:campaign-wizard-edit', args=[new_client.pk, campaign.pk]), {
                # Campaign Details
                'name': 'Test Campaign',
                'error_url': 'http://www.error.com',
                'thanks_url': 'http://www.thanks.com',
                'content_url': 'http://www.content.com',
                'include_empty_fallback': True,
                'enabled-filters-1': '"state.eq.California"',
                # FB Object
                'og_title': 'Test Title',
                'org_name': 'Test Organization',
                'msg1_pre': 'Hey, ',
                'msg1_post': ' How goes it?',
                'msg2_pre': 'Hey 2, ',
                'msg2_post': ' How goes it 2?',
                'og_image': 'http://imgur.com/VsiPr',
                'sharing_prompt': 'SHARE IT',
                'sharing_button': 'Show Your Support!',
                'og_description': 'Description of FB stuff'
            }
        )
        self.assertStatusCode(response, 302)
        campaign = new_client.campaigns.exclude(rootcampaign_properties=None).get()
        content = new_client.clientcontent.get()
        self.assertEqual(content, content0)
        self.assertTrue(props.get().fallback_campaign)
        encoded = encryptedslug.make_slug(campaign, content)
        self.assertEqual(props.values_list('client_faces_url', flat=True).get(),
                         'https://apps.facebook.com/{}/share/{}'.format(new_client.fb_app.name, encoded))
Esempio n. 10
0
 def test_create_campaign_wizard_no_filtering(self):
     app = relational.FBApp.objects.create(
         appid=1,
         name='testing',
         secret='sekret',
     )
     new_client = app.clients.create(name='Test Client')
     self.assertFalse(new_client.filters.exists())
     self.assertFalse(new_client.fbobjects.exists())
     self.assertFalse(new_client.choicesets.exists())
     self.assertFalse(new_client.buttonstyles.exists())
     self.assertFalse(new_client.campaigns.exists())
     response = self.client.post(
         reverse('targetadmin:campaign-wizard', args=[new_client.pk]), {
             # Campaign Details
             'name': 'Test Campaign',
             'error_url': 'http://www.error.com',
             'thanks_url': 'http://www.thanks.com',
             'content_url': 'http://www.content.com',
             'include_empty_fallback': True,
             # FB Object
             'og_title': 'Test Title',
             'org_name': 'Test Organization',
             'msg1_pre': 'Hey, ',
             'msg1_post': ' How goes it?',
             'msg2_pre': 'Hey 2, ',
             'msg2_post': ' How goes it 2?',
             'og_image': 'http://imgur.com/VsiPr',
             'sharing_prompt': 'SHARE IT',
             'sharing_button': 'Show Your Support',
             'og_description': 'Description of FB stuff'
         }
     )
     self.assertStatusCode(response, 302)
     camp = new_client.campaigns.latest('pk')
     content = new_client.clientcontent.latest('pk')
     cs = camp.campaignchoicesets.get().choice_set
     fb_attr = camp.campaignfbobjects.get().fb_object.fbobjectattribute_set.get()
     self.assertRedirects(response,
         reverse('targetadmin:campaign-wizard-finish', args=[new_client.pk, camp.pk]),
     )
     self.assertIn('Root', cs.name)
     self.assertIn('Root', cs.choicesetfilters.get().filter.name)
     self.assertTrue(new_client.filters.exists())
     self.assertTrue(new_client.fbobjects.exists())
     self.assertTrue(new_client.choicesets.exists())
     self.assertTrue(new_client.buttonstyles.exists())
     self.assertTrue(new_client.campaigns.exists())
     self.assertEqual(new_client.campaigns.count(), 1)
     # 1 empty
     self.assertEqual(new_client.filters.count(), 1)
     self.assertEqual(new_client.choicesets.count(), 1)
     self.assertEqual(fb_attr.og_action, 'support')
     self.assertEqual(fb_attr.og_type, 'cause')
     self.assertEqual(
         camp.campaignproperties.get().client_faces_url,
         'https://apps.facebook.com/{}/share/{}'.format(
             new_client.fb_app.name,
             encryptedslug.make_slug(camp, content)
         )
     )
     (notification,) = mail.outbox
     self.assertIn(camp.name, notification.body)