Skip to content

Commit

Permalink
fixing the problem with create_discord_mapping and adding test case t…
Browse files Browse the repository at this point in the history
…o validate
  • Loading branch information
Paul Philion committed Apr 1, 2024
1 parent 7783cf7 commit 8aa577f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
6 changes: 3 additions & 3 deletions cog_scn.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def callback(self, interaction: discord.Interaction):
if user is None:
log.error(f"Unable to create user from {first}, {last}, {email}, {interaction.user.name}")
else:
self.redmine.create_discord_mapping(user.login, interaction.user.name)
self.redmine.user_mgr.create_discord_mapping(user, interaction.user.name)
await interaction.response.send_message(embeds=[embed])


Expand Down Expand Up @@ -82,14 +82,14 @@ async def add(self, ctx:discord.ApplicationContext, redmine_login:str, member:di
else:
user = self.redmine.user_mgr.find(redmine_login)
if user:
self.redmine.create_discord_mapping(redmine_login, discord_name)
self.redmine.user_mgr.create_discord_mapping(user, discord_name)
await ctx.respond(f"Discord user: {discord_name} has been paired with redmine user: {redmine_login}")
else:
# no user exists for that login
modal = NewUserModal(self.redmine, title="Create new user in Redmine")
await ctx.send_modal(modal)
# reindex users after changes
self.redmine.reindex_users()
self.redmine.user_mgr.reindex_users()


async def sync_thread(self, thread:discord.Thread):
Expand Down
14 changes: 14 additions & 0 deletions test_cog_scn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def setUp(self):
self.cog:SCNCog = self.bot.cogs["SCNCog"] # Note class name, note filename.


async def test_add_self(self):
# invoke "add" to add a discord mapping for the test user.
# setup: remove existing mapping
discord_id = self.user.discord_id
self.user_mgr.remove_discord_mapping(self.user)
self.user_mgr.reindex_users() # to remove the discord ID from the cache

ctx = self.build_context()
await self.cog.add(ctx, self.user.login) # invoke cog to add uer

expected = f"Discord user: {discord_id} has been paired with redmine user: {self.user.login}"
ctx.respond.assert_called_with(expected)


async def test_team_join_leave(self):
test_team_name = "test-team"

Expand Down
11 changes: 10 additions & 1 deletion users.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,23 @@ def remove(self, user: User):


def create_discord_mapping(self, user:User, discord_name:str) -> User:
field_id = 2 ## "Discord ID"search for me in cached custom fields
field_id = 2 ## FIXME "Discord ID"search for me in cached custom fields
fields = {
"custom_fields": [
{ "id": field_id, "value": discord_name } # cf_4, custom field syncdata
]
}
return self.update(user, fields)

# for testing
def remove_discord_mapping(self, user:User) -> User:
field_id = 2 ## FIXME "Discord ID"search for me in cached custom fields
fields = {
"custom_fields": [
{ "id": field_id, "value": "" }
]
}
return self.update(user, fields)

def get_all_teams(self, include_users: bool = True) -> dict[str, Team]:
resp = self.session.get(f"{TEAM_RESOURCE}?limit=100")
Expand Down

0 comments on commit 8aa577f

Please sign in to comment.