def test_add_user_already_exists(session, client):
    args = [session, 'hudson', '*****@*****.**', 'chip', client]
    response1 = add_user_via_endpoint(*args)
    assert len(list(User.query.all())) == 1

    data = add_user_via_endpoint(*args)
    assert data['message'] == 'user name already taken'
    assert data['token'] == None
def test_cant_make_user_with_out_username_or_password(session, client):
    args = [session, '', '*****@*****.**', 'blank', client]
    data = add_user_via_endpoint(*args)
    assert data['message'] == 'There is a field missing'
    assert data['loggedIn'] == False
    assert data['token'] == None

    args = [session, 'hudsaphone', '*****@*****.**', '', client]
    data = add_user_via_endpoint(*args)
    assert data['message'] == 'There is a field missing'
    assert data['loggedIn'] == False
def test_password_gets_hashed_via_endpoint(session, client):
    args = [session, 'hudson', '*****@*****.**', 'chips', client]
    data = add_user_via_endpoint(*args)
    u = User.query.filter_by(username='******')[0]
    password_hash = u.password_hash
    assert 'chips' != password_hash
    assert u.email == '*****@*****.**'
    assert u.check_password('chips') == True
def test_add_user(app, client, session):
    args = [session, 'potato', '*****@*****.**', 'chips', client]
    data = add_user_via_endpoint(*args)
    assert User.query.first().username == 'potato'
    assert User.query.first().email == '*****@*****.**'

    assert data.get('message') == 'success'
    assert data.get('loggedIn') == True
def test_can_get_user_id_from_token(session, client):
    args = [session, 'hudson', '*****@*****.**', 'chip', client]
    data = add_user_via_endpoint(*args)
    tok = data['token']
    u = User.query.first()
    userid = u.id
    user_through_token = User.verify_auth_token(tok)
    assert userid == user_through_token.id
    assert u.username == user_through_token.username
    assert u.email == user_through_token.email
def test_login_required_bad_signature(client, session):
    user_json = add_user_via_endpoint(session, 'hudson', '*****@*****.**',
                                      'adfa', client)
    token = user_json.get('token')
    assert token is not None
    response = post_to_test_login_decorator(client,
                                            dict(Authorization=token + 'a'))
    assert response.status_code == 401
    data = response.get_json()
    assert data.get('message') == 'please login again'
    assert data.get('status') == 'invalid token'
def test_logindecorator_token_valid(client, session):
    user_json = add_user_via_endpoint(session, 'hudson', '*****@*****.**',
                                      'adfa', client)
    token = user_json.get('token')
    user_id = User.query.first().id
    assert token is not None

    response = post_to_test_login_decorator(client, dict(Authorization=token))
    assert response.status_code == 200
    data = response.get_json()
    assert data.get('message') == 'working'
    assert data.get('user') == user_id
def test_create_recipe_list_with_recipes(client,session):
    create_data = [session, 'hudson', '*****@*****.**','asdfad', client]
    user_json = add_user_via_endpoint(*create_data)
    token = user_json.get('token')
    user_id = User.query.first().id
    recipeList = [
        {
            'name': 'recipe1',
            'url': 'http://blah.com/thisandthat',
            'pic_url': 'http://fakeurl.com/recipe234',
            'id': 123
        },
        {
            'name': 'recipe1',
            'url': 'http://blah.com/thisandthat',
            'pic_url': 'http://fakeurl.com/recipe234',
            'id': 134
        },
        {
            'name': 'recipe1',
            'url': 'http://blah.com/thisandthat',
            'pic_url': 'http://fakeurl.com/recipe234',
            'id': 145
        },
    ]

    response = client.post(
                    url_for('users.create_recipe_list'),
                    data=json.dumps({
                        'recipeListName': 'test list',
                        'recipes': recipeList
                    }),
                    content_type='application/json',
                    headers={'Authorization': token}
    )

    assert response.status_code == 200
    data = response.get_json()
    assert data.get('status') == 'success'
    assert data.get('message') == 'hudson added test list'
    recipe_list = RecipeList.query.first()
    assert recipe_list.list_name == 'test list'
    assert recipe_list.list_creator == user_id
    assert recipe_list.id == data.get('recipeListId')
    assert len(list(Recipes.query.all())) == 3
    assert Recipes.query.first().added_by == user_id
    assert Recipes.query.first().recipe_id == 123
def test_create_recipe_endpoint_no_token(client, session):
    create_data = [session, 'hudson', '*****@*****.**','asdfad', client]
    user_json = add_user_via_endpoint(*create_data)
    token = user_json.get('token')
    user_id = User.query.first().id

    recipeList = [
        {
            'name': 'recipe1',
            'url': 'http://blah.com/thisandthat',
            'pic_url': 'http://fakeurl.com/recipe234',
            'id': 123
        },
        {
            'name': 'recipe1',
            'url': 'http://blah.com/thisandthat',
            'pic_url': 'http://fakeurl.com/recipe234',
            'id': 134
        },
        {
            'name': 'recipe1',
            'url': 'http://blah.com/thisandthat',
            'pic_url': 'http://fakeurl.com/recipe234',
            'id': 145
        },
    ]

    response = client.post(
                    url_for('users.create_recipe_list'),
                    data=json.dumps({
                        'recipeListName': 'test list',
                        'recipes': recipeList
                    }),
                    content_type='application/json',
                    headers={}
    )
    assert response.status_code == 401
    data = response.get_json()
    assert data.get('status') == 'unauthorized'
    assert data.get('message') == 'must be logged in'
def test_create_recipe_list_endpoint(client, session):
    create_data = [session, 'hudson', '*****@*****.**','asdfad', client]
    user_json = add_user_via_endpoint(*create_data)
    token = user_json.get('token')
    user_id = User.query.first().id

    response = client.post(
                    url_for('users.create_recipe_list'),
                    data=json.dumps({
                        'recipeListName': 'test list',
                        'recipes': []
                    }),
                    content_type='application/json',
                    headers={'Authorization': token}
    )

    assert response.status_code == 200
    data = response.get_json()
    assert data.get('status') == 'success'
    assert data.get('message') == 'hudson added test list'
    recipe_list = RecipeList.query.first()
    assert recipe_list.list_name == 'test list'
    assert recipe_list.list_creator == user_id
    assert recipe_list.id == data.get('recipeListId')