Note: The default ITS GitLab runner is a shared resource and is subject to slowdowns during heavy usage.
You can run your own GitLab runner that is dedicated just to your group if you need to avoid processing delays.

Commit 9c6e5646 authored by Paul Arthur's avatar Paul Arthur
Browse files

Add Slack tests, fix Slack provider

* Use correct endpoint (/Users/{id}, not /Users/{username}), which is
  returned as part of the user object.

* Correctly check for HTTP success error code (`requests.codes.ok` is only
  '200', not all success responses.)

* Pass JSON data to Requests using `json`, not `data`
parent fe6c722c
......@@ -64,3 +64,6 @@ Tests can be run with `python setup.py test`.
CSAM uses the [pytest](https://pytest.org/) testing framework, so
tests can also be run directly with the command `pytest`.
To run the Slack integration tests, set the `SLACK_API_TOKEN`
environment variable.
......@@ -35,15 +35,17 @@ class SlackUser:
def __init__(self, client, username):
self.client = client
self.username = username
self.user = None
self._update()
def _update(self):
self.user = None
params = {
'filter': 'username eq {}'.format(username),
'filter': 'username eq {}'.format(self.username),
}
r = client.get('/Users', params=params)
r = self.client.get('/Users', params=params)
for user in r.json()['Resources']:
if user['userName'] == username:
if user['userName'] == self.username:
self.user = user
@property
......@@ -56,16 +58,37 @@ class SlackUser:
return not self.user['active']
return False
def create(self):
pass
def create(self, domain='umich.edu'):
if self.exists:
return self.activate()
data = {
"schemas": [
'urn:scim:schemas:core:1.0',
'urn:scim:schemas:extension:enterprise:1.0',
],
'userName': self.username,
'emails': [{
'value': '{}@{}'.format(self.username, domain),
'type': 'work',
'primary': True,
}],
}
r = self.client.post('/Users', json=data)
if r.ok:
self._update()
return True
raise CSAMError('Unable to create {} in Slack'.format(self.username))
def activate(self):
if self.disabled:
data = {
'active': True,
}
r = self.client.patch('/Users/{}'.format(self.user['id'], data=data))
if r.status_code == requests.codes.ok:
r = self.client.patch(self.user['meta']['location'], json=data)
if r.ok:
self._update()
return True
else:
raise CSAMError('Unable to activate {} in Slack'.format(self.username))
......@@ -78,8 +101,9 @@ class SlackUser:
def suspend(self):
if self.user and not self.disabled:
r = self.client.delete('/Users/{}'.format(self.username))
if r.status_code == requests.codes.ok:
r = self.client.delete(self.user['meta']['location'])
if r.ok:
self._update()
return True
else:
raise CSAMError('Unable to suspend {} in Slack'.format(self.username))
......
import os
import pytest
from csam.slack import SlackClient
@pytest.fixture
def slack():
token = os.environ.get('SLACK_API_TOKEN')
if not token:
pytest.skip('No Slack token available')
return
return SlackClient(token)
def test_user_manipulation(slack):
username = 'csam.testuser'
user = slack.user(username)
user.create()
assert user.exists
assert not user.disabled
user.suspend()
assert user.disabled
assert not user.suspend()
assert user.activate()
assert not user.disabled
assert not user.activate()
def test_nonexistent_user(slack):
user = slack.user('thisusernamecanneverexistbecauseitswaytoolong')
assert not user.exists
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment