Skip to content

Commit

Permalink
Merge pull request #398 from KoalaBotUK/fix/base-cog-api
Browse files Browse the repository at this point in the history
fix: base cog api now works
  • Loading branch information
JayDwee committed Apr 1, 2023
2 parents b7822ad + 21aa650 commit 9150cd8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 72 deletions.
10 changes: 4 additions & 6 deletions koala/cogs/base/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,14 @@ async def get_version(self):
return core.get_version()

@parse_request
async def post_load_cog(self, extension, package):
async def post_load_cog(self, extension):
"""
Loads a cog from the cogs folder
:param extension: name of the cog
:param package: package of the cogs
:return:
"""
try:
await core.load_cog(self._bot, extension, package)
await core.load_cog(self._bot, extension)
except BaseException as e:
error = 'Error loading cog: {}'.format(handleActivityError(e))
logger.error(error)
Expand All @@ -147,15 +146,14 @@ async def post_load_cog(self, extension, package):
return {'message': 'Cog loaded'}

@parse_request
async def post_unload_cog(self, extension, package):
async def post_unload_cog(self, extension):
"""
Unloads a cog from the cogs folder
:param extension: name of the cog
:param package: package of the cogs
:return:
"""
try:
await core.unload_cog(self._bot, extension, package)
await core.unload_cog(self._bot, extension)
except BaseException as e:
error = 'Error unloading cog: {}'.format(handleActivityError(e))
logger.error(error)
Expand Down
4 changes: 2 additions & 2 deletions koala/cogs/base/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async def load_cog(self, ctx, extension):
:param ctx: Context of the command
:param extension: The name of the cog
"""
await ctx.send(await core.load_cog(self.bot, extension, koalabot.COGS_PACKAGE))
await ctx.send(await core.load_cog(self.bot, extension))

@commands.command(name="unloadCog", aliases=["unload_cog"])
@commands.check(koalabot.is_owner)
Expand All @@ -198,7 +198,7 @@ async def unload_cog(self, ctx, extension):
:param ctx: Context of the command
:param extension: The name of the cog
"""
await ctx.send(await core.unload_cog(self.bot, extension, koalabot.COGS_PACKAGE))
await ctx.send(await core.unload_cog(self.bot, extension))

@commands.command(name="enableExt", aliases=["enable_koala_ext"])
@commands.check(koalabot.is_admin)
Expand Down
8 changes: 4 additions & 4 deletions koala/cogs/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,18 @@ async def purge(bot: Bot, channel_id, amount):
return await channel.purge(limit=amount+1)


async def load_cog(bot: Bot, extension, package):
async def load_cog(bot: Bot, extension):
"""
Loads a cog from the cogs folder
:param extension:
:param package:
:return:
"""
await bot.load_extension("."+extension, package=package)
await bot.load_extension("."+extension, package=koalabot.COGS_PACKAGE)
return f'{extension} Cog Loaded'


async def unload_cog(bot: Bot, extension, package):
async def unload_cog(bot: Bot, extension):
"""
Unloads a cog from the cogs folder
:param extension:
Expand All @@ -148,7 +148,7 @@ async def unload_cog(bot: Bot, extension, package):
if extension == "base" or extension == "BaseCog":
raise discord.ext.commands.errors.ExtensionError(message=f"Sorry, you can't unload the base cog", name=extension)
else:
await bot.unload_extension("."+extension, package=package)
await bot.unload_extension("."+extension, package=koalabot.COGS_PACKAGE)
return f'{extension} Cog Unloaded'


Expand Down
9 changes: 7 additions & 2 deletions koala/rest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ def build_response(status_code, data):
:param data:
:return:
"""
if data is not None:
body = json.dumps(data, cls=EnhancedJSONEncoder)
else:
body = None

return aiohttp.web.Response(status=status_code,
body=json.dumps(data, cls=EnhancedJSONEncoder),
body=body,
content_type='application/json')


Expand Down Expand Up @@ -92,7 +97,7 @@ async def wrapper(*args, **kwargs):
available_args = {}

