Skip to content

Commit faafb25

Browse files
committed
Answer: Adapt to use CONFIG_TEMPLATE
Replace the use of environment variables with config variable Closes #381
1 parent 4f2b470 commit faafb25

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
AUTOINSTALL_DEPS = True
114114

115115
DEFAULT_CONFIG = {
116+
'answer': {
117+
'ANSWER_END': os.environ.get('ANSWER_END'),
118+
},
116119
'LabHub': {
117120
'GH_TOKEN': os.environ.get('GH_TOKEN'),
118121
'GL_TOKEN': os.environ.get('GL_TOKEN'),

plugins/answer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import json
2-
import os
32
from urllib.parse import quote_plus, urljoin
43

54
from errbot import BotPlugin, botcmd
5+
from utils.mixin import DefaultConfigMixin
66
import requests
77

88

9-
class Answer(BotPlugin):
9+
class Answer(DefaultConfigMixin, BotPlugin):
1010

11+
CONFIG_TEMPLATE = {
12+
'ANSWER_END': None,
13+
}
1114
# Ignore LineLengthBear, PyCodestyleBear
1215
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}'
1316
MESSAGE_LINK = 'https://gitter.im/{uri}?at={idd}'
@@ -26,7 +29,7 @@ def construct_link(text):
2629
@botcmd
2730
def answer(self, msg, arg):
2831
try:
29-
answers = requests.get(urljoin(os.environ['ANSWER_END'], 'answer'),
32+
answers = requests.get(urljoin(self.config['ANSWER_END'], 'answer'),
3033
params={'question': arg}).json()
3134
except json.JSONDecodeError:
3235
self.log.exception('something went wrong while fetching answer for'

tests/answer_test.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import vcr
32
import requests_mock
43

@@ -9,13 +8,16 @@
98
class TestAnswer(IsolatedTestCase):
109

1110
def setUp(self):
12-
super().setUp()
1311
# Ignore InvalidLinkBear
1412
self.answer_end_point = 'http://0.0.0.0:8000'
15-
os.environ['ANSWER_END'] = self.answer_end_point
16-
17-
def tearDown(self):
18-
del os.environ['ANSWER_END']
13+
extra_config = {
14+
'DEFAULT_CONFIG': {
15+
'answer': {
16+
'ANSWER_END': self.answer_end_point,
17+
}
18+
}
19+
}
20+
super().setUp(extra_config=extra_config)
1921

2022
@vcr.use_cassette('tests/cassettes/answer.yaml')
2123
def test_answer(self):
@@ -24,6 +26,8 @@ def test_answer(self):
2426
self.assertIn('Please checkout the following links', self.pop_message())
2527
self.push_message('!answer shell autocompletion')
2628
self.assertIn('Please checkout the following links', self.pop_message())
29+
self.assertCommand('!plugin config answer',
30+
str({'ANSWER_END': self.answer_end_point}))
2731

2832
def test_invalid_json(self):
2933
with requests_mock.Mocker() as m:

0 commit comments

Comments
 (0)