Ejemplo n.º 1
0
    def test_send_tweet_with_image(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        with open(self.placeholder) as fp:
            response = self.client.post(
                reverse('manage:event_edit', args=(event.pk,)),
                dict(self.event_base_data,
                     title=event.title,
                     placeholder_img=fp)
            )
            assert response.status_code == 302, response.status_code
        event = Event.objects.get(pk=event.pk)
        assert event.placeholder_img
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
            include_placeholder=True
        )
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_updateStatusWithMedia(file_path, status):
            ok_(os.path.isfile(file_path))
            eq_(status, event_tweet.text.encode('utf-8'))
            return {'id': '0000000001'}

        mocker = mock.MagicMock()
        mocker.updateStatusWithMedia.side_effect = mocked_updateStatusWithMedia
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(not event_tweet.error)
        ok_(event_tweet.sent_date)
        eq_(event_tweet.tweet_id, '0000000001')
Ejemplo n.º 2
0
    def test_send_tweet_with_image(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        with open(self.placeholder) as fp:
            response = self.client.post(
                reverse('manage:event_edit', args=(event.pk, )),
                dict(self.event_base_data,
                     title=event.title,
                     placeholder_img=fp))
            assert response.status_code == 302, response.status_code
        event = Event.objects.get(pk=event.pk)
        assert event.placeholder_img
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
            include_placeholder=True)
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_updateStatusWithMedia(file_path, status):
            ok_(os.path.isfile(file_path))
            eq_(status, event_tweet.text.encode('utf-8'))
            return {'id': '0000000001'}

        mocker = mock.MagicMock()
        mocker.updateStatusWithMedia.side_effect = mocked_updateStatusWithMedia
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(not event_tweet.error)
        ok_(event_tweet.sent_date)
        eq_(event_tweet.tweet_id, '0000000001')
Ejemplo n.º 3
0
def event_tweets(request, id):
    """Summary of tweets and submission of tweets"""
    data = {}
    event = get_object_or_404(Event, id=id)

    if request.method == 'POST':
        if request.POST.get('cancel'):
            tweet = get_object_or_404(EventTweet,
                                      pk=request.POST.get('cancel'))
            tweet.delete()
            messages.info(request, 'Tweet cancelled')
        elif request.POST.get('send'):
            tweet = get_object_or_404(EventTweet, pk=request.POST.get('send'))
            send_tweet(tweet)
            if tweet.error:
                messages.warning(request, 'Failed to send tweet!')
            else:
                messages.info(request, 'Tweet sent!')
        elif request.POST.get('error'):
            if not request.user.is_superuser:
                return http.HttpResponseForbidden(
                    'Only available for superusers')
            tweet = get_object_or_404(EventTweet, pk=request.POST.get('error'))
            return http.HttpResponse(tweet.error, mimetype='text/plain')
        else:
            raise NotImplementedError
        url = reverse('manage:event_tweets', args=(event.pk, ))
        return redirect(url)

    data['event'] = event
    data['tweets'] = EventTweet.objects.filter(event=event).order_by('id')

    return render(request, 'manage/event_tweets.html', data)
Ejemplo n.º 4
0
    def test_send_tweet_with_image(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        with open(self.placeholder) as fp:
            response = self.client.post(
                reverse('manage:event_edit', args=(event.pk,)),
                dict(self.event_base_data,
                     title=event.title,
                     placeholder_img=fp)
            )
            assert response.status_code == 302, response.status_code
        event = Event.objects.get(pk=event.pk)
        assert event.placeholder_img
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
            include_placeholder=True
        )
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_update_status(status, media_ids):
            ok_(isinstance(media_ids, int))
            eq_(status, event_tweet.text)
            return {'id': '0000000001'}

        mocker = mock.MagicMock()
        mocker.update_status.side_effect = (
            mocked_update_status
        )

        def mocked_upload_media(media):
            ok_(media.name)
            return {'media_id': 1234567890}

        mocker.upload_media.side_effect = (
            mocked_upload_media
        )
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(not event_tweet.error, event_tweet.error)
        ok_(event_tweet.sent_date)
        eq_(event_tweet.tweet_id, '0000000001')
