Ejemplo n.º 1
0
    def createData(self):
        """
		Create Post and Comments because reasons
		:return:
		"""
        # Create User to log in
        token = str(uuid.uuid4().hex)
        user = User(username='******', role_id=4, token=token, approved=1)
        user.hash_password('dancingdanche')  # :))

        # Commit User
        db.session.add(user)
        db.session.commit()

        # Make Post
        data = {
            "token": token,
            "title": "Test",
            "body": "Testing 1,2,3",
            "category": "1"
        }

        response = self.app.test_client().post('/posts/api/post/create',
                                               json=data,
                                               content_type='application/json')

        # Create Comment
        data = {"token": token, "body": "Testing 1,2,3", "post_id": 1}

        response = self.app.test_client().post('/posts/api/comment/create',
                                               json=data,
                                               content_type='application/json')
Ejemplo n.º 2
0
def signup():
    """
    <url>/auth/api/signup

    View that signs up a user and returns a token

    """
    username = request.json.get('username').lower()
    password = request.json.get('password')
    if username is None or password is None:
        return jsonify({"error": "A Field is missing!"})

    user = User.query.filter_by(username=username).first()

    if user is not None:
        return jsonify({"error": "User already exists!"})

    token = str(uuid.uuid4().hex)
    user = User(username=username, role_id=4, approved=1, token=token)
    user.hash_password(password)
    db.session.add(user)
    db.session.commit()

    return jsonify({
        'code': 'Registration Succesful!',
        'token': token,
        "userId": user.id
    })
Ejemplo n.º 3
0
    def test_post_creation(self):
        """
        Test post Creation
        """
        token = str(uuid.uuid4().hex)
        user = User(username='******', role_id=4, token=token, approved=1)
        user.hash_password('dancingdanche')  # :))

        db.session.add(user)
        db.session.commit()

        data = {
            "token": token,
            "title": "Test",
            "body": "Testing 1,2,3",
            "category": "1"
        }

        response = self.app.test_client().post('/posts/api/post/create',
                                               json=data,
                                               content_type='application/json')
        data = json.loads(response.data)
        self.assertEqual(data['code'], 'Success')
        self.assertEqual(data['post_id'], 1)

        post = Post.query.filter_by(id=data['post_id']).first()
        self.assertEqual(post.approved, 1)
Ejemplo n.º 4
0
    def test_lists_of_unapproved_posts_invalid_permissions(self):
        """
        Test Lists of Unapproved Posts with invalid permissions
        """
        token = str(uuid.uuid4().hex)
        token2 = str(uuid.uuid4().hex)

        user = User(username='******', role_id=3, token=token, approved=1)
        user2 = User(username='******',
                     role_id=4,
                     token=token,
                     approved=1)
        user.hash_password('randomrandom')  # :))
        user2.hash_password('abetterpasswordbutnotthebest')

        db.session.add_all([user, user2])
        db.session.commit()

        response = self.app.test_client().get(
            '/posts/api/post/list/unapproved?token={}'.format(token),
            content_type='application/json')
        self.assertEqual(json.loads(response.data),
                         {"error": "Access Denied!"})

        response2 = self.app.test_client().get(
            '/posts/api/post/list/unapproved?token={}'.format(token2),
            content_type='application/json')
        self.assertEqual(json.loads(response2.data),
                         {"error": "Access Denied!"})
Ejemplo n.º 5
0
    def test_get_single_post(self):
        """
        Try to get an individual post
        """

        # Create New Account
        token = str(uuid.uuid4().hex)
        user = User(username='******',
                    role_id=4,
                    token=token,
                    approved=1)
        user.hash_password('this_is_a_password')

        db.session.add(user)
        db.session.commit()

        # Create New Post
        data = {
            "token": token,
            "title": "Test",
            "body": "Testing 1,2,3",
            "category": "1"
        }

        writePost = self.app.test_client().post(
            '/posts/api/post/create',
            json=data,
            content_type='application/json')
        postData = json.loads(writePost.data)

        postId = postData['post_id']

        # Test
        response = self.app.test_client().get(
            '/posts/api/post/{}'.format(postId),
            content_type='application/json')
        data = json.loads(response.data)
        print(data)
        self.assertEqual(data['posts'][0]['title'], "Test")
        self.assertEqual(data['posts'][0]['body'], "Testing 1,2,3")
        self.assertEqual(data['posts'][0]['category'], 1)
        self.assertEqual(data['posts'][0]['approved'], 1)
Ejemplo n.º 6
0
    def test_lists_of_unapproved_posts(self):
        """
        Test Lists of Unapproved Posts
        """
        # Create New Account
        token = str(uuid.uuid4().hex)
        user = User(username='******',
                    role_id=1,
                    token=token,
                    approved=1)
        user.hash_password('randomrandom')  # :))

        db.session.add(user)
        db.session.commit()

        response = self.app.test_client().get(
            '/posts/api/post/list/unapproved?token={}'.format(token),
            content_type='application/json')

        data = json.loads(response.data)

        self.assertEqual(data, {'posts': [[], {}]})
