def test_deserialization(self):
     """Can we deserialize data to a Startup model?"""
     startup_data = get_instance_data(StartupFactory.build())
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug)
         for tag in TagFactory.build_batch(3) + TagFactory.create_batch(2)
     ]
     data = dict(startup_data, tags=tag_urls)
     s_startup = StartupSerializer(data=data,
                                   **context_kwarg("/api/v1/startup/"))
     self.assertTrue(s_startup.is_valid(), msg=s_startup.errors)
     self.assertEqual(
         Startup.objects.count(),
         0,
         "Unexpected initial condition",
     )
     self.assertEqual(
         Tag.objects.count(),
         2,
         "Unexpected initial condition",
     )
     startup = s_startup.save()
     self.assertEqual(
         Startup.objects.count(),
         1,
         "Serialized Startup not saved",
     )
     self.assertCountEqual(
         startup.tags.values_list("slug", flat=True),
         [],
         "Startup had tags associated with it",
     )
     self.assertEqual(Tag.objects.count(), 2, "Serialized Tags saved")
 def test_add_post(self):
     """Can new Tags be created?"""
     self.assertEqual(Tag.objects.count(), 1)
     data = get_instance_data(TagFactory.build())
     with self.login(self.test_user):
         self.post("admin:organizer_tag_add", data=data)
     self.assertEqual(Tag.objects.count(), 2)
 def test_create_post(self):
     """Can we submit a form to create NewsLinks?"""
     newslink_num = NewsLink.objects.count()
     startup = StartupFactory()
     newslink_data = {
         **omit_keys(
             "id",
             get_instance_data(NewsLinkFactory.build()),
         ),
         "startup":
         startup.pk,
     }
     with perm_user(self, "organizer.add_newslink"):
         response = self.post(
             "newslink_create",
             startup_slug=startup.slug,
             data=newslink_data,
         )
         self.assertEqual(
             NewsLink.objects.count(),
             newslink_num + 1,
             response.content.decode("utf8"),
         )
         newslink = NewsLink.objects.get(slug=newslink_data["slug"])
         self.assertEqual(startup.pk, newslink.startup.pk)
         self.assertRedirects(response, newslink.get_absolute_url())
Example #4
0
 def test_deserialization(self):
     """Can we deserialize data to a Post model?"""
     tag_list = TagFactory.create_batch(randint(1, 10))
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug) for tag in tag_list
     ]
     startup_list = StartupFactory.create_batch(randint(1, 10))
     startup_urls = [
         reverse("api-startup-detail", slug=startup.slug)
         for startup in startup_list
     ]
     post_data = remove_m2m(get_instance_data(PostFactory.build()))
     data = dict(
         **post_data,
         tags=tag_urls,
         startups=startup_urls,
     )
     s_post = PostSerializer(
         data=data,
         **context_kwarg(reverse("api-post-list", full=True)),
     )
     self.assertTrue(s_post.is_valid(), msg=s_post.errors)
     post = s_post.save()
     self.assertCountEqual(tag_list, post.tags.all())
     self.assertCountEqual(startup_list, post.startups.all())
 def test_create_post_malicious_form(self):
     """Can we maliciously change startups in NewsLink create?"""
     newslink_num = NewsLink.objects.count()
     startup1 = StartupFactory()
     startup2 = StartupFactory()
     newslink_data = {
         **omit_keys(
             "id",
             get_instance_data(NewsLinkFactory.build()),
         ),
         "startup":
         startup1.pk,
     }
     with perm_user(self, "organizer.add_newslink"):
         response = self.post(
             "newslink_create",
             startup_slug=startup2.slug,
             data=newslink_data,
         )
         self.response_400()
         # ensure NewsLink _not_ created
         self.assertEqual(
             NewsLink.objects.count(),
             newslink_num,
             response.content.decode("utf8"),
         )
 def test_create_post(self):
     """Can we submit a form to create Posts?"""
     with perm_user(self, "blog.add_post"):
         post_num = Post.objects.count()
         tag = TagFactory()
         startup = StartupFactory()
         post_data = {
             **omit_keys(
                 "id",
                 get_instance_data(PostFactory.build()),
             ),
             "tags": [tag.pk],
             "startups": [startup.pk],
         }
         self.assertFalse(
             Post.objects.filter(
                 slug=post_data["slug"]
             ).exists()
         )
         response = self.post(
             "post_create", data=post_data
         )
         self.assertEqual(
             Post.objects.count(),
             post_num + 1,
             response.content.decode("utf8"),
         )
         post = Post.objects.get(slug=post_data["slug"])
         self.assertIn(tag, post.tags.all())
         self.assertIn(startup, post.startups.all())
         self.assertRedirects(
             response, post.get_absolute_url()
         )
 def test_update_post(self):
     """Can we submit a form to update posts?"""
     with perm_user(self, "blog.change_post"):
         post = PostFactory(
             tags=TagFactory.create_batch(randint(1, 5)),
             startups=StartupFactory.create_batch(
                 randint(1, 5)
             ),
         )
         self.assertNotEqual(post.title, "django")
         post_data = omit_keys(
             "id", get_instance_data(post)
         )
         response = self.post(
             "post_update",
             year=post.pub_date.year,
             month=post.pub_date.month,
             slug=post.slug,
             data=dict(post_data, title="django"),
         )
         post.refresh_from_db()
         self.assertEqual(
             post.title,
             "django",
             response.content.decode("utf8"),
         )
         self.assertRedirects(
             response, post.get_absolute_url()
         )
 def test_update_post_malicious_form(self):
     """Can we maliciously change startups in NewsLink update?"""
     startup1 = StartupFactory()
     startup2 = StartupFactory()
     newslink = NewsLinkFactory(startup=startup1)
     new_title = "django"
     self.assertNotEqual(newslink.title, new_title)
     newslink_data = {
         **omit_keys("id", get_instance_data(newslink)),
         "title": new_title,
         "startup": startup2.pk,
     }
     with perm_user(self, "organizer.change_newslink"):
         response = self.post(
             "newslink_update",
             startup_slug=startup1.slug,
             newslink_slug=newslink.slug,
             data=newslink_data,
         )
         self.response_400()
         # ensure NewsLink _not_ update
         newslink.refresh_from_db()
         self.assertNotEqual(
             newslink.title,
             new_title,
             response.content.decode("utf8"),
         )
