def test_ordered_upload(server, auth, req): ordered_id = create_album("Ordered Album", [], server, auth) files = os.listdir("./test_corpus/test_cases") files.sort() fcount = len(files) for i, img in enumerate(files): with open(f"./test_corpus/test_cases/{img}", "rb") as f: res = req.post( server.api("/upload"), headers=auth, files={"mediaUp": (img, f, mimetypes.guess_type(img)[0])}, data={ "data": json.dumps({ "albumID": ordered_id, "order": fcount - i }) }) stat_assert(res, 200) res = req.get(server.api(f"/album/view/{ordered_id}")) stat_assert(res, 200) adata = res.json() assert adata["id"] == ordered_id gdata = adata["photoGroups"][0] assert len(gdata["photos"]) == 2 assert gdata["photos"][0]["id"] == 13 assert gdata["photos"][1]["id"] == 12
def test_delete_photo(server, auth, req): res = req.post( server.api("/photo/delete"), headers=auth, json={"photoIDs": [1]} ) stat_assert(res, 200)
def test_upload_auth(server, req): with open("./test_corpus/jpeg/01.jpeg", "rb") as f: res = req.post( server.api("/upload"), files={"mediaUp": ("01.jpeg", f, "image/jpeg")}, ) stat_assert(res, 401)
def test_photo_untag_auth(server, req): res = req.get(server.api("/photo/untag"), json={ "photoIDs": [4], "tags": ["A"] }) stat_assert(res, 401)
def test_group_invalid_metadata(server, req, auth, gid): res = req.post(server.api(f"/group/edit/{gid}"), headers=auth, json={ "description": 1337, "hasMap": "nah" }) stat_assert(res, 400)
def test_group_metadata_nonexistent(server, auth, req): res = req.post(server.api("/group/edit/17"), headers=auth, json={ "description": "**New description for a group.**", "hasMap": 1, }) stat_assert(res, 404)
def test_bad_format_upload(server, auth, req): with open("./test_corpus/png/01.png", "rb") as f: res = req.post( server.api("/upload"), headers=auth, files={"mediaUp": ("01.png", f, "image/png")}, ) stat_assert(res, 415)
def test_non_image_upload(server, auth, req): with open("./test_corpus/attributions.txt", "rb") as f: res = req.post( server.api("/upload"), headers=auth, files={"mediaUp": ("attributions.txt", f, "text/plain")}, ) stat_assert(res, 415)
def test_photo_move_auth(server, req): res = req.post(server.api("/photo/move"), json={ "photoIDs": [7, 8], "fromGroupID": 5, "toGroupID": 8 }) stat_assert(res, 401)
def test_create_group_auth(server, auth, req, aid, gid): res = req.post(server.api("/group/new"), json={ "albumID": aid, "fromGroup": gid, "photoIDs": [5, 11, 10, 9] }) stat_assert(res, 401)
def test_delete_album(server, auth, req, ta3): res = req.post(server.api("/album/delete"), headers=auth, json={"albumID": ta3}) stat_assert(res, 200) res = req.get(server.api(f"/album/view/{ta3}"), headers=auth) stat_assert(res, 404)
def test_empty_photo_list(server, req): res = req.get(server.api("/dynamic/all")) stat_assert(res, 200) assert len(res.json()) == 0 res = req.get(server.api("/dynamic/unsorted")) stat_assert(res, 200) assert len(res.json()) == 0
def test_album_list_privacy(server, auth, req): res = req.get(server.api("/album/list"), ) stat_assert(res, 200) assert len(res.json()) == 1 res = req.get(server.api("/album/list"), headers=auth) stat_assert(res, 200) assert len(res.json()) == 2
def test_create_private_album(server, auth, req): res = req.post(server.api("/album/new"), headers=auth, json={ "title": "Test Album 2", "isPrivate": 1 }) stat_assert(res, 200)
def priv(server, auth): aid = create_album("Private Album", [], server, auth) res = requests.post(server.api(f"/album/edit/{aid}"), headers=auth, json={ "isPrivate": 1, }) stat_assert(res, 200) return aid
def test_no_repeat_setup(server, req): data = { "siteTitle": "Set Up Again", "userName": "******", "password": "******" } res = req.post(server.api("/setup"), json=data) stat_assert(res, 403) assert "already set up" in res.json()["message"]
def test_reorder_album_list(server, auth, req): res = req.post(server.api("/album/reorderList"), headers=auth, json={"newOrdering": [3, 2, 1]}) stat_assert(res, 200) res = req.get(server.api("/album/list"), headers=auth) stat_assert(res, 200) order = [a["id"] for a in res.json()] assert order == [3, 2, 1]
def test_photo_tag_import(server, req): res = req.get(server.api(f"/album/view/6")) stat_assert(res, 200) adata = res.json() plist = adata["photoGroups"][0]["photos"] pdata = list(filter(lambda x: x["id"] == 4, plist))[0] assert len(pdata["tags"]) == 2 assert "Israel" in pdata["tags"] assert "Jerusalem" in pdata["tags"]
def test_album_view_by_slug(server, req): res = req.get(server.api("/album/view/test-album-1")) stat_assert(res, 200) adata = res.json() assert adata["id"] == 1 assert adata["title"] == "Test Album 1" assert len(adata["photoGroups"]) == 1 assert adata["photoGroups"][0]["id"] == 1 assert len(adata["photoGroups"][0]["photos"]) == 0
def test_album_invalid_metadata(server, req, auth, ta3): res = req.post(server.api(f"/album/edit/{ta3}"), headers=auth, json={ "title": 5, "description": "**New description.**", "isPrivate": "false", "hasMap": "nah", "coverPhoto": "number won" }) stat_assert(res, 400)
def test_album_metadata_nonexistent(server, auth, req): res = req.post(server.api("/album/edit/17"), headers=auth, json={ "title": "Edited Title", "description": "**New description.**", "isPrivate": 1, "hasMap": 1, "coverPhoto": 1 }) stat_assert(res, 404)
def test_reorder_group(server, auth, req, gid): res = req.post(server.api(f"/group/reorder/{gid}"), headers=auth, json={"newOrdering": list(range(11, 1, -1))}) stat_assert(res, 200) res = req.get(server.api(f"/album/view/{gid}")) stat_assert(res, 200) gdata = res.json()["photoGroups"][0] order = [p["id"] for p in gdata["photos"]] assert order == [11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
def test_create_empty_group(server, auth, req, aid): res = req.post( server.api("/group/new"), headers=auth, json={"albumID": aid}, ) stat_assert(res, 200) ngid = res.json()["groupID"] res = req.get(server.api(f"/album/view/{aid}")) stat_assert(res, 200) adata = res.json() assert len(adata["photoGroups"]) == 2 assert adata["photoGroups"][1]["id"] == ngid assert len(adata["photoGroups"][0]["photos"]) == 10 assert len(adata["photoGroups"][1]["photos"]) == 0
def test_orig_getback(server, req): res = req.get(server.api("/dynamic/all")) stat_assert(res, 200) plist = res.json() for i in range(1, 11): pdata = list(filter(lambda x: x["id"] == i, plist))[0] with tempfile.TemporaryFile("w+b") as tf: with req.get(server.api(f"/photo/orig/{i}"), stream=True) as res: stat_assert(res, 200) for segment in res.iter_content(chunk_size=4096): tf.write(segment) tf.seek(0) md5 = hashlib.md5() for segment in iter(lambda: tf.read(4096), b""): md5.update(segment) assert md5.hexdigest() == pdata["hash"]
def test_photo_move_params(server, req, auth): res = req.post(server.api("/photo/move"), headers=auth) stat_assert(res, 400) res = req.post(server.api("/photo/move"), headers=auth, json={ "photoIDs": "three and four", "fromGroupID": 5, "toGroupID": 8 }) stat_assert(res, 400) assert "Invalid or missing parameter 'photoIDs'" in res.json()["message"] res = req.post(server.api("/photo/move"), headers=auth, json={ "photoIDs": [2, 5, "all", "have", "to be numbers"], "fromGroupID": 5, "toGroupID": 8 }) stat_assert(res, 400) assert "Non-numeric values in photoIDs list" in res.json()["message"] res = req.post(server.api("/photo/move"), headers=auth, json={ "photoIDs": [7, 8], "toGroupID": 8 }) stat_assert(res, 400) assert "Missing or non-numeric value for 'fromGroupID'" in res.json( )["message"] res = req.post(server.api("/photo/move"), headers=auth, json={ "photoIDs": [7, 8], "fromGroupID": 5 }) stat_assert(res, 400) assert "Missing or non-numeric value for either" in res.json()["message"]
def test_multiple_uploads(server, auth, req): for i in range(2, 11): fname = f"./test_corpus/jpeg/{i:02}.jpeg" if not os.path.exists(fname): fname = f"./test_corpus/jpeg/{i:02}.jpg" with open(fname, "rb") as f: md5 = hashlib.md5() for segment in iter(lambda: f.read(4096), b""): md5.update(segment) f.seek(0) res = req.post( server.api("/upload"), headers=auth, files={ "mediaUp": (os.path.basename(fname), f, mimetypes.guess_type(fname)[0]) }, ) stat_assert(res, 200) assert res.json()["hash"] == md5.hexdigest()
def test_login(server, req): data = {} res = req.post(server.api("/login"), json=data) stat_assert(res, 400) data["userName"] = "******" res = req.post(server.api("/login"), json=data) stat_assert(res, 400) data["password"] = "******" res = req.post(server.api("/login"), json=data) stat_assert(res, 403) data["userName"] = "******" res = req.post(server.api("/login"), json=data) stat_assert(res, 404) data["userName"] = "******" data["password"] = "******" res = req.post(server.api("/login"), json=data) stat_assert(res, 200)
def test_duplicate_allowed_but_unique(server, auth, req): fname = f"./test_corpus/jpeg/01.jpeg" with open(fname, "rb") as f: res = req.post( server.api("/upload"), headers=auth, files={"mediaUp": ("01.jpeg", f, "image/jpeg")}, ) stat_assert(res, 200) dupe_data = res.json() res = req.get(server.api("/dynamic/all")) stat_assert(res, 200) plist = res.json() assert len(plist) == 11 orig_data = list(filter(lambda x: x["id"] == 1, plist))[0] assert orig_data["hash"] == dupe_data["hash"] assert orig_data["uniq"] != dupe_data["uniq"] assert orig_data["id"] != dupe_data["id"]
def test_photo_tag(server, req, auth): res = req.get(server.api("/photo/tag"), headers=auth, json={ "photoIDs": [2, 3, 4], "tags": ["A"] }) stat_assert(res, 200) res = req.get(server.api(f"/album/view/6")) stat_assert(res, 200) adata = res.json() plist = adata["photoGroups"][0]["photos"] pdata = list(filter(lambda x: x["id"] == 2, plist))[0] assert "A" in pdata["tags"] pdata = list(filter(lambda x: x["id"] == 3, plist))[0] assert "A" in pdata["tags"] pdata = list(filter(lambda x: x["id"] == 4, plist))[0] assert "A" in pdata["tags"] res = req.get(server.api("/photo/tagSet"), json={"tags": ["A"]}) stat_assert(res, 200) tagged_photos = res.json() assert len(tagged_photos) == 3 ids = [tp["id"] for tp in tagged_photos] assert 2 in ids assert 3 in ids assert 4 in ids
def test_album_metadata(server, auth, req, ta3): res = req.get(server.api(f"/album/view/{ta3}")) stat_assert(res, 200) adata = res.json() assert adata["title"] == "Test Album 3" assert adata["description"] == "" assert adata["isPrivate"] == False assert adata["hasMap"] == False assert adata["coverPhoto"] == None res = req.post(server.api(f"/album/edit/{ta3}"), headers=auth, json={ "title": "Edited Title", "description": "**New description.**", "isPrivate": 1, "hasMap": 1, "coverPhoto": 1 }) stat_assert(res, 200) res = req.get(server.api(f"/album/view/{ta3}"), headers=auth) stat_assert(res, 200) adata = res.json() assert adata["title"] == "Edited Title" assert adata["description"] == "**New description.**" assert adata["isPrivate"] == True assert adata["hasMap"] == True assert adata["coverPhoto"]["id"] == 1