Skip to content

Commit

Permalink
Fix unit test and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
weiiwang01 committed Jan 22, 2025
1 parent c110a56 commit a65d5c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
37 changes: 20 additions & 17 deletions src/opencti.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import gql
import gql.dsl
import gql.transport.requests
import graphql


class OpenctiUser(typing.NamedTuple):
Expand Down Expand Up @@ -65,9 +66,11 @@ def __init__(self, url: str, api_token: str) -> None:
transport=transport,
schema=(pathlib.Path(__file__).parent / "opencti.graphql").read_text(),
)
self._dsl_schema = gql.dsl.DSLSchema(self._client.schema)
self._dsl_schema = gql.dsl.DSLSchema(
typing.cast(graphql.GraphQLSchema, self._client.schema)
)

@functools.lru_cache(maxsize=None)
@functools.lru_cache(maxsize=10)
def list_users(self, name_starts_with: str | None = None) -> list[OpenctiUser]:
"""List OpenCTI users.
Expand Down Expand Up @@ -145,15 +148,15 @@ def create_user(
query = gql.dsl.dsl_gql(
gql.dsl.DSLMutation(
self._dsl_schema.Mutation.userAdd.args(
input=dict(
name=name,
user_email=user_email,
first_name="",
last_name="",
password=secrets.token_urlsafe(32),
account_status="Active",
groups=groups,
)
input={
"name": name,
"user_email": user_email,
"first_name": "",
"last_name": "",
"password": secrets.token_urlsafe(32),
"account_status": "Active",
"groups": groups,
}
).select(
self._dsl_schema.User.id,
self._dsl_schema.User.name,
Expand All @@ -173,7 +176,7 @@ def create_user(
api_token=user["api_token"],
)

@functools.lru_cache(maxsize=None)
@functools.lru_cache(maxsize=10)
def list_groups(self) -> list[OpenctiGroup]:
"""List OpenCTI groups.
Expand Down Expand Up @@ -215,11 +218,11 @@ def set_account_status(
self._dsl_schema.Mutation.userEdit(id=user_id).select(
self._dsl_schema.UserEditMutations.fieldPatch(
input=[
dict(
key="account_status",
value=status,
operation="replace",
)
{
"key": "account_status",
"value": status,
"operation": "replace",
}
]
).select(self._dsl_schema.User.id)
)
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ class OpenctiClientMock:
def __init__(self, *_args, **_kwargs):
"""Initialize OpenctiClientMock."""

def list_users(self) -> list[OpenctiUser]:
def list_users(self, name_starts_with: str | None = None) -> list[OpenctiUser]:
"""List OpenCTI users.
Args:
name_starts_with: Name starts with this string.
Returns:
A list of OpenctiUser objects.
"""
return [OpenctiUser(**u) for u in self._users]
return [
OpenctiUser(**u) for u in self._users if u["name"].startswith(name_starts_with or "")
]

def list_groups(self) -> list[OpenctiGroup]:
"""List OpenCTI groups.
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ commands =
# codespell {[vars]lib_path}
codespell {toxinidir} --skip {toxinidir}/.git --skip {toxinidir}/.tox \
--skip {toxinidir}/build --skip {toxinidir}/lib --skip {toxinidir}/venv \
--skip {toxinidir}/.mypy_cache --skip {toxinidir}/icon.svg
--skip {toxinidir}/.mypy_cache --skip {toxinidir}/icon.svg \
--skip {toxinidir}/src/opencti.graphql
# pflake8 wrapper supports config from pyproject.toml
pflake8 {[vars]all_path} --ignore=W503
isort --check-only --diff {[vars]all_path}
Expand Down

0 comments on commit a65d5c1

Please sign in to comment.