| |
@@ -11,24 +11,26 @@
|
| |
from . import WidgetTest
|
| |
|
| |
|
| |
- def mocked_requests_get(*args, **kwargs):
|
| |
- class MockResponse:
|
| |
- def __init__(self, json_data, status_code):
|
| |
- self.json_data = json_data
|
| |
- self.status_code = status_code
|
| |
- self.text = str(json_data)
|
| |
+ 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 json(self):
|
| |
- return self.json_data
|
| |
|
| |
- if '/decause' in args[0]:
|
| |
+ def mocked_requests_get(*args, **kwargs):
|
| |
+ if '/ralph' in kwargs["url"]:
|
| |
data = {
|
| |
"current": 0,
|
| |
"decrements": 0,
|
| |
"increments": 0,
|
| |
"release": "f24",
|
| |
"total": 0,
|
| |
- "username": "decause"
|
| |
+ "username": "ralph"
|
| |
}
|
| |
return MockResponse(json_data=data, status_code=200)
|
| |
|
| |
@@ -36,23 +38,14 @@
|
| |
|
| |
|
| |
def mocked_requests_post(*args, **kwargs):
|
| |
- class MockResponse:
|
| |
- def __init__(self, json_data, status_code):
|
| |
- self.json_data = json_data
|
| |
- self.status_code = status_code
|
| |
- self.text = str(json_data)
|
| |
-
|
| |
- def json(self):
|
| |
- return self.json_data
|
| |
-
|
| |
- if '/decause' in kwargs['url']:
|
| |
+ if '/ralph' in kwargs['url']:
|
| |
data = {
|
| |
"current": 1,
|
| |
"decrements": 0,
|
| |
"increments": 1,
|
| |
"release": "f24",
|
| |
"total": 1,
|
| |
- "username": "decause"
|
| |
+ "username": "ralph"
|
| |
}
|
| |
return MockResponse(json_data=data, status_code=200)
|
| |
|
| |
@@ -62,6 +55,7 @@
|
| |
class ContactsTest(WidgetTest):
|
| |
|
| |
plugin = "contact"
|
| |
+ maxDiff = None
|
| |
|
| |
def setUp(self):
|
| |
super(ContactsTest, self).setUp()
|
| |
@@ -74,32 +68,39 @@
|
| |
self.session.commit()
|
| |
self.widget_idx = widget.idx
|
| |
|
| |
- def test_data_simple(self):
|
| |
- user = FakeAuthorization('ralph')
|
| |
- widget = Widget.query.get(self.widget_idx)
|
| |
- response = self.check_url(
|
| |
- '/ralph/w/contact/%i/' % self.widget_idx, user)
|
| |
- self.assertDictEqual(response.context, {
|
| |
- 'account_age': 'Oct 2010',
|
| |
+ @mock.patch('hubs.widgets.contact.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',
|
| |
- 'karma_url': '/ralph/w/contact/%i/plus-plus/ralph/status'
|
| |
- % self.widget_idx,
|
| |
- 'location': 'United States',
|
| |
+ 'country_code': 'US',
|
| |
'timezone': 'UTC',
|
| |
- 'usergroup': True,
|
| |
- 'edit_mode': False,
|
| |
- 'widget': widget.module,
|
| |
- 'widget_instance': widget,
|
| |
- })
|
| |
-
|
| |
- def test_view_authz(self):
|
| |
- self._test_view_authz()
|
| |
-
|
| |
- @mock.patch('requests.get', side_effect=mocked_requests_get)
|
| |
- def test_plus_plus_get_valid(self, mock_get):
|
| |
- url = "/ralph/w/contact/%d/plus-plus/%s/status" % (
|
| |
- self.widget_idx, "decause")
|
| |
+ 'username': 'ralph',
|
| |
+ }
|
| |
+ mock_fas2.AccountSystem.return_value = fake_account_system
|
| |
+ user = FakeAuthorization('ralph')
|
| |
+ response = self.check_url(
|
| |
+ '/ralph/w/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('requests.request', side_effect=mocked_requests_get)
|
| |
+ def test_plus_plus_get_valid(self, mock_request):
|
| |
+ url = "/ralph/w/contact/%d/plus-plus" % self.widget_idx
|
| |
result = self.app.get(url)
|
| |
expected = {
|
| |
"current": 0,
|
| |
@@ -107,80 +108,77 @@
|
| |
"increments": 0,
|
| |
"release": "f24",
|
| |
"total": 0,
|
| |
- "username": "decause"
|
| |
+ "username": "ralph"
|
| |
}
|
| |
self.assertEqual(result.status_code, 200)
|
| |
self.assertEqual(
|
| |
json.loads(result.get_data(as_text=True)),
|
| |
- expected)
|
| |
+ dict(status="OK", data=expected))
|
| |
|
| |
- @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
- def test_plus_plus_post_increment_valid(self, mock_post):
|
| |
- url = "/ralph/w/contact/%d/plus-plus/%s/update" % (
|
| |
- self.widget_idx, "decause")
|
| |
- user = FakeAuthorization('ralph')
|
| |
+ @mock.patch('requests.request', side_effect=mocked_requests_post)
|
| |
+ def test_plus_plus_post_increment_valid(self, mock_request):
|
| |
+ url = "/ralph/w/contact/%d/plus-plus" % self.widget_idx
|
| |
+ user = FakeAuthorization('decause')
|
| |
with auth_set(hubs.app.app, user):
|
| |
- result = self.app.post(url, data={'increment': True})
|
| |
+ 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": "decause"
|
| |
+ "username": "ralph"
|
| |
}
|
| |
self.assertEqual(result.status_code, 200)
|
| |
self.assertEqual(
|
| |
json.loads(result.get_data(as_text=True)),
|
| |
- expected)
|
| |
-
|
| |
- @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
- def test_plus_plus_post_increment_myself_error(self, mock_post):
|
| |
- url = "/ralph/w/contact/%d/plus-plus/%s/update" % (
|
| |
- self.widget_idx, "ralph")
|
| |
- user = FakeAuthorization('ralph')
|
| |
- with auth_set(hubs.app.app, user):
|
| |
- result = self.app.post(url, data={'increment': True})
|
| |
- self.assertEqual(result.status_code, 403)
|
| |
- self.assertEqual(
|
| |
- result.get_data(as_text=True),
|
| |
- 'You may not modify your own karma.')
|
| |
+ dict(status="OK", data=expected))
|
| |
|
| |
- @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
- def test_plus_plus_post_increment_user_does_not_exist(self, mock_post):
|
| |
- url = "/ralph/w/contact/%d/plus-plus/%s/update" % (
|
| |
- self.widget_idx, "doesnotexist")
|
| |
+ @mock.patch('requests.request', side_effect=mocked_requests_post)
|
| |
+ def test_plus_plus_post_increment_myself_error(self, mock_request):
|
| |
+ url = "/ralph/w/contact/%d/plus-plus" % self.widget_idx
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(hubs.app.app, user):
|
| |
- result = self.app.post(url, data={'increment': True})
|
| |
- self.assertEqual(result.status_code, 404)
|
| |
+ result = self.app.post(
|
| |
+ url,
|
| |
+ content_type="application/json",
|
| |
+ data=json.dumps({'increment': True}))
|
| |
self.assertEqual(
|
| |
- result.get_data(as_text=True),
|
| |
- 'User does not exist')
|
| |
-
|
| |
- @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
- def test_plus_plus_post_increment_no_data_error(self, mock_post):
|
| |
- url = "/ralph/w/contact/%d/plus-plus/%s/update" % (
|
| |
- self.widget_idx, "decause")
|
| |
- user = FakeAuthorization('ralph')
|
| |
+ 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 = "/ralph/w/contact/%d/plus-plus" % self.widget_idx
|
| |
+ user = FakeAuthorization('decause')
|
| |
with auth_set(hubs.app.app, user):
|
| |
- result = self.app.post(url, data={})
|
| |
- self.assertEqual(result.status_code, 400)
|
| |
+ 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(result.get_data(as_text=True), exp_str)
|
| |
+ self.assertEqual(
|
| |
+ json.loads(result.get_data(as_text=True)),
|
| |
+ {"status": "ERROR", "message": exp_str}
|
| |
+ )
|
| |
|
| |
- def test_plus_plus_receiver_does_not_exist(self):
|
| |
- url = "/ralph/w/contact/%d/plus-plus/%s/status" % (
|
| |
- self.widget_idx, "doesnotexist")
|
| |
- result = self.app.get(url)
|
| |
- self.assertEqual(result.status_code, 404)
|
| |
- self.assertEqual(result.get_data(as_text=True), 'User does not exist')
|
| |
-
|
| |
- @mock.patch('requests.get')
|
| |
- def test_plus_plus_connection_error(self, mock_get):
|
| |
- mock_get.side_effect = requests.ConnectionError("connection error")
|
| |
- url = "/ralph/w/contact/%d/plus-plus/%s/status" % (
|
| |
- self.widget_idx, "decause")
|
| |
+ @mock.patch('requests.request')
|
| |
+ def test_plus_plus_connection_error(self, mock_request):
|
| |
+ mock_request.side_effect = requests.ConnectionError("connection error")
|
| |
+ url = "/ralph/w/contact/%d/plus-plus" % self.widget_idx
|
| |
result = self.app.get(url)
|
| |
- self.assertEqual(result.status_code, 504)
|
| |
+ self.assertEqual(
|
| |
+ json.loads(result.get_data(as_text=True)),
|
| |
+ {
|
| |
+ "status": "ERROR",
|
| |
+ "message": "Could not connect to "
|
| |
+ "http://localhost:5001/user/ralph",
|
| |
+ }
|
| |
+ )
|
| |
Fixes #442
There's also a rework of the Contact widget which did not work since the switch to React. The difference is that jQuery auto-executes the
<script>
tags when filling a element with AJAX data, while React does not. As a consequence simple widgets can't use Javascript, they need to be written in React for that.The Contact widget uses javascript to display the user's local time and refresh it every second.