Beispiel #1
2
def create_user():
    """Create a sample User."""
    faker = Faker()
    user = User.objects.create_user(
        username=faker.pronounceable_unique_id(length=30),
        first_name=faker.word().title(),
        last_name=faker.word().title(),
        email=faker.email(),
        password=faker.password())
    return user
Beispiel #2
0
 def setUp(self):
     faker = Faker()
     author = create_user()
     self.page = Page(
         author=author, body=faker.paragraphs(),
         title=faker.words().capitalize())
     self.page.save()
Beispiel #3
0
 def setUp(self):
     faker = Faker()
     author = create_user()
     self.metrics = Metrics(
         author=author, code=faker.paragraphs(),
         name=faker.words(words=2).capitalize(),
         position="SITE_BOTTOM")
     self.metrics.save()
Beispiel #4
0
 def test_drafts_are_not_displayed(self):
     faker = Faker()
     post = Post(
         author=create_user(), body=faker.paragraphs(),
         title=faker.words().capitalize(), is_draft=True)
     post.save()
     PostListView.paginate_by = self.post_count + 1
     response = self.client.get(reverse(POST_LIST_URL))
     self.assertEqual(len(response.context['object_list']), self.post_count)
Beispiel #5
0
 def setUp(self):
     self.post_count = 2
     PostListView.paginate_by = 1
     faker = Faker()
     author = create_user()
     for i in xrange(self.post_count):
         post = Post(
             author=author, body=faker.paragraphs(),
             title=faker.words().capitalize())
         post.save()
Beispiel #6
0
 def setUp(self):
     faker = Faker()
     self.body = faker.paragraphs(paragraphs=2, sentences=2)
     self.guest_name = faker.words(words=2).title()
     create_user()
     self.post = create_post()
     self.post.save()
     self.data = {
         'post': self.post.id,
         'body': self.body}
Beispiel #7
0
def create_post():
    """Create a Post sample."""
    faker = Faker()
    date = faker.datetime()
    post = Post(
        author=User.objects.order_by('?')[0],
        body=faker.paragraphs(),
        title=faker.words().capitalize(),
        created_at=date,
        modified_at=date)
    return post
Beispiel #8
0
 def test_save_new_model(self):
     user = create_user()
     faker = Faker()
     form = self.metricsadmin.get_form(None)
     form.base_fields['name'] = faker.words(words=2).capitalize()
     form.base_fields['code'] = faker.paragraphs()
     form.base_fields['position'] = "SITE_BOTTOM"
     metrics = Metrics()
     request = HttpRequest()
     request.user = user
     self.metricsadmin.save_model(request, metrics, form, True)
     self.assertEqual(metrics.author, user)
Beispiel #9
0
def create_page():
    """Create a sample Page."""
    faker = Faker()
    date = faker.datetime()
    page = Page(
        author=User.objects.order_by("?")[0],
        body=faker.paragraphs(),
        title=faker.words(words=1).capitalize(),
        created_at=date,
        modified_at=date,
    )
    return page
Beispiel #10
0
 def test_save_new_model(self):
     user = create_user()
     faker = Faker()
     form = self.pageadmin.get_form(None)
     form.base_fields['title'] = faker.words().capitalize()
     form.base_fields['body'] = faker.paragraphs()
     form.base_fields['is_draft'] = True
     page = Page()
     request = HttpRequest()
     request.user = user
     self.pageadmin.save_model(request, page, form, True)
     self.assertEqual(page.author, user)
Beispiel #11
0
def create_comment():
    """Create a sample comment."""
    faker = Faker()
    coin = randint(0, 1)
    author = User.objects.order_by('?')[0] if coin else None
    guest_name = None if coin else faker.words(words=2).title()
    comment = Comment(
        post=Post.objects.order_by('?')[0],
        author=author,
        guest_name=guest_name,
        body=faker.paragraphs(paragraphs=2, sentences=2),
        created_at=faker.datetime())
    return comment
Beispiel #12
0
 def test_save_existing_model_by_different_user(self):
     author = create_user()
     faker = Faker()
     page = Page(
         author=author,
         title=faker.words().capitalize(),
         body=faker.paragraphs())
     page.save()
     form = self.pageadmin.get_form(page)
     user = create_user()
     request = HttpRequest()
     request.user = user
     self.pageadmin.save_model(request, page, form, False)
     self.assertNotEqual(page.author, user)
     self.assertGreater(page.modified_at, page.created_at)
Beispiel #13
0
 def setUp(self):
     self.faker = Faker()
     self.author = create_user()
     self.post = create_post()
     self.post.save()
     self.body = self.faker.paragraphs(paragraphs=2, sentences=2)
     self.guest_name = self.faker.words(words=2).title()
