Skip to content

Commit

Permalink
Adds a few more tests to the shareonfacebook command
Browse files Browse the repository at this point in the history
  • Loading branch information
dirtycoder committed Nov 13, 2016
1 parent 0da1fe1 commit 130c62e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
23 changes: 13 additions & 10 deletions pets/meupet/management/commands/shareonfacebook.py
Expand Up @@ -17,23 +17,26 @@ def __init__(self):
self.config = Configuration.objects.first()
super(Command, self).__init__()

def get_token(self):
return self.config.fb_share_token

def get_attachment(self, pet):
link = self.config.fb_share_link
@staticmethod
def get_attachment(pet, url):
attachment = {
'link': link.format(pet.get_absolute_url()),
'link': url.format(pet.get_absolute_url()),
}
return attachment

@staticmethod
def get_message(pet):
return '{0}: {1}, {2}'.format(pet.get_status_display(), pet.name, pet.city)

def handle(self, *args, **options):
api = facebook.GraphAPI(self.get_token())
api = facebook.GraphAPI(self.config.fb_share_token)
url = self.config.fb_share_link

for pet in Pet.objects.get_unpublished_pets():
msg = '{}: {}, {}'.format(pet.get_status_display(), pet.name, pet.city)

api.put_wall_post(msg, attachment=self.get_attachment(pet))
api.put_wall_post(
self.get_message(pet),
attachment=self.get_attachment(pet, url)
)

pet.published = True
pet.save()
50 changes: 32 additions & 18 deletions pets/meupet/tests/test_shareonfacebook_command.py
Expand Up @@ -2,6 +2,8 @@

from django.test import TestCase

from model_mommy import mommy

from meupet.management.commands.shareonfacebook import Command
from meupet.models import Pet, City
from users.models import OwnerProfile
Expand All @@ -11,32 +13,44 @@ class ManagementCommandTest(TestCase):
def setUp(self):
self.admin = OwnerProfile.objects.create_user(username='admin', password='admin')
self.city = City.objects.create(city='Araras')
self.pet = Pet.objects.create(
name='Testing Pet',
city=self.city,
status=Pet.MISSING,
owner=self.admin
)
self.pet = mommy.make(Pet, city=self.city)
self.url = 'http://www.test.com{}'

def test_get_attachment(self):
"""The attachment should be a dict with the 'link' key
being the absolute url to the pet"""
attach = Command.get_attachment(self.pet, self.url)

expected = self.url.format(self.pet.get_absolute_url())

self.assertEqual(expected, attach.get('link', ''))

def test_get_message(self):
"""The message in the post should always contains the
display status, the name of the pet, and the name of the city"""
msg = Command.get_message(self.pet)

expected = '{0}: {1}, {2}'.format(self.pet.get_status_display(), self.pet.name, self.pet.city)

self.assertEqual(expected, msg)

def test_shareonfacebook_command(self):
link = {
'link': 'http://www.test.com/{}'.format(self.pet.get_absolute_url())
}
"""The command should be called with the correct
message and attachment"""
mock = self.call_mocked_command()

mock = self.call_mocked_command(link)
attach = Command.get_attachment(self.pet, self.url)
msg = Command.get_message(self.pet)

expected = 'Desaparecido: Testing Pet, Araras'
mock.assert_called_once_with(expected, attachment=link)
mock.assert_called_once_with(msg, attachment=attach)

def call_mocked_command(self, link):
def call_mocked_command(self):
mock = MagicMock()
mock.get_token.return_value = 'token'
mock.get_attachment.return_value = link
mock.fb_share_token = 'token'
mock.fb_share_link = self.url

cmd = Command()

cmd.get_attachment = mock.get_attachment
cmd.get_token = mock.get_token
cmd.config = mock

with patch('facebook.GraphAPI.put_wall_post') as mock:
cmd.handle()
Expand Down

0 comments on commit 130c62e

Please sign in to comment.