def test_delete_tag(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_tag(name="test create") client.delete("/api/tag/1/") assert client.get("/api/tag/1/").json()["detail"] == "Tag not found"
def test_read_club(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() assert client.get(f"/api/club/1/").json()["id"] == 1 assert client.get(f"/api/club/2/").json()["detail"] == "Club not found"
def test_read_tag(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_tag(name="test getter") assert client.get(f"/api/tag/1/").json()["name"] == "test getter" assert client.get(f"/api/tag/2/").json()["detail"] == "Tag not found"
def test_add_tags_to_note(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() note_id = api_util.create_note(content="test1").json()["id"] tag_id = api_util.create_tag(name="testtag1").json()["id"] client.put(f"/api/note/{note_id}/tag/add/", json={ "tags": [tag_id], "club_tags": [] }) get_notes = client.get(f"/api/note/{note_id}/").json() assert get_notes["tags"][0]["name"] == "testtag1" get_tags = client.get(f"/api/tag/{tag_id}/").json() assert get_tags["name"] == "testtag1" invalid_tag_res = client.put(f"/api/note/{note_id}/tag/add/", json={ "tags": [100], "club_tags": [] }).json() assert invalid_tag_res["detail"] == "Tag not found" invalid_note_res = client.put(f"/api/note/100/tag/", json={ "tags": [1], "club_tags": [] }).json() assert invalid_note_res["detail"] == "Not Found"
def test_create_user(client): unauthenticated_res = client.get( "/api/user/" ) # make unauthenticated request to user assert unauthenticated_res.status_code == 401, unauthenticated_res.text assert unauthenticated_res.json()["detail"] == "Not authenticated" api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() response = client.get("/api/user/") assert response.status_code == 200, response.text assert ( client.post( "/api/token", data={"username": "******", "password": "******"} ).json()["detail"] == "Incorrect username or password" ) assert ( client.post( "/api/token", data={"username": "******", "password": "******"} ).json()["detail"] == "Incorrect username or password" )
def test_google_books_query(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() query = "Harry+Potter" res = client.get(f"/api/book/google_books/q={query}").json()["volumes"] assert len(res)
def test_read_tags(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_tag(name="test getter") assert (client.get(f"/api/tags/?club_id=1").json()["tags"][0]["name"] == "test getter")
def test_update_tag_and_archived_functionality(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_tag(name="test create") api_util.update_tag(tag_id=1, name="test update") assert client.get(f"/api/tag/1/").json()["name"] == "test update" assert api_util.update_tag(tag_id=1, archived=True) assert client.get(f"/api/tag/1/").json()["archived"] == True
def test_join_club(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() api_util.create_user2_and_authenticate() # switch to user 2 client.put(f"/api/club/1/join/") # join the club assert len(client.get("/api/club/1/").json()["users"]) == 2
def test_read_note(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() note_id = api_util.create_note(content="test content").json()["id"] response = client.get(f"/api/note/{note_id}/") assert response.status_code == 200, response.text data = response.json() assert data["content"] == "test content"
def test_create_club_tag(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_book() api_util.create_club() response = api_util.create_club_tag(name="a new tag name") assert response.status_code == 200, response.text data = response.json() assert data["name"] == "a new tag name"
def test_read_clubs(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() assert len(client.get(f"/api/clubs/").json()["clubs"]) == 1 api_util.create_user2_and_authenticate() # switch to user 2 assert not len(client.get(f"/api/clubs/").json()["clubs"])
def test_get_user(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() data = client.get("/api/user/").json() assert data["full_name"] == "Patrick M Kelly" # default user full name assert data["id"] == 1 # first ID assert data["email"] == "*****@*****.**" # default email assert not data.get("password") and not data.get( "hashed_password" ) # make sure password is never sent
def test_add_and_remove_book_to_club(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() club_id = api_util.create_club().json()["id"] book_id = api_util.create_book().json()["id"] client.put(f"/api/club/{club_id}/book/{book_id}/add/") assert len(client.get("/api/club/1/").json()["books"]) == 1 client.put(f"/api/club/{club_id}/book/{book_id}/remove/") assert len(client.get("/api/club/1/").json()["books"]) == 0
def test_create_note(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_book() response = api_util.create_note(content="test note") assert response.status_code == 200, response.text data = response.json() assert data["content"] == "test note" assert data["private"] == False assert data["archived"] == False
def test_follow_user(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() user1_id = client.get("/api/user/").json()["id"] api_util.create_user2_and_authenticate() user2_id = client.get("/api/user/").json()["id"] client.put(f"/api/user/relationship/{user1_id}/follow/") assert client.get("/api/user/").json()["following"][0]["id"] == user1_id api_util.create_user_and_authenticate() assert client.get("/api/user/").json()["followers"][0]["id"] == user2_id
def test_create_tag(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() tag_name = "a new tag name" response = api_util.create_tag(name=tag_name) assert response.status_code == 200, response.text data = response.json() assert data["name"] == "a new tag name" assert (api_util.create_tag(name="a new tag name").json()["detail"] == f"Tag with name {tag_name} already exists")
def test_add_and_remove_book_from_user(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() book_id = api_util.create_book().json()["id"] client.put(f"/api/user/book/{book_id}/add/") assert len(client.get("/api/user/").json()["books"]) == 1 assert len(client.get(f"/api/book/{book_id}/").json()["users"]) == 1 client.put(f"/api/user/book/{book_id}/remove/") assert len(client.get("/api/user/").json()["books"]) == 0 assert len(client.get(f"/api/book/{book_id}/").json()["users"]) == 0
def test_read_books(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() first_res = client.get(f"/api/book/google_books/q=Harry+Potter").json()["volumes"][ 0 ] api_util.create_book( google_books_id=first_res["id"], google_books_self_link=first_res["selfLink"] ) google_book_id = client.get("/api/book/1/").json()["google_books_id"] assert client.get("/api/book/1/").json()["google_books_self_link"] assert client.get(f"/api/book/1/google_book/").json()["id"] == google_book_id
def test_update_note(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() note_id = api_util.create_note().json()[ "id"] # defaults are private=False archived=False updated_res = api_util.update_note(note_id=note_id, content="test update", private=True, archived=True).json() assert updated_res["content"] == "test update" assert updated_res["private"] == True assert updated_res["archived"] == True
def test_create_club(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() name = "We are a fun club" club_id = api_util.create_club(name=name).json()["id"] response = client.get(f"/api/club/{club_id}/") assert response.status_code == 200, response.text data = response.json() assert data["id"] assert data["name"] == name assert data["admin_user_id"] == 1 assert data["users"][0]["id"] == 1 assert len(data["users"]) == 1
def test_delete_club_tag(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_book() api_util.create_club() api_util.create_club_tag(name="test create") client.delete("/api/club_tag/1/") assert client.get( "/api/club_tag/1/").json()["detail"] == "ClubTag not found" api_util.create_club_tag(name="test create") api_util.create_user2_and_authenticate() # create/login as another user ### Fails because user is not admin assert client.delete( "/api/club_tag/1/").json()["detail"] == "ClubTag not found"
def test_delete_club(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() client.delete("/api/club/1/") assert client.get("/api/club/1/").json()["detail"] == "Club not found" assert not len(client.get("/api/clubs/").json()["clubs"]) api_util.create_club() api_util.create_user2_and_authenticate() # switch to user 2 client.put(f"/api/club/1/join/") # join the club assert ( client.delete("/api/club/1/").json()["detail"] == "Non-admin cannot delete club" )
def test_read_personal_notes(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() api_util.create_note(content="public_not_archived") for_archiving_id = api_util.create_note(content="test2").json()["id"] api_util.update_note(content="public_archived", note_id=for_archiving_id, archived=True) api_util.create_note(content="private_not_archived", private=True) private_for_archiving_id = api_util.create_note(content="test4", private=True).json()["id"] api_util.update_note( content="private_archived", note_id=private_for_archiving_id, private=True, archived=True, ) private_not_archived = client.get(f"/api/notes/me/?book_id=1").json() assert [note["content"] for note in private_not_archived["notes"]] == [ "public_not_archived", "private_not_archived", ] public_including_archived_res = client.get( f"/api/notes/me/?book_id=1&include_archived=True").json() assert [ note["content"] for note in public_including_archived_res["notes"] ] == [ "public_not_archived", "public_archived", "private_not_archived", "private_archived", ] public_not_including_archived_res = client.get( f"/api/notes/me/?book_id=1&include_private=False&include_archived=True" ).json() assert [ note["content"] for note in public_not_including_archived_res["notes"] ] == [ "public_not_archived", "public_archived", ]
def test_remove_tags_from_note(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() note_id = api_util.create_note(content="test1").json()["id"] tag_id = api_util.create_tag(name="testtag1").json()["id"] client.put(f"/api/note/{note_id}/tag/add/", json={ "tags": [tag_id], "club_tags": [] }) client.put(f"/api/note/{note_id}/tag/remove/", json={ "tags": [tag_id], "club_tags": [] }) assert not len(client.get(f"/api/note/{note_id}/").json()["tags"])
def test_read_club_tags(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_book() api_util.create_club() api_util.create_club_tag(name="test getter") assert (client.get(f"/api/club_tags/?club_id=1").json()["club_tags"][0] ["name"] == "test getter") api_util.create_user2_and_authenticate() # create/login as another user assert (client.get(f"/api/club_tags/?club_id=1").json()["detail"] == "ClubTag not found") client.put(f"/api/club/1/join/") # join the club api_util.create_club_tag(name="test getter2") assert (client.get(f"/api/club_tags/?club_id=1").json()["club_tags"][1] ["name"] == "test getter2")
def test_create_books(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() book_id = api_util.create_book( google_books_id="aslkasdf", google_books_self_link="https://aasdf" ).json()["id"] assert client.get(f"/api/book/{book_id}/").json()["google_books_id"] == "aslkasdf" assert ( client.get(f"/api/book/{book_id}/").json()["google_books_self_link"] == "https://aasdf" ) api_util.create_user2_and_authenticate() book_id2 = api_util.create_book( google_books_id="aslkasdf", google_books_self_link="https://aasdf" ).json()["id"] assert book_id == book_id2
def test_update_club_and_is_active_functionality(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() api_util.create_club() assert ( client.put("/api/club/2/", json={"is_active": False, "name": "Newish"}).json()[ "detail" ] == "Club not found" ) client.put("/api/club/1/", json={"is_active": False, "name": "Newish"}).json() assert client.get("/api/club/1/").json()["is_active"] == False assert client.get("/api/club/1/").json()["name"] == "Newish" api_util.create_user2_and_authenticate() # switch to user 2 client.put(f"/api/club/1/join/") # join the club api_util.authenticate() client.put("/api/club/1/", json={"is_active": False, "name": "Newish"}) assert not len(client.get("/api/clubs/").json()["clubs"])
def test_delete_notes(client): api_util = utils.MockApiRequests(client) api_util.create_user_and_authenticate() note_id = api_util.create_note(content="test1").json()["id"] tag_id = api_util.create_tag(name="testtag1").json()["id"] client.put(f"/api/note/{note_id}/tag/add/", json={ "tags": [tag_id], "club_tags": [] }) client.delete(f"/api/note/{note_id}/") assert client.get( f"/api/note/{note_id}/").json()["detail"] == "Note not found" assert not client.get(f"/api/notes/club/?book_id=1").json().get("notes") assert not client.get(f"/api/tag/{tag_id}/").json().get("notes") user_1_note_id = api_util.create_note(content="test delete").json()["id"] api_util.create_user2_and_authenticate() # create/login as another user assert (client.delete(f"/api/note/{user_1_note_id}/").json()["detail"] == "Note not found")