import os
import tempfile
import io
import pytest


from models import setup_demo_db
from fras_app import create_app
app = create_app()

@pytest.fixture
def client():
    db_fd, app.config['DATABASE'] = tempfile.mkstemp()
    app.config['TESTING'] = True

    with app.test_client() as client:
        with app.app_context():
            setup_demo_db()
        yield client

    os.close(db_fd)
    os.unlink(app.config['DATABASE'])
    os.remove("fras_data.db")


login_cred_admin = {
    "login_id": 'admin',
    "password": '<PASSWORD>'
}
login_cred_staff = {
    "login_id": 'test',
    "password": '<PASSWORD>'
}
def test_login_render(client):
    """Testing Login Rendered Template
    """
    rv = client.get('/fras/app/index.html#/login')
    assert rv.status_code == 200

def test_login_admin(client):
    """Testing Login API for admin user
    {'body': {'nav': {'links': [], 'menus': {'Attendance':
    [{'href': '#/att.detail', 'label': 'View Attendance'},
    {'href': '#/att.mark', 'label': 'Mark Attendance'}],
    'Manage Users': [{'href': '#/kf.find', 'label': 'Known Faces'},
    {'href': '#/kf.detail', 'label': 'Add Face'},
    {'href': '#/add.users', 'label': 'Bulk Upload Users'}]}},
    'user': {'id': 2, 'login_id': 'admin',
    'name': 'ADMIN USER', 'role': 'SU', 'role_name': 'Superuser'}},
    'status': 'OK'}
    """
    # rv = client.get('/fras/app/index.html#/login')
    # assert rv.status_code == 200

    rv = client.post('/fras/app/login', json=login_cred_admin)
    json_data = rv.get_json()
    #print(json_data["body"]["user"])
    #print(rv.body)
    assert json_data["body"]["user"]["id"] == 2
    assert json_data["body"]["user"]["login_id"] == 'admin'
    assert json_data["body"]["user"]["name"] == 'ADMIN USER'
    assert json_data["body"]["user"]["role"] == "SU"
    assert json_data["body"]["user"]["role_name"] == 'Superuser'
    assert rv.status_code == 200

def test_login_staff(client):
    """Testing Login API for
    """
    rv = client.post('/fras/app/login', json=login_cred_staff)
    json_data = rv.get_json()
    print(json_data["body"]["user"])
    assert json_data["body"]["user"]["id"] == 1
    assert json_data["body"]["user"]["login_id"] == 'test'
    assert json_data["body"]["user"]["name"] == 'TEST USER'
    assert json_data["body"]["user"]["role"] == "ST"
    assert json_data["body"]["user"]["role_name"] == 'Staff'
    assert rv.status_code == 200

def test_login_invalid(client):
    """Testing Login API for invalid login
    """
    rv = client.post('/fras/app/login', json={
    "login_id": 'random',
    "password": '<PASSWORD>'
    })
    assert "Invalid user/password." in str(rv.data)

def test_login_parameter_missing(client):
    """Testing Login API for mission parameters
    """
    rv = client.post('/fras/app/login', json={})
    assert "Invalid user/password." in str(rv.data)



def test_logout(client):
    """Testing Logout API for admin user
    """
    client.post('/fras/app/login', json=login_cred_admin)
    rv = client.post('/fras/app/logout', json=login_cred_admin)
    json_data = rv.get_json()
    print(json_data["body"])
    assert "Logged out." in str(rv.data)


def test_current_user(client):
    """Testing Login API fro admin user

    """

    client.post('/fras/app/login', json=login_cred_admin)
    rv = client.get('/fras/app/current_user')
    json_data = rv.get_json()
    assert json_data["body"]["user"]["id"] == 2
    assert json_data["body"]["user"]["login_id"] == 'admin'
    assert json_data["body"]["user"]["name"] == 'ADMIN USER'
    assert json_data["body"]["user"]["role"] == "SU"
    assert json_data["body"]["user"]["role_name"] == 'Superuser'

    assert rv.status_code == 200


def test_users_upload(client):
    """Testing Login API fro admin user

    """

    client.post('/fras/app/login', json=login_cred_admin)

    data = dict()
    data['files'] = {"users_file":(io.BytesIO(b"abcdef"), 'test.jpg')}

    rv = client.post('/fras/app/users_upload',data=data, follow_redirects=True,
        content_type='multipart/form-data')
    json_data = rv.get_json()
    print(json_data)
    ## doesnt work
    assert rv.status_code == 200

import os
import tempfile
import io
import pytest


from models import setup_demo_db
from fras_app import create_app
app = create_app()

@pytest.fixture
def client():
    db_fd, app.config['DATABASE'] = tempfile.mkstemp()
    app.config['TESTING'] = True

    with app.test_client() as client:
        with app.app_context():
            setup_demo_db()
        yield client

    os.close(db_fd)
    os.unlink(app.config['DATABASE'])
    os.remove("fras_data.db")


