def test_rotate_note(self): """Test rotating the page with a note""" document = DocumentFactory(page_count=3) note = NoteFactory.create(document=document, page_number=1) x1, x2, y1, y2 = note.x1, note.x2, note.y1, note.y2 assert None not in (x1, x2, y1, y2) modifications = [ { "page_spec": [[0, 2]], "modifications": [{"type": "rotate", "angle": "cc"}], } ] send_post_process(document, modifications) # still one note assert document.notes.count() == 1 note.refresh_from_db() # not moved assert note.page_number == 1 # but is rotated assert ( note.x1 == (1 - y2) and note.x2 == (1 - y1) and note.y1 == x1 and note.y2 == x2 ) document.refresh_from_db() assert document.page_count == 3
def test_rotate_note_and_copy(self): """Test rotating the page with a note Ensure copied note is not double rotated """ document = DocumentFactory(page_count=3) note = NoteFactory.create(document=document, page_number=1) x1, x2, y1, y2 = note.x1, note.x2, note.y1, note.y2 assert None not in (x1, x2, y1, y2) modifications = [ { "page_spec": [1, 1, 1], "modifications": [{"type": "rotate", "angle": "cc"}], } ] send_post_process(document, modifications) # two notes assert document.notes.count() == 3 notes = document.notes.all() for note in notes: # all notes should be rotated the same way assert ( note.x1 == (1 - y2) and note.x2 == (1 - y1) and note.y1 == x1 and note.y2 == x2 )
def test_list_queries(self, client, expand): """Queries should be constant""" small_size = 1 documents = DocumentFactory.create_batch(small_size) projects = ProjectFactory.create_batch(small_size, documents=documents) for document in documents: NoteFactory.create_batch(small_size, document=document) SectionFactory.create_batch(small_size, document=document) reset_queries() client.get( f"/api/projects/{projects[0].pk}/documents/?expand={expand}") num_queries = len(connection.queries) size = 10 documents = DocumentFactory.create_batch(size) projects = ProjectFactory.create_batch(size, documents=documents) for document in documents: NoteFactory.create_batch(size, document=document) SectionFactory.create_batch(size, document=document) reset_queries() response = client.get( f"/api/projects/{projects[0].pk}/documents/?expand={expand}") assert num_queries == len(connection.queries) assert len(response.json()["results"]) == size
def test_remove_note(self): """Test removing the page with a note""" document = DocumentFactory(page_count=3) note = NoteFactory.create(document=document, page_number=1) modifications = [{"page_spec": [0, 2]}] send_post_process(document, modifications) # the note is moved to the first page as a page note assert document.notes.count() == 1 note.refresh_from_db() # moved to the first page assert note.page_number == 0 # as a full page note assert note.x1 is None document.refresh_from_db() assert document.page_count == 2
def setup_solr(django_db_setup, django_db_blocker): """Set up the models for the search tests `django_db_setup` causes pytest to initiate the test database """ # pylint: disable=unused-argument solr = pysolr.Solr(settings.SOLR_URL, auth=settings.SOLR_AUTH) with django_db_blocker.unblock(): try: organizations = {} users = {} documents = {} notes = {} # this enables searching through notes for users in that org entitlement = OrganizationEntitlementFactory() for org in ORGANIZATIONS: if org.pop("entitlement", None) == "org": org["entitlement"] = entitlement organizations[org["id"]] = OrganizationFactory(**org) for user in USERS: user = user.copy() org = user.pop("organization") users[user["id"]] = UserFactory( membership__organization=organizations[org], **user) for doc in DOCUMENTS: doc = doc.copy() user = doc.pop("user") org = doc.pop("organization") documents[doc["id"]] = DocumentFactory( user=users[user], organization=organizations[org], **doc) for note in NOTES: note = note.copy() user = note.pop("user") org = note.pop("organization") doc = note.pop("document") notes[note["id"]] = NoteFactory( user=users[user], organization=organizations[org], document=documents[doc], **note, ) for proj in PROJECTS: ProjectFactory( id=proj["id"], title=proj["title"], user=users[proj["user"]], edit_documents=[documents[d] for d in proj["documents"]], collaborators=[users[a] for a in proj["collaborators"]], edit_collaborators=[ users[a] for a in proj["edit_collaborators"] ], ) for doc in documents.values(): solr.add([doc.solr()]) solr.commit() yield finally: Document.objects.all().delete() Project.objects.all().delete() User.objects.all().delete() Organization.objects.all().delete() solr.delete(q="*:*")
def test_note_rules(): anonymous = AnonymousUser() owner = UserFactory(name="owner") organization_member = UserFactory(name="org") edit_collaborator = UserFactory(name="edit") view_collaborator = UserFactory(name="view") organization = OrganizationFactory(members=[owner, organization_member]) organization_member.organization = organization public_note = NoteFactory(user=owner, organization=organization, access=Access.public, title="public") organization_note = NoteFactory(user=owner, organization=organization, access=Access.organization, title="org") private_note = NoteFactory(user=owner, organization=organization, access=Access.private, title="private") invisible_note = NoteFactory( user=owner, organization=organization, access=Access.invisible, title="invisible", ) documents = [ public_note.document, organization_note.document, private_note.document, invisible_note.document, ] ProjectFactory(edit_collaborators=[edit_collaborator], edit_documents=documents) ProjectFactory(collaborators=[view_collaborator], documents=documents) for user, note, can_view, can_change in [ (anonymous, public_note, True, False), (anonymous, organization_note, False, False), (anonymous, private_note, False, False), (anonymous, invisible_note, False, False), (owner, public_note, True, True), (owner, organization_note, True, True), (owner, private_note, True, True), (owner, invisible_note, False, False), (organization_member, public_note, True, False), (organization_member, organization_note, False, False), (organization_member, private_note, False, False), (organization_member, invisible_note, False, False), (edit_collaborator, public_note, True, False), (edit_collaborator, organization_note, False, False), (edit_collaborator, private_note, False, False), (edit_collaborator, invisible_note, False, False), (view_collaborator, public_note, True, False), (view_collaborator, organization_note, False, False), (view_collaborator, private_note, False, False), (view_collaborator, invisible_note, False, False), ]: assert (user.has_perm("documents.view_note", note) is can_view), f"{user.get_full_name()} - {note.title}" assert (user.has_perm("documents.change_note", note) is can_change), f"{user.get_full_name()} - {note.title}" assert (user.has_perm("documents.delete_note", note) is can_change), f"{user.get_full_name()} - {note.title}"
def test_store_statistics(): documents = [] doc_data = [ (Access.public, 10, 10), (Access.organization, 20, 20), (Access.private, 30, 30), (Access.invisible, 1, 40), ] for access, num, page_count in doc_data: documents.extend( DocumentFactory.create_batch(num, access=access, page_count=page_count) ) # to test user / org uniqueness count documents[1].user = documents[0].user documents[1].organization = documents[0].organization documents[1].save() documents[2].organization = documents[0].organization documents[2].save() note_data = [ (Access.public, 35), (Access.organization, 25), (Access.private, 15), (Access.invisible, 0), ] for access, num in note_data: NoteFactory.create_batch(num, access=access, document=documents[0]) num_projects = 42 ProjectFactory.create_batch(num_projects) store_statistics() stats = Statistics.objects.first() assert stats.date == date.today() - timedelta(1) assert stats.total_documents == sum(d[1] for d in doc_data) assert stats.total_documents_public == doc_data[0][1] assert stats.total_documents_organization == doc_data[1][1] assert stats.total_documents_private == doc_data[2][1] assert stats.total_documents_invisible == doc_data[3][1] assert stats.total_pages == sum(d[1] * d[2] for d in doc_data) assert stats.total_pages_public == doc_data[0][1] * doc_data[0][2] assert stats.total_pages_organization == doc_data[1][1] * doc_data[1][2] assert stats.total_pages_private == doc_data[2][1] * doc_data[2][2] assert stats.total_pages_invisible == doc_data[3][1] * doc_data[3][2] assert stats.total_notes == sum(n[1] for n in note_data) assert stats.total_notes_public == note_data[0][1] assert stats.total_notes_organization == note_data[1][1] assert stats.total_notes_private == note_data[2][1] assert stats.total_notes_invisible == note_data[3][1] assert stats.total_projects == num_projects # one repeat user on public documents assert stats.total_users_uploaded == sum(d[1] for d in doc_data) - 1 assert stats.total_users_public_uploaded == doc_data[0][1] - 1 assert stats.total_users_organization_uploaded == doc_data[1][1] assert stats.total_users_private_uploaded == doc_data[2][1] # two repeat organizations on public documents assert stats.total_organizations_uploaded == sum(d[1] for d in doc_data) - 2 assert stats.total_organizations_public_uploaded == doc_data[0][1] - 2 assert stats.total_organizations_organization_uploaded == doc_data[1][1] assert stats.total_organizations_private_uploaded == doc_data[2][1]
def note(): return NoteFactory()