Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Use mock in SlackChannelTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
ymyzk committed Jun 25, 2015
1 parent 97c4a6c commit e1e5ef7
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions tests/tests/backends/test_slack.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from copy import deepcopy
try:
from unittest import mock
except ImportError:
import mock

from django.conf import settings
from django.test import TestCase
import requests

from channels.backends.slack import SlackChannel
from channels.exceptions import HttpError, ImproperlyConfigured
Expand All @@ -17,16 +22,18 @@ def setUp(self):
self.channel = SlackChannel(**config)

def test_init(self):
with self.assertRaises(TypeError):
SlackChannel(**{})

with self.assertRaises(ImproperlyConfigured):
conf = deepcopy(config)
conf["icon_emoji"] = ":+1:"
conf["icon_url"] = "http://www.example.com/"
SlackChannel(**conf)

def test_send(self):
@mock.patch("requests.post")
def test_send(self, m):
response = requests.Response()
response.status_code = requests.codes.ok
m.return_value = response

self.channel.send("Test message.\nhttps://slack.com/")

self.channel.send("Test message. `unfurl_links=True`\n"
Expand Down Expand Up @@ -67,12 +74,13 @@ def test_send(self):
}
})

def test_send_fail(self):
conf = deepcopy(config)
conf["url"] = "https://hooks.slack.com/services/123/456/7890"
channel = SlackChannel(**conf)
@mock.patch("requests.post")
def test_send_fail_invalid_url(self, m):
response = requests.Response()
response.status_code = requests.codes.forbidden
m.return_value = response

with self.assertRaises(HttpError):
channel.send("Test message", fail_silently=False)
self.channel.send("Test message", fail_silently=False)

channel.send("Test message", fail_silently=True)
self.channel.send("Test message", fail_silently=True)

0 comments on commit e1e5ef7

Please sign in to comment.