예제 #1
0
    def test_delete_post_wrong_user(self):
        """delete_post should raise error if wrong user attempts to del it."""
        james = UserEntity.register(username='******', password='******')
        kimmy = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        self.assertRaises(myExceptions.EditOthersPosts, a.delete_post, kimmy)
예제 #2
0
    def test_UserEntity_register_same_username(self):
        """
        UserEntity.register should return 'NotUnique'-exception
        if trying to register an account with the same username.
        """

        UserEntity.register(username='******', password='******')
        UserEntity.register(username='******', password='******')
        self.assertRaises(myExceptions.NotUnique,
                          UserEntity.register,
                          username='******',
                          password='******')
예제 #3
0
    def test_delete_post(self):
        """delete_post should delete the post and its comments and votes."""
        james = UserEntity.register(username='******', password='******')
        kimmy = UserEntity.register(username='******', password='******')
        dane = UserEntity.register(username='******', password='******')
        joe = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        b = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='b',
                                         article='content',
                                         created_by=james)

        a.vote(voteBy=kimmy, voteType='up')
        a.vote(voteBy=dane, voteType='up')
        a.vote(voteBy=joe, voteType='down')
        b.vote(voteBy=kimmy, voteType='up')
        b.vote(voteBy=dane, voteType='up')
        b.vote(voteBy=joe, voteType='down')

        a.add_comment(commentBy=kimmy, comment='abc')
        a.add_comment(commentBy=dane, comment='abc')
        a.add_comment(commentBy=joe, comment='123')
        b.add_comment(commentBy=kimmy, comment='abc')
        b.add_comment(commentBy=dane, comment='abc')
        b.add_comment(commentBy=joe, comment='123')

        a.delete_post(james)
        votes = VotesEntity.all()
        for vote in votes:
            self.assertNotEqual(vote.voteOn.title,
                                'a',
                                msg="Votes of post 'a' should be deleted")
        comments = CommentsEntity.all()
        for comment in comments:
            self.assertNotEqual(comment.commentOn.title,
                                'a',
                                msg="Comments of post 'a' should be deleted")

        bloe_entries = BlogEntity.all()
        for blog_entity in bloe_entries:
            self.assertNotEqual(blog_entity.title,
                                'a',
                                msg="Post 'a' should be deleted")
예제 #4
0
    def test_get_comments_on_post(self):
        """get_comments_on_post should return all comments on post."""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        c1 = CommentsEntity.comment_on_post(commentBy=james,
                                            commentOn=a,
                                            comment='abc')

        c2 = CommentsEntity.comment_on_post(commentBy=james,
                                            commentOn=a,
                                            comment='123')

        comments = CommentsEntity.get_comments_on_post(a)
        self.assertEqual(comments.count(), 2, msg='should return 2 comments')
        self.assertEqual(comments[0].comment,
                         c1.comment,
                         msg='should match first comment')
        self.assertEqual(comments[1].comment,
                         c2.comment,
                         msg='should match first comment')
예제 #5
0
 def initialize(self, *a, **kw):
     """Retrieve the user-cookier on every new page-load."""
     webapp2.RequestHandler.initialize(self, *a, **kw)
     username = self.read_secure_cookie('user')
     if username:
         self.user = UserEntity.by_name(username)  # noqa
     else:
         self.user = False  # noqa
예제 #6
0
    def test_edit_blog_entry_wrong_user(self):
        """edit_blog_post should fail if using a title from different post."""
        james = UserEntity.register(username='******', password='******')
        jimmy = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        self.assertRaises(
            myExceptions.EditOthersPosts,
            a.edit_blog_entry,
            title='a',
            article='contents',
            created_by=jimmy,
        )
예제 #7
0
    def test_blog_vote_on_others_post(self):
        """user should be able to vote on other posts"""
        james = UserEntity.register(username='******', password='******')
        john = UserEntity.register(username='******', password='******')
        jimbo = UserEntity.register(username='******', password='******')
        jake = UserEntity.register(username='******', password='******')
        jonas = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a title',
                                         article='a content',
                                         created_by=james)

        a.vote(voteBy=john, voteType='up')
        a.vote(voteBy=jimbo, voteType='down')
        a.vote(voteBy=jake, voteType='up')
        a.vote(voteBy=jonas, voteType='up')

        self.assertEqual(
            a.getVotes(), {
                'up': 3,
                'down': 1
            },
            msg=
            "'getvotes' should return a dict with keyword 'up' and 'down, here with values 3 and 1'"
        )  # noqa

        self.assertEqual(a.getVotesFromUser(john),
                         'up',
                         msg="'voting 'up' should set the vote to 'up'")

        a.vote(voteBy=john, voteType='up')
        self.assertEqual(a.getVotesFromUser(john),
                         None,
                         msg="'Voting 'up' twice should remove the vote")

        self.assertEqual(a.getVotesFromUser(jimbo),
                         'down',
                         msg="'voting 'down' should set the vote to 'down'")

        a.vote(voteBy=jimbo, voteType='down')
        self.assertEqual(a.getVotesFromUser(jimbo),
                         None,
                         msg="'Voting 'down' twice should remove the vote")
