def test_get_all_connections_handles_no_users(self, mock_user_class): mock_user_class.objects.all.return_value = [] actual = get_all_connections() expected = [] self.assertListEqual(actual, expected)
def test_get_all_connections_returns(self, mock_friend_class, mock_user_class): mock_user_class.objects.all.return_value = [MagicMock(username='******'), MagicMock(username='******'), MagicMock(username='******'), MagicMock(username='******')] mock_friend_class.objects.friends.side_effect = ([MagicMock(username='******')], [MagicMock(username='******'), MagicMock(username='******')], [], [MagicMock(username='******')]) actual = get_all_connections() expected = [{'username': '******', 'friends': ['ben']}, {'username': '******', 'friends': ['joe', 'james']}, {'username': '******', 'friends': []}, {'username': '******', 'friends': ['james']}] self.assertListEqual(actual, expected)
def all_connections(request): if request.user.is_superuser: response = get_all_connections() return HttpResponse(dumps(response)) else: return HttpResponse(status=401)