def create(self, request, format=None): serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) slack = SlackInvite() try: slack.invite(request.data['email']) except SlackException as e: raise serializers.ValidationError({'detail': str(e)}) return Response()
def setUp(self): self.slack_invite = SlackInvite()
class SlackInviteTest(TestCase): def setUp(self): self.slack_invite = SlackInvite() def test_url_returns_string(self): self.assertIsInstance(self.slack_invite._url(), str) def test_match_email_invalidEmail_returnsFalse(self): self.assertFalse(self.slack_invite._match_email('mail.example')) self.assertFalse(self.slack_invite._match_email('mail@example.')) self.assertFalse(self.slack_invite._match_email('@example.com')) self.assertTrue(self.slack_invite._match_email('*****@*****.**')) def test_invite_matchEmailFails_raisesException(self): with self.assertRaises(SlackException): self.slack_invite.invite('invalid@email') @patch('apps.slack.utils.requests.post') def test_invite_callsRequestsPost(self, post_mock): post_mock.side_effect = [ MagicMock(status_code=200, json=lambda: {'ok': True}) ] mail = "*****@*****.**" self.slack_invite.invite(mail) self.assertTrue(post_mock.called) @patch('apps.slack.utils.requests.post') def test_invite_statusCodeIsNot200_raisesException(self, post_mock): post_mock.side_effect = [MagicMock(status_code=403)] with self.assertRaises(SlackException): self.slack_invite.invite("*****@*****.**") @patch('apps.slack.utils.requests.post') def test_invite_okIsNotTrue_raisesException(self, post_mock): post_mock.side_effect = [ MagicMock(status_code=200, json=lambda: { 'ok': False, 'error': 'Mocked!' }) ] with self.assertRaises(SlackException): self.slack_invite.invite("*****@*****.**") @patch('apps.slack.utils.log.error') @patch('apps.slack.utils.requests.post') def test_invite_invalidAuth_logsError(self, post_mock, log_mock): post_mock.side_effect = [ MagicMock(status_code=200, json=lambda: { 'ok': False, 'error': 'invalid_auth' }) ] with self.assertRaises(SlackException): self.slack_invite.invite('*****@*****.**') self.assertTrue(log_mock.called)