def setUp(self): """We add the last_recent_id and last_favorite_id to prevent the fetcher fetching multiple pages of tweets. Keeps things simpler. """ user_1 = UserFactory(screen_name='jill', twitter_id=1) user_2 = UserFactory(screen_name='debs', twitter_id=2) self.account_1 = AccountWithCredentialsFactory(user=user_1, last_recent_id=100, last_favorite_id=100) self.account_2 = AccountWithCredentialsFactory(user=user_2, last_recent_id=100, last_favorite_id=100)
def test_does_not_update_user(self): "Shouldn't fetch user from API on save if it already has a user" self.add_response(body=self.make_verify_credentials_body(), call='account/verify_credentials') user = UserFactory(twitter_id=12552, screen_name='oldname') account = AccountWithCredentialsFactory(user=user) self.assertIsInstance(account.user, User) # Screen name is not changed to the one in our mocked API response: self.assertEqual(account.user.screen_name, 'oldname') self.assertEqual(0, len(responses.calls))
def test_saves_users(self): "Updates the Account's user data in the DB with fetched data." user = UserFactory(twitter_id=12552, screen_name='philgyford', name='This should change') account = AccountWithCredentialsFactory(id=4, user=user) self.add_response(body=self.make_response_body()) result = VerifyFetcher(screen_name='philgyford').fetch() user_reloaded = User.objects.get(screen_name='philgyford') self.assertEqual(user_reloaded.name, 'Phil Gyford')
def test_creates_user(self, fetch_avatar): "Should fetch user from API on save if it has no user" # Just make the mocked method return the User that's passed in: fetch_avatar.side_effect = lambda value: value self.add_response(body=self.make_verify_credentials_body(), call='account/verify_credentials') account = AccountWithCredentialsFactory(user=None) self.assertIsInstance(account.user, User) self.assertEqual(account.user.screen_name, 'philgyford') self.assertEqual(1, len(responses.calls)) self.assertEqual( '%s/%s.json' % (self.api_url, 'account/verify_credentials'), responses.calls[0].request.url)
def test_fetch_for_account_fails(self): "Returns error message if API call fails" self.add_response( body='{"errors":[{"message":"Could not authenticate you","code":32}]}', status=401) account = AccountWithCredentialsFactory.build(user=None) result = FetchVerify(account=account).fetch() self.assertEqual(result['account'], 'Unsaved Account') self.assertEqual(1, len(responses.calls)) self.assertEqual( '%s/%s.json' % (self.api_url, 'account/verify_credentials'), responses.calls[0].request.url) self.assertTrue('Could not authenticate you' in result['messages'][0])
def test_update_user_returns_error_message(self): "Returns an error message if there's an API error." self.add_response( body='{"errors":[{"message":"Could not authenticate you","code":32}]}', call='account/verify_credentials', status=401) # Not saving (as that generates another request): account = AccountWithCredentialsFactory.build(user=None) result = account.updateUserFromTwitter() self.assertEqual(1, len(responses.calls)) self.assertEqual( '%s/%s.json' % (self.api_url, 'account/verify_credentials'), responses.calls[0].request.url) self.assertFalse(result['success']) self.assertIn('Could not authenticate you', result['messages'][0])
def test_fetch_for_account_fails(self): "Returns error message if API call fails" self.add_response( body= '{"errors":[{"message":"Could not authenticate you","code":32}]}', status=401) account = AccountWithCredentialsFactory.build(user=None) result = FetchVerify(account=account).fetch() self.assertEqual(result['account'], 'Unsaved Account') self.assertEqual(1, len(responses.calls)) self.assertEqual( '%s/%s.json' % (self.api_url, 'account/verify_credentials'), responses.calls[0].request.url) self.assertTrue('Could not authenticate you' in result['messages'][0])
def test_update_user_updates_user(self, fetch_avatar): "Sets the Account's user and returns it if all is well." # Just make the mocked method return the User that's passed in: fetch_avatar.side_effect = lambda value: value self.add_response(body=self.make_verify_credentials_body(), call='account/verify_credentials') # Not saving (as that generates another request): account = AccountWithCredentialsFactory.build(user=None) result = account.updateUserFromTwitter() self.assertTrue(result['success']) self.assertIsInstance(result['user'], User) self.assertIsInstance(account.user, User) self.assertEqual(account.user.screen_name, 'philgyford') self.assertEqual(1, len(responses.calls)) self.assertEqual( '%s/%s.json' % (self.api_url, 'account/verify_credentials'), responses.calls[0].request.url)
def test_fetch_for_account_creates(self, fetch_avatar): "Saves and returns new user after successful API call" # Just make the mocked method return the User that's passed in: fetch_avatar.side_effect = lambda value: value self.add_response(body=self.make_response_body()) account = AccountWithCredentialsFactory.build(id=4, user=None) result = FetchVerify(account=account).fetch() new_user = User.objects.get(twitter_id=12552) self.assertEqual(result['account'], 'Account: 4') self.assertIsInstance(result['user'], User) self.assertEqual(result['user'].screen_name, 'philgyford') self.assertEqual(new_user.screen_name, 'philgyford') self.assertEqual(1, len(responses.calls)) self.assertEqual( '%s/%s.json' % (self.api_url, 'account/verify_credentials'), responses.calls[0].request.url)
def test_has_credentials_true(self): self.add_response(body=self.make_verify_credentials_body(), call='account/verify_credentials') account = AccountWithCredentialsFactory.build(user=None) self.assertTrue(account.has_credentials())