def test_post_invalid_missing_title(self):
        self.users_login('admin@localhost', is_admin=True)
        data = {'body': 'article_body'}

        self.client.post(self.url, data)

        self.assertEqual(Article.all().count(), 0)
class IndexView(BlogMixin, ListView):
    """
    The index page of the site. Contains the body and the titles of
    all the articles.
    """
    template_name = 'index.html'
    queryset = Article.all().order('-created_at')
    def test_user_not_authenticated(self):
        Article(title='title', body='body').put()
        url = reverse('article_admin_delete', kwargs={'id': 1})

        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(Article.all().count(), 1)
    def test_post_valid(self):
        self.users_login('admin@localhost', is_admin=True)
        data = {'title': 'article_title', 'body': 'article_body'}

        resp = self.client.post(self.url, data)

        self.assertRedirects(resp, reverse('index'))
        self.assertEqual(Article.all().count(), 1)
    def test_article_exist(self):
        key = Article(title='title', body='body').put()
        self.users_login('admin@localhost', is_admin=True)
        url = reverse('article_admin_delete', kwargs={'id': key.id()})

        resp = self.client.get(url)

        self.assertRedirects(resp, reverse('index'))
        self.assertEqual(Article.all().count(), 0)
    def test_article_does_not_exist(self):
        Article(key_name='randomkey', title='title', body='body').put()
        self.users_login('admin@localhost', is_admin=True)
        url = reverse('article_admin_delete', kwargs={'id': 1})

        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 404)
        self.assertEqual(Article.all().count(), 1)
    def test_user_not_admin(self):
        Article(title='title', body='body').put()
        self.users_login('user@localhost', is_admin=False)
        url = reverse('article_admin_delete', kwargs={'id': 1})

        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(Article.all().count(), 1)