Ejemplo n.º 5
0
def event_tweets(request, id):
    """Summary of tweets and submission of tweets"""
    data = {}
    event = get_object_or_404(Event, id=id)

    if request.method == 'POST':
        if request.POST.get('cancel'):
            tweet = get_object_or_404(
                EventTweet,
                pk=request.POST.get('cancel')
            )
            tweet.delete()
            messages.info(request, 'Tweet cancelled')
        elif request.POST.get('send'):
            tweet = get_object_or_404(
                EventTweet,
                pk=request.POST.get('send')
            )
            send_tweet(tweet)
            if tweet.error:
                messages.warning(request, 'Failed to send tweet!')
            else:
                messages.info(request, 'Tweet sent!')
        elif request.POST.get('error'):
            if not request.user.is_superuser:
                return http.HttpResponseForbidden(
                    'Only available for superusers'
                )
            tweet = get_object_or_404(
                EventTweet,
                pk=request.POST.get('error')
            )
            return http.HttpResponse(tweet.error, content_type='text/plain')
        else:
            raise NotImplementedError
        url = reverse('manage:event_tweets', args=(event.pk,))
        return redirect(url)

    data['event'] = event
    data['tweets'] = EventTweet.objects.filter(event=event).order_by('id')

    return render(request, 'manage/event_tweets.html', data)
Ejemplo n.º 6
0
    def test_send_tweet_with_image(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        with open(self.placeholder) as fp:
            response = self.client.post(
                reverse('manage:event_edit', args=(event.pk, )),
                dict(self.event_base_data,
                     title=event.title,
                     placeholder_img=fp))
            assert response.status_code == 302, response.status_code
        event = Event.objects.get(pk=event.pk)
        assert event.placeholder_img
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
            include_placeholder=True)
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_update_status(status, media_ids):
            ok_(isinstance(media_ids, int))
            eq_(status, event_tweet.text)
            return {'id': '0000000001'}

        mocker = mock.MagicMock()
        mocker.update_status.side_effect = (mocked_update_status)

        def mocked_upload_media(media):
            ok_(media.name)
            return {'media_id': 1234567890}

        mocker.upload_media.side_effect = (mocked_upload_media)
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(not event_tweet.error, event_tweet.error)
        ok_(event_tweet.sent_date)
        eq_(event_tweet.tweet_id, '0000000001')
Ejemplo n.º 7
0
    def test_send_tweet_with_error(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
        )
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_update_status(status):
            raise NameError('bla')

        mocker = mock.MagicMock()
        mocker.update_status.side_effect = mocked_update_status
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(event_tweet.error)
        ok_(event_tweet.sent_date)
        ok_(not event_tweet.tweet_id)
Ejemplo n.º 8
0
    def test_send_tweet_with_error(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
        )
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_update_status(status):
            raise NameError('bla')

        mocker = mock.MagicMock()
        mocker.update_status.side_effect = mocked_update_status
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(event_tweet.error)
        ok_(event_tweet.sent_date)
        ok_(not event_tweet.tweet_id)
Ejemplo n.º 9
0
    def test_send_tweet_without_image(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
        )
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_updateStatus(status):
            eq_(status, event_tweet.text.encode('utf-8'))
            return {'id': '0000000001'}

        mocker = mock.MagicMock()
        mocker.updateStatus.side_effect = mocked_updateStatus
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(not event_tweet.error)
        ok_(event_tweet.sent_date)
        eq_(event_tweet.tweet_id, '0000000001')
Ejemplo n.º 10
0
    def test_send_tweet_without_image(self, mocked_twython):
        event = Event.objects.get(title='Test event')
        event_tweet = EventTweet.objects.create(
            event=event,
            text=u'\xa310,000 for a cup of tea? #testing',
        )
        assert not event_tweet.sent_date
        assert not event_tweet.tweet_id

        def mocked_updateStatus(status):
            eq_(status, event_tweet.text.encode('utf-8'))
            return {'id': '0000000001'}

        mocker = mock.MagicMock()
        mocker.updateStatus.side_effect = mocked_updateStatus
        mocked_twython.return_value = mocker

        send_tweet(event_tweet)
        # fetch it again, to assure it got saved
        event_tweet = EventTweet.objects.get(pk=event_tweet.pk)
        ok_(not event_tweet.error)
        ok_(event_tweet.sent_date)
        eq_(event_tweet.tweet_id, '0000000001')