def test_RedditAuthFail(self): def raiseError(): raise Exception('unexpected') try: # backup existing praw ini, create our own if os.path.isfile('praw.ini'): os.rename('praw.ini', '_praw.ini') with open('praw.ini', 'w', newline="\n") as f: f.write('[testbot]\n') f.write('check_for_updates=false\n') f.write('client_id=badid\n') f.write('client_secret=badsecret\n') f.write('refresh_token=badtoken\n') f.write('user_agent=praw:hearthscan-test:1.0 (by /u/b0ne123)') Config.CONFIG = None with self.assertRaises(prawcore.exceptions.ResponseException), \ TempFile('db') as seenDB: RedditBot(subreddits=[], newLimit=1, sleep=0, connectAttempts=1, iniSite='testbot', dbName=seenDB) \ .run(raiseError) finally: removeFile('praw.ini') if os.path.isfile('_praw.ini'): os.rename('_praw.ini', 'praw.ini')
def main(): log.debug('main() hearthscan-bot starting') # load constant values constants = Constants() # init answered comments sqlite DB answeredDB = commentDB.DB() # load card DB url = 'https://raw.githubusercontent.com/d-schmidt/hearthscan-bot/master/data/tempinfo.json' cardDB = CardDB(constants=constants, tempJSONUrl=url) # init hs helper for hearthstone stuff helper = HSHelper(cardDB, constants) # pm spam filter cache pmUserCache = {} def submissionListener(r, submission): answerSubmission(submission, helper) def commentListener(r, comment): answerComment(r, comment, answeredDB, helper) def mentionListener(r, comment): answerMention(r, comment, answeredDB, helper) def pmListener(r, message): answerPM(r, message, pmUserCache, helper) def postAction(): cleanPMUserCache(pmUserCache) cardDB.refreshTemp() try: RedditBot(subreddits=credentials.subreddits, newLimit=250, connectAttempts=5, userBlacklist=set(credentials.userBlacklist)) \ .withSubmissionListener(submissionListener) \ .withCommentListener(commentListener) \ .withMentionListener(mentionListener) \ .withPMListener(pmListener) \ .run(postAction) except: log.exception('main() RedditBot failed unexpectedly') finally: log.warning('main() leaving hearthscan-bot') answeredDB.close()
def test_RedditAuth(self): # will fail for missing/bad praw.ini with TempFile('db') as seenDB: RedditBot(subreddits=[], newLimit=1, sleep=0, connectAttempts=1, dbName=seenDB) \ .run(lambda: removeFile(RedditBot.LOCK_FILE))