Example #9
0
 def test_detail_update(self):
     """Can we update a Post via PUT?"""
     post = PostFactory(title="first")
     count = Post.objects.count()
     tag_list = TagFactory.create_batch(randint(1, 10))
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug) for tag in tag_list
     ]
     startup_list = StartupFactory.create_batch(randint(1, 10))
     startup_urls = [
         reverse("api-startup-detail", slug=startup.slug)
         for startup in startup_list
     ]
     self.put(
         "api-post-detail",
         year=post.pub_date.year,
         month=post.pub_date.month,
         slug=post.slug,
         data=dict(
             remove_m2m(get_instance_data(post)),
             title="second",
             tags=tag_urls,
             startups=startup_urls,
         ),
     )
     self.response_200()
     self.assertEqual(count, Post.objects.count())
     post.refresh_from_db()
     self.assertEqual("second", post.title)
     self.assertCountEqual(tag_list, post.tags.all())
     self.assertCountEqual(startup_list, post.startups.all())
 def test_deserialization(self):
     """Can we deserialize data to a Tag model?"""
     tag_data = get_instance_data(TagFactory.build())
     s_tag = TagSerializer(data=tag_data, **context_kwarg("/api/v1/tag/"))
     self.assertTrue(s_tag.is_valid(), s_tag.errors)
     tag = s_tag.save()
     self.assertTrue(Tag.objects.filter(pk=tag.pk).exists())
Example #11
0
 def test_list_create_m2m(self):
     """Can new Posts be related with tags & startups?"""
     count = Post.objects.count()
     tag_list = TagFactory.create_batch(randint(1, 10))
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug) for tag in tag_list
     ]
     startup_list = StartupFactory.create_batch(randint(1, 10))
     startup_urls = [
         reverse("api-startup-detail", slug=startup.slug)
         for startup in startup_list
     ]
     post = PostFactory.build()
     self.post(
         "api-post-list",
         data=dict(
             remove_m2m(get_instance_data(post)),
             tags=tag_urls,
             startups=startup_urls,
         ),
     )
     self.response_201()
     self.assertEqual(count + 1, Post.objects.count())
     post = Post.objects.get(slug=post.slug, pub_date=post.pub_date)
     self.assertCountEqual(tag_list, post.tags.all())
     self.assertCountEqual(startup_list, post.startups.all())
Example #12
0
 def test_slug_validation(self):
     """Do we error if slug is create?"""
     data = omit_keys(
         "slug",
         get_instance_data(StartupFactory.build()),
     )
     sform = StartupForm({**data, "slug": "create"})
     self.assertFalse(sform.is_valid())
