Skip to content

Commit 90b8396

Browse files
committed
fix tests
1 parent 1c8d0ed commit 90b8396

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

src/slack.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, **params):
1919
self.oauth_install_uri = params.get('oauth_install_uri')
2020
self.oauth_redirect_uri = params.get('oauth_redirect_uri')
2121
self.oauth_success_uri = params.get('oauth_success_uri') \
22-
or 'slack://channel?team={team_id}&id={channel_id}'
22+
or 'slack://app?team={TEAM_ID}&id={APP_ID}'
2323
self.routes = params.get('routes') or {}
2424
self.signing_secret = params.get('signing_secret')
2525
self.signing_version = params.get('signing_version') or 'v0'
@@ -67,9 +67,9 @@ def install(self, event, method):
6767
team_id = result.get('team', {}).get('id')
6868
channel_id = result.get('incoming_webhook', {}).get('channel_id')
6969
location = self.oauth_success_uri.format(
70-
APP_ID=app_id,
71-
TEAM_ID=team_id,
72-
CHANNEL_ID=channel_id,
70+
APP_ID=app_id or '',
71+
TEAM_ID=team_id or '',
72+
CHANNEL_ID=channel_id or '',
7373
)
7474

7575
return result, location
@@ -80,8 +80,8 @@ def install_url(self):
8080
if query:
8181
query += '&'
8282
query += urlencode({
83-
'state': self.state,
84-
'redirect_uri': self.oauth_redirect_uri,
83+
'state': self.state or '',
84+
'redirect_uri': self.oauth_redirect_uri or '',
8585
})
8686
return urlunsplit(url + [query, fragment])
8787

@@ -99,10 +99,8 @@ def post(self, body, method, **headers):
9999
ok = resdata['ok']
100100

101101
# Log response & return
102-
if ok:
103-
logger.info('RESPONSE [%d] %s', res.status, json.dumps(resdata))
104-
else:
105-
logger.error('RESPONSE [%d] %s', res.status, json.dumps(resdata))
102+
log = logger.info if ok else logger.error
103+
log('RESPONSE [%d] %s', res.status, json.dumps(resdata))
106104
return resdata
107105

108106
def post_request(self, body, method, **headers):

tests/index_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def setup(self):
1111
index.slack.oauth_install_uri = 'https://example.com/install'
1212
index.slack.oauth_redirect_uri = 'https://example.com/success'
1313
index.slack.state = 'state'
14+
index.slack.token = 'token'
1415
index.slack.verify = False
1516

1617
index.events.publish = mock.MagicMock()
@@ -40,7 +41,10 @@ def test_get_install(self):
4041
'headers': {
4142
'content-type': 'application/json; charset=utf-8',
4243
'content-length': '0',
43-
'location': 'https://example.com/install?state=state',
44+
'location': (
45+
'https://example.com/install?state=state&'
46+
'redirect_uri=https%3A%2F%2Fexample.com%2Fsuccess'
47+
),
4448
}
4549
}
4650
assert ret == exp
@@ -109,7 +113,10 @@ def test_head_install(self):
109113
'headers': {
110114
'content-type': 'application/json; charset=utf-8',
111115
'content-length': '0',
112-
'location': 'https://example.com/install?state=state',
116+
'location': (
117+
'https://example.com/install?state=state&'
118+
'redirect_uri=https%3A%2F%2Fexample.com%2Fsuccess'
119+
),
113120
}
114121
}
115122
assert ret == exp
@@ -244,4 +251,5 @@ def test_post(self):
244251
index.slack.post.assert_called_once_with(
245252
{'text': 'FIZZ'},
246253
'api/chat.postMessage',
254+
authorization='Bearer token'
247255
)

tests/logger_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ def setup(self):
2525
'-'
2626
),
2727
])
28-
def test_attach(self, event, context, name, awsRequestId):
28+
def test_bind(self, event, context, name, awsRequestId):
2929
logger = getLogger(name, stream=self.stream)
3030
logger.setLevel('DEBUG')
3131

32-
@logger.attach
32+
@logger.bind
3333
def handler(event=None, context=None):
3434
logger.warning('TEST')
3535
return {'ok': True}

tests/slack_test.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def test_handle_not_found(self):
3535
@mock.patch('src.slack.Slack.post')
3636
def test_install(self, mock_post):
3737
res = {
38-
'incoming_webhook': {'channel_id': 'C0123456789'},
38+
'app_id': 'A0123456789',
3939
'team': {'id': 'T0123456789'},
4040
}
41-
loc = 'slack://channel?team=T0123456789&id=C0123456789'
41+
loc = 'slack://app?team=T0123456789&id=A0123456789'
4242
mock_post.return_value = res
4343
event = {
4444
'queryStringParameters': {
@@ -72,15 +72,7 @@ def test_install_err(self, event, redir):
7272
if redir:
7373
self.subject.oauth_error_uri = redir
7474
ret = self.subject.install(HttpEvent(event), '<oauth-method>')
75-
exp = {
76-
'statusCode': 302,
77-
'body': None,
78-
'headers': {
79-
'content-length': '0',
80-
'content-type': 'application/json; charset=utf-8',
81-
'location': redir
82-
}
83-
}
75+
exp = (None, redir)
8476
assert ret == exp
8577
else:
8678
with pytest.raises(Exception):
@@ -90,12 +82,12 @@ def test_install_err(self, event, redir):
9082
(
9183
'state-1',
9284
'https://example.com/install',
93-
'https://example.com/install?state=state-1',
85+
'https://example.com/install?state=state-1&redirect_uri='
9486
),
9587
(
9688
'state-2',
9789
'https://example.com/install?fizz=buzz',
98-
'https://example.com/install?fizz=buzz&state=state-2',
90+
'https://example.com/install?fizz=buzz&state=state-2&redirect_uri='
9991
),
10092
])
10193
def test_install_url(self, state, oauth_install_uri, exp):
@@ -117,7 +109,6 @@ def test_post(self):
117109
'url': 'https://slack.com/api/chat.postMessage',
118110
'data': b'{"test": "FIZZ"}',
119111
'headers': {
120-
'authorization': 'Bearer <token>',
121112
'content-type': 'application/json; charset=utf-8',
122113
},
123114
},
@@ -130,7 +121,6 @@ def test_post(self):
130121
'url': 'https://slack.com/api/files.upload',
131122
'data': b'test=FIZZ',
132123
'headers': {
133-
'authorization': 'Bearer <token>',
134124
'content-type': 'application/x-www-form-urlencoded',
135125
},
136126
},

0 commit comments

Comments
 (0)