login_cred_admin = {
    "login_id": 'admin',
    "password": '<PASSWORD>'
}
login_cred_staff = {
    "login_id": 'test',
    "password": '<PASSWORD>'
}
def test_login_render(client):
    """Testing Login Rendered Template
    """
    rv = client.get('/fras/app/index.html#/login')
    assert rv.status_code == 200

def test_login_admin(client):
    """Testing Login API for admin user
    {'body': {'nav': {'links': [], 'menus': {'Attendance':
    [{'href': '#/att.detail', 'label': 'View Attendance'},
    {'href': '#/att.mark', 'label': 'Mark Attendance'}],
    'Manage Users': [{'href': '#/kf.find', 'label': 'Known Faces'},
    {'href': '#/kf.detail', 'label': 'Add Face'},
    {'href': '#/add.users', 'label': 'Bulk Upload Users'}]}},
    'user': {'id': 2, 'login_id': 'admin',
    'name': 'ADMIN USER', 'role': 'SU', 'role_name': 'Superuser'}},
    'status': 'OK'}
    """
    # rv = client.get('/fras/app/index.html#/login')
    # assert rv.status_code == 200

    rv = client.post('/fras/app/login', json=login_cred_admin)
    json_data = rv.get_json()
    #print(json_data["body"]["user"])
    #print(rv.body)
    assert json_data["body"]["user"]["id"] == 2
    assert json_data["body"]["user"]["login_id"] == 'admin'
    assert json_data["body"]["user"]["name"] == 'ADMIN USER'
    assert json_data["body"]["user"]["role"] == "SU"
    assert json_data["body"]["user"]["role_name"] == 'Superuser'
    assert rv.status_code == 200

def test_login_staff(client):
    """Testing Login API for
    """
    rv = client.post('/fras/app/login', json=login_cred_staff)
    json_data = rv.get_json()
    print(json_data["body"]["user"])
    assert json_data["body"]["user"]["id"] == 1
    assert json_data["body"]["user"]["login_id"] == 'test'
    assert json_data["body"]["user"]["name"] == 'TEST USER'
    assert json_data["body"]["user"]["role"] == "ST"
    assert json_data["body"]["user"]["role_name"] == 'Staff'
    assert rv.status_code == 200

def test_login_invalid(client):
    """Testing Login API for invalid login
    """
    rv = client.post('/fras/app/login', json={
    "login_id": 'random',
    "password": '<PASSWORD>'
    })
    assert "Invalid user/password." in str(rv.data)

def test_login_parameter_missing(client):
    """Testing Login API for mission parameters
    """
    rv = client.post('/fras/app/login', json={})
    assert "Invalid user/password." in str(rv.data)



def test_logout(client):
    """Testing Logout API for admin user
    """
    client.post('/fras/app/login', json=login_cred_admin)
    rv = client.post('/fras/app/logout', json=login_cred_admin)
    json_data = rv.get_json()
    print(json_data["body"])
    assert "Logged out." in str(rv.data)


def test_current_user(client):
    """Testing Login API fro admin user

    """

    client.post('/fras/app/login', json=login_cred_admin)
    rv = client.get('/fras/app/current_user')
    json_data = rv.get_json()
    assert json_data["body"]["user"]["id"] == 2
    assert json_data["body"]["user"]["login_id"] == 'admin'
    assert json_data["body"]["user"]["name"] == 'ADMIN USER'
    assert json_data["body"]["user"]["role"] == "SU"
    assert json_data["body"]["user"]["role_name"] == 'Superuser'

    assert rv.status_code == 200


def test_users_upload(client):
    """Testing Login API fro admin user

    """

    client.post('/fras/app/login', json=login_cred_admin)

    data = dict()
    data['files'] = {"users_file":(io.BytesIO(b"abcdef"), 'test.jpg')}

    rv = client.post('/fras/app/users_upload',data=data, follow_redirects=True,
        content_type='multipart/form-data')
    json_data = rv.get_json()
    print(json_data)
    ## doesnt work
    assert rv.status_code == 200

Testing Login Rendered Template #/login') Testing Login API for admin user     {'body': {'nav': {'links': [], 'menus': {'Attendance':     [{'href': '#/att.detail', 'label': 'View Attendance'},     {'href': '#/att.mark', 'label': 'Mark Attendance'}],     'Manage Users': [{'href': '#/kf.find', 'label': 'Known Faces'},     {'href': '#/kf.detail', 'label': 'Add Face'},     {'href': '#/add.users', 'label': 'Bulk Upload Users'}]}},     'user': {'id': 2, 'login_id': 'admin',     'name': 'ADMIN USER', 'role': 'SU', 'role_name': 'Superuser'}},     'status': 'OK'} # rv = client.get('/fras/app/index.html#/login') # assert rv.status_code == 200 #print(json_data["body"]["user"]) #print(rv.body) Testing Login API for Testing Login API for invalid login Testing Login API for mission parameters Testing Logout API for admin user Testing Login API fro admin user Testing Login API fro admin user ## doesnt work