Ejemplo n.º 1
0
def app():

    app = create_app('testing')

    with app.app_context():

        user = User(username='******',
                    email='*****@*****.**',
                    password=generate_password_hash('testpass')).save()
        user2 = User(username='******',
                     email='*****@*****.**',
                     password=generate_password_hash('sampion')).save()

        post = Post(
            title='hello helloo',
            body='I really think I still messed up, I can never be perfect',
            author=user).save()
        post2 = Post(title='Nataka kuongea na riddy',
                     body='Tafadhali Kaka, kwangu hii ni wrong number',
                     author=user2).save()

    yield app

    db_c = connect('test_clownbot')
    db_c.drop_database('test_clownbot')  #see how to improve on this one
Ejemplo n.º 2
0
def test_update(client, auth, app):
    auth.login()

    with app.app_context():
        post = Post.objects(title='hello helloo').first()

        assert client.get(f'/{post.id}/update').status_code == 200
        client.post(f'/{post.id}/update',
                    data={
                        'title': 'updated',
                        'body': ''
                    })

        post2 = Post.objects(id=post.id).first()
        assert post2['title'] == 'updated'
Ejemplo n.º 3
0
def test_update_validate(client, auth, app):
    auth.login()

    with app.app_context():
        post = Post.objects(title='hello helloo').first()

    response = client.post(f'{post.id}/update', data={'title': '', 'body': ''})
    assert b'Title is required.' in response.data
Ejemplo n.º 4
0
def test_delete(client, auth, app, blog_id):
    auth.login()
    response = client.post(f'/{blog_id}/delete')
    assert response.headers['Location'] == 'http://localhost/'

    with app.app_context():
        post = Post.objects(id=blog_id).first()
        assert post is None
Ejemplo n.º 5
0
def test_create(client, auth, app):
    auth.login()
    assert client.get('/create').status_code == 200
    client.post('/create',
                data={
                    'title': 'created',
                    'body': 'I am creating youuu'
                })

    with app.app_context():
        post = Post.objects(title='created').first()
        assert post
Ejemplo n.º 6
0
def test_author_required(app, client, auth):
    # change the post author to another user
    with app.app_context():
        post = Post.objects(title='hello helloo').first()
        assert post is not None
    auth.login(username='******', password='******')
    # current user can't modify other user's post
    assert client.post(f'/{post.id}/update').status_code == 403
    assert client.post(f'/{post.id}/delete').status_code == 403
    # current user doesn't see edit link
    assert b'href="/' + str(
        post.id).encode() + b'/update"' not in client.get('/').data
Ejemplo n.º 7
0
def test_index(client, auth, app):
    response = client.get('/')
    assert b"Log In" in response.data
    assert b"Register" in response.data

    auth.login()
    response = client.get('/')
    assert b'Log Out' in response.data
    assert b'hello helloo' in response.data
    assert b'by testniqquh on' in response.data
    assert b'Tafadhali Kaka' in response.data
    with app.app_context():
        post = Post.objects(title='hello helloo').first()
        assert b'href="/' + str(
            post.id).encode() + b'/update"' in response.data
Ejemplo n.º 8
0
def blog_id(app):
    with app.app_context():
        post = Post.objects(title='hello helloo').first()
    return post.id