2023-03-17 23:51:09 +00:00
|
|
|
import datetime
|
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
from canaille.app import models
|
2023-03-16 17:39:28 +00:00
|
|
|
from webtest import Upload
|
|
|
|
|
|
|
|
|
|
|
|
def test_photo(testclient, user, jpeg_photo):
|
2023-02-05 17:57:18 +00:00
|
|
|
user.photo = [jpeg_photo]
|
2023-03-16 17:39:28 +00:00
|
|
|
user.save()
|
2023-04-08 19:34:09 +00:00
|
|
|
user.reload()
|
2023-03-17 23:51:09 +00:00
|
|
|
|
2023-02-05 17:57:18 +00:00
|
|
|
res = testclient.get("/profile/user/photo")
|
2023-03-16 17:39:28 +00:00
|
|
|
assert res.body == jpeg_photo
|
2023-02-05 17:57:18 +00:00
|
|
|
assert res.last_modified == user.last_modified
|
2023-03-17 23:51:09 +00:00
|
|
|
etag = res.etag
|
|
|
|
assert etag
|
|
|
|
|
|
|
|
res = testclient.get(
|
2023-02-05 17:57:18 +00:00
|
|
|
"/profile/user/photo",
|
2023-03-17 23:51:09 +00:00
|
|
|
headers={
|
|
|
|
"If-Modified-Since": (
|
|
|
|
res.last_modified + datetime.timedelta(days=1)
|
|
|
|
).strftime("%a, %d %b %Y %H:%M:%S UTC")
|
|
|
|
},
|
|
|
|
status=304,
|
|
|
|
)
|
|
|
|
assert not res.body
|
|
|
|
|
|
|
|
res = testclient.get(
|
2023-02-05 17:57:18 +00:00
|
|
|
"/profile/user/photo",
|
2023-03-17 23:51:09 +00:00
|
|
|
headers={"If-None-Match": etag},
|
|
|
|
status=304,
|
|
|
|
)
|
|
|
|
assert not res.body
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_photo_invalid_user(testclient, user):
|
2023-02-05 17:57:18 +00:00
|
|
|
res = testclient.get("/profile/invalid/photo", status=404)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_photo_absent(testclient, user):
|
2023-02-05 17:57:18 +00:00
|
|
|
assert not user.photo
|
|
|
|
res = testclient.get("/profile/user/photo", status=404)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_photo_invalid_path(testclient, user):
|
|
|
|
testclient.get("/profile/user/invalid", status=404)
|
|
|
|
|
|
|
|
|
|
|
|
def test_photo_on_profile_edition(
|
|
|
|
testclient,
|
|
|
|
logged_user,
|
|
|
|
jpeg_photo,
|
|
|
|
):
|
|
|
|
# Add a photo
|
|
|
|
res = testclient.get("/profile/user", status=200)
|
2023-02-05 17:57:18 +00:00
|
|
|
res.form["photo"] = Upload("logo.jpg", jpeg_photo)
|
|
|
|
res.form["photo_delete"] = False
|
2023-03-16 17:39:28 +00:00
|
|
|
res = res.form.submit(name="action", value="edit")
|
|
|
|
assert ("success", "Profile updated successfuly.") in res.flashes
|
|
|
|
res = res.follow()
|
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
logged_user.reload()
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-02-05 17:57:18 +00:00
|
|
|
assert [jpeg_photo] == logged_user.photo
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
# No change
|
|
|
|
res = testclient.get("/profile/user", status=200)
|
2023-02-05 17:57:18 +00:00
|
|
|
res.form["photo_delete"] = False
|
2023-03-16 17:39:28 +00:00
|
|
|
res = res.form.submit(name="action", value="edit")
|
|
|
|
assert ("success", "Profile updated successfuly.") in res.flashes
|
|
|
|
res = res.follow()
|
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
logged_user.reload()
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-02-05 17:57:18 +00:00
|
|
|
assert [jpeg_photo] == logged_user.photo
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
# Photo deletion
|
|
|
|
res = testclient.get("/profile/user", status=200)
|
2023-02-05 17:57:18 +00:00
|
|
|
res.form["photo_delete"] = True
|
2023-03-16 17:39:28 +00:00
|
|
|
res = res.form.submit(name="action", value="edit")
|
|
|
|
assert ("success", "Profile updated successfuly.") in res.flashes
|
|
|
|
res = res.follow()
|
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
logged_user.reload()
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
assert logged_user.photo == []
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
# Photo deletion AND upload, this should never happen
|
|
|
|
res = testclient.get("/profile/user", status=200)
|
2023-02-05 17:57:18 +00:00
|
|
|
res.form["photo"] = Upload("logo.jpg", jpeg_photo)
|
|
|
|
res.form["photo_delete"] = True
|
2023-03-16 17:39:28 +00:00
|
|
|
res = res.form.submit(name="action", value="edit")
|
|
|
|
assert ("success", "Profile updated successfuly.") in res.flashes
|
|
|
|
res = res.follow()
|
|
|
|
|
2023-04-08 19:34:09 +00:00
|
|
|
logged_user.reload()
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-02-05 17:57:18 +00:00
|
|
|
assert [] == logged_user.photo
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
2023-05-20 16:28:31 +00:00
|
|
|
def test_photo_on_profile_creation(testclient, jpeg_photo, logged_admin):
|
2023-03-16 17:39:28 +00:00
|
|
|
res = testclient.get("/users", status=200)
|
2023-04-09 09:37:04 +00:00
|
|
|
assert models.User.get_from_login("foobar") is None
|
2023-03-16 17:39:28 +00:00
|
|
|
res.mustcontain(no="foobar")
|
|
|
|
|
|
|
|
res = testclient.get("/profile", status=200)
|
2023-02-05 17:57:18 +00:00
|
|
|
res.form["photo"] = Upload("logo.jpg", jpeg_photo)
|
|
|
|
res.form["user_name"] = "foobar"
|
|
|
|
res.form["family_name"] = "Abitbol"
|
|
|
|
res.form["email"] = "george@abitbol.com"
|
2023-03-16 17:39:28 +00:00
|
|
|
res = res.form.submit(name="action", value="edit", status=302).follow(status=200)
|
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
user = models.User.get_from_login("foobar")
|
2023-02-05 17:57:18 +00:00
|
|
|
assert user.photo == [jpeg_photo]
|
2023-03-16 17:39:28 +00:00
|
|
|
user.delete()
|
|
|
|
|
|
|
|
|
2023-05-20 16:28:31 +00:00
|
|
|
def test_photo_deleted_on_profile_creation(testclient, jpeg_photo, logged_admin):
|
2023-03-16 17:39:28 +00:00
|
|
|
res = testclient.get("/users", status=200)
|
2023-04-09 09:37:04 +00:00
|
|
|
assert models.User.get_from_login("foobar") is None
|
2023-03-16 17:39:28 +00:00
|
|
|
res.mustcontain(no="foobar")
|
|
|
|
|
|
|
|
res = testclient.get("/profile", status=200)
|
2023-02-05 17:57:18 +00:00
|
|
|
res.form["photo"] = Upload("logo.jpg", jpeg_photo)
|
|
|
|
res.form["photo_delete"] = True
|
|
|
|
res.form["user_name"] = "foobar"
|
|
|
|
res.form["family_name"] = "Abitbol"
|
|
|
|
res.form["email"] = "george@abitbol.com"
|
2023-03-16 17:39:28 +00:00
|
|
|
res = res.form.submit(name="action", value="edit", status=302).follow(status=200)
|
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
user = models.User.get_from_login("foobar")
|
2023-02-05 17:57:18 +00:00
|
|
|
assert user.photo == []
|
2023-03-16 17:39:28 +00:00
|
|
|
user.delete()
|