import json import mock import pytest from httmock import HTTMock from django.contrib.auth.models import User, Group from django.test import override_settings from combo.data.models import Page from combo.apps.dataviz.models import Gauge, ChartNgCell from .test_public import login, normal_user pytestmark = pytest.mark.django_db @pytest.fixture def cell(): page = Page(title='One', slug='index') page.save() cell = Gauge(page=page, order=0, placeholder='content') cell.data_source = '[test_url]/XXX' cell.save() return cell def test_jsonp_gauge(app, cell): with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}): resp = app.get('/') assert 'data-gauge-count-jsonp-url="http://www.example.net/XXX"' in resp.text def test_json_gauge(app, cell): cell.jsonp_data_source = False cell.save() with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}): with mock.patch('combo.apps.dataviz.views.requests.get') as requests_get: requests_get.return_value = mock.Mock(content='xxx', status_code=200) resp = app.get('/') assert 'data-gauge-count-url="/ajax/gauge-count/%s/"' % cell.id in resp.text resp = app.get('/ajax/gauge-count/%s/' % cell.id) assert resp.text == 'xxx' assert requests_get.call_args[0][0] == 'http://www.example.net/XXX' VISUALIZATION_JSON = [ { 'data-url': 'https://bijoe.example.com/visualization/1/json/', 'path': 'https://bijoe.example.com/visualization/1/iframe/?signature=123', 'name': 'example visualization (X)', 'slug': 'example', }, { 'data-url': 'https://bijoe.example.com/visualization/2/json/', 'path': 'https://bijoe.example.com/visualization/2/iframe/?signature=123', 'name': 'second visualization (Y)', 'slug': 'second', }, { 'data-url': 'https://bijoe.example.com/visualization/3/json/', 'path': 'https://bijoe.example.com/visualization/3/iframe/?signature=123', 'name': 'third visualization (X/Y)', 'slug': 'third', }, { 'data-url': 'https://bijoe.example.com/visualization/4/json/', 'path': 'https://bijoe.example.com/visualization/4/iframe/?signature=123', 'name': 'fourth visualization (no axis)', 'slug': 'fourth', }, ] def bijoe_mock(url, request): if url.path == '/visualization/json/': return {'content': json.dumps(VISUALIZATION_JSON), 'request': request, 'status_code': 200} if url.path == '/visualization/1/json/': response = { 'format': '1', 'data': [222, 134, 53], 'axis': { 'x_labels': ['web', 'mail', 'email'] } } return {'content': json.dumps(response), 'request': request, 'status_code': 200} if url.path == '/visualization/2/json/': response = { 'format': '1', 'data': [222, 134, 53], 'axis': { 'y_labels': ['web', 'mail', 'email'] } } return {'content': json.dumps(response), 'request': request, 'status_code': 200} if url.path == '/visualization/3/json/': response = { 'format': '1', 'data': [ [222, 134, 53], [122, 114, 33], ], 'axis': { 'x_labels': ['web', 'mail', 'email'], 'y_labels': ['foo', 'bar'], } } return {'content': json.dumps(response), 'request': request, 'status_code': 200} if url.path == '/visualization/4/json/': response = { 'format': '1', 'data': 222, 'axis': {} } return {'content': json.dumps(response), 'request': request, 'status_code': 200} def test_chartng_cell(app): page = Page(title='One', slug='index') page.save() with override_settings(KNOWN_SERVICES={ 'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com', 'secret': 'combo', 'orig': 'combo'}}}): with HTTMock(bijoe_mock): cell = ChartNgCell(page=page, order=1) cell.data_reference = 'plop:example' cell.save() assert cell.cached_json == VISUALIZATION_JSON[0] # bar chart = cell.get_chart() assert chart.__class__.__name__ == 'Bar' assert chart.x_labels == ['web', 'mail', 'email'] assert chart.raw_series == [([222, 134, 53], {'title': ''})] # horizontal bar cell.chart_type = 'horizontal-bar' chart = cell.get_chart() assert chart.__class__.__name__ == 'HorizontalBar' assert chart.x_labels == ['web', 'mail', 'email'] assert chart.raw_series == [([222, 134, 53], {'title': ''})] # pie cell.chart_type = 'pie' chart = cell.get_chart() assert chart.__class__.__name__ == 'Pie' assert chart.x_labels == ['web', 'mail', 'email'] assert chart.raw_series == [ ([222], {'title': u'web'}), ([134], {'title': u'mail'}), ([53], {'title': u'email'}) ] # data in Y cell.chart_type = 'bar' cell.data_reference = 'plop:second' cell.save() assert cell.cached_json == VISUALIZATION_JSON[1] chart = cell.get_chart() assert chart.x_labels == ['web', 'mail', 'email'] assert chart.raw_series == [([222, 134, 53], {'title': ''})] # data in X/Y cell.chart_type = 'bar' cell.data_reference = 'plop:third' cell.save() assert cell.cached_json == VISUALIZATION_JSON[2] chart = cell.get_chart() assert chart.x_labels == ['web', 'mail', 'email'] assert chart.raw_series == [ ([222, 134, 53], {'title': u'foo'}), ([122, 114, 33], {'title': u'bar'}), ] # single data point cell.chart_type = 'bar' cell.data_reference = 'plop:fourth' cell.save() assert cell.cached_json == VISUALIZATION_JSON[3] chart = cell.get_chart() assert chart.x_labels == [''] assert chart.raw_series == [([222], {'title': ''})] def test_chartng_cell_view(app, normal_user): page = Page(title='One', slug='index') page.save() with override_settings(KNOWN_SERVICES={ 'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com', 'secret': 'combo', 'orig': 'combo'}}}): with HTTMock(bijoe_mock): cell = ChartNgCell(page=page, order=1, placeholder='content') cell.data_reference = 'plop:example' cell.save() resp = app.get('/') assert 'min-height: 250px' in resp.text assert '/api/dataviz/graph/1/' in resp.text resp = app.get('/api/dataviz/graph/1/?width=400') assert resp.content_type == 'image/svg+xml' page.public = False page.save() resp = app.get('/api/dataviz/graph/1/?width=400', status=403) page.public = True page.save() group = Group(name='plop') group.save() cell.public = False cell.groups = [group] cell.save() resp = app.get('/api/dataviz/graph/1/?width=400', status=403) app = login(app, username='normal-user', password='normal-user') resp = app.get('/api/dataviz/graph/1/?width=400', status=403) normal_user.groups = [group] normal_user.save() resp = app.get('/api/dataviz/graph/1/?width=400', status=200) # table visualization cell.chart_type = 'table' cell.save() resp = app.get('/') assert '222' in resp.text def test_chartng_cell_manager(app, admin_user): page = Page(title='One', slug='index') page.save() app = login(app) with override_settings(KNOWN_SERVICES={ 'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com', 'secret': 'combo', 'orig': 'combo'}}}): with HTTMock(bijoe_mock): cell = ChartNgCell(page=page, order=1, placeholder='content') cell.data_reference = 'plop:example' cell.save() resp = app.get('/manage/pages/%s/' % page.id) assert resp.form['cdataviz_chartngcell-%s-data_reference' % cell.id].options == [ (u'plop:example', True, u'example visualization (X)'), (u'plop:fourth', False, u'fourth visualization (no axis)'), (u'plop:second', False, u'second visualization (Y)'), (u'plop:third', False, u'third visualization (X/Y)'), ]