def test_follow_should_create_notification(self): """ should create a notification when a user is followed """ user = make_user() auth_token = user.auth_token.key user_to_follow = make_user() headers = {'HTTP_AUTHORIZATION': 'Token %s' % auth_token} data = { 'username': user_to_follow.username, } url = self._get_url() self.client.post(url, data, **headers, format='multipart') self.assertTrue( FollowNotification.objects.filter( follower=user, notification__owner=user_to_follow).exists())
def test_can_add_media_image_to_draft_post(self): """ should be able to add a media image to a draft post """ user = make_user() headers = make_authentication_headers_for_user(user) draft_post = user.create_public_post(is_draft=True) image_width = random.randint(100, 500) image_height = random.randint(100, 500) image = Image.new('RGB', (image_width, image_height)) tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') image.save(tmp_file) tmp_file.seek(0) data = {'file': tmp_file} url = self._get_url(post=draft_post) response = self.client.put(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_200_OK) draft_post.refresh_from_db() self.assertEqual(draft_post.status, Post.STATUS_DRAFT) self.assertTrue(draft_post.media.exists()) post_media = draft_post.media.all() self.assertEqual(len(post_media), 1) post_media_image = post_media[0] self.assertEqual(post_media_image.type, PostMedia.MEDIA_TYPE_IMAGE) self.assertEqual(post_media_image.order, 0) post_image = post_media_image.content_object self.assertTrue(hasattr(post_image, 'image')) self.assertEqual(post_image.width, image_width) self.assertEqual(post_image.width, image_width) # Not for long though self.assertTrue(hasattr(draft_post, 'image'))
def test_removes_member_from_moderator_when_adding_as_administrator(self): """ should remove the member from moderators if added as an administrator """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user, type='P') user_to_make_admnistrator = make_user() user_to_make_admnistrator.join_community_with_name(community_name=community.name) user.add_moderator_with_username_to_community_with_name(username=user_to_make_admnistrator, community_name=community.name) url = self._get_url(community_name=community.name) response = self.client.put(url, { 'username': user_to_make_admnistrator.username }, **headers) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertFalse( user_to_make_admnistrator.is_moderator_of_community_with_name(community_name=community.name))
def test_rejecting_a_follow_request_does_not_follow_the_rejecting_user( self): """ should be able to reject a follow request and automatically follow the rejecting user and return 200 """ user = make_user(visibility=User.VISIBILITY_TYPE_PRIVATE) headers = make_authentication_headers_for_user(user) user_requesting_to_follow = make_user() user_requesting_to_follow.create_follow_request_for_user(user=user) data = { 'username': user_requesting_to_follow.username, } url = self._get_url() response = self.client.post(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse( user_requesting_to_follow.is_following_user(user=user))
def test_succeeds_on_non_blacklisted_root_domain_with_blacklisted_subdomain( self): """ should succeed when calling with a non blacklisted root domain that also has a blacklisted subdomain and return 403 """ user = make_user() headers = make_authentication_headers_for_user(user) request_url = 'blogspot.com' url = self._get_url() make_proxy_blacklisted_domain(domain='test.blogspot.com') response = self.client.get(url, {'url': request_url}, **headers) self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
def test_join_community_with_invite_removes_community_invite_notification( self): """ should able to join a private community with an invite and remove the community invite notification """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user) 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.assertFalse( CommunityInviteNotification.objects.filter( community_invite__invited_user=user, community_invite__creator=other_user).exists())
def test_cant_add_community_administrator_if_not_member(self): """ should not be able to add a community administrator if user is not member of community """ user = make_user() other_user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=other_user, type='P') user_to_make_admnistrator = make_user() user_to_make_admnistrator.join_community_with_name( community_name=community.name) url = self._get_url(community_name=community.name) response = self.client.put( url, {'username': user_to_make_admnistrator.username}, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse( user_to_make_admnistrator.is_administrator_of_community_with_name( community_name=community.name))
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))
def test_cant_retrieve_reported_community_post(self): """ should not be able to retrieve reported community post """ user = make_user() community_creator = make_user() community = make_community(creator=community_creator) post_creator = make_user() user.join_community_with_name(community_name=community.name) post_creator.join_community_with_name(community_name=community.name) post = post_creator.create_community_post(community_name=community.name, text=make_fake_post_text()) user.report_post(post=post, category_id=make_moderation_category().pk) url = self._get_url(post=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_create_public_community(self): """ should be able to create a public community and return 201 """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_description = fake.text( max_nb_chars=settings.COMMUNITY_DESCRIPTION_MAX_LENGTH) community_rules = fake.text( max_nb_chars=settings.COMMUNITY_RULES_MAX_LENGTH) community_user_adjective = make_community_user_adjective() community_users_adjective = make_community_users_adjective() community_color = fake.hex_color() community_categories = [] community_type = 'P' for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'description': community_description, 'rules': community_rules, 'user_adjective': community_user_adjective, 'users_adjective': community_users_adjective, 'color': community_color, 'categories': community_categories } url = self._get_url() response = self.client.put(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue( Community.objects.filter(name=community_name, title=community_title, description=community_description, rules=community_rules, user_adjective=community_user_adjective, users_adjective=community_users_adjective, color=community_color, type=community_type).count() == 1)
def test_can_delete_own_post(self): """ should be able to delete 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) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(Post.objects.filter(pk=post.pk).count() == 0)
def test_cant_retrieve_community_banned_from_post(self): """ should not be able to retrieve a community banned from post and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) community_owner = make_user() community = make_community(creator=community_owner) post = community_owner.create_community_post(text=make_fake_post_text(), community_name=community.name) user.join_community_with_name(community_name=community.name) user.comment_post(post, text=make_fake_post_comment_text()) community_owner.ban_user_with_username_from_community_with_name(username=user.username, community_name=community.name) url = self._get_url(post) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_can_open_post_if_administrator_of_community(self): """ should be able to open post if 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(admin) response = self.client.post(url, **headers) parsed_response = json.loads(response.content) self.assertTrue(response.status_code, status.HTTP_200_OK) post.refresh_from_db() self.assertFalse(post.is_closed) self.assertFalse(parsed_response['is_closed'])
def test_can_retrieve_public_community_member_of_post(self): """ should be able to retrieve a public community member of post and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) community_owner = make_user() community = make_community(creator=community_owner) post = community_owner.create_community_post(text=make_fake_post_text(), community_name=community.name) user.join_community_with_name(community_name=community.name) url = self._get_url(post) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_post = json.loads(response.content) self.assertEqual(response_post['id'], post.pk)
def test_cannot_update_administrated_community_name_to_taken_name(self): """ should not be able to update an administrated community name to an existing one and return 400 """ user = make_user() other_user = make_user() other_community = make_community(creator=other_user) community = make_community(creator=user) data = {'name': other_community.name} headers = make_authentication_headers_for_user(user) 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.assertNotEqual(community.name, other_community.name)
def test_cannot_invite_user_to_community_part_of_with_invites_disabled(self): """ should not able to invite a user to join a community part of with invites enabled 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.invites_enabled = False 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(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(user_to_invite.is_invited_to_community_with_name(community_name=community.name))
def test_reacting_on_foreign_post_comment_doesnt_send_push_notification_when_post_muted(self, send_post_comment_reaction_push_notification_call): """ should NOT send a push notification when reacting on a foreign post comment when post muted """ user = make_user() reactor = make_user() headers = make_authentication_headers_for_user(reactor) post = user.create_public_post(text=make_fake_post_text()) post_comment = user.comment_post_with_id(post_id=post.pk, text=make_fake_post_comment_text()) user.mute_post_with_id(post_id=post.pk) emoji_group = make_reactions_emoji_group() post_comment_reaction_emoji_id = make_emoji(group=emoji_group).pk data = self._get_create_post_comment_reaction_request_data(post_comment_reaction_emoji_id) url = self._get_url(post=post, post_comment=post_comment) self.client.put(url, data, **headers) send_post_comment_reaction_push_notification_call.assert_not_called()
def test_should_be_able_to_unsubscribe_to_notifications_for_community_if_member( self): """ should be able to unsubscribe 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) community_member.enable_new_post_notifications_for_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) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse( CommunityNotificationsSubscription.objects.get( subscriber=community_member, community=community).new_post_notifications)
def test_can_set_language(self): """ should be able to set user language and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) language = make_random_language() response = self.client.post(self.url, {'language_id': language.id}, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) user.refresh_from_db() self.assertTrue(user.language.id, language.id)
def test_reacting_in_foreign_post_comment_creates_notification(self): """ should create a notification when reacting on a foreign post comment """ user = make_user() reactor = make_user() headers = make_authentication_headers_for_user(reactor) post = user.create_public_post(text=make_fake_post_text()) post_comment = user.comment_post_with_id(post_id=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 data = self._get_create_post_comment_reaction_request_data(post_comment_reaction_emoji_id) url = self._get_url(post=post, post_comment=post_comment) self.client.put(url, data, **headers) self.assertTrue(PostCommentReactionNotification.objects.filter( post_comment_reaction__emoji__id=post_comment_reaction_emoji_id, notification__owner=user).exists())
def test_cant_ban_user_from_community_if_member(self): """ should not be able to ban user from a community if is member 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 user.join_community_with_name(community_name) 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_cannot_change_password_without_new_password(self): """ should not be able to update the authenticated user password without the new password """ user = make_user() current_raw_password = user.password user.update_password( user.password) # make sure hashed password is stored headers = make_authentication_headers_for_user(user) data = {'current_password': current_raw_password} response = self.client.patch(self.url, data, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_cannot_unfollow_from_unexisting_follow(self): """ should not be able to unfollow from an unexisting follow and return 400 """ user = make_user() not_followed_user = make_user() auth_token = user.auth_token.key headers = {'HTTP_AUTHORIZATION': 'Token %s' % auth_token} data = { 'username': not_followed_user.username } url = self._get_url() response = self.client.post(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(user.is_following_user(not_followed_user))
def test_non_member_cannot_update_community(self): """ a non member of a community should not be able to update a community """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user) new_community_name = make_community_name() data = {'name': new_community_name} 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.assertNotEqual(community.name, new_community_name)
def test_moderator_cannot_delete_community(self): """ should not be able to delete a merely moderated 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 user.join_community_with_name(community_name=community_name) other_user.add_moderator_with_username_to_community_with_name( username=user.username, 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_retrieve_joined_communities_offset(self): """ should be able to retrieve all own communities with an offset return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) total_amount_of_communities = 10 offset = 5 community_creator = make_user() communities = [] for i in range(0, total_amount_of_communities): community = make_community(creator=community_creator) communities.append(community) offsetted_communities = communities[offset: total_amount_of_communities] offsetted_communities_ids = [community.pk for community in offsetted_communities] for community in communities: user.join_community_with_name(community_name=community.name) url = self._get_url() response = self.client.get(url, { 'offset': offset }, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_communities = json.loads(response.content) self.assertEqual(len(response_communities), total_amount_of_communities - offset) for response_community in response_communities: response_community_id = response_community.get('id') self.assertIn(response_community_id, offsetted_communities_ids)
def test_does_not_retrieve_private_community_part_of_post_with_hashtag( self): """ should not retrieve a private community part of post with a givne hashtag and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) community_creator = make_user() community = make_community(creator=community_creator, type=Community.COMMUNITY_TYPE_PRIVATE) post_creator = make_user() community_creator.invite_user_with_username_to_community_with_name( community_name=community.name, username=post_creator.username) post_creator.join_community_with_name(community_name=community.name) community_creator.invite_user_with_username_to_community_with_name( community_name=community.name, username=user.username) user.join_community_with_name(community_name=community.name) hashtag = make_hashtag() fake_post_text = make_fake_post_text( ) + ' and a little hashtag #%s' % hashtag.name post_creator.create_community_post(community_name=community.name, text=fake_post_text) url = self._get_url(hashtag_name=hashtag.name) response = self.client.get(url, **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_retrieve_banned_users_of_community_if_admin(self): """ should be able to retrieve the banned users of a community if is an admin and return 200 :return: """ user = make_user() headers = make_authentication_headers_for_user(user) other_user = make_user() community = make_community(creator=other_user) community_name = community.name user.join_community_with_name(community_name) other_user.add_administrator_with_username_to_community_with_name(username=user.username, community_name=community.name) amount_of_banned_users = 5 banned_users_ids = [] for i in range(0, amount_of_banned_users): community_member = make_user() other_user.ban_user_with_username_from_community_with_name(username=community_member.username, community_name=community_name) banned_users_ids.append(community_member.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_banned_users = json.loads(response.content) self.assertEqual(len(response_banned_users), len(banned_users_ids)) for response_banned_user in response_banned_users: response_member_id = response_banned_user.get('id') self.assertIn(response_member_id, banned_users_ids)
def test_cannot_delete_foreign_reaction_in_folowed_user_encircled_post( self): """ should not be able to delete foreign reaction in a followed user encircled post and return 400 """ user = make_user() user_to_follow = make_user() circle = make_circle(creator=user_to_follow) user.follow_user_with_id(user_to_follow.pk) foreign_user = make_user() foreign_user.connect_with_user_with_id(user_to_follow.pk) user_to_follow.confirm_connection_with_user_with_id( foreign_user.pk, circles_ids=[circle.pk]) post = user_to_follow.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk]) emoji_group = make_reactions_emoji_group() post_reaction_emoji_id = make_emoji(group=emoji_group).pk post_reaction = foreign_user.react_to_post_with_id( post.pk, emoji_id=post_reaction_emoji_id, emoji_group_id=emoji_group.pk) url = self._get_url(post_reaction=post_reaction, post=post) headers = make_authentication_headers_for_user(user) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue( PostReaction.objects.filter(id=post_reaction.pk).count() == 1)
def test_cannot_delete_foreign_reaction_in_followed_user_public_post_comment( self): """ should not be able to delete foreign reaction in a followed user public post comment and return 400 """ user = make_user() user_to_follow = make_user() user.follow_user_with_id(user_to_follow.pk) foreign_user = make_user() post = user_to_follow.create_public_post(text=make_fake_post_text()) post_comment = user_to_follow.comment_post_with_id( post_id=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 = foreign_user.react_to_post_comment_with_id( post.pk, emoji_id=post_comment_reaction_emoji_id, ) url = self._get_url(post_comment_reaction=post_comment_reaction, post=post, post_comment=post_comment) headers = make_authentication_headers_for_user(user) response = self.client.delete(url, **headers) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue( PostCommentReaction.objects.filter( id=post_comment_reaction.pk).count() == 1)