def test_get_messages_from_wrong_chat(auth_client): chat: Chat = ChatFactory(user1=ProfileFactory(), user2=ProfileFactory()) url = reverse('chat:chat-detail', kwargs={'pk':chat.id}) response = auth_client.get(url) assert response.status_code == 400 assert response.data['detail'] == 'user can\'t get messages from a chat he is not in'
def test_send_message_to_wrong_chat(auth_client): '''Send message to chat user is not in''' chat: Chat = ChatFactory( user1=ProfileFactory(), user2=ProfileFactory() ) url = reverse('chat:chat-list') data = {'chat':chat.id, 'message':'Hello world!'} response = auth_client.post(url, data=data) assert response.status_code == 400 assert response.data['detail'] == 'user can\'t chat with unmatched users'
def test_get_user_chats(auth_client): '''Test if user gets all chat he tooks part in''' users_count = random.randrange(1, 10) url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) user_profile = Profile.objects.get(id=response.data['id']) for i in range(users_count): user: User = UserFactory() profile: Profile = ProfileFactory( fname=user.first_name, lname=user.last_name, user=user, gender='F' ) location: Location = LocationFactory(profile=profile) if i % 2 == 0: chat: Chat = ChatFactory(user1=user_profile, user2=profile) else: chat: Chat = ChatFactory(user1=profile, user2=user_profile) url = reverse('chat:chat-list') response = auth_client.get(url) assert response.status_code == 200 assert len(response.data) == users_count
def test_get_messages_from_chat(auth_client): '''Get messages from user specified chat test''' url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) user_profile = Profile.objects.get(id=response.data['id']) user: User = UserFactory() profile: Profile = ProfileFactory( fname=user.first_name, lname=user.last_name, user=user, gender='F' ) location: Location = LocationFactory(profile=profile) chat: Chat = ChatFactory(user1=user_profile, user2=profile) messages_count = random.randint(1, 10) for i in range(messages_count): message = factory.Faker('sentence') if i % 2 == 0: user = user_profile else: user = profile message: Message = MessageFactory(message=message, sender=user, chat=chat) url = reverse('chat:chat-detail', kwargs={'pk':chat.id}) response = auth_client.get(url) assert response.status_code == 200 assert len(response.data) == messages_count
def test_send_message(auth_client): ''' Test whether user can send message and it's sent to correct chat ''' url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) user_profile = Profile.objects.get(id=response.data['id']) user: User = UserFactory() profile: Profile = ProfileFactory( fname=user.first_name, lname=user.last_name, user=user, gender='F' ) location: Location = LocationFactory(profile=profile) chat: Chat = ChatFactory(user1=user_profile, user2=profile) url = reverse('chat:chat-list') data = {'chat':chat.id, 'message':'Hello world!'} response = auth_client.post(url, data=data) assert response.status_code == 201 assert response.data['sender']['id'] == user_profile.id assert response.data['chat']['id'] == chat.id
def test_swipe_limit(auth_client): '''Test whether user cant swipe anymore according to his subscription limit''' url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) user_profile = Profile.objects.get(id=response.data['id']) limit = settings.BASIC_SUBSCRIPTION_SWIPES if user_profile.vip else settings.VIP_SUBSCRIBTION_SWIPES gender = 'F' if user_profile.gender == 'M' else 'M' last_profile = None for i in range(limit + 1): user: User = UserFactory() profile: Profile = ProfileFactory(fname=user.first_name, lname=user.last_name, user=user, gender=gender) location: Location = LocationFactory(profile=profile) if i == limit: last_profile = profile break swipe = SwipeFactory(profile=user_profile, swiped=profile, liked=random.choice([True, False])) url = reverse('activities:swipe-list') data = {'swiped': last_profile.id, 'liked': True} response = auth_client.post(url, data=data) assert response.status_code == 400 assert response.data['detail'] == 'swipes limit is exceeded for today'
def test_get_matches(auth_client): '''Test getting matches of current user''' url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) user_profile = Profile.objects.get(id=response.data['id']) user: User = UserFactory() profile: Profile = ProfileFactory(fname=user.first_name, lname=user.last_name, user=user, gender='F') location: Location = LocationFactory(profile=profile) swipe_user: Swipe = SwipeFactory(profile=user_profile, swiped=profile, liked=True) swipe: Swipe = SwipeFactory(profile=profile, swiped=user_profile, liked=True) url = reverse('activities:swipe-list') response = auth_client.get(url) assert (response.data[0]['profile']['id'] == profile.id and response.data[0]['swiped']['id'] == user_profile.id)
def auth_client(): client = APIClient() user: User = UserFactory() profile: Profile = ProfileFactory(fname=user.first_name, lname=user.last_name, user=user) location: Location = LocationFactory(profile=profile) refresh = RefreshToken.for_user(user) client.credentials(HTTP_AUTHORIZATION=f'Bearer {refresh.access_token}') return client
def test_get_matched_profile(auth_client): '''Getting info of matched and unmatched users''' url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) user_profile = Profile.objects.get(id=response.data['id']) user: User = UserFactory() profile: Profile = ProfileFactory(fname=user.first_name, lname=user.last_name, user=user, gender='F') location: Location = LocationFactory(profile=profile) swipe_user: Swipe = SwipeFactory(profile=user_profile, swiped=profile, liked=True) swipe: Swipe = SwipeFactory(profile=profile, swiped=user_profile, liked=True) url = reverse('app:profile-detail', kwargs={'pk': profile.id}) response = auth_client.get(url) assert response.status_code == 200 user_unmatched: User = UserFactory() profile_unmatched: Profile = ProfileFactory(fname=user.first_name, lname=user.last_name, user=user, gender='F') location_unmatched: Location = LocationFactory(profile=profile) url = reverse('app:profile-detail', kwargs={'pk': profile_unmatched.id}) response = auth_client.get(url) assert response.status_code == 400 assert response.data[ 'detail'] == 'user can\'t get info about unmatched profile'
def test_already_swiped_user(auth_client): '''Test whether user already swiped user he wants to swipe now''' user: User = UserFactory() profile: Profile = ProfileFactory(fname=user.first_name, lname=user.last_name, user=user, gender='F') location: Location = LocationFactory(profile=profile) url = reverse('activities:swipe-list') data = {'swiped': profile.id, 'liked': True} response = auth_client.post(url, data=data) assert response.status_code == 201 response_repeat = auth_client.post(url, data=data) assert response_repeat.status_code == 400 assert response_repeat.data[ 'detail'] == 'user have already swiped this profile'
def test_user_swipe(auth_client): '''User swipe test''' url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) id = response.data['id'] user: User = UserFactory() profile: Profile = ProfileFactory(fname=user.first_name, lname=user.last_name, user=user, gender='F') location: Location = LocationFactory(profile=profile) url = reverse('activities:swipe-list') data = {'swiped': profile.id, 'liked': True} response = auth_client.post(url, data=data) assert response.status_code == 201 assert response.data['swipe']['profile']['id'] == id assert response.data['swipe']['swiped']['id'] == profile.id
def test_create_chat_on_match(auth_client): '''Test whether new chat created when users are matched''' url = reverse('app:profile-list') + '?me=true' response = auth_client.get(url) user_profile = Profile.objects.get(id=response.data['id']) user: User = UserFactory() profile: Profile = ProfileFactory( fname=user.first_name, lname=user.last_name, user=user, gender='F' ) location: Location = LocationFactory(profile=profile) swipe: Swipe = SwipeFactory(profile=profile, swiped=user_profile, liked=True) url = reverse('activities:swipe-list') data = {'swiped':profile.id, 'liked':True} response = auth_client.post(url, data=data) assert response.status_code == 201 assert response.data['match'] == True assert Chat.objects.filter(user1=user_profile, user2=profile).count() == 1