Beispiel #14
0
 def test_save_existing_model_by_different_user(self):
     author = create_user()
     faker = Faker()
     metrics = Metrics(
         author=author,
         name=faker.words(words=2).capitalize(),
         code=faker.paragraphs(),
         position="SITE_BOTTOM")
     metrics.save()
     form = self.metricsadmin.get_form(metrics)
     user = create_user()
     request = HttpRequest()
     request.user = user
     self.metricsadmin.save_model(request, metrics, form, False)
     self.assertNotEqual(metrics.author, user)
     self.assertGreater(metrics.modified_at, metrics.created_at)
Beispiel #15
0
class CreateCommentFormTest(TestCase):
    _multiprocess_can_split_ = True

    class AuthenticatedUser(User):
        def is_authenticated(self):
            return True

    def setUp(self):
        self.faker = Faker()
        self.body = self.faker.paragraphs(paragraphs=2, sentences=2)
        self.guest_name = self.faker.words(words=2).title()
        self.author = create_user()
        self.post = create_post()
        self.post.save()

    def test_guest_name_has_to_be_present_for_anonymous_user(self):
        data = {
            'body': self.body,
            'post': self.post.id}
        form = CommentForm(data=data)
        self.assertFalse(form.is_valid())
        self.assertIn(_("This field is required."), form.errors['guest_name'])

    def test_guest_name_is_not_required_for_logged_in_user(self):
        request = HttpRequest()
        request.user = self.AuthenticatedUser(
            username=self.faker.pronounceable_unique_id(length=30),
            password=self.faker.password(),
            email=self.faker.email())
        data = {
            'body': self.body,
            'post': self.post.id}
        form = CommentForm(data=data, request=request)
        self.assertTrue(form.is_valid())

    def test_save_anonymous_user(self):
        data = {
            'body': self.body,
            'post': self.post.id,
            'guest_name': self.guest_name}
        form = CommentForm(data=data)
        self.assertTrue(form.is_valid())
        comment = form.save()
        self.assertEqual(comment.body, self.body)
        self.assertEqual(comment.post, self.post)
        self.assertEqual(comment.guest_name, self.guest_name)
        self.assertLessEqual(comment.created_at, timezone.now())
Beispiel #16
0
class CommentModelTest(TestCase):
    _multiprocess_can_split_ = True

    def setUp(self):
        self.faker = Faker()
        self.author = create_user()
        self.post = create_post()
        self.post.save()
        self.body = self.faker.paragraphs(paragraphs=2, sentences=2)
        self.guest_name = self.faker.words(words=2).title()

    def test_model_properties(self):
        comment = Comment(
            post=self.post, author=self.author,
            guest_name=self.guest_name, body=self.body)
        self.assertEqual(comment.body, self.body)
        self.assertEqual(comment.author, self.author)
        self.assertEqual(comment.guest_name, self.guest_name)

    def test_model_unicode_is_a_part_of_comment_body(self):
        comment = Comment(
            post=self.post, author=self.author,
            guest_name=self.guest_name, body=self.body)
        self.assertIn(comment.__unicode__()[:-3], self.body)
        self.assertLessEqual(len(comment.__unicode__()), 80)

    def test_model_unicode_trims_spaces_and_punctuation(self):
        word = self.faker.word()
        punctuation = ' .'
        comment = Comment(
            post=self.post, author=self.author,
            guest_name=self.guest_name,
            body=word + punctuation * (80 / len(punctuation)))
        self.assertEqual(comment.__unicode__()[:-3], word)

    def test_get_author_returns_present_author_or_guest_name(self):
        cases = [(self.author, None), (None, self.guest_name)]
        for (author, guest_name) in cases:
            comment = Comment(
                post=self.post, author=author,
                guest_name=guest_name, body=self.body)
            self.assertEqual(comment.get_author(), author or guest_name)
Beispiel #17
0
 def wrapper(self):
     faker = Faker()
     username = faker.pronounceable_unique_id(length=30)
     password = faker.password()
     user = User.objects.create_user(
         username=username,
         first_name=faker.word().title(),
         last_name=faker.word().title(),
         email=faker.email(),
         password=password)
     user.save()
     self.client.login(username=username, password=password)
     return func(self)
Beispiel #18
0
 def setUp(self):
     faker = Faker()
     self.author = create_user()
     self.code = faker.paragraphs()
     self.name = faker.words(words=4).capitalize()
     self.position = "SITE_BOTTOM"
Beispiel #19
0
 def setUp(self):
     faker = Faker()
     self.author = create_user()
     self.body = faker.paragraphs()
     self.title = faker.words().capitalize()