Example #13
0
 def test_detail_update_404(self):
     """Do we generate 404 if startup not found?"""
     url = reverse("api-startup-detail", slug="nonexistent")
     self.put(
         url,
         data=get_instance_data(StartupFactory.build()),
     )
     self.response_404()
 def test_change_post(self):
     """Can existing Tags be modified?"""
     t2 = TagFactory()
     data = dict(name="a new tag name")
     self.assertNotEqual(get_instance_data(t2), data)
     with self.login(self.test_user):
         self.post(
             "admin:organizer_tag_change",
             data=data,
             object_id=t2.pk,
         )
         self.response_302()
     t2.refresh_from_db()
     self.assertEqual(
         omit_keys("id", "slug", get_instance_data(t2)),
         data,
     )
     self.assertEqual(Tag.objects.count(), 2)
Example #15
0
 def test_list_create(self):
     """Does Startup list view create new objects via POST?"""
     self.assertEqual(Startup.objects.count(), 0)
     self.post(
         "api-startup-list",
         data=get_instance_data(StartupFactory.build()),
     )
     self.response_201()
     self.assertEqual(Startup.objects.count(), 1)
Example #16
0
 def test_list_create(self):
     """Can we create Posts via the POST to list view?"""
     count = Post.objects.count()
     self.post(
         "api-post-list",
         data=remove_m2m(get_instance_data(PostFactory.build())),
     )
     self.response_201()
     self.assertEqual(count + 1, Post.objects.count())
 def test_tag_create_post(self):
     """Can we submit a form to create tags?"""
     with perm_user(self, "organizer.add_tag"):
         self.assertEqual(Tag.objects.count(), 0)
         tag_data = omit_keys("id", get_instance_data(TagFactory.build()))
         response = self.post("tag_create", data=tag_data)
         self.assertEqual(Tag.objects.count(), 1, response.content)
         tag = Tag.objects.get(slug=slugify(tag_data["name"]))
         self.assertRedirects(response, tag.get_absolute_url())
 def test_serialization(self):
     """Does an existing Tag serialize correctly?"""
     tag = TagFactory()
     tag_url = reverse("api-tag-detail", slug=tag.slug, full=True)
     s_tag = TagSerializer(tag, **context_kwarg(tag_url))
     self.assertEqual(
         omit_keys("url", s_tag.data),
         omit_keys("id", get_instance_data(tag)),
     )
     self.assertEqual(s_tag.data["url"], tag_url)
 def test_tag_update_post(self):
     """Can we submit a form to update tags?"""
     tag = TagFactory()
     self.assertNotEqual(tag.name, "django")
     tag_data = omit_keys("id", "name", get_instance_data(tag))
     with perm_user(self, "organizer.change_tag"):
         response = self.post(
             "tag_update",
             slug=tag.slug,
             data=dict(**tag_data, name="django"),
         )
         tag.refresh_from_db()
         self.assertEqual(tag.name, "django", response.content)
         self.assertRedirects(response, tag.get_absolute_url())
Example #20
0
 def test_detail_update(self):
     """Can we update a Startup via PUT?"""
     startup = StartupFactory(name="first")
     url = reverse("api-startup-detail", slug=startup.slug)
     self.put(
         url,
         data={
             **get_instance_data(startup),
             "name": "second",
         },
     )
     self.response_200()
     startup.refresh_from_db()
     self.assertEqual(startup.name, "second")
 def test_deserialization(self):
     """Can we deserialize data to a NewsLink model?"""
     startup_url = reverse(
         "api-startup-detail",
         slug=StartupFactory().slug,
         full=True,
     )
     nl_data = omit_keys(
         "startup",
         get_instance_data(NewsLinkFactory.build()),
     )
     data = dict(**nl_data, startup=startup_url)
     s_nl = NewsLinkSerializer(data=data,
                               **context_kwarg("/api/v1/newslink/"))
     self.assertTrue(s_nl.is_valid(), msg=s_nl.errors)
Example #22
0
 def test_creation(self):
     """Can we save new posts based on input?"""
     tag = TagFactory()
     startup = StartupFactory()
     post = PostFactory.build()
     self.assertFalse(Post.objects.filter(slug=post.slug).exists())
     bounded_form = PostForm(
         data={
             **get_instance_data(post),
             "tags": [tag.pk],
             "startups": [startup.pk],
         })
     self.assertTrue(bounded_form.is_valid(), bounded_form.errors)
     bounded_form.save()
     self.assertTrue(Post.objects.filter(slug=post.slug).exists())