if (request.method == "POST" or request.method == "PUT") and request.has_body:
body = await request.post()
body = await request.json()
for arg in wanted_args:
if arg in body:
available_args[arg] = body[arg]
Expand Down
96 changes: 48 additions & 48 deletions tests/cogs/base/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def test_get_activities_missing_param(api_client):
'''

async def test_put_schedule_activity(api_client):
resp = await api_client.put('/scheduled-activity', data=(
resp = await api_client.put('/scheduled-activity', json=(
{
'activity_type': 'playing',
'message': 'test',
Expand All @@ -67,7 +67,7 @@ async def test_put_schedule_activity(api_client):


async def test_put_schedule_activity_missing_param(api_client):
resp = await api_client.put('/scheduled-activity', data=(
resp = await api_client.put('/scheduled-activity', json=(
{
'activity_type': 'playing',
'message': 'test',
Expand All @@ -80,7 +80,7 @@ async def test_put_schedule_activity_missing_param(api_client):


async def test_put_schedule_activity_bad_activity(api_client):
resp = await api_client.put('/scheduled-activity', data=(
resp = await api_client.put('/scheduled-activity', json=(
{
'activity_type': 'invalidActivity',
'message': 'test',
Expand All @@ -93,7 +93,7 @@ async def test_put_schedule_activity_bad_activity(api_client):


async def test_put_schedule_activity_bad_start_time(api_client):
resp = await api_client.put('/scheduled-activity', data=(
resp = await api_client.put('/scheduled-activity', json=(
{
'activity_type': 'playing',
'message': 'test',
Expand All @@ -106,7 +106,7 @@ async def test_put_schedule_activity_bad_start_time(api_client):


async def test_put_schedule_activity_bad_end_time(api_client):
resp = await api_client.put('/scheduled-activity', data=(
resp = await api_client.put('/scheduled-activity', json=(
{
'activity_type': 'invalidActivity',
'message': 'test',
Expand All @@ -125,7 +125,7 @@ async def test_put_schedule_activity_bad_end_time(api_client):


async def test_put_set_activity(api_client):
resp = await api_client.put('/activity', data=(
resp = await api_client.put('/activity', json=(
{
'activity_type': 'playing',
'name': 'test',
Expand All @@ -138,7 +138,7 @@ async def test_put_set_activity(api_client):


async def test_put_set_activity_bad_req(api_client):
resp = await api_client.put('/activity', data=(
resp = await api_client.put('/activity', json=(
{
'activity_type': 'invalidActivity',
'name': 'test',
Expand All @@ -149,7 +149,7 @@ async def test_put_set_activity_bad_req(api_client):


async def test_put_set_activity_missing_param(api_client):
resp = await api_client.put('/activity', data=(
resp = await api_client.put('/activity', json=(
{
'activity_type': 'invalidActivity',
'url': 'test.com'
Expand Down Expand Up @@ -203,53 +203,53 @@ async def test_get_support_link(api_client):
'''