Ejemplo n.º 7
0
    def test_comment_creation(self):
        """
        Test comment Creation
        """
        token = str(uuid.uuid4().hex)
        user = User(username='******', role_id=4, token=token, approved=1)
        user.hash_password('todothinkofsomething')  # :))

        db.session.add(user)
        db.session.commit()

        # Make New Post
        dataMakePost = {
            "token": token,
            "title": "Test",
            "body": "Testing 1,2,3",
            "category": "1"
        }

        makePost = self.app.test_client().post('/posts/api/post/create',
                                               json=dataMakePost,
                                               content_type='application/json')
        makePostData = json.loads(makePost.data)

        data = {
            "token": token,
            "body": "Testing 1,2,3",
            "post_id": makePostData['post_id']
        }

        response = self.app.test_client().post('/posts/api/comment/create',
                                               json=data,
                                               content_type='application/json')
        data = json.loads(response.data)
        self.assertEqual(data['code'], 'Success')
        self.assertEqual(data['comment_id'], 1)

        comment = Comment.query.filter_by(id=data['comment_id']).first()
        self.assertEqual(comment.approved, 1)
Ejemplo n.º 8
0
    def setUp(self):
        db.create_all()

        # Create Database Records
        roles = [
            Role(name="Administrator"),
            Role(name="Moderator"),
            Role(name="Volunteer"),
            Role(name="User")
        ]

        user1 = User(username='******',
                     role_id=4,
                     approved=0,
                     token=str(uuid.uuid4().hex))
        user1.hash_password('randompassword')
        user2 = User(username='******',
                     role_id=1,
                     approved=0,
                     token=str(uuid.uuid4().hex))
        user2.hash_password('muzikalnatamilche')
        user3 = User(
            username='******',
            role_id=1,
            approved=1,
            token=str(uuid.uuid4().hex))  # Samo za Aksa ova... Vidi Facebook
        user3.hash_password('krstavicaniedna')
        users = [user1, user2, user3]

        device = DevicesNotificationHandlers(
            user_id=2, notificationToken='ExponentPushToken[NESHO]')
        device1 = DevicesNotificationHandlers(
            user_id=3, notificationToken='ExponentPushToken[NESHO]')
        devices = [device, device1]

        messageCount = MessageCounter()
        chatSessionCount = ChatSessionsCounter()

        db.session.add_all(roles)
        db.session.add(messageCount)
        db.session.add(chatSessionCount)
        db.session.add_all(users)
        db.session.add_all(devices)

        db.session.commit()
Ejemplo n.º 9
0
def init_db():
    """
    Initialize Simple Database for starting a new instance
    """
    # Create Db
    db.drop_all()
    print('Creating database...')
    db.create_all()

    # Create Roles
    print('Creating Roles...')
    roles = [Role(name="Administrator"), Role(name="Moderator"), Role(name="Volunteer"), Role(name="User")]
    db.session.add_all(roles)

    # Create Basic Users
    print('Creating Users...')

    username = '******'
    password = '******'

    user1 = User(username=username, role_id=4, approved=0, token=str(uuid.uuid4().hex))
    user1.hash_password(password)

    print('Created User with username: {} and Password: {}'.format(username, password))

    username = '******'
    letters = string.ascii_lowercase
    password = ''.join(random.choice(letters) for i in range(15))

    user2 = User(username=username, role_id=1, approved=0, token=str(uuid.uuid4().hex))
    user2.hash_password(password)

    users = [user1, user2]

    db.session.add_all(users)
    db.session.commit()

    print('Created Admin User with username: {} and Password: {}'.format(username, password))

    # Create Posts, Comments

    print('Creating Post...')
    test = 'Hello World!'

    post = Post(author_id=1, title=test, body=test, category=1, approved=1, likes=0)

    db.session.add(post)
    db.session.commit()

    print('Creating Comment...')

    comment = Comment(author_id=1, body=test, approved=1, likes=0)

    db.session.add(comment)
    db.session.commit()

    # Create Counts

    print('Creating Message Counter and Chat Session Counter...')

    messageCount = MessageCounter()
    chatSessionCount = ChatSessionsCounter()
    db.session.add(messageCount)
    db.session.add(chatSessionCount)

    # Register Notification Token

    print('Registering Notification Token for user...')

    device = DevicesNotificationHandlers(user_id=1, notificationToken='ExponentPushToken[NESHO]')
    db.session.add(device)
    db.session.commit()

    print('Sucesfully Filled out Database!')