def test_create_url(method, exists): if exists: expected = 'https://slack.com/api/{}'.format(method) assert SlackApi.create_url(method) == expected else: with pytest.raises(SlackApiError): SlackApi.create_url(method)
def test_init(getenv, kwargs, need_token): getenv.return_value = DUMMY_TOKEN if need_token: api = SlackApi.from_env() getenv.assert_called_once_with(SlackApi.TOKEN_ENV_VAR) else: api = SlackApi(**kwargs) assert api.api_token == DUMMY_TOKEN
async def slackSetup(self): dirs = appdirs.AppDirs('slackups', 'slackups') botTokenPath = os.path.join(dirs.user_config_dir, 'bot.token') adminTokenPath = os.path.join(dirs.user_config_dir, 'admin.token') try: botToken = open(botTokenPath, 'r').read().strip() except: logger.exception("Loading bot token %s", botTokenPath) sys.exit(0) try: adminToken = open(adminTokenPath, 'r').read().strip() except: logger.exception("Loading admin token %s", adminTokenPath) sys.exit(0) self.slackAPI = SlackApi(adminToken) self.slackAPI.call = self.slackAPI.execute_method channels = await self.slackAPI.call('channels.list') print("Got channels: " + str(channels)) groups = await self.slackAPI.call('groups.list') print("Got groups: " + str(channels)) ims = await self.slackAPI.call('im.list') print("Got IMs: " + str(ims)) self.bot = await AdminBot.from_api_token(botToken) self.bot.main = self await self.bot.setup()
async def setup(self, token=None, id_=None): if id_: try: dirs = appdirs.AppDirs('slackups', 'slackups') tokencache = os.path.join(dirs.user_cache_dir, str(id_) + '-slack.json') token_ = open(tokencache, 'r').read().strip() except: logger.info("No cached token at " + tokencache) if token == None: return "ERROR: No token" else: token = token_ self.token = token self.client = SlackApi(token) self.client.call = self.client.execute_method res = await self.client.call('auth.test') logger.info(res) if not 'ok' in res or not res['ok'] or not 'user_id' in res: logger.warning("Invalid slack token!") return "ERROR: Invalid token?" self.id_ = res['user_id'] dirs = appdirs.AppDirs('slackups', 'slackups') tokencache = os.path.join(dirs.user_cache_dir, str(self.id_) + '-slack.json') open(tokencache, 'w').write(token) logger.info("Slack user set up! " + self.id_) return "Slack token accepted!"
async def test_execute_method(aiohttp, status, result, error): json_future = asyncio.Future() json_future.set_result(result) resp_future = asyncio.Future() resp_future.set_result( mock.MagicMock(status=status, message='failed', **{'json.return_value': json_future})) aiohttp.get.return_value = resp_future api = SlackApi(api_token=DUMMY_TOKEN) method = 'auth.test' if error is None: assert await api.execute_method(method) == result else: with pytest.raises(error): await api.execute_method(method) aiohttp.get.assert_called_once_with( 'https://slack.com/api/{}?token={}'.format(method, DUMMY_TOKEN))
def test_init(get_api_token, args, need_token): get_api_token.return_value = DUMMY_TOKEN api = SlackApi(*args) assert api.token == DUMMY_TOKEN if need_token: get_api_token.assert_called_once_with()