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 (`` 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 test`.
CSAM uses the [pytest]( 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
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
......@@ -56,16 +58,37 @@ class SlackUser:
return not self.user['active']
return False
def create(self):
def create(self, domain=''):
if self.exists:
return self.activate()
data = {
"schemas": [
'userName': self.username,
'emails': [{
'value': '{}@{}'.format(self.username, domain),
'type': 'work',
'primary': True,
r ='/Users', json=data)
if r.ok:
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 ==
r = self.client.patch(self.user['meta']['location'], json=data)
if r.ok:
return True
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 ==
r = self.client.delete(self.user['meta']['location'])
if r.ok:
return True
raise CSAMError('Unable to suspend {} in Slack'.format(self.username))
import os
import pytest
from csam.slack import SlackClient
def slack():
token = os.environ.get('SLACK_API_TOKEN')
if not token:
pytest.skip('No Slack token available')
return SlackClient(token)
def test_user_manipulation(slack):
username = 'csam.testuser'
user = slack.user(username)
assert user.exists
assert not user.disabled
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