async def test_post_load_cog(api_client):
resp = await api_client.post('/load-cog', data=(
resp = await api_client.post('/load-cog', json=(
{
'extension': 'announce',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))
assert resp.status == OK
text = await resp.text()
assert text == '{"message": "Cog loaded"}'

async def test_post_load_base_cog(api_client):
resp = await api_client.post('/load-cog', data=(
resp = await api_client.post('/load-cog', json=(
{
'extension': 'base',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))
assert resp.status == OK
text = await resp.text()
assert text == '{"message": "Cog loaded"}'

async def test_post_load_cog_bad_req(api_client):
resp = await api_client.post('/load-cog', data=(
resp = await api_client.post('/load-cog', json=(
{
'extension': 'invalidCog',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))
assert resp.status == UNPROCESSABLE_ENTITY
assert await resp.text() == '422: Error loading cog: Invalid extension'

async def test_post_load_cog_missing_param(api_client):
resp = await api_client.post('/load-cog', data=(
{
'extension': 'invalidCog'
}))
assert resp.status == BAD_REQUEST
assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"
# async def test_post_load_cog_missing_param(api_client):
# resp = await api_client.post('/load-cog', json=(
# {
# 'extension': 'invalidCog'
# }))
# assert resp.status == BAD_REQUEST
# assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"

async def test_post_load_cog_already_loaded(api_client):
await api_client.post('/load-cog', data=(
await api_client.post('/load-cog', json=(
{
'extension': 'announce',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))

resp = await api_client.post('/load-cog', data=(
resp = await api_client.post('/load-cog', json=(
{
'extension': 'announce',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))
assert resp.status == UNPROCESSABLE_ENTITY
assert await resp.text() == '422: Error loading cog: Already loaded'
Expand All @@ -261,43 +261,43 @@ async def test_post_load_cog_already_loaded(api_client):
'''

async def test_post_unload_cog(api_client):
await api_client.post('/load-cog', data=(
await api_client.post('/load-cog', json=(
{
'extension': 'announce',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))

resp = await api_client.post('/unload-cog', data=(
resp = await api_client.post('/unload-cog', json=(
{
'extension': 'announce',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))
assert resp.status == OK
text = await resp.text()
assert text == '{"message": "Cog unloaded"}'

async def test_post_unload_cog_not_loaded(api_client):
resp = await api_client.post('/unload-cog', data=(
resp = await api_client.post('/unload-cog', json=(
{
'extension': 'announce',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))
assert resp.status == UNPROCESSABLE_ENTITY
assert await resp.text() == '422: Error unloading cog: Extension not loaded'

async def test_post_unload_cog_missing_param(api_client):
resp = await api_client.post('/unload-cog', data=(
{
'extension': 'invalidCog'
}))
assert resp.status == BAD_REQUEST
assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"
# async def test_post_unload_cog_missing_param(api_client):
# resp = await api_client.post('/unload-cog', json=(
# {
# 'extension': 'invalidCog'
# }))
# assert resp.status == BAD_REQUEST
# assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"

async def test_post_unload_base_cog(api_client):
resp = await api_client.post('/unload-cog', data=(
resp = await api_client.post('/unload-cog', json=(
{
'extension': 'BaseCog',
'package': koalabot.COGS_PACKAGE
# 'package': koalabot.COGS_PACKAGE
}))
assert resp.status == UNPROCESSABLE_ENTITY
text = await resp.text()
Expand All @@ -313,7 +313,7 @@ async def test_post_unload_base_cog(api_client):
async def test_post_enable_extension(api_client, bot):
await koalabot.load_all_cogs(bot)
guild: discord.Guild = dpytest.get_config().guilds[0]
resp = await api_client.post('/enable-extension', data=({
resp = await api_client.post('/enable-extension', json=({
'guild_id': guild.id,
'koala_ext': 'Announce'
}))
Expand All @@ -325,7 +325,7 @@ async def test_post_enable_extension(api_client, bot):
async def test_post_enable_extension_bad_req(api_client):
guild: discord.Guild = dpytest.get_config().guilds[0]

resp = await api_client.post('/enable-extension', data=(
resp = await api_client.post('/enable-extension', json=(
{
'guild_id': guild.id,
'koala_ext': 'Invalid Extension'
Expand All @@ -336,7 +336,7 @@ async def test_post_enable_extension_bad_req(api_client):

async def test_post_enable_extension_missing_param(api_client):
guild: discord.Guild = dpytest.get_config().guilds[0]
resp = await api_client.post('/enable-extension', data=(
resp = await api_client.post('/enable-extension', json=(
{
'guild_id': guild.id
}))
Expand All @@ -354,13 +354,13 @@ async def test_post_enable_extension_missing_param(api_client):
async def test_post_disable_extension(api_client, bot):
await koalabot.load_all_cogs(bot)
guild: discord.Guild = dpytest.get_config().guilds[0]
setup = await api_client.post('/enable-extension', data=({
setup = await api_client.post('/enable-extension', json=({
'guild_id': guild.id,
'koala_ext': 'Announce'
}))
assert setup.status == OK

resp = await api_client.post('/disable-extension', data=({
resp = await api_client.post('/disable-extension', json=({
'guild_id': guild.id,
'koala_ext': 'Announce'
}))
Expand All @@ -370,7 +370,7 @@ async def test_post_disable_extension(api_client, bot):

async def test_post_disable_extension_not_enabled(api_client):
guild: discord.Guild = dpytest.get_config().guilds[0]
resp = await api_client.post('/disable-extension', data=({
resp = await api_client.post('/disable-extension', json=({
'guild_id': guild.id,
'koala_ext': 'Announce'
}))
Expand All @@ -380,7 +380,7 @@ async def test_post_disable_extension_not_enabled(api_client):

async def test_post_disable_extension_missing_param(api_client):
guild: discord.Guild = dpytest.get_config().guilds[0]
resp = await api_client.post('/disable-extension', data=({
resp = await api_client.post('/disable-extension', json=({
'guild_id': guild.id
}))
assert resp.status == BAD_REQUEST
Expand All @@ -390,7 +390,7 @@ async def test_post_disable_extension_missing_param(api_client):
async def test_post_disable_extension_bad_req(api_client):
guild: discord.Guild = dpytest.get_config().guilds[0]

resp = await api_client.post('/disable-extension', data=(
resp = await api_client.post('/disable-extension', json=(
{
'guild_id': guild.id,
'koala_ext': 'Invalid Extension'
Expand Down
Loading

0 comments on commit 9150cd8

Please sign in to comment.