Skip to content

Commit b6ce4fc

Browse files
committed
add tests for logging and mentee regression test
1 parent abe2412 commit b6ce4fc

9 files changed

Lines changed: 305 additions & 75 deletions

File tree

pybot/endpoints/slack/actions.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,33 @@ async def claim_mentee(action: Action, app: SirBot):
111111
Attempts to update airtable with the new request status and updates the claim
112112
button allowing it to be reset if needed.
113113
"""
114-
update_airtable = True
115-
clicker_id = action['user']['id']
116-
request_record = action['actions'][0]['name']
117-
click_type = action['actions'][0]['value']
118-
119-
response = base_response(action)
120-
121-
user_info = await app.plugins['slack'].api.query(methods.USERS_INFO, dict(user=clicker_id))
122-
clicker_email = user_info['user']['profile']['email']
123-
124-
if click_type == 'mentee_claimed':
125-
mentor_id = await app.plugins['airtable'].api.mentor_id_from_slack_email(clicker_email)
126-
if mentor_id:
127-
attachment = mentee_claimed_attachment(clicker_id, request_record)
114+
try:
115+
update_airtable = True
116+
clicker_id = action['user']['id']
117+
request_record = action['actions'][0]['name']
118+
click_type = action['actions'][0]['value']
119+
120+
response = base_response(action)
121+
122+
user_info = await app.plugins['slack'].api.query(methods.USERS_INFO, dict(user=clicker_id))
123+
clicker_email = user_info['user']['profile']['email']
124+
125+
if click_type == 'mentee_claimed':
126+
mentor_id = await app.plugins['airtable'].api.mentor_id_from_slack_email(clicker_email)
127+
if mentor_id:
128+
attachment = mentee_claimed_attachment(clicker_id, request_record)
129+
else:
130+
update_airtable = False
131+
attachment = action['original_message']['attachments']
132+
attachment[0]['text'] = f":warning: <@{clicker_id}>'s slack Email not found in Mentor table. :warning:"
128133
else:
129-
update_airtable = False
130-
attachment = action['original_message']['attachments']
131-
attachment[0]['text'] = f":warning: <@{clicker_id}>'s slack Email not found in Mentor table. :warning:"
132-
else:
133-
mentor_id = ''
134-
attachment = mentee_unclaimed_attachment(clicker_id, request_record)
134+
mentor_id = ''
135+
attachment = mentee_unclaimed_attachment(clicker_id, request_record)
135136

136-
response['attachments'] = attachment
137+
response['attachments'] = attachment
137138

138-
await app.plugins['slack'].api.query(methods.CHAT_UPDATE, response)
139-
if update_airtable:
140-
await app.plugins['airtable'].api.update_request(request_record, mentor_id)
139+
await app.plugins['slack'].api.query(methods.CHAT_UPDATE, response)
140+
if update_airtable:
141+
await app.plugins['airtable'].api.update_request(request_record, mentor_id)
142+
except Exception as ex:
143+
print(ex)

pybot/endpoints/slack/messages.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def create_endpoints(plugin):
1414
plugin.on_message(".*", message_deleted, subtype="message_deleted")
1515
plugin.on_message(".*\!tech", tech_tips)
1616
plugin.on_message(".*\<\!here\>", here_bad)
17-
plugin.on_message(".*\<\!channel\>", here_bad)
17+
plugin.on_message(".*\<\!channel\>", here_bad)
1818
plugin.on_message(".*@here", here_bad)
1919
plugin.on_message(".*@channel", here_bad)
2020
plugin.on_message(".*codervets", not_named)
@@ -23,6 +23,7 @@ def create_endpoints(plugin):
2323
def not_bot_message(event: Message):
2424
return 'message' not in event or 'subtype' not in event['message'] or event['message']['subtype'] != 'bot_message'
2525

26+
2627
def not_bot_delete(event: Message):
2728
return 'previous_message' not in event or 'bot_id' not in event['previous_message']
2829

@@ -44,7 +45,7 @@ async def tech_tips(event: Message, app: SirBot):
4445
f'tech logging: {event}')
4546
try:
4647
tech_terms: dict = await TechTerms(event['channel'], event['user'],
47-
event.get('text'), app).grab_values()
48+
event.get('text'), app).grab_values()
4849

4950
await app.plugins["slack"].api.query(methods.CHAT_POST_MESSAGE, tech_terms['message'])
5051
except Exception as E:

tests/conftest.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
pytest_plugins = ("slack.tests.plugin",)
1+
import copy
2+
3+
import pytest
4+
from sirbot import SirBot
5+
from sirbot.plugins.slack import SlackPlugin
6+
7+
from pybot import endpoints
8+
from pybot.plugins import AirtablePlugin
9+
from tests import data
10+
11+
pytest_plugins = ("slack.tests.plugin",)
12+
13+
14+
@pytest.fixture(
15+
params={**data.Action.__members__})
16+
def action(request):
17+
if isinstance(request.param, str):
18+
payload = copy.deepcopy(data.Action[request.param].value)
19+
else:
20+
payload = copy.deepcopy(request.param)
21+
return payload
22+
23+
24+
@pytest.fixture
25+
async def bot(loop):
26+
b = SirBot(loop=loop)
27+
slack = SlackPlugin(
28+
token='token',
29+
verify='supersecuretoken',
30+
bot_user_id='bot_user_id',
31+
bot_id='bot_id',
32+
)
33+
airtable = AirtablePlugin()
34+
endpoints.slack.create_endpoints(slack)
35+
b.load_plugin(slack)
36+
b.load_plugin(airtable)
37+
38+
return b
39+
40+
@pytest.fixture
41+
async def slack_bot(bot):
42+
slack = SlackPlugin(
43+
token='token',
44+
verify='supersecuretoken',
45+
bot_user_id='bot_user_id',
46+
bot_id='bot_id',
47+
)
48+
endpoints.slack.create_endpoints(slack)
49+
bot.load_plugin(slack)

tests/data/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .actions import Action

tests/data/actions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import json
2+
from enum import Enum
3+
4+
claim_event = {
5+
'type': 'interactive_message',
6+
'user': {'id': 'U123'},
7+
'actions': [{'name': 'rec123', 'value': 'mentee_claimed'}],
8+
'original_message': {'text': 'some text'},
9+
'channel': {'id': 'abc'},
10+
'message_ts': '123123.123',
11+
'callback_id': 'claim_mentee',
12+
"token": "supersecuretoken",
13+
"team_id": "T000AAA0A",
14+
"api_app_id": "A0AAAAAAA",
15+
"authed_teams": ["T000AAA0A"],
16+
"event_id": "AAAAAAA",
17+
"event_time": 123456789,
18+
}
19+
20+
unclaim_event = {
21+
'type': 'interactive_message',
22+
'user': {'id': 'U123'},
23+
'actions': [{'name': 'rec123', 'value': 'mentee_unclaimed'}],
24+
'original_message': {'text': 'some text'},
25+
'channel': {'id': 'abc'},
26+
'message_ts': '123123.123',
27+
'callback_id': 'claim_mentee',
28+
"token": "supersecuretoken",
29+
"team_id": "T000AAA0A",
30+
"api_app_id": "A0AAAAAAA",
31+
"authed_teams": ["T000AAA0A"],
32+
"event_id": "AAAAAAA",
33+
"event_time": 123456789,
34+
}
35+
36+
raw_claim_event = {"payload": json.dumps(claim_event)}
37+
raw_unclaim_event = {"payload": json.dumps(unclaim_event)}
38+
39+
40+
class Action(Enum):
41+
claim_mentee = raw_claim_event
42+
unclaim_mentee = raw_unclaim_event

tests/data/events.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
TEAM_JOIN = {
2+
"token": "supersecuretoken",
3+
"team_id": "T000AAA0A",
4+
"api_app_id": "A0AAAAAAA",
5+
"event": {
6+
"type": "team_join",
7+
"channel": "C00000A00",
8+
"user": {"id": "U0AAAA",
9+
"team_id": "T000AAA0A",
10+
"name": "test",
11+
"real_name": "test testerson",
12+
},
13+
"event_ts": "123456789.000001",
14+
},
15+
"type": "event_callback",
16+
"authed_teams": ["T000AAA0A"],
17+
"event_id": "AAAAAAA",
18+
"event_time": 123456789,
19+
}
20+
21+
new_message = {'type': 'message', 'user': 'U8FDR1603', 'text': 'test3',
22+
'client_msg_id': '025cc728-fcb5-4dd7-8920-619a605bb631', 'ts': '1540497949.000100',
23+
'channel': 'GDNHHNCTV', 'event_ts': '1540497949.000100', 'channel_type': 'mpim'}
24+
edit_message = {
25+
"token": "supersecuretoken",
26+
"team_id": "T000AAA0A",
27+
"api_app_id": "A0AAAAAAA",
28+
"type": "event_callback",
29+
"authed_teams": ["T000AAA0A"],
30+
"event_id": "AAAAAAA",
31+
"event_time": 123456789,
32+
'event': {'type': 'message',
33+
'message': {'type': 'message',
34+
'user': 'U8FDR1603',
35+
'text': 'nevermind',
36+
'client_msg_id': '9aff714d-8674-42fc-986c-0c1c06cca3fc',
37+
'edited': {'user': 'U8FDR1603', 'ts': '1540497210.000000'},
38+
'ts': '1540497204.000100'
39+
},
40+
'subtype': 'message_changed',
41+
'hidden': True,
42+
'channel': 'C8DA69KM4',
43+
'previous_message': {'type': 'message', 'user': 'U8FDR1603', 'text': 'two',
44+
'client_msg_id': '9aff714d-8674-42fc-986c-0c1c06cca3fc',
45+
'ts': '1540497204.000100'},
46+
'event_ts': '1540497210.000100', 'ts': '1540497210.000100', 'channel_type': 'channel'}
47+
}
48+
delete_message = {'type': 'message', 'deleted_ts': '1540497676.000100', 'subtype': 'message_deleted', 'hidden': True,
49+
'channel': 'GDNHHNCTV',
50+
'previous_message': {'type': 'message', 'user': 'U8FDR1603', 'text': 'testing3',
51+
'client_msg_id': 'b5694bd6-6ed0-4ddd-bf84-3e2c8165c624',
52+
'ts': '1540497676.000100'}, 'event_ts': '1540497684.000100',
53+
'ts': '1540497684.000100', 'channel_type': 'mpim'}
54+
55+
MESSAGE_EDIT = {
56+
"token": "supersecuretoken",
57+
"team_id": "T000AAA0A",
58+
"api_app_id": "A0AAAAAAA",
59+
"event": {
60+
"type": "message",
61+
"message": {
62+
"type": "message",
63+
"user": "U000AA000",
64+
"text": "hello world",
65+
"edited": {"user": "U000AA000", "ts": "1513882449.000000"},
66+
"ts": "123456789.000001",
67+
},
68+
"subtype": "message_changed",
69+
"hidden": True,
70+
"channel": "C00000A00",
71+
"previous_message": {
72+
"type": "message",
73+
"user": "U000AA000",
74+
"text": "foo bar",
75+
"ts": "123456789.000001",
76+
},
77+
"event_ts": "123456789.000002",
78+
"ts": "123456789.000002",
79+
},
80+
"type": "event_callback",
81+
"authed_teams": ["T000AAA0A"],
82+
"event_id": "AAAAAAA",
83+
"event_time": 123456789,
84+
}
85+
86+
MESSAGE_DELETE = {
87+
"token": "supersecuretoken",
88+
"team_id": "T000AAA0A",
89+
"api_app_id": "A0AAAAAAA",
90+
"event": {
91+
"type": "message",
92+
"message": {
93+
"type": "message",
94+
"user": "U000AA000",
95+
"text": "hello world",
96+
"edited": {"user": "U000AA000", "ts": "1513882449.000000"},
97+
"ts": "123456789.000001",
98+
},
99+
"subtype": "message_deleted",
100+
"hidden": True,
101+
"channel": "C00000A00",
102+
"previous_message": {
103+
"type": "message",
104+
"user": "U000AA000",
105+
"text": "foo bar",
106+
"ts": "123456789.000001",
107+
},
108+
"event_ts": "123456789.000002",
109+
"ts": "123456789.000002",
110+
},
111+
"type": "event_callback",
112+
"authed_teams": ["T000AAA0A"],
113+
"event_id": "AAAAAAA",
114+
"event_time": 123456789,
115+
}
116+
117+
PLAIN_MESSAGE = {
118+
"token": "supersecuretoken",
119+
"team_id": "T000AAA0A",
120+
"api_app_id": "A0AAAAAAA",
121+
"event": {
122+
"type": "message",
123+
"message": {
124+
"type": "message",
125+
"user": "U000AA000",
126+
"text": "hello world",
127+
"edited": {"user": "U000AA000", "ts": "1513882449.000000"},
128+
"ts": "123456789.000001",
129+
},
130+
"channel": "C00000A00",
131+
"event_ts": "123456789.000002",
132+
"ts": "123456789.000002",
133+
},
134+
"type": "event_callback",
135+
"authed_teams": ["T000AAA0A"],
136+
"event_id": "AAAAAAA",
137+
"event_time": 123456789,
138+
}

tests/event_data.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/test_slack_actions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from asynctest import CoroutineMock
2+
from sirbot import SirBot
3+
4+
5+
async def test_claim_mentee_response_attachment_is_list(action: dict, test_client, bot: SirBot):
6+
slack_mock = CoroutineMock(return_value={"user": {"profile": {"email": "email@email.com"}}})
7+
airtable_mock = CoroutineMock(return_value="U123")
8+
bot['plugins']['slack'].api.query = slack_mock
9+
bot['plugins']['airtable'].api.mentor_id_from_slack_email = airtable_mock
10+
bot['plugins']['airtable'].api.update_request = airtable_mock
11+
12+
client = await test_client(bot)
13+
await client.post('/slack/actions', data=action)
14+
assert isinstance(slack_mock.call_args[0][1].event['attachments'], list)
15+

0 commit comments

Comments
 (0)