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_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 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())
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"), )
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_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_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())
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 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_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)
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())
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)
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())
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)
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")
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_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_create_post(self): """Can we submit a form to create Startups?""" startup_num = Startup.objects.count() tag = TagFactory() startup_data = { **omit_keys( "id", get_instance_data(StartupFactory.build()), ), "tags": [tag.pk], } with perm_user(self, "organizer.add_startup"): response = self.post("startup_create", data=startup_data) self.assertEqual( Startup.objects.count(), startup_num + 1, response.content.decode("utf8"), ) startup = Startup.objects.get(slug=startup_data["slug"]) self.assertIn(tag, startup.tags.all()) self.assertRedirects(response, startup.get_absolute_url())
def get_tag_data(tag): """Strip unchecked fields from Tag""" return omit_keys("id", get_instance_data(tag))
def get_startup_data(startup): """Strip unchecked fields from Startup""" return omit_keys("id", "tags", get_instance_data(startup))
def get_newslink_data(newslink): """Strip unchecked fields from NewsLink""" return omit_keys("id", "startup", get_instance_data(newslink))
def get_post_data(post): """Strip post of unchecked fields""" return omit_keys( "id", "tags", "startups", get_instance_data(post) )