| |
@@ -1,206 +0,0 @@
|
| |
- from __future__ import unicode_literals
|
| |
-
|
| |
- import json
|
| |
-
|
| |
- import mock
|
| |
- import requests
|
| |
-
|
| |
- import hubs
|
| |
- from hubs.models import Hub, Widget
|
| |
- from hubs.tests import FakeAuthorization, auth_set
|
| |
- from hubs.widgets.contact.functions import GetFASInfo
|
| |
- from . import WidgetTest
|
| |
-
|
| |
-
|
| |
- class MockResponse:
|
| |
- def __init__(self, json_data, status_code):
|
| |
- self.json_data = json_data
|
| |
- self.status_code = status_code
|
| |
- self.text = str(json_data)
|
| |
- self.ok = (status_code == 200)
|
| |
-
|
| |
- def json(self):
|
| |
- return self.json_data
|
| |
-
|
| |
-
|
| |
- def mocked_requests_get(*args, **kwargs):
|
| |
- if '/ralph' in kwargs["url"]:
|
| |
- data = {
|
| |
- "current": 0,
|
| |
- "decrements": 0,
|
| |
- "increments": 0,
|
| |
- "release": "f24",
|
| |
- "total": 0,
|
| |
- "username": "ralph"
|
| |
- }
|
| |
- return MockResponse(json_data=data, status_code=200)
|
| |
-
|
| |
- return MockResponse({}, 404)
|
| |
-
|
| |
-
|
| |
- def mocked_requests_post(*args, **kwargs):
|
| |
- if '/ralph' in kwargs['url']:
|
| |
- data = {
|
| |
- "current": 1,
|
| |
- "decrements": 0,
|
| |
- "increments": 1,
|
| |
- "release": "f24",
|
| |
- "total": 1,
|
| |
- "username": "ralph"
|
| |
- }
|
| |
- return MockResponse(json_data=data, status_code=200)
|
| |
-
|
| |
- return MockResponse({}, 404)
|
| |
-
|
| |
-
|
| |
- class ContactsTest(WidgetTest):
|
| |
-
|
| |
- plugin = "contact"
|
| |
- maxDiff = None
|
| |
-
|
| |
- def setUp(self):
|
| |
- super(ContactsTest, self).setUp()
|
| |
- hub = Hub.by_name('ralph', "user")
|
| |
- self.widget = Widget(
|
| |
- plugin='contact',
|
| |
- index=1,
|
| |
- )
|
| |
- hub.widgets.append(self.widget)
|
| |
- self.session.commit()
|
| |
- self.widget_idx = self.widget.idx
|
| |
-
|
| |
- @mock.patch('hubs.widgets.contact.functions.fedora.client.fas2')
|
| |
- def test_data_simple(self, mock_fas2):
|
| |
- fake_account_system = mock.Mock()
|
| |
- fake_account_system.person_by_username.return_value = {
|
| |
- 'creation': '2010-10-01',
|
| |
- 'email': 'ralph@fedoraproject.org',
|
| |
- 'ircnick': 'ralph',
|
| |
- 'country_code': 'US',
|
| |
- 'timezone': 'UTC',
|
| |
- 'username': 'ralph',
|
| |
- }
|
| |
- mock_fas2.AccountSystem.return_value = fake_account_system
|
| |
- user = FakeAuthorization('ralph')
|
| |
- response = self.check_url(
|
| |
- '/widgets/contact/%i/data' % self.widget_idx, user)
|
| |
- self.assertDictEqual(
|
| |
- json.loads(response.get_data(as_text=True)),
|
| |
- {
|
| |
- "status": "OK",
|
| |
- "data": {
|
| |
- 'account_age': 'Oct 2010',
|
| |
- 'email': 'ralph@fedoraproject.org',
|
| |
- 'ircnick': 'ralph',
|
| |
- 'country': 'United States',
|
| |
- 'timezone': 'UTC',
|
| |
- 'timezone_offset': 0,
|
| |
- 'username': 'ralph',
|
| |
- }
|
| |
- })
|
| |
-
|
| |
- @mock.patch('hubs.widgets.contact.functions.fedmsg_config')
|
| |
- def test_no_fas_credentials_function(self, fedmsg_config):
|
| |
- fedmsg_config.__getitem__.return_value = {}
|
| |
- func = GetFASInfo(self.widget)
|
| |
- result = func.execute()
|
| |
- self.assertIsNone(result)
|
| |
-
|
| |
- @mock.patch('hubs.widgets.contact.views.fedmsg_config')
|
| |
- def test_no_fas_credentials_view(self, fedmsg_config):
|
| |
- fedmsg_config.__getitem__.return_value = {}
|
| |
- user = FakeAuthorization('ralph')
|
| |
- response = self.check_url(
|
| |
- '/widgets/contact/%i/data' % self.widget_idx, user)
|
| |
- self.assertDictEqual(
|
| |
- json.loads(response.get_data(as_text=True)),
|
| |
- {
|
| |
- "status": "ERROR",
|
| |
- "message": "No FAS credentials configured, report this "
|
| |
- "to the system administrator.",
|
| |
- })
|
| |
-
|
| |
- @mock.patch('requests.request', side_effect=mocked_requests_get)
|
| |
- def test_plus_plus_get_valid(self, mock_request):
|
| |
- url = "/widgets/contact/%d/plus-plus" % self.widget_idx
|
| |
- result = self.app.get(url)
|
| |
- expected = {
|
| |
- "current": 0,
|
| |
- "decrements": 0,
|
| |
- "increments": 0,
|
| |
- "release": "f24",
|
| |
- "total": 0,
|
| |
- "username": "ralph"
|
| |
- }
|
| |
- self.assertEqual(result.status_code, 200)
|
| |
- self.assertEqual(
|
| |
- json.loads(result.get_data(as_text=True)),
|
| |
- dict(status="OK", data=expected))
|
| |
-
|
| |
- @mock.patch('requests.request', side_effect=mocked_requests_post)
|
| |
- def test_plus_plus_post_increment_valid(self, mock_request):
|
| |
- url = "/widgets/contact/%d/plus-plus" % self.widget_idx
|
| |
- user = FakeAuthorization('decause')
|
| |
- with auth_set(hubs.app.app, user):
|
| |
- result = self.app.post(
|
| |
- url,
|
| |
- content_type="application/json",
|
| |
- data=json.dumps({'increment': True}))
|
| |
- expected = {
|
| |
- "current": 1,
|
| |
- "decrements": 0,
|
| |
- "increments": 1,
|
| |
- "release": "f24",
|
| |
- "total": 1,
|
| |
- "username": "ralph"
|
| |
- }
|
| |
- self.assertEqual(result.status_code, 200)
|
| |
- self.assertEqual(
|
| |
- json.loads(result.get_data(as_text=True)),
|
| |
- dict(status="OK", data=expected))
|
| |
-
|
| |
- @mock.patch('requests.request', side_effect=mocked_requests_post)
|
| |
- def test_plus_plus_post_increment_myself_error(self, mock_request):
|
| |
- url = "/widgets/contact/%d/plus-plus" % self.widget_idx
|
| |
- user = FakeAuthorization('ralph')
|
| |
- with auth_set(hubs.app.app, user):
|
| |
- result = self.app.post(
|
| |
- url,
|
| |
- content_type="application/json",
|
| |
- data=json.dumps({'increment': True}))
|
| |
- self.assertEqual(
|
| |
- json.loads(result.get_data(as_text=True)),
|
| |
- {
|
| |
- "status": "ERROR",
|
| |
- "message": "You may not modify your own karma.",
|
| |
- })
|
| |
-
|
| |
- @mock.patch('requests.request', side_effect=mocked_requests_post)
|
| |
- def test_plus_plus_post_increment_no_data_error(self, mock_request):
|
| |
- url = "/widgets/contact/%d/plus-plus" % self.widget_idx
|
| |
- user = FakeAuthorization('decause')
|
| |
- with auth_set(hubs.app.app, user):
|
| |
- result = self.app.post(
|
| |
- url,
|
| |
- content_type="application/json",
|
| |
- data=json.dumps({}))
|
| |
- exp_str = "You must set 'decrement' or 'increment' " \
|
| |
- "with a boolean value in the body"
|
| |
- self.assertEqual(
|
| |
- json.loads(result.get_data(as_text=True)),
|
| |
- {"status": "ERROR", "message": exp_str}
|
| |
- )
|
| |
-
|
| |
- @mock.patch('requests.request')
|
| |
- def test_plus_plus_connection_error(self, mock_request):
|
| |
- mock_request.side_effect = requests.ConnectionError("connection error")
|
| |
- url = "/widgets/contact/%d/plus-plus" % self.widget_idx
|
| |
- result = self.app.get(url)
|
| |
- self.assertEqual(
|
| |
- json.loads(result.get_data(as_text=True)),
|
| |
- {
|
| |
- "status": "ERROR",
|
| |
- "message": "Could not connect to "
|
| |
- "http://localhost:5001/user/ralph",
|
| |
- }
|
| |
- )
|
| |
Fixes #462 again.