def test_view_object_viewer_bool_false(self): self._log_as_viewer() obj = mommy.make(TestClass, bool_field=False) response = self.client.get(obj.get_absolute_url()) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(0, len(soup.select(".bool_field_is_true"))) self.assertEqual(1, len(soup.select(".bool_field_is_false")))
def test_view_login(self): """test the user can view the login page""" url = reverse("login") response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertTrue(len(soup.select("input[name=email]")) > 0) self.assertTrue(len(soup.select("input[name=password]")) > 0) self.assertEqual(0, len(soup.select("input[name=username]")))
def test_view_new_link(self): url = reverse('coop_cms_new_link') self._log_as_editor() response = self.client.get(url) self.assertEqual(200, response.status_code) self.assertEqual(Link.objects.count(), 0) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select("input#id_title"))) self.assertEqual(1, len(soup.select("input#id_url"))) self.assertEqual(1, len(soup.select("input#id_sites_0"))) self.assertNotEqual(None, soup.select("input#id_sites_0")[0].get("checked", None)) self.assertEqual(0, len(soup.select("input#id_sites_1")))
def test_view_reason_unknown(self): """Check custom page with unknown""" factory = RequestFactory() request = factory.get('/') request.user = AnonymousUser() response = csrf_failure(request, "?") self.assertEqual(403, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(0, len(soup.select('.cookies-error'))) self.assertEqual(0, len(soup.select('.referer-error'))) self.assertEqual(1, len(soup.select('.unknown-error')))
def test_view_newsletter_items(self): self._log_as_editor() article_class = get_article_class() site = Site.objects.get_current() other_site = mommy.make(Site) article_1 = mommy.make(article_class, slug="test1", in_newsletter=True) article_2 = mommy.make(article_class, slug="test2", in_newsletter=True, sites=[other_site]) article_3 = mommy.make(article_class, slug="test3", in_newsletter=True) article_4 = mommy.make(article_class, slug="test4", in_newsletter=False) article_2.sites.clear() article_2.sites.add(other_site) article_2.save() ct = ContentType.objects.get_for_model(article_class) self.assertEqual(3, NewsletterItem.objects.count()) item_1 = NewsletterItem.objects.get(content_type=ct, object_id=article_1.id) item_2 = NewsletterItem.objects.get(content_type=ct, object_id=article_2.id) item_3 = NewsletterItem.objects.get(content_type=ct, object_id=article_3.id) newsletter = mommy.make( Newsletter, subject="a little intro for this newsletter", template="test/newsletter_red.html", items=[item_1] ) url = reverse("coop_cms_newsletter_settings", args=[newsletter.id]) response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) options = soup.select("#id_items option") self.assertEqual(2, len(options)) self.assertEqual( sorted([int(id_["value"]) for id_ in options]), sorted([item.id for item in [item_1, item_3]]) ) options = soup.select("#id_items option[selected=selected]") self.assertEqual(1, len(options)) self.assertEqual( sorted([int(id_["value"]) for id_ in options]), sorted([item.id for item in [item_1]]) )
def test_save_newsletter_items_no_newsletter(self): self._log_as_editor() article_class = get_article_class() site = Site.objects.get_current() other_site = mommy.make(Site) article_1 = mommy.make(article_class, slug="test1", in_newsletter=True) article_2 = mommy.make(article_class, slug="test2", in_newsletter=True) article_3 = mommy.make(article_class, slug="test3", in_newsletter=True) content_type = ContentType.objects.get_for_model(article_class) self.assertEqual(3, NewsletterItem.objects.count()) item_1 = NewsletterItem.objects.get(content_type=content_type, object_id=article_1.id) item_2 = NewsletterItem.objects.get(content_type=content_type, object_id=article_2.id) item_3 = NewsletterItem.objects.get(content_type=content_type, object_id=article_3.id) article_2.in_newsletter = False article_2.save() self.assertEqual(2, NewsletterItem.objects.count()) newsletter = mommy.make( Newsletter, subject="a little intro for this newsletter", template="test/newsletter_red.html", items=[item_1] ) data = { "subject": "test", "template": "test/newsletter_blue.html", 'items': [item_2.id, item_3.id] } url = reverse("coop_cms_newsletter_settings", args=[newsletter.id]) response = self.client.post(url, data=data) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select(".errorlist")), 1) self.assertEqual(len(soup.select("#id_items")[0].parent.select(".errorlist")), 1) newsletter = Newsletter.objects.get(id=newsletter.id) self.assertNotEqual(newsletter.subject, data["subject"]) self.assertNotEqual(newsletter.template, data["template"]) self.assertEqual([item_1.id], [x.id for x in newsletter.items.all()])
def test_view_new_article(self): self._log_as_editor() url = reverse('coop_cms_new_article') response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select("#id_dummy")))
def test_view_object_viewer(self): self._log_as_viewer() obj = mommy.make(TestClass) response = self.client.get(obj.get_absolute_url()) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual("ABC", soup.select("#properties")[0].text)
def test_article_category(self): self._log_as_editor() article_class = get_article_class() site1 = Site.objects.get(id=settings.SITE_ID) site2 = mommy.make(Site) cat = mommy.make(ArticleCategory) self.assertEqual(list(cat.sites.all()), [site1]) cat2 = mommy.make(ArticleCategory) cat2.sites.remove(site1) cat2.sites.add(site2) cat2.save() self.assertEqual(list(cat2.sites.all()), [site2]) cat3 = mommy.make(ArticleCategory) cat3.sites.remove(site1) cat3.save() self.assertEqual(list(cat3.sites.all()), []) art1 = mommy.make(article_class, slug="test", category=cat, publication=BaseArticle.PUBLISHED) url = reverse('coop_cms_article_settings', args=[art1.id]) response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) cat_choices = soup.select("select#id_category option") self.assertEqual(2, len(cat_choices)) self.assertEqual("", cat_choices[0]["value"]) self.assertEqual(str(cat.id), cat_choices[1]["value"]) self.assertEqual(cat.name, cat_choices[1].text)
def test_article_settings_no_sites(self): other_site = mommy.make(Site) initial_data = {'title': "test", 'content': "this is my article content"} article_class = get_article_class() art1 = article_class.objects.create(publication=BaseArticle.PUBLISHED, **initial_data) self._log_as_editor() data = { 'template': get_article_templates(None, self.user)[0][0], 'category': '', 'publication': BaseArticle.PUBLISHED, 'publication_date': "2013-01-01 12:00:00", 'headline': True, 'in_newsletter': True, 'summary': 'short summary', 'navigation_parent': None, 'sites': [] } response = self.client.post(reverse('coop_cms_article_settings', args=[art1.id]), data=data, follow=True) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("ul.errorlist")), 1) self.assertEqual(article_class.objects.exclude(id=art1.id).count(), 0) art1 = article_class.objects.get(id=art1.id) self.assertNotEqual(art1.summary, data['summary']) self.assertEqual(sorted([a.id for a in art1.sites.all()]), [settings.SITE_ID])
def test_show_carousel_several_images(self): """display an album""" media_filer = mommy.make(MediaFilter, name='Album Homepage') image1 = mommy.make(Image, _create_files=True) image1.filters.add(media_filer) image1.save() image2 = mommy.make(Image, _create_files=True) image2.filters.add(media_filer) image2.save() image3 = mommy.make(Image, _create_files=True) template_content = ''' {% load coop_utils %} {% coop_image_list 'Album Homepage' as homepage_album %} {% include "test/carousel.html" with image_list=homepage_album %}') ''' tpl = Template(template_content) html = tpl.render(Context({})) soup = BeautifulSoup(html) tags = soup.select('img') self.assertEqual(2, len(tags)) self.assertEqual( sorted([img.get_absolute_url() for img in (image1, image2)]), sorted([tag['src'] for tag in tags]), )
def test_new_article_invalid_category_site(self): article_class = get_article_class() category1 = mommy.make(ArticleCategory) category1.sites.clear() category1.sites.add(Site.objects.get_current()) category1.save() category2 = mommy.make(ArticleCategory) category2.sites.clear() category2.save() self._log_as_editor() data = { 'title': "Un titre", 'category': category2.id, 'publication': BaseArticle.DRAFT, 'template': get_article_templates(None, self.user)[0][0], 'navigation_parent': None, 'sites': [settings.SITE_ID] } response = self.client.post(reverse('coop_cms_new_article'), data=data, follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(article_class.objects.count(), 0) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("ul.errorlist")), 1)
def test_create_edit_poc(self): tpl = Template('{% load coop_edition %}{% coop_piece_of_html "test" %}') html = tpl.render(Context({"inline_html_edit": True})) self.assertNotEqual(html, "") soup = BeautifulSoup(html) tags = soup.select("#html_editor_html_editor__coop_cms__PieceOfHtml__div_id__test__content") self.assertEqual(len(tags), 1) self.assertEqual(tags[0].text, "") tags_hidden = soup.select("#html_editor_html_editor__coop_cms__PieceOfHtml__div_id__test__content_hidden") self.assertEqual(len(tags_hidden), 1) self.assertEqual(tags_hidden[0].get("value", ""), "") self.assertEqual(PieceOfHtml.objects.count(), 1) poc = PieceOfHtml.objects.all()[0] self.assertEqual(poc.div_id, "test")
def test_view_new_link_two_sites(self): mommy.make(Site) url = reverse('coop_cms_new_link') self._log_as_editor() response = self.client.get(url) self.assertEqual(200, response.status_code) self.assertEqual(Link.objects.count(), 0) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select("input#id_title"))) self.assertEqual(1, len(soup.select("input#id_url"))) self.assertEqual(1, len(soup.select("input#id_sites_0"))) self.assertEqual(1, len(soup.select("input#id_sites_1"))) elt1 = soup.select("input#id_sites_0")[0] elt2 = soup.select("input#id_sites_1")[0] for elt in (elt1, elt2): # Only the current site is checked self.assertEqual('checked' in elt, elt["value"] == settings.SITE_ID)
def test_additional_field_on_create(self): self._log_as_editor() url = reverse("coop_cms_new_newsletter") response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select("#id_dummy")))
def test_view_article_settings(self): self._log_as_editor() article_class = coop_settings.get_article_class() article = mommy.make(article_class, slug="test") url = reverse('coop_cms_article_settings', args=[article.id]) response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select("#id_dummy")))
def test_edit_formset_no_objects(self): self._log_as_editor() url = reverse('coop_cms_testapp_formset_edit') response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select('form')))
def test_edit_poc_extra_id(self): poc = mommy.make(PieceOfHtml, div_id="test", content="HELLO!!!", extra_id="1") tpl = Template('{% load coop_edition %}{% coop_piece_of_html "test" extra_id=1 %}') html = tpl.render(Context({"inline_html_edit": True})) soup = BeautifulSoup(html) #print html tags = soup.select("input[type=hidden]") self.assertEqual(len(tags), 1) div_selector = tags[0].attrs['id'] div_selector = div_selector.replace("_hidden", "") tags = soup.select("#"+div_selector) self.assertEqual(len(tags), 1) self.assertEqual(tags[0].text, poc.content) self.assertEqual(PieceOfHtml.objects.count(), 1) poc = PieceOfHtml.objects.all()[0] self.assertEqual(poc.div_id, "test") self.assertEqual(poc.extra_id, "1")
def test_view_article_set_homepage(self): self._log_as_editor(can_add=True) article_class = get_article_class() art = mommy.make(article_class, slug="test", publication=BaseArticle.PUBLISHED) url = art.get_edit_url() response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) url = reverse('coop_cms_set_homepage', args=[art.id]) links = soup.select('.coop-bar a[href="{0}"]'.format(url)) self.assertEqual(1, len(links))
def test_add_link_no_title(self): self._log_as_editor() data = {'title': "", 'url': "http://www.google.fr", 'sites': [settings.SITE_ID]} response = self.client.post(reverse('coop_cms_new_link'), data=data, follow=True) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("ul.errorlist")), 1) self.assertEqual(Link.objects.count(), 0)
def test_show_empty_carousel_missing_filter(self): """display an album""" template_content = ''' {% load coop_utils %} {% coop_image_list 'Album Homepage' as homepage_album %} {% include "test/carousel.html" with image_list=homepage_album %}') ''' tpl = Template(template_content) html = tpl.render(Context({})) soup = BeautifulSoup(html) self.assertEqual(0, len(soup.select('img')))
def test_additional_field_on_edit(self): self._log_as_editor() newsletter = mommy.make( Newsletter, subject="a little intro for this newsletter", template="test/newsletter_blue.html", ) url = reverse("coop_cms_newsletter_settings", args=[newsletter.id]) response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select("#id_dummy")))
def test_slug_edition_published_can_edit(self): settings.COOP_CMS_CAN_EDIT_ARTICLE_SLUG = True self._log_as_staff_editor() article_class = get_article_class() article = mommy.make(article_class, slug="test", publication=BaseArticle.PUBLISHED) view_name = 'admin:%s_%s_change' % (get_model_app(article_class), get_model_name(article_class)) url = reverse(view_name, args=[article.id]) response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) if is_localized(): from modeltranslation.utils import build_localized_fieldname for (lang, _name) in settings.LANGUAGES: field_name = build_localized_fieldname('slug', lang) self.assertEqual(soup.select("#id_" + field_name)[0]["type"], "text") else: self.assertEqual(soup.select("#id_slug")[0]["type"], "text")
def test_view_formset_one_object(self): self._log_as_viewer() obj = mommy.make(TestClass) url = reverse('coop_cms_testapp_formset') response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(0, len(soup.select('form'))) self.assertContains(response, obj.field1) self.assertContains(response, obj.field2) self.assertContains(response, obj.other_field)
def test_register_refuse_wrong_email(self): """It should display error""" url = reverse('registration_register') data = { 'email': 'john', 'password1': 'blabla-123', 'password2': 'blabla-123', 'terms_of_service': True, } self.assertEqual(User.objects.filter(email=data['email']).count(), 0) response = self.client.post(url, data=data) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select('.errorlist')), 1) self.assertEqual(User.objects.filter(email=data['email']).count(), 0) self.assertEqual(len(mail.outbox), 0)
def test_show_carousel_context_var(self): """display an album""" media_filer = mommy.make(MediaFilter, name='Album Homepage') image = mommy.make(Image, _create_files=True) image.filters.add(media_filer) image.save() template_content = ''' {% load coop_utils %} {% coop_image_list album_name as homepage_album %} {% include "test/carousel.html" with image_list=homepage_album %}') ''' tpl = Template(template_content) html = tpl.render(Context({'album_name': 'Album Homepage'})) soup = BeautifulSoup(html) self.assertEqual(1, len(soup.select('img')))
def test_new_article_title_required(self): article_class = get_article_class() self._log_as_editor() data = { 'title': "", 'publication': BaseArticle.DRAFT, 'template': get_article_templates(None, self.user)[0][0], 'navigation_parent': None, 'sites': [settings.SITE_ID] } response = self.client.post(reverse('coop_cms_new_article'), data=data, follow=True) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(article_class.objects.count(), 0) self.assertEqual(len(soup.select("ul.errorlist")), 1)
def test_view_register(self): """It should display form""" user_count = User.objects.count() url = reverse('registration_register') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(User.objects.count(), user_count) self.assertEqual(len(mail.outbox), 0) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("input#id_email")), 1) self.assertEqual(len(soup.select("input#id_password1")), 1) self.assertEqual(len(soup.select("input#id_password2")), 1) self.assertEqual(len(soup.select("input#id_username")), 1) self.assertEqual(len(soup.select("input#id_terms_of_service")), 1) self.assertEqual(soup.select("input#id_email")[0]['type'], 'email') self.assertEqual(soup.select("input#id_password1")[0]['type'], 'password') self.assertEqual(soup.select("input#id_password2")[0]['type'], 'password') self.assertEqual(soup.select("input#id_username")[0]['type'], 'hidden') self.assertEqual(soup.select("input#id_terms_of_service")[0]['type'], 'checkbox')
def test_get_language_switcher(self): """get article with locale slug""" original_text = '*!-+' * 10 translated_text = ':%@/' * 9 art1 = get_article_class().objects.create(title="Home", content=original_text) origin_lang = settings.LANGUAGES[0][0] trans_lang = settings.LANGUAGES[1][0] setattr(art1, 'title_' + trans_lang, 'Accueil') setattr(art1, 'content_' + trans_lang, translated_text) art1.save() url = reverse('coop_cms_switch_language_popup') response = self.client.get(url) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select('select#id_language option')), len(settings.LANGUAGES))
def test_post_clone_action_invalid(self): """it should not clone and show error""" action_type_1 = mommy.make(models.ActionType) action_type_2 = mommy.make(models.ActionType) action_type_1.next_action_types.add(action_type_2) action_type_1.save() action = mommy.make(models.Action, type=action_type_1) data = { 'action_type': 'bla' } response = self.client.post( reverse('crm_clone_action', args=[action.id]), data=data ) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select(".error-msg")), 1) self.assertEqual(1, models.Action.objects.count())
def test_search_contacts_and_entities_by_date(self): """by modification date""" # when saved 'modified' is updated contact1 = mommy.make( models.Contact, lastname="ABCD", email="*****@*****.**", main_contact=True, has_left=False, ) contact1.entity.name = 'Tiny Corp' contact1.entity.default_contact.delete() contact1.entity.save() url = reverse('search') data = {"gr0-_-contacts_and_entities_by_change_date-_-0": '{0} {0}'.format(date.today().strftime("%d/%m/%Y"))} response = self.client.post(url, data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(0, len(soup.select('.field-error'))) self.assertContains(response, contact1.lastname)
def test_view_emailing_sent(self): """view form no emailing sent""" mommy.make(Emailing, status=Emailing.STATUS_EDITING) emailing2 = mommy.make(Emailing, status=Emailing.STATUS_SENDING, scheduling_dt=datetime.now()) emailing3 = mommy.make( Emailing, status=Emailing.STATUS_SENT, scheduling_dt=datetime.now(), sending_dt=datetime.now() ) mommy.make(Emailing, status=Emailing.STATUS_SCHEDULED, scheduling_dt=datetime.now()) url = reverse('search_get_field', args=['emailing_sent']) url += "?block=gr0&count=0" response = self.client.get(url) self.assertEqual(200, response.status_code) data = response_as_json(response) soup = BeautifulSoup(data['form']) selector = soup.select("select#id_gr0-_-emailing_sent-_-0 option") self.assertEqual(3, len(selector)) self.assertEqual("", selector[0]["value"]) self.assertEqual(emailing3.id, int(selector[1]["value"])) self.assertEqual(emailing2.id, int(selector[2]["value"]))
def test_new_article_invalid_site(self): article_class = get_article_class() self._log_as_editor() data = { 'title': "Un titre", 'publication': BaseArticle.DRAFT, 'template': get_article_templates(None, self.user)[0][0], 'navigation_parent': None, 'sites': [settings.SITE_ID, 999] } response = self.client.post(reverse('coop_cms_new_article'), data=data, follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(article_class.objects.count(), 0) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("ul.errorlist")), 1)
def test_view_opportunity_contact_has_left(self): """view opportunity with contact who left""" entity1 = mommy.make(models.Entity, relationship_date='2012-01-30') opp1 = mommy.make(models.Opportunity) contact1 = mommy.make(models.Contact, lastname='ABC', entity=entity1) contact2 = mommy.make(models.Contact, lastname='DEF', entity=entity1, has_left=True) act1 = mommy.make(models.Action, opportunity=opp1) act1.contacts.add(contact1) act1.contacts.add(contact2) act1.save() response = self.client.get(reverse('crm_view_opportunity', args=[opp1.id])) self.assertEqual(200, response.status_code) self.assertContains(response, contact1.lastname) self.assertContains(response, contact2.lastname) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("td.ut-contact-{0} .ut-has-left".format(contact1.id))), 0) self.assertEqual(len(soup.select("td.ut-contact-{0} .ut-has-left".format(contact2.id))), 1)
def test_view_clone_action_2_types(self): """it should display a select with allowed types""" action_type_1 = mommy.make(models.ActionType, order_index=1) action_type_2 = mommy.make(models.ActionType, order_index=2) action_type_3 = mommy.make(models.ActionType, order_index=3) action_type_1.next_action_types.add(action_type_2) action_type_1.next_action_types.add(action_type_3) action_type_1.save() action = mommy.make(models.Action, type=action_type_1) response = self.client.get( reverse('crm_clone_action', args=[action.id])) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) html_fields = soup.select("#id_action_type") self.assertEqual(1, len(html_fields)) options = html_fields[0].select("option") self.assertEqual(len(options), 2) self.assertEqual(options[0]["value"], "{0}".format(action_type_2.id)) self.assertEqual(options[1]["value"], "{0}".format(action_type_3.id))
def test_no_name(self): """save search without name""" url = reverse("search_save", args=[0]) group1 = mommy.make(models.Group) search_data = { "gr0-_-group-_-0": group1.id, } data = { 'name': "", 'search_id': 0, "search_fields": json.dumps(search_data), } response = self.client.post(url, data=data) self.assertEqual(response.status_code, 200) self.assertEqual(Search.objects.count(), 0) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select(".field-error")), 1)
def test_action_menu_status_ony_empty(self): """test custom menus are displayed""" action_type1 = mommy.make(models.ActionType) action_status1 = mommy.make(models.ActionStatus) action_status2 = mommy.make(models.ActionStatus) action_type1.allowed_status.add(action_status1) action_type1.allowed_status.add(action_status2) action_type1.save() action = mommy.make(models.Action, type=action_type1, status=action_status1) entity = mommy.make(models.Entity) action.entities.add(entity) action.save() menu1 = mommy.make( models.ActionMenu, action_type=action_type1, icon="cog", label="DO ME", view_name='crm_do_action', a_attrs='class="colorbox-form"', order_index=2 ) menu2 = mommy.make( models.ActionMenu, action_type=action_type1, icon="remove", label="DELETE ME", view_name='crm_delete_action', a_attrs='target="_blank"', order_index=1 ) menu2.only_for_status.add(action_status2) menu2.save() url = reverse('crm_view_entity', args=[entity.id]) response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) li_tags = soup.select('.ut-custom-action-menu-item') self.assertEqual(len(li_tags), 1) self.assertEqual(li_tags[0].text.strip(), menu1.label) self.assertEqual(li_tags[0].a['class'], ['colorbox-form']) self.assertEqual(li_tags[0].a['href'], reverse('crm_do_action', args=[action.id])) self.assertEqual(li_tags[0].a.i['class'], ['fas', 'fa-{0}'.format(menu1.icon)])
def test_search_email_empty(self): """search by empty email""" entity1 = mommy.make(models.Entity, name="My tiny corp") contact1 = mommy.make(models.Contact, email="*****@*****.**", entity=entity1, lastname="ABCD", main_contact=True, has_left=False) contact3 = mommy.make(models.Contact, entity=entity1, lastname="IJKL", main_contact=True, has_left=False) entity2 = mommy.make(models.Entity, name="Other corp", email="*****@*****.**") contact2 = mommy.make(models.Contact, entity=entity2, lastname="WXYZ", main_contact=True, has_left=False) url = reverse('search') data = {"gr0-_-contact_entity_email-_-0": ""} response = self.client.post(url, data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select('.field-error'))) self.assertNotContains(response, entity1.name) self.assertNotContains(response, contact1.lastname) self.assertNotContains(response, contact3.lastname) self.assertNotContains(response, entity2.name) self.assertNotContains(response, contact2.lastname)
def test_article_settings_unknown_sites(self): other_site = mommy.make(Site) initial_data = { 'title': "test", 'content': "this is my article content" } article_class = get_article_class() art1 = article_class.objects.create(publication=BaseArticle.PUBLISHED, **initial_data) self._log_as_editor() data = { 'template': get_article_templates(None, self.user)[0][0], 'category': '', 'publication': BaseArticle.PUBLISHED, 'publication_date': "2013-01-01 12:00:00", 'headline': True, 'in_newsletter': True, 'summary': 'short summary', 'navigation_parent': None, 'sites': [settings.SITE_ID, 999] } response = self.client.post(reverse('coop_cms_article_settings', args=[art1.id]), data=data, follow=True) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("ul.errorlist")), 1) self.assertEqual(article_class.objects.exclude(id=art1.id).count(), 0) art1 = article_class.objects.get(id=art1.id) self.assertNotEqual(art1.summary, data['summary']) self.assertEqual(sorted([a.id for a in art1.sites.all()]), [settings.SITE_ID])
def test_view_new_emailing(self): """test view form when BALAFON_EMAILING_SENDER_CHOICES is not set""" contact1 = mommy.make(models.Contact, lastname="ABCD", main_contact=True, has_left=False) mommy.make(Newsletter) mommy.make(models.SubscriptionType) # TODO settings.BALAFON_EMAILING_SENDER_CHOICES = [] data = {'contacts': [contact1.id]} url = reverse('search_emailing') response = self.client.post(url, data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) node = soup.select("#id_from_email")[0] self.assertEqual("hidden", node["type"])
def test_suggestion_entity_emails(self): """check that contact inside an entity with same email are suggested""" beatles = mommy.make(models.Entity, email="*****@*****.**") contact1 = mommy.make(models.Contact, firstname="John", lastname="Lennon", email="*****@*****.**") contact2 = mommy.make(models.Contact, firstname="John", lastname="Lennon") contact3 = mommy.make(models.Contact, firstname="Ringo", lastname="Star", email="*****@*****.**") contact4 = mommy.make(models.Contact, firstname="Gallagher", lastname="Noel") contact5 = mommy.make(models.Contact, entity=beatles, firstname="Georges", lastname="Harrison") contact6 = beatles.default_contact url = reverse("crm_same_as", args=[contact1.id]) response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) ids = [int(x["value"]) for x in soup.select("select option")] self.assertEqual(4, len(ids)) self.assertFalse(contact1.id in ids) self.assertTrue(contact2.id in ids) self.assertTrue(contact3.id in ids) self.assertFalse(contact4.id in ids) self.assertTrue(contact5.id in ids) self.assertTrue(contact6.id in ids)
def test_subscribe_newsletter_no_email(self): """subscribe without setting an email""" group1 = mommy.make(models.Group, name="ABC", subscribe_form=True) url = reverse("emailing_subscribe_newsletter") data = { 'lastname': 'Dupond', 'firstname': 'Pierre', 'groups': str(group1.id), 'entity_type': 0, 'email': '', } self._patch_with_captcha(url, data) self.assertEqual(models.Contact.objects.count(), 0) response = self.client.post(url, data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select("ul.errorlist")), 1) self.assertEqual(models.Contact.objects.count(), 0) self.assertEqual(len(mail.outbox), 0)
def test_post_select_unknown_id(self): """it should return js code""" contact = mommy.make(models.Contact, lastname="Abc", firstname='Joe') data = { 'object_id': contact.id + 1, 'object_type': 'contact', 'name': contact.fullname } expected_data = { 'id': contact.id, 'type': 'contact', 'name': contact.fullname, 'url': contact.get_preview_url(), } response = self.client.post(reverse('crm_select_contact_or_entity'), data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select('.field-error')), 1)
def test_show_carousel(self): """display an album""" media_filer = mommy.make(MediaFilter, name='Album Homepage') image = mommy.make(Image, _create_files=True) image.filters.add(media_filer) image.save() mommy.make(Image) template_content = ''' {% load coop_utils %} {% coop_image_list 'Album Homepage' as homepage_album %} {% include "test/carousel.html" with image_list=homepage_album %}') ''' tpl = Template(template_content) html = tpl.render(Context({})) soup = BeautifulSoup(html) tags = soup.select('img') self.assertEqual(1, len(tags)) self.assertEqual(image.get_absolute_url(), tags[0]['src'])
def test_suggestion_list_two_choices_existing_same_as(self): """test suggestions with existing same as""" contact1 = mommy.make(models.Contact, firstname="John", lastname="Lennon") contact2 = mommy.make(models.Contact, firstname="John", lastname="Lennon") contact3 = mommy.make(models.Contact, firstname="Ringo", lastname="Star") contact4 = mommy.make(models.Contact, firstname="John", lastname="Lennon") same_as = models.SameAs.objects.create() for contact in [contact1, contact2]: contact.same_as = same_as contact.save() url = reverse("crm_same_as", args=[contact1.id]) response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) ids = [int(x["value"]) for x in soup.select("select option")] self.assertEqual(1, len(ids)) self.assertFalse(contact1.id in ids) self.assertFalse(contact2.id in ids) self.assertFalse(contact3.id in ids) self.assertTrue(contact4.id in ids)
def test_has_address_mix(self, has_address=True): """has address contacts and entities""" city1 = mommy.make(models.City, name="ZooPark") mommy.make(models.City, name="VodooPark") entity1, contact1 = self._make_contact("ABCD", city1, "44444") contact1.city = None contact1.address = "123 something street" contact1.save() entity2, contact2 = self._make_contact("EFGH", None, "", '123 something street') contact2.city = city1 contact2.zip_code = "" contact2.save() url = reverse('search') data = {"gr0-_-has_address-_-0": 1 if has_address else 0} response = self.client.post(url, data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(0, len(soup.select('.field-error'))) if has_address: self.assertNotContains(response, entity1.name) self.assertNotContains(response, contact1.lastname) self.assertNotContains(response, entity2.name) self.assertNotContains(response, contact2.lastname) else: self.assertContains(response, entity1.name) self.assertContains(response, contact1.lastname) self.assertContains(response, entity2.name) self.assertContains(response, contact2.lastname)
def test_get_language_switcher(self): """get article with locale slug""" original_text = '*!-+' * 10 translated_text = ':%@/' * 9 art1 = get_article_class().objects.create(title="Home", content=original_text) origin_lang = settings.LANGUAGES[0][0] trans_lang = settings.LANGUAGES[1][0] setattr(art1, 'title_' + trans_lang, 'Accueil') setattr(art1, 'content_' + trans_lang, translated_text) art1.save() url = reverse('coop_cms_switch_language_popup') response = self.client.get(url) soup = BeautifulSoup(response.content) self.assertEqual(len(soup.select('select#id_language option')), len(settings.LANGUAGES))
def test_show_carousel(self): """display an album""" media_filer = mommy.make(MediaFilter, name='Album Homepage') image = mommy.make(Image, _create_files=True) image.filters.add(media_filer) image.save() mommy.make(Image) template_content = ''' {% load coop_utils %} {% coop_image_list 'Album Homepage' as homepage_album %} {% include "test/carousel.html" with image_list=homepage_album %}') ''' tpl = Template(template_content) html = tpl.render(Context({})) soup = BeautifulSoup(html) tags = soup.select('img') self.assertEqual(1, len(tags)) self.assertEqual(image.get_absolute_url(), tags[0]['src'])
def test_edit_group_invalid_background_color(self): """it should not modify the group""" group = mommy.make(models.Group) data = { 'name': 'my group name', 'description': 'my group description', 'fore_color': '#123', 'background_color': 'hello', } response = self.client.post(reverse('crm_edit_group', args=[group.id]), data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) errors = soup.select('ul.errorlist li') self.assertEqual(len(errors), 1) group = models.Group.objects.get(id=group.id) self.assertNotEqual(group.name, data['name']) self.assertNotEqual(group.description, data['description']) self.assertNotEqual(group.fore_color, data['fore_color']) self.assertNotEqual(group.background_color, data['background_color'])
def test_view_with_blocks(self): """test view article with block templatetag inside the cms_edit template tag""" article_class = coop_settings.get_article_class() article = mommy.make( article_class, title="This is my article", content="<p>This is my <b>content</b></p>", template='coop_cms/test_app/custom_tag_template.html') response = self.client.get(article.get_absolute_url()) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual(3, len(soup.select("ul.custom li"))) self.assertContains(response, article.title) self.assertContains(response, article.content) self.assertContains(response, "*** HELLO FROM CHILD ***") self.assertContains(response, "*** HELLO FROM PARENT ***") self.assertContains(response, "*** HELLO FROM BLOCK ***")
def test_view_new_emailing_language(self): """view emailing with several languages: language is a select""" mommy.make(models.SubscriptionType) contact1 = mommy.make(models.Contact, lastname="ABCD", main_contact=True, has_left=False) contact2 = mommy.make(models.Contact, lastname="EFGH", main_contact=True, has_left=False) data = { 'contacts': ";".join([str(x) for x in [contact1.id, contact2.id]]), } url = reverse('search_emailing') response = self.client.post(url, data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual(1, len(soup.select("select#id_lang")))
def test_open_search(self): """test open an existing search""" group1 = mommy.make(models.Group) search = mommy.make(Search) search_group = mommy.make(SearchGroup, name="gr0", search=search) mommy.make(SearchField, field='group', value='{0}'.format(group1.id), is_list=False, search_group=search_group) url = reverse("search", args=[search.id]) response = self.client.get(url) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) self.assertEqual( soup.select("input[name=gr0-_-group-_-0]")[0]["value"], '{0}'.format(group1.id))
def _test_search_multi_zones(self, cities=None, data=None, entity_search=False): """search by zone""" city1 = cities[0] city2 = cities[1] city3 = mommy.make(models.City, name="BlablaPark") entity1 = mommy.make(models.Entity, city=city1 if entity_search else None) contact1a = entity1.default_contact contact1a.lastname = "ABCDEFGH" contact1a.main_contact = True contact1a.has_left = False contact1a.city = city1 contact1a.save() contact1b = mommy.make(models.Contact, entity=entity1, lastname="IJKLMNOP", main_contact=True, has_left=False) entity2 = mommy.make(models.Entity, city=city2 if entity_search else None) contact2 = entity2.default_contact contact2.lastname = "DEFGHIJKL" contact2.main_contact = True contact2.has_left = False contact2.city = None if entity_search else city2 contact2.save() entity3 = mommy.make(models.Entity) contact3 = entity3.default_contact contact3.lastname = "MNOPQRST" contact3.main_contact = True contact3.has_left = False contact3.city = city3 contact3.save() url = reverse('search') response = self.client.post(url, data=data) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertEqual([], soup.select('.field-error')) self.assertContains(response, entity1.name) self.assertContains(response, contact1a.lastname) if entity_search: self.assertContains(response, contact1b.lastname) else: self.assertNotContains(response, contact1b.lastname) self.assertContains(response, entity2.name) self.assertContains(response, contact2.lastname) self.assertNotContains(response, entity3.name) self.assertNotContains(response, contact3.lastname)
def test_view_search(self): """test view search and field choice is populated""" response = self.client.get(reverse('search')) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) self.assertTrue(len(soup.select("#id_field_choice option")) > 0)