Skip to content

Commit

Permalink
feature(models): Tests pass with ids removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
acederberg committed Sep 5, 2024
1 parent eb6c7be commit 57d00b8
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 110 deletions.
4 changes: 2 additions & 2 deletions src/captura/controllers/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def check_one(collection: Collection) -> Collection:

match self.method:
case H.GET if allow_public:
if not collection.public and collection.id_user != token_user.id:
if not collection.public and collection.uuid_user != token_user.uuid:
raise ErrAccessCollection.httpexception(
"_msg_private",
403,
Expand All @@ -497,7 +497,7 @@ def check_one(collection: Collection) -> Collection:
)
return collection
case H.GET | H.POST | H.DELETE | H.PUT | H.PATCH:
if token_user.id != collection.id_user:
if token_user.uuid != collection.uuid_user:
raise ErrAccessCollection.httpexception(
"_msg_modify",
403,
Expand Down
26 changes: 13 additions & 13 deletions src/captura/controllers/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ def create_assignment(
data: Data[ResolvedAssignmentDocument] | Data[ResolvedAssignmentCollection],
target: Collection | Document,
) -> Assignment:
id_source_name = f"id_{data.data.kind_source.name}"
id_target_name = f"id_{data.data.kind_target.name}"
id_source_value = data.data.source.id # type: ignore
uuid_source_name = f"uuid_{data.data.kind_source.name}"
uuid_target_name = f"uuid_{data.data.kind_target.name}"
uuid_source_value = data.data.source.uuid # type: ignore

kwargs = {
"uuid": secrets.token_urlsafe(8),
id_source_name: id_source_value,
id_target_name: target.id,
uuid_source_name: uuid_source_value,
uuid_target_name: target.uuid,
}
return Assignment(
**kwargs,
Expand Down Expand Up @@ -414,17 +414,17 @@ def create_grant(
case bad:
raise ValueError(f"Invalid source `{bad}`.")

id_source_name = f"id_{data.data.kind_source.name}"
id_target_name = f"id_{data.data.kind_target.name}"
id_source_value = data.data.source.id # type: ignore
uuid_source_name = f"uuid_{data.data.kind_source.name}"
uuid_target_name = f"uuid_{data.data.kind_target.name}"
uuid_source_value = data.data.source.uuid # type: ignore

kwargs = {
"uuid": secrets.token_urlsafe(8),
"uuid_parent": grant_parent_uuid,
"pending_from": pending_from,
"pending": True,
id_source_name: id_source_value,
id_target_name: target.id,
uuid_source_name: uuid_source_value,
uuid_target_name: target.uuid,
}
return Grant(**kwargs, **self.create_data.model_dump())

Expand Down Expand Up @@ -512,8 +512,8 @@ def document(
data.data.token_user_grants = {
user.uuid: (
Grant(
id_user=user.id,
id_document=document.id,
uuid_user=user.uuid,
uuid_document=document.uuid,
level=Level.own,
pending=False,
pending_from=PendingFrom.created,
Expand Down Expand Up @@ -748,7 +748,7 @@ def collection(self, data: Data[ResolvedCollection]) -> Data[ResolvedCollection]
session,
param.uuid_user,
)
collection.id_user = user.id
collection.uuid_user = user.uuid
data.event.children.append(
Event(
**self.event_common,
Expand Down
8 changes: 4 additions & 4 deletions src/captura/controllers/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ def split_assocs(
model_assoc.uuid.in_(data.data.uuid_assoc),
and_(
model_target.uuid.in_(data.data.uuid_target),
getattr(model_assoc, "id_" + data.data.kind_source.name)
== data.data.source.id,
getattr(model_assoc, "uuid_" + data.data.kind_source.name)
== data.data.source.uuid,
),
)
)
Expand Down Expand Up @@ -474,7 +474,7 @@ def _collection(
.join(Document)
.where(
Document.uuid.in_(uuids(documents)), # type: ignore[type-var]
Assignment.id_collection == collection.id,
Assignment.uuid_collection == collection.uuid,
)
)
assignments = {
Expand Down Expand Up @@ -591,7 +591,7 @@ def _document(
select(Assignment)
.join(Collection)
.where(
Assignment.id_document == document.id,
Assignment.uuid_document == document.uuid,
Collection.uuid.in_(uuid_collections),
)
)
Expand Down
4 changes: 0 additions & 4 deletions src/captura/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ class BaseSecondarySchema(BaseSchema): ...


class BasePrimaryTableExtraSchema(BaseSchema):
id: fields.FieldID
deleted: fields.FieldDeleted


Expand Down Expand Up @@ -450,9 +449,6 @@ def enum_as_name(item: enum.Enum): # type: ignore

# Metadata
uuid_parent: Optional[fields.FieldUUID] = None
uuid_user_granter: Optional[fields.FieldUUID] = (
None # should it reeally be optional
)


class GrantExtraSchema(GrantSchema):
Expand Down
1 change: 0 additions & 1 deletion src/captura/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def test_assets(cls, v: str) -> str:
@classmethod
def simulatus_assets(cls, v: str) -> str:
o = path.join(PATH_SIMULATUS_ASSETS, v)
print(o)
return o

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions src/captura/views/assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def get_assignments_document(
select(Collection)
.join(Assignment)
.where(
Assignment.id_document == document.id,
Assignment.uuid_document == document.uuid,
Assignment.deleted == false(),
Collection.deleted == false(),
)
Expand Down Expand Up @@ -288,7 +288,7 @@ def get_assignments_collection(
.where(
Document.deleted == false(),
Assignment.deleted == false(),
Assignment.id_collection == collection.id,
Assignment.uuid_collection == collection.uuid,
)
)
if uuid_document is not None:
Expand Down
2 changes: 1 addition & 1 deletion src/captura/views/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def post_document(
(document,) = data_create.data.documents

grant, *_ = data_create.data.token_user_grants.values()
grant.id_document = document.id
grant.uuid_document = document.uuid
create.session.add(grant)
create.session.commit()

Expand Down
6 changes: 3 additions & 3 deletions src/simulatus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def get_collections_retry_callback(self):
session = self.session
logger.warning("Calling `get_collections_retry_callback`.")

collection = Mk.collection(id_user=self.user.uuid)
collection = Mk.collection(uuid_user=self.user.uuid)
session.add(collection)
session.commit()
session.refresh(collection)
Expand Down Expand Up @@ -666,7 +666,7 @@ def callback(q):

if order_by_document_count:
q_ids = (
select(Collection.uuid.label("id_collection"))
select(Collection.uuid.label("uuid_collection"))
.join(Assignment)
.group_by(Collection.uuid)
.having(func.count(Assignment.uuid_document) > 0)
Expand Down Expand Up @@ -912,7 +912,7 @@ def get_data_secondary(

# NOTE: Get assocs. Assocs are always labeled by their
model_assoc = resolve_model(Resolved.kind_assoc) # type: ignore
uuid_source_name = f"id_{Resolved._attr_name_source}"
uuid_source_name = f"uuid_{Resolved._attr_name_source}"
uuid_target_name = f"uuid_{Resolved.kind_target.name}"

q = (
Expand Down
12 changes: 6 additions & 6 deletions src/simulatus/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def content(self, v: Any) -> None:
@classmethod
def q_flat(cls, *additional_fields):
return select(
User.id.label("id_user"),
User.uuid.label("id_user"),
cls.uuid.label("uuid"),
cls.uuid_parent.label("uuid_parent"),
cls.uuid_user.label("uuid_user"),
Expand All @@ -271,16 +271,16 @@ def q_content_data_count(cls, user: User | None = None):
q_assignment = select(func.count(Assignment.uuid))

if user is not None:
q_document = select(Grant.id_document).where(
q_document = select(Grant.uuid_document).where(
Grant.pending_from == fields.PendingFrom.created,
Grant.id_user == user.id,
Grant.uuid_user == user.uuid,
)
q_document = select(func.count()).select_from(q_document.subquery())
q_collection = q_collection.join(User).where(User.uuid == user.uuid)
q_assignment = q_assignment.join(Collection).where(
Collection.id_user == user.id
Collection.uuid_user == user.uuid
)
q_grant = q_grant.where(Grant.id_user == user.id)
q_grant = q_grant.where(Grant.uuid_user == user.uuid)
q_event = select(func.count(Event.uuid)).where(Event.uuid_user == user.uuid)

return select(
Expand Down Expand Up @@ -335,7 +335,7 @@ def q_select(cls, user: User | None = None):
.group_by(User.uuid, *_grant_agg)
)
if user is not None:
q_reports_grants = q_reports_grants.where(Grant.id_user == user.id)
q_reports_grants = q_reports_grants.where(Grant.uuid_user == user.uuid)

count_per_user: Any = literal_column("count_per_user")
res_columns: Tuple[Any, ...] = (
Expand Down
14 changes: 7 additions & 7 deletions tests/test_controllers/test_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def test_d_fn(self) -> None:
# quser = (
# select(Document.uuid)
# .select_from(Document)
# .join(Grant, onclause=Grant.id_document == Document.id)
# .join(User, onclause=User.id == Grant.id_user)
# .join(Grant, onclause=Grant.uuid_document == Document.uuid)
# .join(User, onclause=User.uuid == Grant.uuid_user)
# .where(
# User.uuid == "000-000-000",
# Grant.level >= level.value,
Expand Down Expand Up @@ -528,7 +528,7 @@ def test_overloads(self, dummy: DummyProvider, count):
def test_private(self, dummy: DummyProvider, count):
(collection,) = dummy.get_collections(1)
(collection_other,) = dummy.get_collections(1, other=True)
assert collection_other.id_user != dummy.user.id
assert collection_other.uuid_user != dummy.user.uuid

collection.deleted, collection.public = False, False
collection_other.deleted, collection_other.public = False, False
Expand Down Expand Up @@ -560,7 +560,7 @@ def test_private(self, dummy: DummyProvider, count):

# NOTE: User can access their own private collection
res = access.collection(collection.uuid)
assert res.id_user == access.token_user.id
assert res.uuid_user == access.token_user.uuid
assert collection.uuid == res.uuid

# TODO: Private users cannot have public collections. How to resolve?
Expand Down Expand Up @@ -662,7 +662,7 @@ def test_modify(self, dummy: DummyProvider, count):
# Can when owner, impersonate owner.
collection_res = access.collection(collection.uuid)
assert collection_res.uuid == collection.uuid
assert collection.id_user == dummy.user.id
assert collection.uuid_user == dummy.user.uuid


class TestAccessDocument(BaseTestAccess):
Expand All @@ -675,8 +675,8 @@ def test_document_other(self, dummy: DummyProvider):
for document in dummy.get_documents(25, other=True):
n_grants = dummy.session.scalar(
select(func.count(Grant.uuid)).where(
Grant.id_user == dummy.user.id,
Grant.id_document == document.id,
Grant.uuid_user == dummy.user.uuid,
Grant.uuid_document == document.uuid,
)
)
assert not n_grants
Expand Down
6 changes: 3 additions & 3 deletions tests/test_controllers/test_assoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_split_assocs(self, dummy: DummyProvider, count: int):
assoc := data.data.assoc.get(uuid_assoc)
) is not None, "All assocs should be in data."
assert assoc.deleted is True
assert assoc.id_document == data.data.document.id
assert assoc.uuid_document == data.data.document.uuid
uuid_target_deleted.add(assoc.uuid_collection)

uuid_target_active = set()
Expand All @@ -55,7 +55,7 @@ def test_split_assocs(self, dummy: DummyProvider, count: int):
select(func.count(Assignment.uuid))
.join(Collection)
.where(
Assignment.id_document == data.data.document.id,
Assignment.uuid_document == data.data.document.uuid,
Collection.uuid == uuid_target,
)
)
Expand Down Expand Up @@ -214,7 +214,7 @@ def test_grant_document(self, dummy: DummyProvider, count: int):
.join(Document)
.where(
User.uuid.in_(data.data.uuid_users),
Document.id == data.data.document.id,
Document.uuid == data.data.document.uuid,
)
)
assert session.scalar(q) == 0
Expand Down
Loading

0 comments on commit 57d00b8

Please sign in to comment.