| |
@@ -6,14 +6,15 @@
|
| |
from hubs.app import app
|
| |
from hubs.models import Hub, User, Widget
|
| |
from hubs.widgets import registry
|
| |
- from hubs.tests import APPTest, FakeAuthorization, auth_set
|
| |
+ from hubs.tests import APPTest, FakeAuthorization, auth_set, widget_instance
|
| |
|
| |
|
| |
class TestAPIHubWidgets(APPTest):
|
| |
|
| |
def test_get_widgets(self):
|
| |
- expected_ids = [32, 33, 31, 34, 35, 36, 37, 38, 39, 40, 51]
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/")
|
| |
+ hub = Hub.by_name('ralph', "user")
|
| |
+ expected_ids = [41, 42, 40, 43, 44, 45, 46, 47, 48, 49, 66]
|
| |
+ response = self.check_url("/api/hubs/%s/widgets/" % hub.id)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "OK")
|
| |
self.assertListEqual(
|
| |
@@ -21,11 +22,11 @@
|
| |
expected_ids)
|
| |
|
| |
def test_get_widgets_private(self):
|
| |
- hub = Hub.by_name('ralph')
|
| |
+ hub = Hub.by_name('ralph', "user")
|
| |
hub.config["visibility"] = "private"
|
| |
self.session.commit()
|
| |
# Private hubs are not accessible to anonymous users.
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/", code=403)
|
| |
+ response = self.check_url("/api/hubs/%s/widgets/" % hub.id, code=403)
|
| |
self.assertDictEqual(
|
| |
json.loads(response.get_data(as_text=True)),
|
| |
{
|
| |
@@ -38,10 +39,11 @@
|
| |
decause = User.by_username("decause")
|
| |
hub.subscribe(decause, role='member')
|
| |
self.check_url(
|
| |
- "/api/hubs/ralph/widgets/", user=FakeAuthorization("decause"))
|
| |
+ "/api/hubs/%s/widgets/" % hub.id,
|
| |
+ user=FakeAuthorization("decause"))
|
| |
|
| |
def test_get_widgets_preview(self):
|
| |
- hub = Hub.by_name('ralph')
|
| |
+ hub = Hub.by_name('ralph', "user")
|
| |
hub.config["visibility"] = "preview"
|
| |
meetings = Widget.query.filter_by(
|
| |
hub=hub, plugin="meetings").first()
|
| |
@@ -49,27 +51,27 @@
|
| |
meetings.visibility = "restricted"
|
| |
# Restricted widgets in preview hubs aren't displayed to
|
| |
# anonymous users.
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/")
|
| |
+ response = self.check_url("/api/hubs/%s/widgets/" % hub.id)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "OK")
|
| |
self.assertNotIn(
|
| |
"meetings", [w["name"] for w in response_data["data"]])
|
| |
# But they are displayed to members.
|
| |
response = self.check_url(
|
| |
- "/api/hubs/ralph/widgets/", user=FakeAuthorization('ralph'))
|
| |
+ "/api/hubs/%s/widgets/" % hub.id, user=FakeAuthorization('ralph'))
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "OK")
|
| |
self.assertIn(
|
| |
"meetings", [w["name"] for w in response_data["data"]])
|
| |
|
| |
def test_get_removed_widget(self):
|
| |
- hub = Hub.get("ralph")
|
| |
+ hub = Hub.by_name("ralph", "user")
|
| |
widget = Widget(
|
| |
hub=hub, plugin="does-not-exist",
|
| |
left=True, index=-1, _config="{}")
|
| |
self.session.add(widget)
|
| |
response = self.check_url(
|
| |
- "/api/hubs/ralph/widgets/", user=FakeAuthorization('ralph'))
|
| |
+ "/api/hubs/%s/widgets/" % hub.id, user=FakeAuthorization('ralph'))
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertNotIn(
|
| |
"does-not-exist", [w["name"] for w in response_data["data"]])
|
| |
@@ -79,7 +81,9 @@
|
| |
# the widgets in the registry. At the moment, the subscription
|
| |
# widget is the only widget that is resctrited (it is not available)
|
| |
# on the group hub.
|
| |
- response = self.check_url("/api/hubs/ralph/available-widgets/")
|
| |
+ ralph_hub = Hub.by_name("ralph", "user")
|
| |
+ response = self.check_url(
|
| |
+ "/api/hubs/%s/available-widgets/" % ralph_hub.id)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "OK")
|
| |
self.assertListEqual(
|
| |
@@ -88,12 +92,14 @@
|
| |
|
| |
# next, test that a group hub returns all the widgets other than the
|
| |
# subscription widget
|
| |
- response = self.check_url("/api/hubs/infra/available-widgets/")
|
| |
+ infra_hub = Hub.by_name("infra", "team")
|
| |
+ response = self.check_url(
|
| |
+ "/api/hubs/%s/available-widgets/" % infra_hub.id)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "OK")
|
| |
self.assertListEqual(
|
| |
[w["name"] for w in response_data["data"]],
|
| |
- [w.name for w in registry.values() if "group" in w.hub_types])
|
| |
+ [w.name for w in registry.values() if "team" in w.hub_types])
|
| |
|
| |
def test_post_invalid_request(self):
|
| |
invalid_data = [
|
| |
@@ -112,11 +118,12 @@
|
| |
'position': 'invalid_position',
|
| |
},
|
| |
]
|
| |
+ hub = Hub.by_name("ralph", "user")
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(app, user):
|
| |
for data in invalid_data:
|
| |
result = self.app.post(
|
| |
- '/api/hubs/ralph/widgets/',
|
| |
+ '/api/hubs/%s/widgets/' % hub.id,
|
| |
content_type="application/json",
|
| |
data=json.dumps(data))
|
| |
self.assertEqual(result.status_code, 400)
|
| |
@@ -131,11 +138,12 @@
|
| |
'position': 'right',
|
| |
},
|
| |
]
|
| |
+ hub = Hub.by_name("ralph", "user")
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(app, user):
|
| |
for data in invalid_data:
|
| |
result = self.app.post(
|
| |
- '/api/hubs/ralph/widgets/',
|
| |
+ '/api/hubs/%s/widgets/' % hub.id,
|
| |
content_type="application/json",
|
| |
data=json.dumps(data))
|
| |
self.assertEqual(result.status_code, 400)
|
| |
@@ -143,6 +151,7 @@
|
| |
self.assertIn(expected_str, result.get_data(as_text=True))
|
| |
|
| |
def test_post_valid_widget_name_no_config(self):
|
| |
+ hub = Hub.by_name("ralph", "user")
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(app, user):
|
| |
data = {
|
| |
@@ -151,25 +160,23 @@
|
| |
"config": {},
|
| |
}
|
| |
result = self.app.post(
|
| |
- '/api/hubs/ralph/widgets/',
|
| |
+ '/api/hubs/%s/widgets/' % hub.id,
|
| |
content_type="application/json",
|
| |
data=json.dumps(data))
|
| |
self.assertEqual(result.status_code, 200)
|
| |
self.assertEqual(
|
| |
json.loads(result.get_data(as_text=True)),
|
| |
{"status": "OK"})
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/")
|
| |
+ response = self.check_url("/api/hubs/%s/widgets/" % hub.id)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "OK")
|
| |
self.assertIn("memberships",
|
| |
[w["name"] for w in response_data["data"]])
|
| |
|
| |
def test_post_valid_widget_name_with_config(self):
|
| |
+ hub = Hub.by_name("ralph", "user")
|
| |
self.assertEqual(
|
| |
- Widget.query.filter(
|
| |
- Hub.name == "ralph",
|
| |
- Widget.plugin == "about",
|
| |
- ).count(), 1)
|
| |
+ Widget.query.filter_by(hub=hub, plugin="about").count(), 1)
|
| |
data = {
|
| |
"name": "about",
|
| |
"config": {'text': 'text of widget'},
|
| |
@@ -178,7 +185,7 @@
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(app, user):
|
| |
result = self.app.post(
|
| |
- '/api/hubs/ralph/widgets/',
|
| |
+ '/api/hubs/%s/widgets/' % hub.id,
|
| |
content_type="application/json",
|
| |
data=json.dumps(data))
|
| |
self.assertEqual(result.status_code, 200)
|
| |
@@ -186,17 +193,13 @@
|
| |
json.loads(result.get_data(as_text=True)),
|
| |
{"status": "OK"})
|
| |
self.assertEqual(
|
| |
- Widget.query.filter(
|
| |
- Hub.name == "ralph",
|
| |
- Widget.plugin == "about",
|
| |
- ).count(), 2)
|
| |
+ Widget.query.filter_by(hub=hub, plugin="about").count(), 2)
|
| |
|
| |
def test_post_invalid_config(self):
|
| |
+ hub = Hub.by_name("ralph", "user")
|
| |
self.assertEqual(
|
| |
- Widget.query.join(Hub).filter(
|
| |
- Hub.name == "ralph",
|
| |
- Widget.plugin == "meetings",
|
| |
- ).count(), 1)
|
| |
+ Widget.query.filter_by(
|
| |
+ hub=hub, plugin="meetings").count(), 1)
|
| |
data = {
|
| |
"name": "meetings",
|
| |
"config": {
|
| |
@@ -208,7 +211,7 @@
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(app, user):
|
| |
result = self.app.post(
|
| |
- '/api/hubs/ralph/widgets/',
|
| |
+ '/api/hubs/%s/widgets/' % hub.id,
|
| |
content_type="application/json",
|
| |
data=json.dumps(data))
|
| |
self.assertEqual(result.status_code, 200)
|
| |
@@ -229,18 +232,25 @@
|
| |
|
| |
class TestAPIHubWidget(APPTest):
|
| |
|
| |
+ def setUp(self):
|
| |
+ super(TestAPIHubWidget, self).setUp()
|
| |
+ self.hub = Hub.by_name("ralph", "user")
|
| |
+ self.widget = widget_instance("ralph", "pagure_pr")
|
| |
+ self.url = "/api/hubs/%s/widgets/%s/" % (self.hub.id, self.widget.idx)
|
| |
+
|
| |
def test_get_logged_in(self):
|
| |
user = FakeAuthorization('ralph')
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/37/", user=user)
|
| |
+ response = self.check_url(self.url, user=user)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "OK")
|
| |
self.assertEqual(response_data["data"]["name"], "pagure_pr")
|
| |
|
| |
def test_get_logged_out(self):
|
| |
- hub = Hub.by_name('ralph')
|
| |
- hub.config["visibility"] = "private"
|
| |
+ self.hub.config["visibility"] = "private"
|
| |
+ widget = widget_instance("ralph", "pagure_pr")
|
| |
self.session.commit()
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/31/", code=403)
|
| |
+ response = self.check_url(
|
| |
+ "/api/hubs/%s/widgets/%s/" % (self.hub.id, widget.idx), code=403)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "ERROR")
|
| |
|
| |
@@ -251,7 +261,7 @@
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(app, user):
|
| |
result = self.app.put(
|
| |
- '/api/hubs/ralph/widgets/37/',
|
| |
+ self.url,
|
| |
content_type="application/json",
|
| |
data=json.dumps({}))
|
| |
self.assertEqual(result.status_code, 200)
|
| |
@@ -261,7 +271,7 @@
|
| |
self.assertTrue(queue.enqueue.called)
|
| |
task = queue.enqueue.call_args_list[0][0][0]
|
| |
expected = {
|
| |
- "hub": "ralph", "idx": 37,
|
| |
+ "hub": "ralph", "idx": self.widget.idx,
|
| |
"type": "widget-cache",
|
| |
"fn_name": "GetPRs",
|
| |
}
|
| |
@@ -271,7 +281,7 @@
|
| |
user = FakeAuthorization('decause')
|
| |
with auth_set(app, user):
|
| |
result = self.app.put(
|
| |
- '/api/hubs/ralph/widgets/37/',
|
| |
+ self.url,
|
| |
content_type="application/json",
|
| |
data=json.dumps({"config": {"text": "Defaced!"}}))
|
| |
self.assertEqual(result.status_code, 403)
|
| |
@@ -279,22 +289,23 @@
|
| |
def test_delete(self):
|
| |
user = FakeAuthorization('ralph')
|
| |
with auth_set(app, user):
|
| |
- result = self.app.delete('/api/hubs/ralph/widgets/37/')
|
| |
+ result = self.app.delete(self.url)
|
| |
self.assertEqual(result.status_code, 200)
|
| |
self.assertEqual(
|
| |
json.loads(result.get_data(as_text=True)),
|
| |
{"status": "OK"})
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/")
|
| |
+ response = self.check_url("/api/hubs/%s/widgets/" % self.hub.id)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertNotIn(37, [w["idx"] for w in response_data["data"]])
|
| |
|
| |
def test_delete_unauthorized(self):
|
| |
user = FakeAuthorization('decause')
|
| |
with auth_set(app, user):
|
| |
- response = self.app.delete('/api/hubs/ralph/widgets/37/')
|
| |
+ response = self.app.delete(self.url)
|
| |
self.assertEqual(response.status_code, 403)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
self.assertEqual(response_data["status"], "ERROR")
|
| |
- response = self.check_url("/api/hubs/ralph/widgets/")
|
| |
+ response = self.check_url("/api/hubs/%s/widgets/" % self.hub.id)
|
| |
response_data = json.loads(response.get_data(as_text=True))
|
| |
- self.assertIn(37, [w["idx"] for w in response_data["data"]])
|
| |
+ self.assertIn(
|
| |
+ self.widget.idx, [w["idx"] for w in response_data["data"]])
|
| |
Fixes #158
Once again, a big batch of commits. Please read one by one in order.