def test_password_are_random(user_data): user_id_1 = helper.add_user(user_data)["id"] user_id_2 = helper.add_user({**user_data, "email": "*****@*****.**"})["id"] user_1 = db.session.query(User).get(user_id_1) user_2 = db.session.query(User).get(user_id_2) assert user_1.password_hash != user_2.password_hash
def test_add_user_bad_email(user_data, db_session): assert db_session.query(User).all() == [] with pytest.raises(_SchemaLoadError): helper.add_user({**user_data, "email": "Not valid email"}) assert db_session.query(User).all() == []
def test_post_user_with_existing_email(client, user_data, db_session): helper.add_user(user_data) response = client.post("/api/users", json=user_data) data = json.loads(response.data.decode()) assert response.status_code == 400 assert "already used" in data["email"]
def test_add_dislike(user_data, dislike_data, db_session): assert db_session.query(DisLike).all() == [] user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id dislike_id = helper.add_dislike(dislike_data)["id"] assert db_session.query(DisLike).get(dislike_id)
def test_add_like(user_data, like_data, db_session): assert db_session.query(Like).all() == [] user_id = helper.add_user(user_data)["id"] like_data["user_id"] = user_id like_id = helper.add_like(like_data)["id"] assert db_session.query(Like).get(like_id)
def test_delete_dislike(user_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id dislike_id = helper.add_dislike(dislike_data)["id"] assert db_session.query(DisLike).get(dislike_id) helper.delete_dislike(dislike_id) assert db_session.query(DisLike).get(dislike_id) is None
def other_user(user_data, like_data, dislike_data, db_session): user_id = helper.add_user({**user_data, "email": "*****@*****.**"})["id"] helper.add_like({**like_data, "user_id": user_id}) helper.add_dislike({**dislike_data, "user_id": user_id}) user = db_session.query(User).get(user_id) yield user pass
def test_delete_like(user_data, like_data, db_session): user_id = helper.add_user(user_data)["id"] like_data["user_id"] = user_id like_id = helper.add_like(like_data)["id"] assert db_session.query(Like).get(like_id) helper.delete_like(like_id) assert db_session.query(Like).get(like_id) is None
def current_user(user_data, like_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] helper.add_like({**like_data, "user_id": user_id}) helper.add_dislike({**dislike_data, "user_id": user_id}) user = db_session.query(User).get(user_id) old_current_user = me.current_user me.current_user = user yield user me.current_user = old_current_user
def fill_db(likes, dislikes): user_data = dict( first_name="United", last_name="Remote", email="*****@*****.**", password="******" ) user_id = helper.add_user(user_data)["id"] likes = [dict(place_id=generate_place_id(), user_id=user_id) for _ in range(likes)] dislikes = [dict(place_id=generate_place_id(), user_id=user_id) for _ in range(dislikes)] [helper.add_like(like) for like in likes] [helper.add_dislike(dislike) for dislike in dislikes]
def test_bad_login(client, user_data): helper.add_user(user_data) response = client.post("/api/auth/login", json={}) data = json.loads(response.data.decode()) assert response.status_code == 400 assert data["message"] == "Empty json" missing_payload = {"password": user_data["password"]} response = client.post("/api/auth/login", json=missing_payload) data = json.loads(response.data.decode()) assert response.status_code == 400 assert data["message"] == "Badly formatted json" wrong_password = { "email": user_data["email"], "password": user_data["password"] + "f" } response = client.post("/api/auth/login", json=wrong_password) data = json.loads(response.data.decode()) assert response.status_code == 403 assert data["message"] == "Bad Credentials"
def test_add_dislike_missing_data(user_data, dislike_data, db_session): assert db_session.query(DisLike).all() == [] user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id # Missing user_id with pytest.raises(_SchemaLoadError): helper.add_dislike({"place_id": dislike_data["place_id"]}) assert db_session.query(DisLike).all() == [] # Missing place_id with pytest.raises(_SchemaLoadError): helper.add_dislike({"user_id": dislike_data["user_id"]}) assert db_session.query(DisLike).all() == []
def post_user(): user_data = request.json if user_data: validation_errors = UserSchema().validate(user_data) if validation_errors: return jsonify(validation_errors), 400 # Check that email is unique with session_manager(db.session) as db_session: if db_session.query(User).filter( User.email == user_data["email"]).first(): return jsonify({"email": "This email is already used"}), 400 created_user = helper.add_user(user_data) return jsonify(created_user), 201 return jsonify({"message": "Empty json payload"}), 400
def test_fresh_dislikes(user_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id dislike_id = helper.add_dislike(dislike_data)["id"] user = db_session.query(User).get(user_id) dislike = db_session.query(DisLike).get(dislike_id) assert len(user.fresh_dislikes) == 1 assert user.fresh_dislikes[0].id == dislike_id # Update created_at dislike.created_at = datetime.now(tz=timezone.utc) - config.DISLIKE_EXPIRY db_session.add(dislike) db_session.commit() db_session.expunge(user) db_session.expunge(dislike) user = db_session.query(User).get(user_id) assert len(user.fresh_dislikes) == 0
def test_purge_dislikes_helpers(user_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id nbr_dislikes = 5 [helper.add_dislike(dislike_data)["id"] for _ in range(nbr_dislikes)] user = db_session.query(User).get(user_id) assert db_session.query(DisLike).count() == nbr_dislikes user._dislikes[0].created_at = datetime.now( tz=timezone.utc) - config.DISLIKE_EXPIRY user._dislikes[1].created_at = datetime.now( tz=timezone.utc) - config.DISLIKE_EXPIRY db_session.add(user._dislikes[0]) db_session.add(user._dislikes[1]) db_session.commit() helper.purge_dislikes() assert db_session.query(DisLike).count() == nbr_dislikes - 2
def test_user_password(user_data): user_id = helper.add_user(user_data)["id"] user = db.session.query(User).get(user_id) assert user.check_password(user_data["password"]) is True assert user.check_password(user_data["password"] + "g") is False assert user.check_password(user.password_hash) is False
def test_valid_login(client, user_data): helper.add_user(user_data) payload = {"email": user_data["email"], "password": user_data["password"]} response = client.post("/api/auth/login", json=payload) assert response.status_code == 200
def test_add_user(user_data, db_session): assert db_session.query(User).all() == [] user_id = helper.add_user(user_data)["id"] assert db_session.query(User).get(user_id)