From 8ea5f90ed429e6065d44b23e0638024d9fe6b2ab Mon Sep 17 00:00:00 2001 From: Paul Philion Date: Thu, 14 Mar 2024 17:34:00 -0700 Subject: [PATCH] ticket 541: better python specing == 3.11, better block testing --- cog_scn.py | 2 +- test_cog_scn.py | 14 +++++++++++++- test_redmine.py | 1 + tickets.py | 2 +- users.py | 18 +++++++++++------- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/cog_scn.py b/cog_scn.py index cab9ec4..9ad815a 100644 --- a/cog_scn.py +++ b/cog_scn.py @@ -234,7 +234,7 @@ async def unblock(self, ctx:discord.ApplicationContext, username:str): log.debug(f"unblocking {username}") user = self.redmine.user_mgr.find(username) if user: - self.redmine.unblock_user(user) + self.redmine.user_mgr.unblock(user) await ctx.respond(f"Unblocked user: {user.login}") else: log.debug("trying to unblock unknown user '{username}', ignoring") diff --git a/test_cog_scn.py b/test_cog_scn.py index b2cf6ec..4d58380 100755 --- a/test_cog_scn.py +++ b/test_cog_scn.py @@ -109,7 +109,6 @@ async def test_block_user(self): # call block ctx = self.build_context() await self.cog.block(ctx, self.user.login) - self.redmine.user_mgr.reindex_teams() # confirmed blocked self.assertTrue(self.redmine.user_mgr.is_blocked(self.user)) @@ -122,6 +121,19 @@ async def test_block_user(self): self.redmine.remove_ticket(ticket.id) + async def test_unblock_user(self): + # call block + ctx = self.build_context() + await self.cog.block(ctx, self.user.login) + + # confirmed blocked + self.assertTrue(self.redmine.user_mgr.is_blocked(self.user)) + + # unblock + await self.cog.unblock(ctx, self.user.login) + self.assertFalse(self.redmine.user_mgr.is_blocked(self.user)) + + async def test_locked_during_sync_ticket(self): """ Unhandled exception in internal background task 'sync_all_threads'. diff --git a/test_redmine.py b/test_redmine.py index 15409d6..846295e 100755 --- a/test_redmine.py +++ b/test_redmine.py @@ -39,6 +39,7 @@ def test_blocked_create_ticket(self): # create ticket for blocked ticket = self.create_test_ticket() self.assertIsNotNone(ticket) + log.info(f"ticket: {ticket}") self.assertEqual("Reject", ticket.status.name) # remove the ticket and unbluck the user diff --git a/tickets.py b/tickets.py index f8c659d..4be9126 100644 --- a/tickets.py +++ b/tickets.py @@ -297,7 +297,7 @@ def create(self, user: User, message: Message) -> Ticket: "content_type": a.content_type, }) - log.debug(f"### data: {data}") + #log.debug(f"POST data: {data}") response = self.session.post(ISSUES_RESOURCE, json.dumps(data), user.login) # check status diff --git a/users.py b/users.py index 2119fd6..e5e7d2d 100644 --- a/users.py +++ b/users.py @@ -425,8 +425,11 @@ def is_user_in_team(self, user: User, teamname:str) -> bool: return False team = self.get_team_by_name(teamname) + return self.is_user_in(user, team) - if team: + + def is_user_in(self, user: User, team:Team) -> bool: + if user and team: for team_user in team.users: if team_user.id == user.id: return True @@ -456,16 +459,17 @@ def unblock(self, user) -> None: def join_team(self, user: User, teamname:str) -> None: - # look up user ID - #user = self.find_user(username) - #if user is None: - # raise RedmineException(f"Unknown user name: {username}", "[n/a]") - # map teamname to team team = self.get_team_by_name(teamname) - if team.id is None: + if team is None: raise RedmineException(f"Unknown team name: {teamname}", "[n/a]") + # calling POST when the user is already in the team results in a + # 422 Unprocessable Entity error, so checking first. + # Expected behavior is idempotent: if already in the team, return as expected (no exception) + if self.is_user_in(user, team): + return # already done + # POST to /group/ID/users.json data = { "user_id": user.id