# -*- coding: utf-8 -*-
"""
    Flaskr Tests
    ~~~~~~~~~~~~

    Tests the Flaskr application.

    :copyright: (c) 2015 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
"""

import os
import tempfile
import pytest
from flaskr.factory import create_app
from flaskr.blueprints.flaskr import init_db


@pytest.fixture
def app(request):

    db_fd, temp_db_location = tempfile.mkstemp()
    config = {
        'DATABASE': temp_db_location,
        'TESTING': True,
        'DB_FD': db_fd
    }

    app = create_app(config=config)

    with app.app_context():
        init_db()
        yield app


@pytest.fixture
def client(request, app):

    client = app.test_client()

    def teardown():
        os.close(app.config['DB_FD'])
        os.unlink(app.config['DATABASE'])
    request.addfinalizer(teardown)

    return client


def login(client, username, password):
    return client.post('/login', data=dict(
        username=username,
        password=password
    ), follow_redirects=True)


def logout(client):
    return client.get('/logout', follow_redirects=True)


def test_empty_db(client):
    """Start with a blank database."""
    rv = client.get('/')
    assert b'No entries here so far' in rv.data


def test_login_logout(client, app):
    """Make sure login and logout works"""
    rv = login(client, app.config['USERNAME'],
               app.config['PASSWORD'])
    assert b'You were logged in' in rv.data
    rv = logout(client)
    assert b'You were logged out' in rv.data
    rv = login(client,app.config['USERNAME'] + 'x',
               app.config['PASSWORD'])
    assert b'Invalid username' in rv.data
    rv = login(client, app.config['USERNAME'],
               app.config['PASSWORD'] + 'x')
    assert b'Invalid password' in rv.data


def test_messages(client, app):
    """Test that messages work"""
    login(client, app.config['USERNAME'],
          app.config['PASSWORD'])
    rv = client.post('/add', data=dict(
        title='<Hello>',
        text='<strong>HTML</strong> allowed here'
    ), follow_redirects=True)
    assert b'No entries here so far' not in rv.data
    assert b'&lt;Hello&gt;' in rv.data
    assert b'<strong>HTML</strong> allowed here' in rv.data

@@ -342,19 +342,11 @@
 app(
-request
 ):%0A
-%0A
     
@@ -352,32 +352,23 @@
  db_fd, 
-temp_db_location
+db_path
  = tempf
@@ -420,24 +420,15 @@
 E': 
-temp_db_location
+db_path
 ,%0A  
@@ -454,38 +454,14 @@
 ue,%0A
-        'DB_FD': db_fd%0A
     %7D%0A
-%0A
     
@@ -562,219 +562,85 @@
 pp%0A%0A
-%0A@pytest.fixture%0Adef client(request, app):%0A%0A    client = app.test_client()%0A%0A    def teardown():%0A        os.close(app.config%5B'DB_FD'%5D)%0A        os.unlink(app.config%5B'DATABASE'%5D)%0A    request.addfinalizer(teardown)%0A
+    os.close(db_fd)%0A    os.unlink(db_path)%0A%0A%0A@pytest.fixture%0Adef client(app):
 %0A   
@@ -647,22 +647,33 @@
  return 
+app.test_
 client
+()
 %0A%0A%0Adef l

fix windows failure to remove temp file