|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +from starlette.testclient import TestClient |
| 4 | +from database.session import DbSession |
| 5 | + |
| 6 | +from tests.testutils.users import logged_in_user, ALICE |
| 7 | +from database.model.bookmark.bookmark import Bookmark |
| 8 | +from database.session import DbSession |
| 9 | +from tests.testutils.users import register_asset, register_user |
| 10 | +from datetime import datetime |
| 11 | +from database.model.agent.person import Person |
| 12 | +from database.model.agent.contact import Contact |
| 13 | + |
| 14 | + |
| 15 | +def test_create_bookmark( |
| 16 | + client: TestClient, |
| 17 | + person: Person) -> None: |
| 18 | + |
| 19 | + identifier = register_asset(person) |
| 20 | + |
| 21 | + with logged_in_user(): |
| 22 | + response = client.post( |
| 23 | + f"/bookmarks?resource_identifier={identifier}", |
| 24 | + headers={"Authorization": "fake token"}, |
| 25 | + ) |
| 26 | + assert response.status_code == HTTPStatus.OK |
| 27 | + bookmark = response.json() |
| 28 | + assert bookmark["resource_identifier"] == identifier |
| 29 | + now = datetime.utcnow() |
| 30 | + assert (now - datetime.fromisoformat(bookmark["created_at"])).total_seconds() < 2 |
| 31 | + |
| 32 | + |
| 33 | +def test_create_bookmark_for_non_existing_resource(client: TestClient) -> None: |
| 34 | + # Create bookmark for non existing resource. |
| 35 | + |
| 36 | + with logged_in_user(): |
| 37 | + response = client.post( |
| 38 | + f"/bookmarks?resource_identifier=wrong_indetifier", |
| 39 | + headers={"Authorization": "fake token"}, |
| 40 | + ) |
| 41 | + |
| 42 | + assert response.status_code == HTTPStatus.NOT_FOUND |
| 43 | + assert response.json()["detail"] == f"Resource wrong_indetifier does not exist." |
| 44 | + |
| 45 | + |
| 46 | +def test_create_duplicate( |
| 47 | + client: TestClient, |
| 48 | + person: Person |
| 49 | +) -> None: |
| 50 | + |
| 51 | + with DbSession() as session: |
| 52 | + identifier = register_asset(person) |
| 53 | + user = register_user(ALICE, session) |
| 54 | + now = datetime.utcnow() |
| 55 | + bookmark = Bookmark( |
| 56 | + user_identifier=user.subject_identifier, |
| 57 | + resource_identifier=identifier, |
| 58 | + created_at=now |
| 59 | + ) |
| 60 | + session.add(bookmark) |
| 61 | + session.commit() |
| 62 | + |
| 63 | + # Attempt to create a duplicate bookmark |
| 64 | + with logged_in_user(ALICE): |
| 65 | + response = client.post( |
| 66 | + f"/bookmarks?resource_identifier={identifier}", |
| 67 | + headers={"Authorization": "fake token"}, |
| 68 | + ) |
| 69 | + |
| 70 | + assert response.status_code == HTTPStatus.OK |
| 71 | + bookmark = response.json() |
| 72 | + assert bookmark["resource_identifier"] == identifier |
| 73 | + assert bookmark["created_at"] == now.isoformat() |
| 74 | + |
| 75 | + |
| 76 | +def test_get_bookmarks(client: TestClient, person: Person, contact: Contact) -> None: |
| 77 | + with DbSession() as session: |
| 78 | + prsn_id = register_asset(person) |
| 79 | + contact_id = register_asset(contact) |
| 80 | + user = register_user(ALICE, session) |
| 81 | + session.commit() |
| 82 | + |
| 83 | + # Add a bookmark |
| 84 | + with logged_in_user(ALICE): |
| 85 | + response = client.post( |
| 86 | + f"/bookmarks?resource_identifier={prsn_id}", |
| 87 | + headers={"Authorization": "fake token"}, |
| 88 | + ) |
| 89 | + assert response.status_code == HTTPStatus.OK |
| 90 | + |
| 91 | + response = client.post( |
| 92 | + f"/bookmarks?resource_identifier={contact_id}", |
| 93 | + headers={"Authorization": "fake token"}, |
| 94 | + ) |
| 95 | + assert response.status_code == HTTPStatus.OK |
| 96 | + |
| 97 | + # Fetch bookmarks |
| 98 | + response = client.get( |
| 99 | + "/bookmarks", |
| 100 | + headers={"Authorization": "fake token"}, |
| 101 | + ) |
| 102 | + |
| 103 | + assert response.status_code == HTTPStatus.OK |
| 104 | + bookmarks = response.json() |
| 105 | + assert len(bookmarks) == 2 |
| 106 | + |
| 107 | + |
| 108 | +def test_delete_bookmark( |
| 109 | + client: TestClient, |
| 110 | + person: Person |
| 111 | +) -> None: |
| 112 | + |
| 113 | + |
| 114 | + with DbSession() as session: |
| 115 | + identifier = register_asset(person) |
| 116 | + register_user(ALICE, session) |
| 117 | + session.commit() |
| 118 | + |
| 119 | + with logged_in_user(ALICE): |
| 120 | + response = client.post( |
| 121 | + f"/bookmarks?resource_identifier={identifier}", |
| 122 | + headers={"Authorization": "fake token"}, |
| 123 | + ) |
| 124 | + assert response.status_code == HTTPStatus.OK |
| 125 | + |
| 126 | + |
| 127 | + with logged_in_user(ALICE): |
| 128 | + response = client.delete( |
| 129 | + f"/bookmarks?resource_identifier={identifier}", |
| 130 | + headers={"Authorization": "fake token"}, |
| 131 | + ) |
| 132 | + assert response.status_code == HTTPStatus.OK |
| 133 | + assert response.json() == None |
| 134 | + |
| 135 | + # Confirm it's deleted |
| 136 | + with logged_in_user(ALICE): |
| 137 | + response = client.get( |
| 138 | + "/bookmarks", |
| 139 | + headers={"Authorization": "fake token"}, |
| 140 | + ) |
| 141 | + assert response.status_code == HTTPStatus.OK |
| 142 | + assert all(b["resource_identifier"] != identifier for b in response.json()) |
0 commit comments