Example #23
0
 def test_slug_validation(self):
     """Do we error if slug conflicts with URL?"""
     conflicts = ["delete", "update", "add_article"]
     startup = StartupFactory()
     for url_path in conflicts:
         with self.subTest(slug=url_path):
             nl_form = NewsLinkForm(
                 dict(
                     get_instance_data(
                         NewsLinkFactory.build()
                     ),
                     slug=url_path,
                     startup=startup.pk,
                 )
             )
             self.assertFalse(nl_form.is_valid())
 def test_update_post(self):
     """Can we submit a form to update startups?"""
     startup = StartupFactory(tags=TagFactory.create_batch(randint(1, 5)))
     self.assertNotEqual(startup.name, "django")
     startup_data = omit_keys("id", get_instance_data(startup))
     with perm_user(self, "organizer.change_startup"):
         response = self.post(
             "startup_update",
             slug=startup.slug,
             data=dict(startup_data, name="django"),
         )
         startup.refresh_from_db()
         self.assertEqual(
             startup.name,
             "django",
             response.content.decode("utf8"),
         )
         self.assertRedirects(response, startup.get_absolute_url())
Example #25
0
 def test_update(self):
     """Can we update posts based on input?"""
     tag = TagFactory()
     startup = StartupFactory()
     post = PostFactory(tags=[tag], startups=[startup])
     self.assertNotEqual(post.title, "django")
     pform = PostForm(
         instance=post,
         data=dict(
             get_instance_data(post),
             title="django",
             tags=[tag.pk],
             startups=[startup.pk],
         ),
     )
     self.assertTrue(pform.is_valid(), pform.errors)
     pform.save()
     post.refresh_from_db()
     self.assertEqual(post.title, "django")
Example #26
0
 def test_update(self):
     """Can we updated newslinks based on input?"""
     startup = StartupFactory()
     newslink = NewsLinkFactory(startup=startup)
     self.assertNotEqual(newslink.title, "django")
     nl_form = NewsLinkForm(
         instance=newslink,
         data=dict(
             omit_keys(
                 "name", get_instance_data(newslink)
             ),
             title="django",
             startups=[startup.pk],
         ),
     )
     self.assertTrue(nl_form.is_valid(), nl_form.errors)
     nl_form.save()
     newslink.refresh_from_db()
     self.assertEqual(newslink.title, "django")
Example #27
0
 def test_update(self):
     """Can we updated startups based on input?"""
     tag = TagFactory()
     startup = StartupFactory(tags=[tag])
     self.assertNotEqual(startup.name, "django")
     sform = StartupForm(
         instance=startup,
         data=dict(
             omit_keys(
                 "name", get_instance_data(startup)
             ),
             name="django",
             tags=[tag.pk],
         ),
     )
     self.assertTrue(sform.is_valid(), sform.errors)
     sform.save()
     startup.refresh_from_db()
     self.assertEqual(startup.name, "django")
 def test_serialization(self):
     """Does an existing NewsLink serialize correctly?"""
     nl = NewsLinkFactory()
     nl_url = f"/api/v1/newslink/{nl.slug}"
     s_nl = NewsLinkSerializer(nl, **context_kwarg(nl_url))
     self.assertNotIn("id", s_nl.data)
     self.assertIn("url", s_nl.data)
     self.assertEqual(
         omit_keys("url", "startup", s_nl.data),
         omit_keys("id", "startup", get_instance_data(nl)),
     )
     self.assertEqual(
         self.client.get(s_nl.data["url"]).status_code,
         200,
     )
     self.assertEqual(
         self.client.get(s_nl.data["startup"]).status_code,
         200,
     )
 def test_update_post(self):
     """Can we submit a form to update newslinks?"""
     startup = StartupFactory()
     newslink = NewsLinkFactory(startup=startup)
     self.assertNotEqual(newslink.title, "django")
     newslink_data = omit_keys("id", get_instance_data(newslink))
     with perm_user(self, "organizer.change_newslink"):
         response = self.post(
             "newslink_update",
             startup_slug=startup.slug,
             newslink_slug=newslink.slug,
             data=dict(newslink_data, title="django"),
         )
         newslink.refresh_from_db()
         self.assertEqual(
             newslink.title,
             "django",
             response.content.decode("utf8"),
         )
         self.assertRedirects(response, newslink.get_absolute_url())
 def test_serialization(self):
     """Does an existing Startup serialize correctly?"""
     tag_list = TagFactory.create_batch(3)
     startup = StartupFactory(tags=tag_list)
     startup_url = reverse(
         "api-startup-detail",
         slug=startup.slug,
         full=True,
     )
     s_startup = StartupSerializer(startup, **context_kwarg(startup_url))
     self.assertEqual(
         omit_keys("url", "tags", s_startup.data),
         omit_keys("id", "tags", get_instance_data(startup)),
     )
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug, full=True)
         for tag in tag_list
     ]
     self.assertCountEqual(s_startup.data["tags"], tag_urls)
     self.assertEqual(s_startup.data["url"], startup_url)