Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Add some tests to verify the setting of the channel topic
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinpinto committed Dec 18, 2015
1 parent 83d65f0 commit 6147f24
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
9 changes: 2 additions & 7 deletions charlesbot_rundeck/rundeck_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ def get_topic_channel_id(self):
return self.topic_channel_id
return None

def get_locked_by_user(self):
return self.locked_by_user

@asyncio.coroutine
def lock_or_unlock_rundeck_job(self, rundeck_job_obj, lock_job):
"""
Expand Down Expand Up @@ -122,20 +119,18 @@ def get_execution_status_message(self, lock_job):
"""
Return an appropriate user-facing message
"""
locked_by_user = self.get_locked_by_user()
if lock_job:
return ":lock: Rundeck executions locked by <@%s> :lock:" % locked_by_user # NOQA
return ":lock: Rundeck executions locked by <@%s> :lock:" % self.locked_by_user # NOQA
return "Rundeck executions enabled! :white_check_mark:"

@asyncio.coroutine
def set_channel_topic(self, lock_job):
topic_channel_id = yield from self.get_topic_channel_id()
locked_by_user = self.get_locked_by_user()
if not topic_channel_id:
return
topic_message = ""
if lock_job:
topic_message = ":lock: Rundeck executions locked by @%s :lock:" % locked_by_user # NOQA
topic_message = ":lock: Rundeck executions locked by @%s :lock:" % self.locked_by_user # NOQA
yield from self.slack.api_call('channels.setTopic',
channel=topic_channel_id,
topic=topic_message)
Expand Down
93 changes: 93 additions & 0 deletions tests/test_set_channel_topic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import asynctest
import json
from asynctest.mock import patch
from asynctest.mock import call


class TestSetChannelTopic(asynctest.TestCase):

def setUp(self):
patcher1 = patch('charlesbot_rundeck.rundeck_lock.RundeckLock.seed_job_list') # NOQA
self.addCleanup(patcher1.stop)
self.mock_seed_job_list = patcher1.start()

patcher2 = patch('charlesbot.slack.slack_connection.SlackConnection.api_call') # NOQA
self.addCleanup(patcher2.stop)
self.mock_api_call = patcher2.start()

from charlesbot_rundeck.rundeck_lock import RundeckLock
self.rd_lock = RundeckLock("token",
"url",
"channel",
[])

def test_topic_channel_not_set(self):
self.rd_lock.topic_channel = None
yield from self.rd_lock.set_channel_topic(True)
self.assertEqual(self.mock_api_call.mock_calls, [])

def test_topic_channel_id_already_set(self):
self.rd_lock.locked_by_user = "bob"
self.rd_lock.topic_channel = "chan1"
self.rd_lock.topic_channel_id = "C1234"
yield from self.rd_lock.set_channel_topic(True)
calls = [
call("channels.setTopic",
channel="C1234",
topic=":lock: Rundeck executions locked by @bob :lock:")
]
self.assertEqual(self.mock_api_call.mock_calls, calls)

def test_topic_channel_not_found(self):
channels = {
"ok": True,
"channels": [
{
"name": "chan1",
},
{
"name": "chan2",
},
{
"name": "chan3",
},
]
}
self.rd_lock.locked_by_user = "bob"
self.rd_lock.topic_channel = "chan4"
self.mock_api_call.side_effect = [json.dumps(channels), None]
yield from self.rd_lock.set_channel_topic(True)
calls = [
call("channels.list", exclude_archived=1),
]
self.assertEqual(self.mock_api_call.mock_calls, calls)

def test_topic_channel_found(self):
channels = {
"ok": True,
"channels": [
{
"name": "chan1",
"id": "C1",
},
{
"name": "chan2",
"id": "C2",
},
{
"name": "chan3",
"id": "C3",
},
]
}
self.rd_lock.locked_by_user = "bob"
self.rd_lock.topic_channel = "chan2"
self.mock_api_call.side_effect = [json.dumps(channels), None]
yield from self.rd_lock.set_channel_topic(False)
calls = [
call("channels.list", exclude_archived=1),
call("channels.setTopic",
channel="C2",
topic="")
]
self.assertEqual(self.mock_api_call.mock_calls, calls)

0 comments on commit 6147f24

Please sign in to comment.