예제 #8
0
 def test_UserEntity_register(self):
     """UserEntity.register should create the account, with correct data."""
     UserEntity.register(username='******', password='******').put()
     user = UserEntity.gql("WHERE username = '******'")[0]
     user2 = UserEntity.by_name('Jamie')
     user3 = UserEntity.by_name('Kelly')
     self.assertEqual(user.username, 'Jamie',
                      'gql-query should return a user.')
     self.assertEqual(
         user2.username, 'Jamie',
         'UserEntity.by_name should return a user for username.')
     self.assertEqual(
         user3, None,
         'UserEntity.by_name should return None if the user does not exist')
     self.assertNotEqual(
         user.password, 'password123',
         'userpassword should be hashed, not stored as plain text')
     self.assertEqual(len(user.password), 60,
                      'userpassword should be hashed to 60 characters')
예제 #9
0
    def test_create_blog_entry(self):
        """."""
        james = UserEntity.register(username='******', password='******')

        self.assertEqual(type(
            BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)),
                         BlogEntity,
                         msg="'create_blog_entry' should return a BlogEntity")
예제 #10
0
    def test_blog_add_comment(self):
        """.add_comment should return a CommentEnity if valid"""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a title',
                                         article='a content',
                                         created_by=james)

        self.assertEqual(
            type(a.add_comment(commentBy=james, comment='Awesome')),
            CommentsEntity)
예제 #11
0
    def test_edit_blog_entry_check_data(self):
        """edit_blog_post should fail if using a title from different post."""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        a.edit_blog_entry(title='abc', article='123', created_by=james)
        self.assertEqual(a.title, 'abc', msg=None)
        self.assertEqual(a.article, '123', msg=None)
예제 #12
0
    def test_blog_vote_on_own_post_fail(self):
        """user cannot vote on own post"""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a title',
                                         article='a content',
                                         created_by=james)

        self.assertRaises(myExceptions.VoteOnOwnPostNotAllowed,
                          a.vote,
                          voteBy=james,
                          voteType='up')
예제 #13
0
    def test_edit_comment(self):
        """edit_comment should return a comment with corrected text."""
        james = UserEntity.register(username='******', password='******')
        jane = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        c = CommentsEntity.comment_on_post(commentBy=james,
                                           commentOn=a,
                                           comment='abc')

        new_comment = '123'
        self.assertRaises(myExceptions.EditOthersComments,
                          c.edit_comment,
                          comment=new_comment,
                          commentBy=jane)

        self.assertEquals(
            c.edit_comment(comment=new_comment, commentBy=james).comment,
            new_comment)
예제 #14
0
    def test_comment_on_post(self):
        """comment_on_post should return a CommentEntity."""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        self.assertEqual(
            type(
                CommentsEntity.comment_on_post(commentBy=james,
                                               commentOn=a,
                                               comment='abc')), CommentsEntity)
예제 #15
0
    def post(self):
        """Register the user if form is filled out correctly."""
        username = self.request.get("username")
        email = self.request.get("email")
        password = self.request.get("password")
        verify = self.request.get("verify")

        username_valid = verify_signup.valid_username(username)
        password_valid = verify_signup.valid_password(password)
        passwords_matches = verify_signup.verify_passwords_matches(
            password, verify)
        email_valid = verify_signup.valid_email(email)

        if email == "":
            email_valid = True
        if not (username_valid and password_valid and passwords_matches
                and email_valid):
            self.render(
                "signup.html",
                error_username_invalid=not username_valid,
                error_password_invalid=not password_valid,
                error_passwords_mismatch=not passwords_matches,
                error_email_invalid=not email_valid,
                username=username,
                email=email,
            )  # noqa
        else:
            try:
                UserEntity.register(username, password, email)
                self.perform_login(username)
            except myExceptions.NotUnique:
                self.render(
                    "signup.html",
                    errro_username_already_in_use=True,
                    username=username,
                    email=email,
                )  # noqa
