def test_can_get_only_pending_requests(self): users = User.objects.all() Friendship(from_account=users[1], to_account=self.user).save() Friendship(from_account=self.user, to_account=users[2]).save() Friendship(from_account=users[3], to_account=self.user, is_accepted=True).save() response = self.get(url=self.url + "pending/").json() self.assertEqual(len(response), 1) self.assertEqual(response[0]["friend"]["username"], users[1].username)
def test_can_get_only_hidden_friends(self): users = User.objects.all() Friendship(from_account=users[1], to_account=self.user, is_hidden=True).save() Friendship(from_account=users[2], to_account=self.user).save() Friendship(from_account=self.user, to_account=users[3], is_hidden=True).save() Friendship(from_account=self.user, to_account=users[4]).save() response = self.get(url=self.url + "hidden/").json() self.assertEqual(len(response), 1) self.assertEqual(response[0]["friend"]["username"], users[1].username)
def test_can_get_all_friends(self): users = User.objects.all() Friendship(from_account=users[1], to_account=self.user, is_hidden=True).save() Friendship(from_account=self.user, to_account=users[2], from_blocking=True).save() Friendship(from_account=users[3], to_account=self.user, from_blocking=True).save() Friendship(from_account=self.user, to_account=users[4], is_accepted=True).save() self.assertEqual(len(self.get(url=self.url + "all/").json()), 4)
def setUp(self): super().setUp() for user in get_user_model().objects.all(): create_device(user) Friendship(from_account=self.user, to_account=user, is_accepted=True).save() self.mocked_send_fcm_message.reset_mock()
def test_can_get_only_blocked_friends(self): users = User.objects.all() Friendship(from_account=users[1], to_account=self.user, to_blocking=True).save() Friendship(from_account=self.user, to_account=users[2], to_blocking=True).save() Friendship(from_account=users[3], to_account=self.user).save() Friendship(from_account=self.user, to_account=users[4], from_blocking=True).save() response = self.get(url=self.url + "blocked/").json() self.assertEqual(len(response), 2) self.assertListEqual([u["friend"]["username"] for u in response], [users[1].username, users[4].username])
def test_cannot_add_user_from_which_friend_request_was_not_accepted(self): friend = get_user_model().objects.last() Friendship(to_account=self.user, from_account=friend).save() self.assert400WithError( self.post( dict(type="place", place=dict(latitude=0, longitude=1), participants=[friend.id])), "friends")
def test_can_create_new_meeting_on_shortest_path(self): friend = get_user_model().objects.last() Friendship(from_account=self.user, to_account=friend, is_accepted=True).save() self.assertEqual( self.post(dict(type="shortest", participants=[friend.id])).status_code, status.HTTP_201_CREATED)
def test_hidden_user_auto_refuse_meetings(self): friend = get_user_model().objects.last() friend.hidden = True Friendship(from_account=self.user, to_account=friend, is_accepted=True).save() self.post( dict(type="place", place=dict(latitude=0, longitude=1), participants=[friend.id])) self.assertFalse(Participant.objects.get(user=friend).accepted)
def test_organiser_accepted_meeting(self): friend = get_user_model().objects.last() Friendship(from_account=self.user, to_account=friend, is_accepted=True).save() self.post( dict(type="place", place=dict(latitude=0, longitude=1), participants=[friend.id])) self.assertTrue(Participant.objects.get(user=self.user).accepted)
def test_can_create_new_meeting_on_person(self): friend = get_user_model().objects.last() Friendship(from_account=self.user, to_account=friend, is_accepted=True).save() self.assertEqual( self.post(dict(type="person", participants=[friend.id])).status_code, status.HTTP_201_CREATED) self.assertEqual(Meeting.objects.first().status, Meeting.STATUS_PENDING)
def test_accept_friendship_send_push_notification(self): friend = get_user_model().objects.get(id=2) friendship = Friendship(from_account=friend, to_account=self.user) friendship.save() self.mocked_send_fcm_message.reset_mock() self.put(dict(is_accepted=True), url=API_V1 + "users/friends/{}/".format(friendship.id)) self.mocked_send_fcm_message.assert_called_once_with( registration_id=friend.get_device().registration_id, message_title=ANY, message_body=ANY, message_icon=ANY, data_message={ "type": "friend-request-accepted", "friendship": friendship.id }, sound=ANY, badge=ANY, )
def test_cannot_add_user_that_is_inactive(self): friend = get_user_model().objects.last() friend.is_active = False friend.save() friendships = [ Friendship(to_account=self.user, from_account=friend, is_accepted=True), Friendship(from_account=self.user, to_account=friend, is_accepted=True) ] for friendship in friendships: friendship.save() self.assert400WithError( self.post( dict(type="place", place=dict(latitude=0, longitude=1), participants=[friend.id])), "friends") friendship.delete()
def test_cannot_be_friend_twice_with_same_person_when_added(self): Friendship(from_account=User.objects.last(), to_account=self.user).save() self.assertContains(self.post(dict(friend=User.objects.last().id)), "already", status_code=status.HTTP_400_BAD_REQUEST)
def test_cannot_see_when_blocked(self): Friendship(from_account=self.user, to_account=User.objects.last(), to_blocking=True).save() self.assertFalse(self.get(self.url + "all/").json()[0]["is_blocked"], msg="Could see that I was blocked")
def setUp(self): super().setUp() Friendship(from_account=self.user, to_account=User.objects.all()[2]).save() Friendship(from_account=User.objects.all()[3], to_account=self.user).save()