Skip to content

Commit

Permalink
Answer: Adapt to use CONFIG_TEMPLATE
Browse files Browse the repository at this point in the history
Replace the use of environment variables with
config variable

Closes #381
  • Loading branch information
nvzard committed Aug 11, 2018
1 parent a32333d commit 71fca61
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
AUTOINSTALL_DEPS = True

DEFAULT_CONFIG = {
'answer': {
'ANSWER_END': os.environ.get('ANSWER_END'),
},
'LabHub': {
'GH_TOKEN': os.environ.get('GH_TOKEN'),
'GL_TOKEN': os.environ.get('GL_TOKEN'),
Expand Down
9 changes: 6 additions & 3 deletions plugins/answer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import json
import os
from urllib.parse import quote_plus, urljoin

from errbot import BotPlugin, botcmd
from utils.mixin import DefaultConfigMixin
import requests


class Answer(BotPlugin):
class Answer(DefaultConfigMixin, BotPlugin):

CONFIG_TEMPLATE = {
'ANSWER_END': None,
}
# Ignore LineLengthBear, PyCodestyleBear
SURVEY_LINK = 'https://docs.google.com/forms/d/e/1FAIpQLSeD8lqMWAwJx0Mewlpc5Sbeo3MH5Yi9fSfXA6jnk07-aIURSA/viewform?usp=pp_url&entry.1236347280={question}&entry.1734934116={response}&entry.75323266={message_link}'
MESSAGE_LINK = 'https://gitter.im/{uri}?at={idd}'
Expand All @@ -26,7 +29,7 @@ def construct_link(text):
@botcmd
def answer(self, msg, arg):
try:
answers = requests.get(urljoin(os.environ['ANSWER_END'], 'answer'),
answers = requests.get(urljoin(self.config['ANSWER_END'], 'answer'),
params={'question': arg}).json()
except json.JSONDecodeError:
self.log.exception('something went wrong while fetching answer for'
Expand Down
17 changes: 11 additions & 6 deletions tests/answer_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import vcr
import requests_mock

Expand All @@ -9,13 +8,16 @@
class TestAnswer(IsolatedTestCase):

def setUp(self):
super().setUp()
# Ignore InvalidLinkBear
self.answer_end_point = 'http://0.0.0.0:8000'
os.environ['ANSWER_END'] = self.answer_end_point

def tearDown(self):
del os.environ['ANSWER_END']
extra_config = {
'DEFAULT_CONFIG': {
'answer': {
'ANSWER_END': self.answer_end_point,
}
}
}
super().setUp(extra_config=extra_config)

@vcr.use_cassette('tests/cassettes/answer.yaml')
def test_answer(self):
Expand All @@ -24,6 +26,9 @@ def test_answer(self):
self.assertIn('Please checkout the following links', self.pop_message())
self.push_message('!answer shell autocompletion')
self.assertIn('Please checkout the following links', self.pop_message())
self.assertCommand('!plugin config answer',
# Ignore InvalidLinkBear
'{\'ANSWER_END\': \'http://0.0.0.0:8000\'}')

def test_invalid_json(self):
with requests_mock.Mocker() as m:
Expand Down

0 comments on commit 71fca61

Please sign in to comment.