예제 #16
0
    def test_create_blog_entry_with_existing_title(self):
        """BlogEntity.register should fail with a non-unique title."""
        james = UserEntity.register(username='******', password='******')

        BlogEntity.create_blog_entry(parent=blog_key(),
                                     title='a',
                                     article='content',
                                     created_by=james)
        self.assertRaises(
            myExceptions.NotUnique,
            BlogEntity.create_blog_entry,
            parent=blog_key(),
            title='a',
            article='content',
            created_by=james,
        )
예제 #17
0
    def test_get_by_id_str(self):
        """get_by_id_str should return the correct comment."""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        c = CommentsEntity.comment_on_post(commentBy=james,
                                           commentOn=a,
                                           comment='abc')

        comment_id = str(c.key().id())
        self.assertEqual(type(CommentsEntity.get_by_id_str(comment_id)),
                         CommentsEntity,
                         msg=None)
예제 #18
0
    def test_blog_get_comments(self):
        """.get_comment should return a list of all CommentEnities on a post"""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a title',
                                         article='a content',
                                         created_by=james)

        a.add_comment(commentBy=james, comment='test')

        self.assertEqual(
            type(a.get_comments()[0]),
            CommentsEntity,
            msg="'get_comments should return a query of CommentsEntity'")

        self.assertEqual(a.get_comments().count(), 1)

        self.assertEqual(a.get_comments()[0].comment, 'test')
예제 #19
0
    def test_blog_get_by(self):
        """get_by_id_str and by_title should return the correct BlogEntity"""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a title',
                                         article='a content',
                                         created_by=james)

        a_id = str(a.key().id())

        self.assertEqual(BlogEntity.get_by_id_str(a_id).key().id(),
                         a.key().id(),
                         msg="'by_title' should return the 'a'-blog_entry")

        self.assertEqual(
            BlogEntity.by_title('a title').key().id(),
            a.key().id(),
            msg="'get_by_id_str' should return the 'a'-blog_entry")
예제 #20
0
    def post(self):
        """Log in user(set cookie) if user and password matches."""
        username = self.request.get("username")
        password = self.request.get("password")
        remember_me = self.request.get("remember_me")

        valid_login = UserEntity.check_username_password(username, password)

        if valid_login:
            expires = None
            if remember_me:
                expires = 30
            self.perform_login(username, expires)
        else:
            self.render(
                "login.html",
                error_invalid_login=True,
                username=username,
                remember_me=remember_me,
            )  # noqa
예제 #21
0
    def test_edit_blog_entry_with_existing_title(self):
        """edit_blog_post should fail if using a title from different post."""
        james = UserEntity.register(username='******', password='******')

        a = BlogEntity.create_blog_entry(parent=blog_key(),
                                         title='a',
                                         article='content',
                                         created_by=james)

        BlogEntity.create_blog_entry(parent=blog_key(),
                                     title='b',
                                     article='content',
                                     created_by=james)

        self.assertRaises(
            myExceptions.NotUnique,
            a.edit_blog_entry,
            title='b',
            article='contents',
            created_by=james,
        )
예제 #22
0
"""Used for dev-purposes to have some test-data quickly."""
from random import randint
from Entities import BlogEntity, blog_key, UserEntity
import myExceptions

UserEntity.register(username='******', password='******')
UserEntity.register(username='******', password='******')
UserEntity.register(username='******', password='******')
UserEntity.register(username='******', password='******')
UserEntity.register(username='******', password='******')
UserEntity.register(username='******', password='******')
UserEntity.register(username='******', password='******')
UserEntity.register(username='******', password='******')


title_list = [
    'Id ex est cupidatat aliqua reprehenderit magna tempor.',
    'Ea sint fugiat amet nulla reprehenderit nostrud esse deserun.',
    'Pariatur ut consequat cupidatat exercitation ullamco eiusmod officia.',
    'Cillum incididunt ullamco adipisicing aute dolore amet adipisicing.',
    'Ut sit magna duis est eiusmod incididunt deserunt ea elit sunt amet.',
    'Laborum non enim quis fugiat culpa fugiat dolore consequat laboris.',
    'Cupidatat minim enim consequat laboris aute elit et excepteur nisi.',
    'Anim non ipsum labore enim consectetur pariatur laborum officia.',
    'Nisi aliquip tempor qui labore occaecat id deserunt et.',
    'Eu deserunt adipisicing laborum sint labore proident ipsum.',
    'Eiusmod dolore cupidatat pariatur anim deserunt voluptate minim non.',
    'Eiusmod velit proident minim amet exercitation sunt duis.'
]

article_paragraphgs = [
예제 #23
0
 def render(self, template, **kw):
     """Helper function for rendering templates."""
     username = self.read_secure_cookie('user')
     kw['signed_in'] = username
     kw['user'] = UserEntity.by_name(username)
     self.write(self.render_str(template, **kw))