def test_multipe_users_can_share_recipe_list(session, client): user1 = add_user(session) user2 = add_user(session, 'potato', '*****@*****.**', 'asdfa') recipe_list = add_recipe_list(session, user1, 'test list') user2.recipe_lists.append(recipe_list) assert RecipeList.query.count() == 1 assert user1 in RecipeList.query.first().users assert user2 in RecipeList.query.first().users token_u1 = user1.generate_auth_token() response_u1 = client.get( url_for('users.get_recipe_lists'), headers={'Authorization': token_u1} ) assert response_u1 == 200 data = response_u1.get_json()['recipeList'][0] assert data['listName'] == 'test list' token_u2 = user2.generate_auth_token() response_u2 = client.get( url_for('users.get_recipe_lists'), headers={'Authorization': token_u2} ) assert response_u2 == 200 data = response_u1.get_json()['recipeList'][0] assert data['listName'] == 'test list' assert len(data['users']) == 2 assert [user1.id, user1.username] in data['users'] assert [user2.id, user2.username] in data['users']
def add_user_recipe_list_and_recipes(session): u = add_user(session) token = u.generate_auth_token().decode('utf-8') recipe_list = add_recipe_list(session, u.id, 'test list') r1 = Recipes( recipe_name='potato', recipe_url='http://blah.com', pic_url='http://picturesofpotatoes.com', recipe_list_id=recipe_list.id, recipe_id=12, added_by=u.id ) r2 = Recipes( recipe_name='potato', recipe_url='http://blah.com', pic_url='http://picturesofpotatoes.com', recipe_list_id=recipe_list.id, recipe_id=123, added_by=u.id ) session.add(r1) session.add(r2) session.commit() assert Recipes.query.count() == 2 assert len(RecipeList.query.first().recipes) == 2 return (u, token, recipe_list)
def test_add_recipe_to_recipe_list(client,session): # construct user u = add_user(session) token = u.generate_auth_token().decode('utf-8') recipe_list = add_recipe_list(session, u.id, 'test list') # add recipes via endpoint recipe_data = [{ 'name': 'potatos', 'url': 'http://recipes.com/potatoesforeverone', 'pic_url': 'http://somanypictures.of.potatoes.com', 'id': 123 }] response = client.post( url_for('users.add_recipes_to_list'), data=json.dumps({ 'targetListId': recipe_list.id, 'recipes': recipe_data }), content_type="application/json", headers={"Authorization": token} ) assert response.status_code == 200 assert len(recipe_list.recipes) == 1 data = response.get_json() assert data['status'] == 'success' assert data['message'] == f'{recipe_list.list_name} updated' assert len(data['updatedRecipes']) == 1 assert data['updatedRecipes'][0]['name'] == Recipes.query.first().recipe_name
def test_user_login(session, client): # DONT FORGET TO WRITE THESE TESTS u = add_user(session) assert len(list(User.query.all())) == 1 data = login_user_via_endpoint(client, 'hudson', 'chips') assert 'logged in success' in data['message'] assert data['loggedIn'] token = data['token'] assert User.verify_auth_token(token) == u
def test_logindecorator_expired_token(client, session): u = add_user(session) token = u.generate_auth_token(1) assert token is not None time.sleep(2) response = post_to_test_login_decorator(client, dict(Authorization=token)) assert response.status_code == 401 data = response.get_json() assert data.get('message') == 'please login again' assert data.get('status') == 'invalid token'
def test_user_login_no_password_and_no_username(session, client): u = add_user(session) # test no username data = login_user_via_endpoint(client, '', 'chips') assert 'field missing' in data['message'] assert data['token'] is None # test no password data = login_user_via_endpoint(client, 'hudson', '') assert 'field missing' in data['message'] assert data['token'] is None
def test_get_recipe_list(session, client): user = add_user(session) token = user.generate_auth_token() recipe_list = add_recipe_list(session, user, 'test list') assert RecipeList.query.count() == 1 response = client.get( url_for('users.get_recipe_lists'), headers={'Authorization': token} ) assert response.status_code == 200 data = response.get_json()['recipeList'][0] assert data['listName'] == 'test list' assert data['creator'] == user.id assert data['recipes'] == []
def test_user_can_get_multiple_lists(session, client): user = add_user(session) token = user.generate_auth_token() recipe_list1 = add_recipe_list(session, user, 'test list 1') recipe_list2 = add_recipe_list(session, user, 'test list 2') assert RecipeList.query.count() == 2 response = client.get( url_for('users.get_recipe_lists'), headers={'Authorization': token} ) assert response.status_code == 200 data = response.get_json()['recipeList'] for list_ in data: assert list_['creator'] == user.id assert 'test list' in list_['listName'] assert len(list_['recipes']) == 0
def test_recipes_on_list_come_through(session, client): user = add_user(session) recipe_list = add_recipe_list(session, user, 'test list') r1 = Recipes( recipe_name='recipe1', recipe_url='https://recipe.com', pic_url='http://thisismyfakeurl.com', recipe_list_id=recipe_list.id, recipe_id=123, added_by=user.id ) session.add(r1) session.commit() token = user.generate_auth_token() response = client.get( url_for('users.get_recipe_lists'), headers={'Authorization': token} ) assert response.status_code == 200 data = response.get_json()['recipeList'][0] assert len(data['recipes']) == 1
def test_user_cannot_login_with_wrong_pword(session, client): u = add_user(session) data = login_user_via_endpoint(client, 'hudson', 'wrongpassword') assert 'invalid password' in data['message'] assert not data['loggedIn'] assert data['token'] is None