def test_can_join_private_community_with_invite(self): """ should be able to join a private community with an invite and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user, type='T') other_user.invite_user_with_username_to_community_with_name(username=user.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_201_CREATED) self.assertTrue(user.is_member_of_community_with_name(community_name=community.name))
def test_can_remove_excluded_community(self): """ should be able to remove an community exclusion """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user) user.exclude_community_with_name_from_top_posts(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_202_ACCEPTED) self.assertFalse( user.has_excluded_community_with_name( community_name=community.name))
def test_cannot_exclude_private_community(self): """ should not be able to exclude a private community from top posts """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user, type=Community.COMMUNITY_TYPE_PRIVATE) 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.assertFalse( user.has_excluded_community_with_name( community_name=community.name))
def test_cannot_exclude_community_already_excluded(self): """ should not be able to exclude a community if already excluded from top posts """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user) user.exclude_community_with_name_from_top_posts(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_excluded_community_with_name( community_name=community.name))
def test_can_favorite_joined_community(self): """ should be able to favorite a joined 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) url = self._get_url(community_name=community.name) response = self.client.put(url, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue(user.has_favorite_community_with_name(community.name))
def test_cant_unfavorite_not_favorite_community(self): """ should not be able to unfavorite a non 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) url = self._get_url(community_name=community.name) response = self.client.delete(url, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(user.has_favorite_community_with_name(community.name))
def test_can_delete_administrated_community_cover(self): """ should be able to delete the cover of an administrated community and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user) community.cover.save('cover.jpg', File(make_community_cover())) 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) community.refresh_from_db() self.assertTrue(not community.cover)
def test_user_cannot_delete_community(self): """ should not be able to delete a community and return 400 """ user = make_user() other_user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=other_user) community_name = community.name url = self._get_url(community_name=community_name) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue(Community.objects.filter(pk=community.pk).exists())
def test_cannot_retrieve_members_of_community_banned_from(self): """ should not be able to retrieve the community members if user has been banned from community """ 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_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_can_filter_moderators_from_members_of_community(self): """ should be able to filter the moderators from the retrieve the members of a community """ 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 amount_of_community_members = 5 community_members_ids = [ other_user.pk ] for i in range(0, amount_of_community_members): community_member = make_user() community_member.join_community_with_name(community_name=community_name) community_members_ids.append(community_member.pk) amount_of_community_moderators = 5 for i in range(0, amount_of_community_moderators): community_moderator = make_user() community_moderator.join_community_with_name(community_name=community_name) other_user.add_moderator_with_username_to_community_with_name(username=community_moderator.username, community_name=community.name) url = self._get_url(community_name=community.name) response = self.client.get(url, { 'exclude': Community.EXCLUDE_COMMUNITY_MODERATORS_KEYWORD, }, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_members = json.loads(response.content) self.assertEqual(len(response_members), len(community_members_ids)) for response_member in response_members: response_member_id = response_member.get('id') self.assertIn(response_member_id, community_members_ids)
def test_can_retrieve_reactions_from_blocking_user_in_a_community_if_staff( self): """ should be able to retrieve the reactions from a blocking user in a community if staff member """ user = make_user() community = make_community(creator=user) post_creator = make_user() blocking_user = make_user() post_creator.join_community_with_name(community_name=community.name) post = post_creator.create_community_post( text=make_fake_post_text(), community_name=community.name) emoji_group = make_reactions_emoji_group() emoji = make_emoji(group=emoji_group) blocking_user.react_to_post_with_id( post_id=post.pk, emoji_id=emoji.pk, ) blocking_user.block_user_with_id(user_id=user.pk) url = self._get_url(post) headers = make_authentication_headers_for_user(user) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_emoji_counts = json.loads(response.content) self.assertEqual(1, len(response_emoji_counts)) response_emoji_count = response_emoji_counts[0] response_emoji_id = response_emoji_count.get('emoji').get('id') response_emoji_count = response_emoji_count.get('count') self.assertEqual(response_emoji_id, emoji.pk) self.assertEqual(1, response_emoji_count)
def test_can_search_community_banned_users_by_name(self): """ should be able to search for community banned users by their name and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user) amount_of_community_banned_users_to_search_for = 5 for i in range(0, amount_of_community_banned_users_to_search_for): banned_user = make_user() banned_user.join_community_with_name(community_name=community.name) user.ban_user_with_username_from_community_with_name(username=banned_user.username, community_name=community.name) banned_user_username = banned_user.profile.name amount_of_characters_to_query = random.randint(1, len(banned_user_username)) query = banned_user_username[0:amount_of_characters_to_query] final_query = '' for character in query: final_query = final_query + (character.upper() if fake.boolean() else character.lower()) url = self._get_url(community_name=community.name) response = self.client.get(url, { 'query': final_query }, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_banned_users = json.loads(response.content) response_banned_users_count = len(response_banned_users) if response_banned_users_count == 1: # Our community creator was not retrieved self.assertEqual(response_banned_users_count, 1) retrieved_banned_user = response_banned_users[0] self.assertEqual(retrieved_banned_user['id'], banned_user.id) else: # Our community creator was retrieved too for response_banned_user in response_banned_users: response_banned_user_id = response_banned_user['id'] self.assertTrue( response_banned_user_id == banned_user.id or response_banned_user_id == user.id) user.unban_user_with_username_from_community_with_name(username=banned_user.username, community_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_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_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_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_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_can_delete_administrated_community_users_adjective(self): """ should be able to delete the administrated community users_adjective and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user) data = {'users_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.users_adjective)
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_should_not_be_able_to_unsubscribe_to_notifications_for_community_if_already_subscribed( self): """ should not be able to unsubscribe to community posts for a community if already unsubscribed """ admin = make_user() community = make_community(creator=admin, type='P') community_member = make_user() community_member.join_community_with_name( community_name=community.name) headers = make_authentication_headers_for_user(community_member) url = self._get_url(community_name=community.name) response = self.client.delete(url, **headers) subscriptions = CommunityNotificationsSubscription.objects.filter( subscriber=community_member, community=community) self.assertEqual(len(subscriptions), 0) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
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_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_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_can_retrieve_posts_from_private_community_member_of(self): """ should be able to retrieve the posts for a private community member of 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') community_name = community.name other_user.invite_user_with_username_to_community_with_name( username=user.username, community_name=community_name) user.join_community_with_name(community_name) amount_of_community_posts = 5 community_posts_ids = [] for i in range(0, amount_of_community_posts): community_member = make_user() other_user.invite_user_with_username_to_community_with_name( username=community_member.username, community_name=community_name) community_member.join_community_with_name( community_name=community_name) community_member_post = community_member.create_community_post( community_name=community.name, text=make_fake_post_text()) community_posts_ids.append(community_member_post.pk) url = self._get_url(community_name=community.name) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_posts = json.loads(response.content) self.assertEqual(len(response_posts), len(community_posts_ids)) for response_post in response_posts: response_post_id = response_post.get('id') self.assertIn(response_post_id, community_posts_ids)
def test_can_retrieve_closed_posts_from_community_if_moderator(self): """ should be able to retrieve closed posts for a community if moderator """ moderator = make_user() admin = make_user() community = make_community(creator=admin, type='P') moderator.join_community_with_name(community_name=community.name) admin.add_moderator_with_username_to_community_with_name( username=moderator.username, community_name=community.name) community_name = community.name amount_of_community_posts = 5 community_posts_ids = [] for i in range(0, amount_of_community_posts): community_member = make_user() community_member.join_community_with_name( community_name=community_name) community_member_post = community_member.create_community_post( community_name=community.name, text=make_fake_post_text()) community_member_post.is_closed = True community_member_post.save() community_posts_ids.append(community_member_post.pk) headers = make_authentication_headers_for_user(moderator) url = self._get_url(community_name=community.name) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_posts = json.loads(response.content) self.assertEqual(len(response_posts), len(community_posts_ids)) for response_post in response_posts: response_post_id = response_post.get('id') self.assertIn(response_post_id, community_posts_ids)
def test_cant_delete_reaction_in_closed_community_post_comment_if_creator( self): """ should NOT be able to delete reaction in closed community post if creator """ user = make_user() admin = make_user() community = make_community(admin) post_creator = make_user() post_creator.join_community_with_name(community_name=community.name) user.join_community_with_name(community_name=community.name) community_post = post_creator.create_community_post( community_name=community.name, text=make_fake_post_comment_text()) post_comment = post_creator.comment_post_with_id( post_id=community_post.pk, text=make_fake_post_comment_text()) emoji_group = make_reactions_emoji_group() post_comment_reaction_emoji_id = make_emoji(group=emoji_group).pk post_comment_reaction = user.react_to_post_comment_with_id( community_post.pk, emoji_id=post_comment_reaction_emoji_id, ) # now close the post community_post.is_closed = True community_post.save() url = self._get_url(post_comment_reaction=post_comment_reaction, post=community_post, post_comment=post_comment) headers = make_authentication_headers_for_user(post_creator) response = self.client.delete(url, **headers) self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) self.assertTrue( PostCommentReaction.objects.filter( id=post_comment_reaction.pk).count() == 1)
def test_can_update_administrated_community_rules(self): """ should be able to update an administrated community rules """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user) new_community_rules = make_community_rules() data = {'rules': new_community_rules} 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.assertEqual(community.rules, new_community_rules)
def test_should_not_be_able_to_unsubscribe_to_notifications_for_community_if_banned( self): """ should not be able to unsubscribe to community posts for a community if banned """ admin = make_user() community = make_community(creator=admin, type='P') user = make_user() user.join_community_with_name(community_name=community.name) user.enable_new_post_notifications_for_community_with_name( community_name=community.name) admin.ban_user_with_username_from_community_with_name( username=user.username, community_name=community.name) headers = make_authentication_headers_for_user(user) url = self._get_url(community_name=community.name) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_should_be_able_to_subscribe_to_notifications_for_community_if_member( self): """ should be able to subscribe to community posts for a community a member """ admin = make_user() community = make_community(creator=admin, type='P') community_member = make_user() community_member.join_community_with_name( community_name=community.name) headers = make_authentication_headers_for_user(community_member) url = self._get_url(community_name=community.name) response = self.client.put(url, **headers) self.assertEqual(response.status_code, status.HTTP_201_CREATED) subscription = CommunityNotificationsSubscription.objects.get( subscriber=community_member) self.assertEqual(subscription.community.name, community.name) self.assertTrue(subscription.new_post_notifications)
def test_cant_remove_creator_from_community_administrators(self): """ should not be able to remove the creator of the community from the administrators """ user = make_user() other_user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=other_user, type='P') user.join_community_with_name(community.name) other_user.add_administrator_with_username_to_community_with_name(username=user.username, community_name=community.name) url = self._get_url(community_name=community.name, username=other_user.username) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue( other_user.is_administrator_of_community_with_name(community_name=community.name))