async def test_link_regenerate_code(mocker): """Links the user, and since they already tried to link, regenerate the linking code.""" mocker.patch(OS_MODULE + ".urandom", mocker.Mock(return_value=CODE_URANDOM)) mock_author = tosurnament_mock.UserMock() await mock_author.create_dm() mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( User(discord_id=tosurnament_mock.DEFAULT_USER_MOCK.id, verified=False, code="test")) cog = tosurnament_mock.mock_cog(auth.get_class(mock_bot)) mock_ctx = tosurnament_mock.CtxMock(mock_bot, mock_author) await cog.link(cog, mock_ctx) assert mock_bot.session.add.call_count == 0 user_matcher = tosurnament_mock.Matcher( User( verified=False, code=CODE_ASCII, )) mock_bot.session.update.assert_called_once_with(user_matcher) assert mock_ctx.message.delete.call_count == 1 cog.send_reply.assert_called_once_with( mocker.ANY, "success", CODE_ASCII, channel=tosurnament_mock.DM_CHANNEL_MOCK)
async def test_auth_not_linked(self): """Tries to authenticate the user but they didn't link their osu! account yet.""" bot_mock = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(base.UserNotLinked): await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock))
async def test_auth_already_verified(self): """Tries to authenticate the user but they are already verified.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=True)) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(auth.UserAlreadyVerified): await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock))
async def test_link_already_verified(): """Links the user but they are already verified.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(tosurnament_mock.DEFAULT_USER_STUB) cog = tosurnament_mock.mock_cog(auth.get_class(mock_bot)) with pytest.raises(auth.UserAlreadyVerified): await cog.link(cog, tosurnament_mock.CtxMock(mock_bot))
async def test_link_already_verified(self): """Links the user but they are already verified.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=True)) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(auth.UserAlreadyVerified): await cog.link( cog, tosurnament_mock.CtxMock(bot_mock), osu_name=tosurnament_mock.USER_NAME, )
async def test_link_user_not_found(self, mock_osu): """Links the user but the osu name/id is not found.""" mock_osu.get_user.return_value = None bot_mock = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(base.UserNotFound): await cog.link( cog, tosurnament_mock.CtxMock(bot_mock), osu_name=tosurnament_mock.USER_NAME, )
async def test_auth_osu_find_user_web_page(self, requests_mock): """Tries to authenticate the user but the osu! website is down (or another error).""" requests_get_mock = mock.Mock() requests_get_mock.status_code = 404 requests_mock.get.return_value = requests_get_mock bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=False)) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(base.OsuError): await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock))
async def test_link_regenerate_code(self): """Links the user, and since they already tried to link, regenerate the linking code.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(osu_id=tosurnament_mock.USER_ID, verified=False, code="test", osu_name="")) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) await cog.link(cog, tosurnament_mock.CtxMock(bot_mock), osu_name=tosurnament_mock.USER_NAME) assert bot_mock.session.add.call_count == 0 user_matcher = tosurnament_mock.Matcher( User(osu_id=tosurnament_mock.USER_ID, verified=False, code=CODE_ASCII, osu_name=tosurnament_mock.USER_NAME) ) bot_mock.session.update.assert_called_once_with(user_matcher) cog.send_reply.assert_called_once_with(mock.ANY, mock.ANY, "success", CODE_ASCII)
async def test_link(self): """Links the user.""" bot_mock = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) await cog.link(cog, tosurnament_mock.CtxMock(bot_mock), osu_name=tosurnament_mock.USER_NAME) bot_mock.session.add.assert_called_once_with( tosurnament_mock.Matcher( User(osu_id=tosurnament_mock.USER_ID, code=CODE_ASCII, osu_name=tosurnament_mock.USER_NAME) ) ) assert bot_mock.session.update.call_count == 0 cog.send_reply.assert_called_once_with(mock.ANY, mock.ANY, "success", CODE_ASCII)
async def test_auth_wrong_code(self, requests_mock): """Tries to authenticate the user but the code in location is wrong.""" requests_get_mock = mock.Mock() requests_get_mock.status_code = 200 requests_get_mock.text = 'location":"' requests_mock.get.return_value = requests_get_mock bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=False, code="1234")) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(auth.WrongCodeError): await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock))
async def test_auth_osu_location_not_found(self, requests_mock): """Tries to authenticate the user but the location of the user on the osu! website is not found.""" requests_get_mock = mock.Mock() requests_get_mock.status_code = 200 requests_get_mock.text = "random text" requests_mock.get.return_value = requests_get_mock bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=False)) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(base.OsuError): await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock))
async def test_auth(self, requests_mock): """Authenticates the user.""" requests_get_mock = mock.Mock() requests_get_mock.status_code = 200 requests_get_mock.text = 'location":"1234' requests_mock.get.return_value = requests_get_mock bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=False, code="1234")) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock)) bot_mock.session.update.assert_called_once_with(tosurnament_mock.Matcher(User(verified=True, code="1234"))) cog.send_reply.assert_called_with(mock.ANY, mock.ANY, "success")
async def test_link(mocker): """Links the user.""" mocker.patch(OS_MODULE + ".urandom", mocker.Mock(return_value=CODE_URANDOM)) mock_author = tosurnament_mock.UserMock() mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(auth.get_class(mock_bot)) mock_ctx = tosurnament_mock.CtxMock(mock_bot, mock_author) await cog.link(cog, mock_ctx) mock_bot.session.add.assert_called_once_with( tosurnament_mock.Matcher( User(discord_id_snowflake=tosurnament_mock.DEFAULT_USER_MOCK.id, verified=False, code=CODE_ASCII))) assert mock_bot.session.update.call_count == 0 assert mock_ctx.message.delete.call_count == 1 cog.send_reply.assert_called_once_with( mocker.ANY, "success", CODE_ASCII, channel=tosurnament_mock.DM_CHANNEL_MOCK)