class SetLastVisitMiddlewareTest(TestCase): def setUp(self): self.user = ProfileFactory() def test_process_response(self): profile_pk = self.user.pk # login self.assertEqual( self.client.login( username=self.user.user.username, password='******'), True) # set last login to a recent date self.user.last_visit = datetime.now() - timedelta(seconds=10) self.user.save() # load a page self.client.get(reverse('homepage')) # the date of last visit should not have been updated profile = get_object_or_404(Profile, pk=profile_pk) self.assertTrue(datetime.now() - profile.last_visit > timedelta(seconds=5)) # set last login to an old date self.user.last_visit = datetime.now() - timedelta(seconds=45) self.user.save() # load a page self.client.get(reverse('homepage')) # the date of last visit should have been updated profile = get_object_or_404(Profile, pk=profile_pk) self.assertTrue(datetime.now() - profile.last_visit < timedelta(seconds=5))
def test_char_count_after_publication(self): """Test the ``get_char_count()`` function. Special care should be taken with this function, since: - The username of the author is, by default "Firmxxx" where "xxx" depends on the tests before ; - The titles (!) also contains a number that also depends on the number of tests before ; - The date is ``datetime.now()`` and contains the months, which is never a fixed number of letters. """ author = ProfileFactory().user author.username = '******' author.save() len_date_now = len(date(datetime.now(), 'd F Y')) article = PublishedContentFactory(type='ARTICLE', author_list=[author], title='Un titre') published = PublishedContent.objects.filter(content=article).first() self.assertEqual(published.get_char_count(), 160 + len_date_now) tuto = PublishableContentFactory(type='TUTORIAL', author_list=[author], title='Un titre') # add a chapter, so it becomes a middle tutorial tuto_draft = tuto.load_version() chapter1 = ContainerFactory(parent=tuto_draft, db_object=tuto, title='Un chapitre') ExtractFactory(container=chapter1, db_object=tuto, title='Un extrait') published = publish_content(tuto, tuto_draft, is_major_update=True) tuto.sha_public = tuto_draft.current_version tuto.sha_draft = tuto_draft.current_version tuto.public_version = published tuto.save() published = PublishedContent.objects.filter(content=tuto).first() self.assertEqual(published.get_char_count(), 335 + len_date_now)
def test_get_avatar_url(self): # if no url was specified -> gravatar ! self.assertEqual(self.user1.get_avatar_url(), 'https://secure.gravatar.com/avatar/{0}?d=identicon'. format(md5(self.user1.user.email.lower()).hexdigest())) # if an url is specified -> take it ! user2 = ProfileFactory() testurl = 'http://test.com/avatar.jpg' user2.avatar_url = testurl self.assertEqual(user2.get_avatar_url(), testurl)
def test_profiler(self): result = self.client.get('/?prof', follow=True) self.assertEqual(result.status_code, 200) admin = ProfileFactory() admin.user.is_superuser = True admin.save() self.assertTrue(self.client.login(username=admin.user.username, password='******')) result = self.client.get('/?prof', follow=True) self.assertEqual(result.status_code, 200)
def test_failure_reaction_karma_with_sanctioned_user(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() profile.can_read = False profile.can_write = False profile.save() self.assertTrue(self.client.login(username=profile.user.username, password='******')) response = self.client.put(reverse('api:content:reaction-karma', args=(reaction.pk,))) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
class SubscriptionsTest(TestCase): def setUp(self): self.userStandard1 = ProfileFactory( email_for_answer=True, email_for_new_mp=True ).user self.userOAuth1 = ProfileFactory( email_for_answer=True, email_for_new_mp=True).user self.userOAuth2 = ProfileFactory( email_for_answer=True, email_for_new_mp=True).user self.userOAuth1.email = '' self.userOAuth2.email = 'this is not an email' self.userOAuth1.save() self.userOAuth2.save() def test_no_emails_for_those_who_have_none(self): """ Test that we do not try to send e-mails to those who have not registered one. """ self.assertEqual(0, len(mail.outbox)) topic = send_mp(author=self.userStandard1, users=[self.userOAuth1], title='Testing', subtitle='', text='', send_by_mail=True, leave=False) self.assertEqual(0, len(mail.outbox)) send_message_mp(self.userOAuth1, topic, '', send_by_mail=True) self.assertEqual(1, len(mail.outbox)) def test_no_emails_for_those_who_have_other_things_in_that_place(self): """ Test that we do not try to send e-mails to those who have not registered a valid one. """ self.assertEqual(0, len(mail.outbox)) topic = send_mp(author=self.userStandard1, users=[self.userOAuth2], title='Testing', subtitle='', text='', send_by_mail=True, leave=False) self.assertEqual(0, len(mail.outbox)) send_message_mp(self.userOAuth2, topic, '', send_by_mail=True) self.assertEqual(1, len(mail.outbox))
def setUp(self): self.user1 = ProfileFactory() self.staff = StaffProfileFactory() # Create a forum for later test self.forumcat = CategoryFactory() self.forum = ForumFactory(category=self.forumcat) self.forumtopic = TopicFactory(forum=self.forum, author=self.staff.user)
def setUp(self): self.userStandard1 = ProfileFactory( email_for_answer=True, email_for_new_mp=True ).user self.userOAuth1 = ProfileFactory( email_for_answer=True, email_for_new_mp=True).user self.userOAuth2 = ProfileFactory( email_for_answer=True, email_for_new_mp=True).user self.userOAuth1.email = '' self.userOAuth2.email = 'this is not an email' self.userOAuth1.save() self.userOAuth2.save()
def test_failure_create_featured_with_user_not_staff(self): profile = ProfileFactory() login_check = self.client.login( username=profile.user.username, password='******' ) self.assertTrue(login_check) response = self.client.get(reverse('featured-resource-create')) self.assertEqual(403, response.status_code)
def setUp(self): self.profile = ProfileFactory() self.private_topic = PrivateTopicFactory(author=self.profile.user) self.private_post = PrivatePostFactory(author=self.profile.user, privatetopic=self.private_topic, position_in_topic=1) self.client = APIClient() client_oauth2 = create_oauth2_client(self.profile.user) authenticate_client(self.client, client_oauth2, self.profile.user.username, 'hostel77') get_cache(extensions_api_settings.DEFAULT_USE_CACHE).clear()
def test_update_mp_participants(self): """ Update participants of a MP. """ another_profile = ProfileFactory() data = {'participants': another_profile.user.id} response = self.client.put( reverse('api-mp-detail', args=[self.private_topic.id]), data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.data.get('participants')[0], data.get('participants'))
def setUp(self): self.profile = ProfileFactory() self.client = APIClient() client_oauth2 = create_oauth2_client(self.profile.user) authenticate_client(self.client, client_oauth2, self.profile.user.username, 'hostel77') self.bot_group = Group() self.bot_group.name = ZDS_APP["member"]["bot_group"] self.bot_group.save() get_cache(extensions_api_settings.DEFAULT_USE_CACHE).clear()
def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.anonymous_account = UserFactory( username=settings.ZDS_APP["member"]["anonymous_account"]) self.bot_group = Group() self.bot_group.name = settings.ZDS_APP["member"]["bot_group"] self.bot_group.save() self.anonymous_account.groups.add(self.bot_group) self.anonymous_account.save() self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.client.force_login(self.profile1.user)
def test_mp_from_profile(self): """Test: Send a MP from a user profile.""" # User to send the MP user2 = ProfileFactory().user # Test if user is correctly added to the MP result = self.client.get( reverse('zds.mp.views.new') + '?username={0}'.format(user2.username), ) # Check username in new MP page self.assertContains(result, user2.username)
def test_useful_post(self): """To test when a member mark a post is usefull.""" user1 = ProfileFactory().user topic1 = TopicFactory(forum=self.forum11, author=self.user) post1 = PostFactory(topic=topic1, author=self.user, position=1) post2 = PostFactory(topic=topic1, author=user1, position=2) post3 = PostFactory(topic=topic1, author=user1, position=3) result = self.client.post(reverse('zds.forum.views.useful_post') + '?message={0}'.format(post2.pk), follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(Post.objects.get(pk=post1.pk).is_useful, False) self.assertEqual(Post.objects.get(pk=post2.pk).is_useful, True) self.assertEqual(Post.objects.get(pk=post3.pk).is_useful, False) # useful the first post result = self.client.post(reverse('zds.forum.views.useful_post') + '?message={0}'.format(post1.pk), follow=False) self.assertEqual(result.status_code, 403) self.assertEqual(Post.objects.get(pk=post1.pk).is_useful, False) self.assertEqual(Post.objects.get(pk=post2.pk).is_useful, True) self.assertEqual(Post.objects.get(pk=post3.pk).is_useful, False) # useful if you aren't author TopicFactory(forum=self.forum11, author=user1) post4 = PostFactory(topic=topic1, author=user1, position=1) post5 = PostFactory(topic=topic1, author=self.user, position=2) result = self.client.post(reverse('zds.forum.views.useful_post') + '?message={0}'.format(post5.pk), follow=False) self.assertEqual(result.status_code, 403) self.assertEqual(Post.objects.get(pk=post4.pk).is_useful, False) self.assertEqual(Post.objects.get(pk=post5.pk).is_useful, False) # useful if you are staff StaffProfileFactory().user self.assertEqual( self.client.login(username=self.user.username, password='******'), True) result = self.client.post(reverse('zds.forum.views.useful_post') + '?message={0}'.format(post4.pk), follow=False) self.assertNotEqual(result.status_code, 403) self.assertEqual(Post.objects.get(pk=post4.pk).is_useful, True) self.assertEqual(Post.objects.get(pk=post5.pk).is_useful, False)
def test_list_members(self): """ To test the listing of the members with and without page parameter. """ # create strange member weird = ProfileFactory() weird.user.username = u"ïtrema718" weird.user.email = u"foo@\xfbgmail.com" weird.user.save() # list of members. result = self.client.get( reverse('member-list'), follow=False ) self.assertEqual(result.status_code, 200) nb_users = len(result.context['members']) # Test that inactive user don't show up unactive_user = UserFactory() unactive_user.is_active = False unactive_user.save() result = self.client.get( reverse('member-list'), follow=False ) self.assertEqual(result.status_code, 200) self.assertEqual(nb_users, len(result.context['members'])) # list of members with page parameter. result = self.client.get( reverse('member-list') + u'?page=1', follow=False ) self.assertEqual(result.status_code, 200) # page which doesn't exist. result = self.client.get( reverse('member-list') + u'?page=1534', follow=False ) self.assertEqual(result.status_code, 404) # page parameter isn't an integer. result = self.client.get( reverse('member-list') + u'?page=abcd', follow=False ) self.assertEqual(result.status_code, 404)
def setUp(self): self.author = ProfileFactory() self.user = ProfileFactory() self.topic = send_mp(author=self.author.user, users=[], title='Title', text='Testing', subtitle='', leave=False) self.topic.participants.add(self.user.user) send_message_mp(self.user.user, self.topic, 'Testing') # humane_delta test periods = ((1, 0), (2, 1), (3, 7), (4, 30), (5, 360)) cont = dict() cont['date_today'] = periods[0][0] cont['date_yesterday'] = periods[1][0] cont['date_last_week'] = periods[2][0] cont['date_last_month'] = periods[3][0] cont['date_last_year'] = periods[4][0] self.context = Context(cont)
def test_success_initial_content(self): author = ProfileFactory().user author2 = ProfileFactory().user tutorial = PublishedContentFactory(author_list=[author, author2]) gallery = GalleryFactory() image = ImageFactory(gallery=gallery) tutorial.image = image tutorial.save() staff = StaffProfileFactory() self.client.force_login(staff.user) response = self.client.get( "{}{}".format( reverse("featured-resource-create"), f"?content_type=published_content&content_id={tutorial.pk}" ) ) initial_dict = response.context["form"].initial self.assertEqual(initial_dict["title"], tutorial.title) self.assertEqual(initial_dict["authors"], f"{author}, {author2}") self.assertEqual(initial_dict["type"], _("Un tutoriel")) self.assertEqual(initial_dict["url"], f"http://testserver{tutorial.get_absolute_url_online()}") self.assertEqual(initial_dict["image_url"], "http://testserver{}".format(image.physical["featured"].url))
def setUp(self): self.user = ProfileFactory().user self.staff = StaffProfileFactory().user _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, self.user.profile) self.assertTrue(self.client.login(username=self.user.username, password="******")) data = {"text": "A new post!"} self.client.post(reverse("post-edit") + "?message={}".format(topic.last_message.pk), data, follow=False) self.post = topic.last_message self.edit = CommentEdit.objects.latest("date")
def test_top_tags_content(self): tags_tuto = ['a', 'b', 'c'] tags_article = ['a', 'd', 'e'] content = PublishedContentFactory(type='TUTORIAL', author_list=[ProfileFactory().user]) content.add_tags(tags_tuto) content.save() tags_tuto = content.tags.all() content = PublishedContentFactory(type='ARTICLE', author_list=[ProfileFactory().user]) content.add_tags(tags_article) content.save() tags_article = content.tags.all() top_tags_tuto = topbar_publication_categories('TUTORIAL').get('tags') top_tags_article = topbar_publication_categories('ARTICLE').get('tags') self.assertEqual(list(top_tags_tuto), list(tags_tuto)) self.assertEqual(list(top_tags_article), list(tags_article))
def test_success_reaction_karma_like_already_disliked(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() vote = CommentVote(user=profile.user, comment=reaction, positive=False) vote.save() self.assertTrue( self.client.login(username=profile.user.username, password="******")) response = self.client.put( reverse("api:content:reaction-karma", args=(reaction.pk, )), {"vote": "like"}) vote.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(vote.positive)
def setUp(self): settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" self.mas = ProfileFactory().user self.overridden_zds_app["member"]["bot_account"] = self.mas.username self.licence = LicenceFactory() self.user_author = ProfileFactory().user self.staff = StaffProfileFactory().user self.tuto = PublishableContentFactory(type="TUTORIAL") self.tuto.authors.add(self.user_author) UserGalleryFactory(gallery=self.tuto.gallery, user=self.user_author, mode="W") self.tuto.licence = self.licence self.tuto.save() self.tuto_draft = self.tuto.load_version() self.part1 = ContainerFactory(parent=self.tuto_draft, db_object=self.tuto) self.chapter1 = ContainerFactory(parent=self.part1, db_object=self.tuto) self.extract1 = ExtractFactory(container=self.chapter1, db_object=self.tuto)
def test_success_post_karma_neutral(self): profile = ProfileFactory() category, forum = create_category() topic = add_topic_in_a_forum(forum, profile) another_profile = ProfileFactory() post = PostFactory(topic=topic, author=another_profile.user, position=2) vote = CommentVote(user=profile.user, comment=post, positive=True) vote.save() self.assertTrue(CommentVote.objects.filter(pk=vote.pk).exists()) self.assertTrue( self.client.login(username=profile.user.username, password='******')) response = self.client.put( reverse('api:forum:post-karma', args=(post.pk, )), {'vote': 'neutral'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(CommentVote.objects.filter(pk=vote.pk).exists())
def test_top_tags_content(self): tags_tuto = ["a", "b", "c"] tags_article = ["a", "d", "e"] content = PublishedContentFactory(type="TUTORIAL", author_list=[ProfileFactory().user]) content.add_tags(tags_tuto) content.save() tags_tuto = content.tags.all() content = PublishedContentFactory(type="ARTICLE", author_list=[ProfileFactory().user]) content.add_tags(tags_article) content.save() tags_article = content.tags.all() top_tags_tuto = topbar_publication_categories("TUTORIAL").get("tags") top_tags_article = topbar_publication_categories("ARTICLE").get("tags") self.assertEqual(list(top_tags_tuto), list(tags_tuto)) self.assertEqual(list(top_tags_article), list(tags_article))
def setUp(self): self.staff = StaffProfileFactory().user settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' self.mas = ProfileFactory().user self.overridden_zds_app['member']['bot_account'] = self.mas.username self.licence = LicenceFactory() self.subcategory = SubCategoryFactory() self.user_author = ProfileFactory().user self.user_staff = StaffProfileFactory().user self.user_guest = ProfileFactory().user self.tuto = PublishableContentFactory(type='TUTORIAL') self.tuto.authors.add(self.user_author) UserGalleryFactory(gallery=self.tuto.gallery, user=self.user_author, mode='W') self.tuto.licence = self.licence self.tuto.subcategory.add(self.subcategory) self.tuto.save() self.beta_forum = ForumFactory( pk=self.overridden_zds_app['forum']['beta_forum_id'], category=ForumCategoryFactory(position=1), position_in_category=1 ) # ensure that the forum, for the beta versions, is created self.tuto_draft = self.tuto.load_version() self.part1 = ContainerFactory(parent=self.tuto_draft, db_object=self.tuto) self.chapter1 = ContainerFactory(parent=self.part1, db_object=self.tuto) self.extract1 = ExtractFactory(container=self.chapter1, db_object=self.tuto) bot = Group(name=self.overridden_zds_app['member']['bot_group']) bot.save()
def setUp(self): self.licence = LicenceFactory() self.subcategory = SubCategoryFactory() self.author = ProfileFactory() self.user = ProfileFactory() self.staff = StaffProfileFactory() self.tuto = PublishableContentFactory(type='TUTORIAL') self.tuto.authors.add(self.author.user) self.tuto.licence = self.licence self.tuto.subcategory.add(self.subcategory) self.tuto.save() self.validation = Validation( content=self.tuto, version=self.tuto.sha_draft, comment_authors='bla', date_proposition=datetime.now(), ) self.validation.save() self.topic = send_mp(author=self.author.user, users=[], title='Title', text='Testing', subtitle='', leave=False) self.topic.participants.add(self.user.user) send_message_mp(self.user.user, self.topic, 'Testing') # humane_delta test periods = ((1, 0), (2, 1), (3, 7), (4, 30), (5, 360)) cont = dict() cont['date_today'] = periods[0][0] cont['date_yesterday'] = periods[1][0] cont['date_last_week'] = periods[2][0] cont['date_last_month'] = periods[3][0] cont['date_last_year'] = periods[4][0] self.context = Context(cont)
def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact='A hat', defaults={'name': 'A hat'}) self.profile1.hats.add(self.hat) self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.assertTrue( self.client.login(username=self.profile1.user.username, password='******'))
def setUp(self): # Create users self.author = ProfileFactory().user self.staff = StaffProfileFactory().user self.outsider = ProfileFactory().user # Create a license self.licence = LicenceFactory() # Create a content self.content = PublishableContentFactory(author_list=[self.author]) # Get information to be reused in tests self.form_url = reverse("content:edit-license", kwargs={"pk": self.content.pk}) self.form_data = { "license": self.licence.pk, "update_preferred_license": False } self.content_data = {"pk": self.content.pk, "slug": self.content.slug} self.content_url = reverse("content:view", kwargs=self.content_data) self.login_url = reverse("member-login") + "?next=" + self.form_url
def test_fail_add_participant_with_no_right(self): profile3 = ProfileFactory() self.client.logout() self.assertTrue(self.client.login(username=profile3.user.username, password="******")) response = self.client.post( reverse("mp-edit-participant", args=[self.topic1.pk, self.topic1.slug]), {"username": profile3.user.username}, ) self.assertEqual(403, response.status_code) self.assertNotIn(profile3.user, PrivateTopic.objects.get(pk=self.topic1.pk).participants.all())
def test_success_get_with_multi_username(self): profile3 = ProfileFactory() response = self.client.get( reverse("mp-new") + "?username="******"&username="******"form"].initial["participants"].split(", "))) self.assertTrue(self.profile1.user.username not in response.context["form"].initial["participants"]) self.assertTrue(self.profile2.user.username in response.context["form"].initial["participants"]) self.assertTrue(profile3.user.username in response.context["form"].initial["participants"])
def test_success_add_participant(self): profile3 = ProfileFactory() response = self.client.post( reverse("mp-edit-participant", args=[self.topic1.pk, self.topic1.slug]), {"username": profile3.user.username}, follow=True, ) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.context["messages"])) self.assertIn(profile3.user, PrivateTopic.objects.get(pk=self.topic1.pk).participants.all())
def test_ban_member_is_not_contactable(self): """ When a member is ban, we hide the button to send a PM. """ user_ban = ProfileFactory() user_ban.can_read = False user_ban.can_write = False user_ban.save() user_1 = ProfileFactory() user_2 = ProfileFactory() phrase = "Envoyer un message privé" result = self.client.get(reverse('member-detail', args=[user_1.user.username]), follow=False) self.assertNotContains(result, phrase) result = self.client.get(reverse('member-detail', args=[user_ban.user.username]), follow=False) self.assertNotContains(result, phrase) self.assertTrue( self.client.login(username=user_2.user.username, password='******')) result = self.client.get(reverse('member-detail', args=[user_1.user.username]), follow=False) self.client.logout() self.assertContains(result, phrase) self.assertTrue( self.client.login(username=user_2.user.username, password='******')) result = self.client.get(reverse('member-detail', args=[user_ban.user.username]), follow=False) self.client.logout() self.assertNotContains(result, phrase) self.assertTrue( self.client.login(username=user_1.user.username, password='******')) result = self.client.get(reverse('member-detail', args=[user_1.user.username]), follow=False) self.client.logout() self.assertNotContains(result, phrase)
def setUp(self): # Create users self.author = ProfileFactory().user self.staff = StaffProfileFactory().user self.outsider = ProfileFactory().user # Create a license self.licence = LicenceFactory() # Create a content self.content = PublishableContentFactory(author_list=[self.author]) # Get information to be reused in tests self.form_url = reverse('content:edit-license', kwargs={'pk': self.content.pk}) self.form_data = { 'license': self.licence.pk, 'update_preferred_license': False } self.content_data = {'pk': self.content.pk, 'slug': self.content.slug} self.content_url = reverse('content:view', kwargs=self.content_data) self.login_url = reverse('member-login') + '?next=' + self.form_url
def test_success_post_karma_like_already_disliked(self): profile = ProfileFactory() category, forum = create_category() topic = add_topic_in_a_forum(forum, profile) another_profile = ProfileFactory() post = PostFactory(topic=topic, author=another_profile.user, position=2) vote = CommentVote(user=profile.user, comment=post, positive=False) vote.save() self.assertTrue( self.client.login(username=profile.user.username, password='******')) response = self.client.put( reverse('api:forum:post-karma', args=(post.pk, )), {'vote': 'like'}) vote.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(vote.positive)
def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) )
def setUp(self): self.profile = ProfileFactory() self.other = ProfileFactory() self.client = APIClient() client_oauth2 = create_oauth2_client(self.profile.user) authenticate_client(self.client, client_oauth2, self.profile.user.username, 'hostel77') self.gallery = GalleryFactory() UserGalleryFactory(user=self.profile.user, gallery=self.gallery) self.image = ImageFactory(gallery=self.gallery) self.gallery_other = GalleryFactory() UserGalleryFactory(user=self.other.user, gallery=self.gallery_other) self.image_other = ImageFactory(gallery=self.gallery_other) self.gallery_shared = GalleryFactory() UserGalleryFactory(user=self.other.user, gallery=self.gallery_shared) UserGalleryFactory(user=self.profile.user, gallery=self.gallery_shared, mode=GALLERY_READ) self.image_shared = ImageFactory(gallery=self.gallery_shared)
def test_dislike_post(self): """Test when a member dislike any post.""" user1 = ProfileFactory().user topic1 = TopicFactory(forum=self.forum11, author=self.user) post1 = PostFactory(topic=topic1, author=self.user, position=1) post2 = PostFactory(topic=topic1, author=user1, position=2) post3 = PostFactory(topic=topic1, author=self.user, position=3) result = self.client.post(reverse('zds.forum.views.dislike_post') + '?message={0}'.format(post2.pk), follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(CommentDislike.objects.all().count(), 1) self.assertEqual(Post.objects.get(pk=post1.pk).like, 0) self.assertEqual(Post.objects.get(pk=post2.pk).like, 0) self.assertEqual(Post.objects.get(pk=post3.pk).like, 0) self.assertEqual(Post.objects.get(pk=post1.pk).dislike, 0) self.assertEqual(Post.objects.get(pk=post2.pk).dislike, 1) self.assertEqual(Post.objects.get(pk=post3.pk).dislike, 0) self.assertEqual( CommentDislike.objects.filter(comments__pk=post1.pk).all().count(), 0) self.assertEqual( CommentDislike.objects.filter(comments__pk=post2.pk).all().count(), 1) self.assertEqual( CommentDislike.objects.filter(comments__pk=post3.pk).all().count(), 0) result = self.client.post(reverse('zds.forum.views.like_post') + '?message={0}'.format(post1.pk), follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(CommentDislike.objects.all().count(), 1) self.assertEqual(Post.objects.get(pk=post1.pk).like, 0) self.assertEqual(Post.objects.get(pk=post2.pk).like, 0) self.assertEqual(Post.objects.get(pk=post3.pk).like, 0) self.assertEqual(Post.objects.get(pk=post1.pk).dislike, 0) self.assertEqual(Post.objects.get(pk=post2.pk).dislike, 1) self.assertEqual(Post.objects.get(pk=post3.pk).dislike, 0) self.assertEqual( CommentDislike.objects.filter(comments__pk=post1.pk).all().count(), 0) self.assertEqual( CommentDislike.objects.filter(comments__pk=post2.pk).all().count(), 1) self.assertEqual( CommentDislike.objects.filter(comments__pk=post3.pk).all().count(), 0)
def test_failure_post_karma_with_sanctioned_user(self): profile = ProfileFactory() category, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) another_profile = ProfileFactory() post = PostFactory(topic=topic, author=another_profile.user, position=2) profile = ProfileFactory() profile.can_read = False profile.can_write = False profile.save() self.assertTrue(self.client.login(username=profile.user.username, password='******')) response = self.client.put(reverse('api:forum:post-karma', args=(post.pk,))) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_get_avatar_url(self): # if no url was specified -> gravatar ! self.assertEqual(self.user1.get_avatar_url(), 'https://secure.gravatar.com/avatar/{0}?d=identicon'. format(md5(self.user1.user.email.lower().encode()).hexdigest())) # if an url is specified -> take it ! user2 = ProfileFactory() testurl = 'http://test.com/avatar.jpg' user2.avatar_url = testurl self.assertEqual(user2.get_avatar_url(), testurl) # if url is relative, send absolute url gallerie_avtar = GalleryFactory() image_avatar = ImageFactory(gallery=gallerie_avtar) user2.avatar_url = image_avatar.physical.url self.assertNotEqual(user2.get_avatar_url(), image_avatar.physical.url) self.assertIn('http', user2.get_avatar_url())
def test_can_write_now(self): self.user1.user.is_active = True self.user1.user.can_write = True self.assertTrue(self.user1.can_write_now()) # Was banned in the past, ban no longer active profile = ProfileFactory() profile.can_write = True profile.end_ban_read = datetime.now() - timedelta(days=1) self.assertTrue(profile.can_write_now()) profile = ProfileFactory() profile.can_write = False profile.is_active = True self.assertFalse(profile.can_write_now()) # Ban is active profile = ProfileFactory() profile.can_write = False profile.end_ban_write = datetime.now() + timedelta(days=1) self.assertFalse(profile.can_write_now()) self.user1.user.is_active = False self.user1.user.can_write = True self.assertFalse(self.user1.can_write_now())
class TestProfile(TestCase): def setUp(self): self.user1 = ProfileFactory() self.staff = StaffProfileFactory() # Create a forum for later test self.forumcat = CategoryFactory() self.forum = ForumFactory(category=self.forumcat) self.forumtopic = TopicFactory(forum=self.forum, author=self.staff.user) def test_unicode(self): self.assertEqual(self.user1.__unicode__(), self.user1.user.username) def test_get_absolute_url(self): self.assertEqual(self.user1.get_absolute_url(), '/membres/voir/{0}/'.format(self.user1.user.username)) # def test_get_city(self): def test_get_avatar_url(self): # if no url was specified -> gravatar ! self.assertEqual(self.user1.get_avatar_url(), 'https://secure.gravatar.com/avatar/{0}?d=identicon'. format(md5(self.user1.user.email.lower()).hexdigest())) # if an url is specified -> take it ! user2 = ProfileFactory() testurl = 'http://test.com/avatar.jpg' user2.avatar_url = testurl self.assertEqual(user2.get_avatar_url(), testurl) def test_get_post_count(self): # Start with 0 self.assertEqual(self.user1.get_post_count(), 0) # Post ! PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) # Should be 1 self.assertEqual(self.user1.get_post_count(), 1) def test_get_topic_count(self): # Start with 0 self.assertEqual(self.user1.get_topic_count(), 0) # Create Topic ! TopicFactory(forum=self.forum, author=self.user1.user) # Should be 1 self.assertEqual(self.user1.get_topic_count(), 1) def test_get_tuto_count(self): # Start with 0 self.assertEqual(self.user1.get_tuto_count(), 0) # Create Tuto ! minituto = MiniTutorialFactory() minituto.authors.add(self.user1.user) minituto.gallery = GalleryFactory() minituto.save() # Should be 1 self.assertEqual(self.user1.get_tuto_count(), 1) def test_get_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_tutos()), 0) # Create Tuto ! minituto = MiniTutorialFactory() minituto.authors.add(self.user1.user) minituto.gallery = GalleryFactory() minituto.save() # Should be 1 tutos = self.user1.get_tutos() self.assertEqual(len(tutos), 1) self.assertEqual(minituto, tutos[0]) def test_get_draft_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_draft_tutos()), 0) # Create Tuto ! drafttuto = MiniTutorialFactory() drafttuto.authors.add(self.user1.user) drafttuto.gallery = GalleryFactory() drafttuto.save() # Should be 1 drafttutos = self.user1.get_draft_tutos() self.assertEqual(len(drafttutos), 1) self.assertEqual(drafttuto, drafttutos[0]) def test_get_public_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_public_tutos()), 0) # Create Tuto ! publictuto = MiniTutorialFactory() publictuto.authors.add(self.user1.user) publictuto.gallery = GalleryFactory() publictuto.sha_public = 'whatever' publictuto.save() # Should be 1 publictutos = self.user1.get_public_tutos() self.assertEqual(len(publictutos), 1) self.assertEqual(publictuto, publictutos[0]) def test_get_validate_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_validate_tutos()), 0) # Create Tuto ! validatetuto = MiniTutorialFactory() validatetuto.authors.add(self.user1.user) validatetuto.gallery = GalleryFactory() validatetuto.sha_validation = 'whatever' validatetuto.save() # Should be 1 validatetutos = self.user1.get_validate_tutos() self.assertEqual(len(validatetutos), 1) self.assertEqual(validatetuto, validatetutos[0]) def test_get_beta_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_beta_tutos()), 0) # Create Tuto ! betatetuto = MiniTutorialFactory() betatetuto.authors.add(self.user1.user) betatetuto.gallery = GalleryFactory() betatetuto.sha_beta = 'whatever' betatetuto.save() # Should be 1 betatetutos = self.user1.get_beta_tutos() self.assertEqual(len(betatetutos), 1) self.assertEqual(betatetuto, betatetutos[0]) def test_get_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_articles()), 0) # Create Tuto ! article = ArticleFactory() article.authors.add(self.user1.user) article.save() # Should be 1 articles = self.user1.get_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_public_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_public_articles()), 0) # Create Tuto ! article = ArticleFactory() article.authors.add(self.user1.user) article.sha_public = 'whatever' article.save() # Should be 1 articles = self.user1.get_public_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_validate_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_validate_articles()), 0) # Create Tuto ! article = ArticleFactory() article.authors.add(self.user1.user) article.sha_validation = 'whatever' article.save() # Should be 1 articles = self.user1.get_validate_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_draft_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_draft_articles()), 0) # Create Tuto ! article = ArticleFactory() article.authors.add(self.user1.user) article.sha_beta = 'whatever' article.save() # Should be 1 articles = self.user1.get_draft_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_posts(self): # Start with 0 self.assertEqual(len(self.user1.get_posts()), 0) # Post ! apost = PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) # Should be 1 posts = self.user1.get_posts() self.assertEqual(len(posts), 1) self.assertEqual(apost, posts[0]) def test_get_invisible_posts_count(self): # Start with 0 self.assertEqual(self.user1.get_invisible_posts_count(), 0) # Post ! PostFactory(topic=self.forumtopic, author=self.user1.user, position=1, is_visible=False) # Should be 1 self.assertEqual(self.user1.get_invisible_posts_count(), 1) def test_get_alerts_posts_count(self): # Start with 0 self.assertEqual(self.user1.get_alerts_posts_count(), 0) # Post and Alert it ! post = PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) Alert.objects.create(author=self.user1.user, comment=post, scope=Alert.FORUM, pubdate=datetime.now()) # Should be 1 self.assertEqual(self.user1.get_alerts_posts_count(), 1) def test_can_read_now(self): self.user1.user.is_active = False self.assertFalse(self.user1.can_write_now()) self.user1.user.is_active = True self.assertTrue(self.user1.can_write_now()) # TODO Some conditions still need to be tested def test_can_write_now(self): self.user1.user.is_active = False self.assertFalse(self.user1.can_write_now()) self.user1.user.is_active = True self.assertTrue(self.user1.can_write_now()) # TODO Some conditions still need to be tested def test_get_followed_topics(self): # Start with 0 self.assertEqual(len(self.user1.get_followed_topics()), 0) # Follow ! TopicFollowed.objects.create(topic=self.forumtopic, user=self.user1.user) # Should be 1 topicsfollowed = self.user1.get_followed_topics() self.assertEqual(len(topicsfollowed), 1) self.assertEqual(self.forumtopic, topicsfollowed[0]) def tearDown(self): if os.path.isdir(settings.REPO_PATH): shutil.rmtree(settings.REPO_PATH) if os.path.isdir(settings.REPO_PATH_PROD): shutil.rmtree(settings.REPO_PATH_PROD) if os.path.isdir(settings.REPO_ARTICLE_PATH): shutil.rmtree(settings.REPO_ARTICLE_PATH) if os.path.isdir(settings.MEDIA_ROOT): shutil.rmtree(settings.MEDIA_ROOT)
def test_reachable_manager(self): # profile types profile_normal = ProfileFactory() profile_superuser = ProfileFactory() profile_superuser.user.is_superuser = True profile_superuser.user.save() profile_inactive = ProfileFactory() profile_inactive.user.is_active = False profile_inactive.user.save() profile_bot = ProfileFactory() profile_bot.user.username = settings.ZDS_APP['member']['bot_account'] profile_bot.user.save() profile_anonymous = ProfileFactory() profile_anonymous.user.username = settings.ZDS_APP['member']['anonymous_account'] profile_anonymous.user.save() profile_external = ProfileFactory() profile_external.user.username = settings.ZDS_APP['member']['external_account'] profile_external.user.save() profile_ban_def = ProfileFactory() profile_ban_def.can_read = False profile_ban_def.can_write = False profile_ban_def.save() profile_ban_temp = ProfileFactory() profile_ban_temp.can_read = False profile_ban_temp.can_write = False profile_ban_temp.end_ban_read = datetime.now() + timedelta(days=1) profile_ban_temp.save() profile_unban = ProfileFactory() profile_unban.can_read = False profile_unban.can_write = False profile_unban.end_ban_read = datetime.now() - timedelta(days=1) profile_unban.save() profile_ls_def = ProfileFactory() profile_ls_def.can_write = False profile_ls_def.save() profile_ls_temp = ProfileFactory() profile_ls_temp.can_write = False profile_ls_temp.end_ban_write = datetime.now() + timedelta(days=1) profile_ls_temp.save() # groups bot = Group(name=settings.ZDS_APP['member']['bot_group']) bot.save() # associate account to groups bot.user_set.add(profile_anonymous.user) bot.user_set.add(profile_external.user) bot.user_set.add(profile_bot.user) bot.save() # test reachable user profiles_reacheable = Profile.objects.contactable_members().all() self.assertIn(profile_normal, profiles_reacheable) self.assertIn(profile_superuser, profiles_reacheable) self.assertNotIn(profile_inactive, profiles_reacheable) self.assertNotIn(profile_anonymous, profiles_reacheable) self.assertNotIn(profile_external, profiles_reacheable) self.assertNotIn(profile_bot, profiles_reacheable) self.assertIn(profile_unban, profiles_reacheable) self.assertNotIn(profile_ban_def, profiles_reacheable) self.assertNotIn(profile_ban_temp, profiles_reacheable) self.assertIn(profile_ls_def, profiles_reacheable) self.assertIn(profile_ls_temp, profiles_reacheable)
class MemberModelsTest(TutorialTestMixin, TestCase): def setUp(self): self.user1 = ProfileFactory() self.staff = StaffProfileFactory() # Create a forum for later test self.forumcat = CategoryFactory() self.forum = ForumFactory(category=self.forumcat) self.forumtopic = TopicFactory(forum=self.forum, author=self.staff.user) def test_get_absolute_url_for_details_of_member(self): self.assertEqual(self.user1.get_absolute_url(), '/membres/voir/{0}/'.format(self.user1.user.username)) def test_get_avatar_url(self): # if no url was specified -> gravatar ! self.assertEqual(self.user1.get_avatar_url(), 'https://secure.gravatar.com/avatar/{0}?d=identicon'. format(md5(self.user1.user.email.lower().encode()).hexdigest())) # if an url is specified -> take it ! user2 = ProfileFactory() testurl = 'http://test.com/avatar.jpg' user2.avatar_url = testurl self.assertEqual(user2.get_avatar_url(), testurl) # if url is relative, send absolute url gallerie_avtar = GalleryFactory() image_avatar = ImageFactory(gallery=gallerie_avtar) user2.avatar_url = image_avatar.physical.url self.assertNotEqual(user2.get_avatar_url(), image_avatar.physical.url) self.assertIn('http', user2.get_avatar_url()) def test_get_post_count(self): # Start with 0 self.assertEqual(self.user1.get_post_count(), 0) # Post ! PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) # Should be 1 self.assertEqual(self.user1.get_post_count(), 1) def test_get_topic_count(self): # Start with 0 self.assertEqual(self.user1.get_topic_count(), 0) # Create Topic ! TopicFactory(forum=self.forum, author=self.user1.user) # Should be 1 self.assertEqual(self.user1.get_topic_count(), 1) def test_get_tuto_count(self): # Start with 0 self.assertEqual(self.user1.get_tuto_count(), 0) # Create Tuto ! minituto = PublishableContentFactory(type='TUTORIAL') minituto.authors.add(self.user1.user) minituto.gallery = GalleryFactory() minituto.save() # Should be 1 self.assertEqual(self.user1.get_tuto_count(), 1) def test_get_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_tutos()), 0) # Create Tuto ! minituto = PublishableContentFactory(type='TUTORIAL') minituto.authors.add(self.user1.user) minituto.gallery = GalleryFactory() minituto.save() # Should be 1 tutos = self.user1.get_tutos() self.assertEqual(len(tutos), 1) self.assertEqual(minituto, tutos[0]) def test_get_draft_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_draft_tutos()), 0) # Create Tuto ! drafttuto = PublishableContentFactory(type='TUTORIAL') drafttuto.authors.add(self.user1.user) drafttuto.gallery = GalleryFactory() drafttuto.save() # Should be 1 drafttutos = self.user1.get_draft_tutos() self.assertEqual(len(drafttutos), 1) self.assertEqual(drafttuto, drafttutos[0]) def test_get_public_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_public_tutos()), 0) # Create Tuto ! publictuto = PublishableContentFactory(type='TUTORIAL') publictuto.authors.add(self.user1.user) publictuto.gallery = GalleryFactory() publictuto.sha_public = 'whatever' publictuto.save() # Should be 0 because publication was not used publictutos = self.user1.get_public_tutos() self.assertEqual(len(publictutos), 0) PublishedContentFactory(author_list=[self.user1.user]) self.assertEqual(len(self.user1.get_public_tutos()), 1) def test_get_validate_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_validate_tutos()), 0) # Create Tuto ! validatetuto = PublishableContentFactory(type='TUTORIAL', author_list=[self.user1.user]) validatetuto.sha_validation = 'whatever' validatetuto.save() # Should be 1 validatetutos = self.user1.get_validate_tutos() self.assertEqual(len(validatetutos), 1) self.assertEqual(validatetuto, validatetutos[0]) def test_get_beta_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_beta_tutos()), 0) # Create Tuto ! betatetuto = PublishableContentFactory(type='TUTORIAL') betatetuto.authors.add(self.user1.user) betatetuto.gallery = GalleryFactory() betatetuto.sha_beta = 'whatever' betatetuto.save() # Should be 1 betatetutos = self.user1.get_beta_tutos() self.assertEqual(len(betatetutos), 1) self.assertEqual(betatetuto, betatetutos[0]) def test_get_article_count(self): # Start with 0 self.assertEqual(self.user1.get_tuto_count(), 0) # Create article ! minituto = PublishableContentFactory(type='ARTICLE') minituto.authors.add(self.user1.user) minituto.gallery = GalleryFactory() minituto.save() # Should be 1 self.assertEqual(self.user1.get_article_count(), 1) def test_get_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_articles()), 0) # Create article ! article = PublishableContentFactory(type='ARTICLE') article.authors.add(self.user1.user) article.save() # Should be 1 articles = self.user1.get_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_public_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_public_articles()), 0) # Create article ! article = PublishableContentFactory(type='ARTICLE') article.authors.add(self.user1.user) article.sha_public = 'whatever' article.save() # Should be 0 articles = self.user1.get_public_articles() self.assertEqual(len(articles), 0) # Should be 1 PublishedContentFactory(author_list=[self.user1.user], type='ARTICLE') self.assertEqual(len(self.user1.get_public_articles()), 1) self.assertEqual(len(self.user1.get_public_tutos()), 0) def test_get_validate_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_validate_articles()), 0) # Create article ! article = PublishableContentFactory(type='ARTICLE') article.authors.add(self.user1.user) article.sha_validation = 'whatever' article.save() # Should be 1 articles = self.user1.get_validate_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_draft_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_draft_articles()), 0) # Create article ! article = PublishableContentFactory(type='ARTICLE') article.authors.add(self.user1.user) article.save() # Should be 1 articles = self.user1.get_draft_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_beta_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_beta_articles()), 0) # Create article ! article = PublishableContentFactory(type='ARTICLE') article.authors.add(self.user1.user) article.sha_beta = 'whatever' article.save() # Should be 1 articles = self.user1.get_beta_articles() self.assertEqual(len(articles), 1) self.assertEqual(article, articles[0]) def test_get_posts(self): # Start with 0 self.assertEqual(len(self.user1.get_posts()), 0) # Post ! apost = PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) # Should be 1 posts = self.user1.get_posts() self.assertEqual(len(posts), 1) self.assertEqual(apost, posts[0]) def test_get_hidden_by_staff_posts_count(self): # Start with 0 self.assertEqual(self.user1.get_hidden_by_staff_posts_count(), 0) # Post and hide it by poster PostFactory(topic=self.forumtopic, author=self.user1.user, position=1, is_visible=False, editor=self.user1.user) # Should be 0 self.assertEqual(self.user1.get_hidden_by_staff_posts_count(), 0) # Post and hide it by staff PostFactory(topic=self.forumtopic, author=self.user1.user, position=1, is_visible=False, editor=self.staff.user) # Should be 1 self.assertEqual(self.user1.get_hidden_by_staff_posts_count(), 1) def test_get_hidden_by_staff_posts_count_staff_poster(self): # Start with 0 self.assertEqual(self.staff.get_hidden_by_staff_posts_count(), 0) # Post and hide it by poster which is staff PostFactory(topic=self.forumtopic, author=self.staff.user, position=1, is_visible=False, editor=self.staff.user) # Should be 0 because even if poster is staff, he is the poster self.assertEqual(self.staff.get_hidden_by_staff_posts_count(), 0) def test_get_active_alerts_count(self): # Start with 0 self.assertEqual(self.user1.get_active_alerts_count(), 0) # Post and Alert it ! post = PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) Alert.objects.create(author=self.user1.user, comment=post, scope='FORUM', pubdate=datetime.now()) # Should be 1 self.assertEqual(self.user1.get_active_alerts_count(), 1) def test_can_read_now(self): profile = ProfileFactory() profile.is_active = True profile.can_read = True self.assertTrue(profile.can_read_now()) # Was banned in the past, ban no longer active profile = ProfileFactory() profile.end_ban_read = datetime.now() - timedelta(days=1) self.assertTrue(profile.can_read_now()) profile = ProfileFactory() profile.is_active = True profile.can_read = False self.assertFalse(profile.can_read_now()) # Ban is active profile = ProfileFactory() profile.is_active = True profile.can_read = False profile.end_ban_read = datetime.now() + timedelta(days=1) self.assertFalse(profile.can_read_now()) self.user1.user.is_active = False self.assertFalse(self.user1.can_read_now()) def test_can_write_now(self): self.user1.user.is_active = True self.user1.user.can_write = True self.assertTrue(self.user1.can_write_now()) # Was banned in the past, ban no longer active profile = ProfileFactory() profile.can_write = True profile.end_ban_read = datetime.now() - timedelta(days=1) self.assertTrue(profile.can_write_now()) profile = ProfileFactory() profile.can_write = False profile.is_active = True self.assertFalse(profile.can_write_now()) # Ban is active profile = ProfileFactory() profile.can_write = False profile.end_ban_write = datetime.now() + timedelta(days=1) self.assertFalse(profile.can_write_now()) self.user1.user.is_active = False self.user1.user.can_write = True self.assertFalse(self.user1.can_write_now()) def test_get_followed_topics(self): # Start with 0 self.assertEqual(len(TopicAnswerSubscription.objects.get_objects_followed_by(self.user1.user)), 0) # Follow ! TopicAnswerSubscription.objects.toggle_follow(self.forumtopic, self.user1.user) # Should be 1 topicsfollowed = TopicAnswerSubscription.objects.get_objects_followed_by(self.user1.user) self.assertEqual(len(topicsfollowed), 1) self.assertEqual(self.forumtopic, topicsfollowed[0]) def test_get_city_with_wrong_ip(self): # Set a local IP to the user self.user1.last_ip_address = '127.0.0.1' # Then the get_city is not found and return empty string self.assertEqual('', self.user1.get_city()) # Same goes for IPV6 # Set a local IP to the user self.user1.last_ip_address = '0000:0000:0000:0000:0000:0000:0000:0001' # Then the get_city is not found and return empty string self.assertEqual('', self.user1.get_city()) def test_remove_token_on_removing_from_dev_group(self): dev = DevProfileFactory() dev.github_token = 'test' dev.save() dev.user.save() self.assertEqual('test', dev.github_token) # remove dev from dev group dev.user.groups.clear() dev.user.save() self.assertEqual('', dev.github_token) def test_reachable_manager(self): # profile types profile_normal = ProfileFactory() profile_superuser = ProfileFactory() profile_superuser.user.is_superuser = True profile_superuser.user.save() profile_inactive = ProfileFactory() profile_inactive.user.is_active = False profile_inactive.user.save() profile_bot = ProfileFactory() profile_bot.user.username = settings.ZDS_APP['member']['bot_account'] profile_bot.user.save() profile_anonymous = ProfileFactory() profile_anonymous.user.username = settings.ZDS_APP['member']['anonymous_account'] profile_anonymous.user.save() profile_external = ProfileFactory() profile_external.user.username = settings.ZDS_APP['member']['external_account'] profile_external.user.save() profile_ban_def = ProfileFactory() profile_ban_def.can_read = False profile_ban_def.can_write = False profile_ban_def.save() profile_ban_temp = ProfileFactory() profile_ban_temp.can_read = False profile_ban_temp.can_write = False profile_ban_temp.end_ban_read = datetime.now() + timedelta(days=1) profile_ban_temp.save() profile_unban = ProfileFactory() profile_unban.can_read = False profile_unban.can_write = False profile_unban.end_ban_read = datetime.now() - timedelta(days=1) profile_unban.save() profile_ls_def = ProfileFactory() profile_ls_def.can_write = False profile_ls_def.save() profile_ls_temp = ProfileFactory() profile_ls_temp.can_write = False profile_ls_temp.end_ban_write = datetime.now() + timedelta(days=1) profile_ls_temp.save() # groups bot = Group(name=settings.ZDS_APP['member']['bot_group']) bot.save() # associate account to groups bot.user_set.add(profile_anonymous.user) bot.user_set.add(profile_external.user) bot.user_set.add(profile_bot.user) bot.save() # test reachable user profiles_reacheable = Profile.objects.contactable_members().all() self.assertIn(profile_normal, profiles_reacheable) self.assertIn(profile_superuser, profiles_reacheable) self.assertNotIn(profile_inactive, profiles_reacheable) self.assertNotIn(profile_anonymous, profiles_reacheable) self.assertNotIn(profile_external, profiles_reacheable) self.assertNotIn(profile_bot, profiles_reacheable) self.assertIn(profile_unban, profiles_reacheable) self.assertNotIn(profile_ban_def, profiles_reacheable) self.assertNotIn(profile_ban_temp, profiles_reacheable) self.assertIn(profile_ls_def, profiles_reacheable) self.assertIn(profile_ls_temp, profiles_reacheable) def test_remove_hats_linked_to_group(self): # create a hat linked to a group hat_name = 'Test hat' hat, _ = Hat.objects.get_or_create(name__iexact=hat_name, defaults={'name': hat_name}) group, _ = Group.objects.get_or_create(name='test_hat') hat.group = group hat.save() # add it to a user self.user1.hats.add(hat) self.user1.save() # the user shound't have the hat through their profile self.assertNotIn(hat, self.user1.hats.all())
def setUp(self): self.user = ProfileFactory()