def test_cannot_delete_administrated_community_categories(self): """ should not be able to update the administrated community categories and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user) data = {'categories': ''} url = self._get_url(community_name=community.name) response = self.client.patch(url, data, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) community.refresh_from_db() self.assertTrue(community.categories.exists())
def test_cant_ban_user_from_community_if_already_banned(self): """ should not be able to ban user from a community if is already banned and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user, type='P') community_name = community.name user_to_ban = make_user() user.ban_user_with_username_from_community_with_name(username=user_to_ban.username, community_name=community_name) url = self._get_url(community_name=community.name) response = self.client.post(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue(user_to_ban.is_banned_from_community_with_name(community.name))
def test_cannot_retrieve_banned_users_of_public_community(self): """ should not be able to retrieve the banned users of a public community and return 400 :return: """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user, type='P') community_name = community.name user_to_ban = make_user() other_user.ban_user_with_username_from_community_with_name(username=user_to_ban.username, community_name=community_name) url = self._get_url(community_name=community.name) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_cant_search_for_reported_hashtag(self): """ should not be able to search for a reported hashtag and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) hashtag_name = make_hashtag_name() hashtag = make_hashtag(name=hashtag_name) report_category = make_moderation_category() user.report_hashtag_with_name(hashtag_name=hashtag_name, category_id=report_category.pk) url = self._get_url() response = self.client.get(url, {'query': hashtag.name}, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) parsed_response = json.loads(response.content) self.assertEqual(len(parsed_response), 0)
def test_can_delete_notifications(self): """ should be able to delete all notifications and return 200 """ user = make_user() amount_of_notifications = 5 notifications_ids = [] for i in range(0, amount_of_notifications): notification = make_notification(owner=user) notifications_ids.append(notification.pk) url = self._get_url() headers = make_authentication_headers_for_user(user) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(Notification.objects.filter(owner=user).exists())
def test_cannot_withdraw_unexisting_invite(self): """ should not be able to withdraw an unexisting invite and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user, type='P') community.invites_enabled = True community.save() user.join_community_with_name(community.name) user_to_invite = make_user() url = self._get_url(community_name=community.name) response = self.client.post(url, {'username': user_to_invite.username}, **headers) self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
def test_logs_community_administrator_added(self): """ should create a log when community administrator was added """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user, type='P') administrator_to_add = make_user() administrator_to_add.join_community_with_name(community_name=community.name) url = self._get_url(community_name=community.name) self.client.put(url, { 'username': administrator_to_add.username }, **headers) self.assertTrue(community.logs.filter(action_type='AA', source_user=user, target_user=administrator_to_add).exists())
def test_invalid_community_name(self): """ should return 400 if the communityName is not a valid one """ user = make_user() headers = make_authentication_headers_for_user(user) url = self._get_url() community_names = ('lifenau!', 'p-o-t-a-t-o', '.a!', 'dexter@', 'ЪциРђЇРЎѓ№ИЈ', 'hola ahi') for community_name in community_names: request_data = self._get_request_data(community_name) response = self.client.post(url, request_data, **headers, format='json') parsed_response = json.loads(response.content) self.assertIn('name', parsed_response) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_cant_create_community_without_mandatory_params(self): """ should NOT be able to create a community without providing mandatory params and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) mandatory_params = ['name', 'type', 'title', 'color', 'categories'] url = self._get_url() response = self.client.put(url, {}, **headers, format='multipart') parsed_response = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) for param in mandatory_params: self.assertIn(param, parsed_response) self.assertTrue(Community.objects.all().count() == 0)
def test_cant_retrieve_reported_connected_user_post(self): """ should not be able to retrieve reported connected user post """ user = make_user() connected_user = make_user() user.connect_with_user_with_id(user_id=connected_user.pk) connected_user_post_circle = make_circle(creator=connected_user) connected_user.confirm_connection_with_user_with_id(user_id=user.pk, circles_ids=[connected_user_post_circle.pk]) connected_user_post = connected_user.create_encircled_post(text=make_fake_post_text(), circles_ids=[connected_user_post_circle.pk]) user.report_post(post=connected_user_post, category_id=make_moderation_category().pk) url = self._get_url(post=connected_user_post) headers = make_authentication_headers_for_user(user) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_can_mute_foreign_post_if_part_of_encircled_post(self): user = make_user() foreign_user = make_user() headers = make_authentication_headers_for_user(user) circle = make_circle(creator=foreign_user) post = foreign_user.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk]) foreign_user.connect_with_user_with_id(user_id=user.pk, circles_ids=[circle.pk]) user.confirm_connection_with_user_with_id(user_id=foreign_user.pk) url = self._get_url(post) response = self.client.post(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(user.has_muted_post_with_id(post.pk))
def test_canot_edit_to_remove_text_from_own_text_only_post(self): """ should not be able to edit to remove the text of an own post and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) initial_text = make_fake_post_text() post = user.create_public_post(text=initial_text) url = self._get_url(post) data = { 'text': '' } response = self.client.patch(url, data, **headers) self.assertTrue(response.status_code, status.HTTP_400_BAD_REQUEST) post.refresh_from_db() self.assertEqual(post.text, initial_text) self.assertFalse(post.is_edited)
def test_can_edit_own_post(self): """ should be able to edit own post and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) post = user.create_public_post(text=make_fake_post_text()) url = self._get_url(post) edited_text = make_fake_post_text() data = { 'text': edited_text } response = self.client.patch(url, data, **headers) self.assertTrue(response.status_code, status.HTTP_200_OK) post.refresh_from_db() self.assertEqual(post.text, edited_text) self.assertTrue(post.is_edited)
def test_cannot_open_post_if_not_administrator_or_moderator_of_community(self): """ should not be able to open post if not moderator/administrator of a community """ user = make_user() admin = make_user() community = make_community(admin) user.join_community_with_name(community_name=community.name) post = user.create_community_post(community.name, text=make_fake_post_text()) post.is_closed = True post.save() url = self._get_url(post) headers = make_authentication_headers_for_user(user) response = self.client.post(url, **headers) self.assertTrue(response.status_code, status.HTTP_400_BAD_REQUEST) post.refresh_from_db() self.assertTrue(post.is_closed)
def test_can_update_user_url(self): """ should be able to update the authenticated user url and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) new_url = fake.url() data = {'url': new_url} url = self._get_url() response = self.client.patch(url, data, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) user.refresh_from_db() self.assertEqual(new_url, user.profile.url)
def test_list_name_taken(self): """ should return status 400 if the listName is taken """ user = make_user() emoji = make_emoji() list = user.create_list(name=make_fake_list_name(), emoji_id=emoji.pk) request_data = {'name': list.name} url = self._get_url() headers = make_authentication_headers_for_user(user) response = self.client.post(url, request_data, format='json', **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_cant_delete_user_with_wrong_password(self): """ should not be able to delete the authenticated user with a wrong password and return 401 """ user = make_user() headers = make_authentication_headers_for_user(user) user_password = fake.password() user.save() data = {'password': user_password} url = self._get_url() response = self.client.post(url, data, **headers) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assertTrue(User.objects.filter(pk=user.pk).exists())
def test_cannot_retrieve_community_banned_from(self): """ should not be able to retrieve a community banned from and return 403 """ user = make_user() headers = make_authentication_headers_for_user(user) community_owner = make_user() community = make_community(creator=community_owner) community_name = community.name user.join_community_with_name(community_name=community.name) community_owner.ban_user_with_username_from_community_with_name(username=user.username, community_name=community.name) url = self._get_url(community_name=community_name) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_cannot_join_already_joined_community(self): """ should not be able to join a private community without an invite """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user, type='O') user.join_community_with_name(community.name) url = self._get_url(community_name=community.name) response = self.client.post(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue( user.is_member_of_community_with_name( community_name=community.name))
def test_cant_favorite_already_favorite_community(self): """ should not be be able to favorite an already favorite community and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user) user.join_community_with_name(community.name) user.favorite_community_with_name(community.name) url = self._get_url(community_name=community.name) response = self.client.put(url, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue(user.has_favorite_community_with_name(community.name))
def test_logs_community_administrator_removed(self): """ should create a log when community administrator was removed """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user, type='P') administrator_to_remove = make_user() administrator_to_remove.join_community_with_name(community_name=community.name) user.add_administrator_with_username_to_community_with_name(username=administrator_to_remove.username, community_name=community.name) url = self._get_url(community_name=community.name, username=administrator_to_remove.username) self.client.delete(url, **headers) self.assertTrue(community.logs.filter(action_type='RA', source_user=user, target_user=administrator_to_remove).exists())
def test_can_unfavorite_favorite_community(self): """ should be able to unfavorite a favorite community and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user) user.join_community_with_name(community.name) user.favorite_community_with_name(community.name) url = self._get_url(community_name=community.name) response = self.client.delete(url, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(user.has_favorite_community_with_name(community.name))
def test_cannot_read_foreign_notification(self): """ should not be able to read a foreign notification and return 400 """ user = make_user() foreign_user = make_user() headers = make_authentication_headers_for_user(user) notification = make_notification(owner=foreign_user) notification_id = notification.pk url = self._get_url(notification_id) response = self.client.post(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue( Notification.objects.filter(id=notification_id, read=False).exists())
def test_cannot_delete_invite_if_invite_is_accepted(self): """ should not be able to delete if an invite already has been accepted """ original_invite_count = 5 user = make_user(invite_count=original_invite_count) nickname = fake.name() invite = user.create_invite(nickname=nickname) invite.created_user = make_user() invite.save() url = self._get_url(invite.pk) headers = make_authentication_headers_for_user(user) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) user.refresh_from_db() self.assertEqual(user.invite_count, original_invite_count - 1) self.assertTrue(UserInvite.objects.filter(id=invite.pk).exists())
def test_cannot_retrieve_received_foreign_follow_requests(self): """ should not be able to retrieve foreign follow requests and return 200 """ user = make_user(visibility=User.VISIBILITY_TYPE_PRIVATE) foreign_user = make_user(visibility=User.VISIBILITY_TYPE_PRIVATE) headers = make_authentication_headers_for_user(user) requesting_user = make_user() requesting_user.create_follow_request_for_user(user=foreign_user) url = self._get_url() response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_follow_requests = json.loads(response.content) self.assertEqual(len(response_follow_requests), 0)
def test_cannot_update_user_username_to_taken_username(self): """ should be able to update the authenticated user username to a taken username and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) user_b = make_user() data = {'username': user_b.username} url = self._get_url() response = self.client.patch(url, data, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) user.refresh_from_db() self.assertNotEqual(user.username, user_b.username)
def test_cant_ban_user_from_community(self): """ should not be able to ban user from a community and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user, type='P') user_to_ban = make_user() url = self._get_url(community_name=community.name) response = self.client.post(url, { 'username': user_to_ban.username }, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(user_to_ban.is_banned_from_community_with_name(community.name))
def test_can_update_user_cover(self): """ should be able to update the authenticated user cover and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) new_cover = make_user_cover() data = {'cover': new_cover} url = self._get_url() response = self.client.patch(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_200_OK) user.refresh_from_db() self.assertIsNotNone(user.profile.cover)
def test_can_delete_administrated_community_user_adjective(self): """ should be able to delete the administrated community user_adjective and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user) data = {'user_adjective': ''} url = self._get_url(community_name=community.name) response = self.client.patch(url, data, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) community.refresh_from_db() self.assertTrue(not community.user_adjective)
def test_can_delete_devices(self): """ should be able to delete all devices and return 200 """ user = make_user() amount_of_devices = 5 devices_ids = [] for i in range(0, amount_of_devices): device = make_device(owner=user) devices_ids.append(device.uuid) url = self._get_url() headers = make_authentication_headers_for_user(user) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(Device.objects.filter(owner=user).exists())