def test_admin_user(self): """ Happy path: - Owner logged in - Short version of article in response (truncated body) - Post created as owner - Post actually saved :) """ self.users_login('owner@localhost', is_admin=True) data = { "title": "some title", "body": "ABC " * 123, # long body } expected_body = ( "ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC " "ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC " "ABC ABC A..." ) response = self.client.post(self.url, data) self.assertEquals(response.status_code, 200) self.assertContains(response, "some title") self.assertContains(response, expected_body) self.assertContains(response, "Written by owner@localhost") self.assertNotEquals(Post.get_by_slug("some-title"), None)
def test_post_by_slug(self): post = Post(title=u"New post") post.put() by_slug = Post.get_by_slug("new-post") self.assertEquals(by_slug, post)
def test_post_by_slug_duplidate_post(self): Post(title=u"New post").put() Post(title=u"New post").put() by_slug = Post.get_by_slug("new-post") self.assertEquals(by_slug, None)
def post(self, request, slug=None, *args, **kwargs): if not users.is_current_user_admin(): return HttpResponseForbidden() post = self.get_object() if not post and slug: raise Http404() form = PostForm(request.POST) if not form.validate(): error_msg = [ "There are problems with the form:", ] error_msg = itertools.chain(error_msg, *form.errors.values()) return HttpResponseBadRequest("<br/>".join(error_msg)) # title has to exit at that point due to the validators new_slug = slugify(form.data["title"]) if slug is None and Post.get_by_slug(new_slug): return HttpResponseBadRequest("Post with this title alread exit") created = False if not post: created = True post = Post() form.populate_obj(post) post.author = users.get_current_user().nickname() post.put() context = self.get_context_data(post=post, short=created) return self.render_to_response(context)
def test_admin_user(self): """ Happy path: - Owner logged in - Full version of article in response - Post created as owner - Post actually updated (same timestamps) """ self.users_login('owner2@localhost', is_admin=True) data = { "title": "new title", "body": "ABC" * 100, } response = self.client.post(self.post.url, data) updated_post = Post.get_by_slug("new-title") self.assertEquals(response.status_code, 200) self.assertContains(response, "new title") self.assertContains(response, "ABC" * 100) self.assertContains(response, "Written by owner2@localhost") self.assertEquals(updated_post.created_at, self.post.created_at)
def test_post_by_slug_incorrect_slug(self): by_slug = Post.get_by_slug("some-slug") self.assertEquals(by_slug, None)
def test_post_by_slug_no_slug(self): by_slug = Post.get_by_slug(None) self.assertEquals(by_slug, None)
def get_object(self): slug = self.kwargs.get("slug") return Post.get_by_slug(slug)