def test_dashboard_with_nonexistent_tab_slug_returns_nothing(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, slug='module', info=['module-info'], title='module-title', options={ 'tabs': [ { 'slug': 'tab-we-want', 'title': 'tab-title' }, { 'slug': 'tab-we-dont-want', } ] }) ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-dont-want') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/module/module-non-existent-tab'} ) data = json.loads(resp.content) assert_that(data, has_entry('status', 404))
def test_removing_nested_module(self): dashboard = DashboardFactory(title='test dashboard') dashboard.owners.add(self.user) parent = ModuleFactory(dashboard=dashboard, title='parent') child = ModuleFactory( parent=parent, dashboard=dashboard, title='module to remove') child_of_child = ModuleFactory( title='child of child', parent=child, dashboard=dashboard) dashboard_data = dashboard.serialize() dashboard_data['modules'][0]['order'] = 1 dashboard_data['modules'][0]['type_id'] = parent.type_id child_data = dashboard_data['modules'][0]['modules'][0] child_of_child_data = child_data['modules'][0] child_of_child_data['order'] = 2 child_of_child_data['type_id'] = child_of_child.type_id dashboard_data['modules'][0]['modules'] = [child_of_child_data] resp = self.client.put( '/dashboard/{}'.format(dashboard.id), json.dumps(dashboard_data, cls=JsonEncoder), content_type="application/json", HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, equal_to(200)) parent_json = json.loads(resp.content)['modules'][0] child_json = parent_json['modules'][0] assert_that(parent_json['title'], equal_to('parent')) assert_that(child_json['title'], equal_to('child of child')) assert_that(child_json['modules'], has_length(0)) assert_that(Module.objects.filter(id=child.id), has_length(0)) assert_that(Module.objects.get(id=child_of_child.id).parent, equal_to(parent))
def test_list_modules_on_dashboard_when_not_owner_returns_404(self): dashboard2 = DashboardFactory( published=True, title='A service', slug='some-slug2', ) ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-1', options={}, order=1) ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-2', options={}, order=2) ModuleFactory(type=self.module_type, dashboard=dashboard2, slug='module-3', options={}, order=1) resp = self.client.get('/dashboard/{}/module'.format(dashboard2.slug), HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, is_(equal_to(404)))
def test_dashboard_with_section_slug_returns_module_and_children(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() parent = ModuleFactory( type=module_type, slug='section-we-want', order=1, dashboard=dashboard ) ModuleFactory( type=module_type, slug='module-we-want', order=2, dashboard=dashboard, parent=parent) resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/section-we-want'}) data = json.loads(resp.content) assert_that(data['modules'], contains(has_entry('slug', 'section-we-want'))) assert_that(len(data['modules']), equal_to(1)) assert_that(data['modules'][0]['modules'], contains(has_entry('slug', 'module-we-want'))) assert_that(data, has_entry('page-type', 'module'))
def test_list_modules_by_uuid_or_slug(self): module1 = ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-1', options={}, order=1) module2 = ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-2', options={}, order=2) resp = self.client.get('/dashboard/{}/module'.format( self.dashboard.slug), HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, is_(equal_to(200))) resp_json = json.loads(resp.content) assert_that(len(resp_json), is_(equal_to(2))) assert_that(resp_json, has_item(has_entry('id', str(module1.id)))) assert_that(resp_json, has_item(has_entry('id', str(module2.id)))) self.client.get('/dashboard/{}/module'.format(self.dashboard.id), HTTP_AUTHORIZATION='Bearer correct-token') resp_json = json.loads(resp.content) assert_that(len(resp_json), is_(equal_to(2))) assert_that(resp_json, has_item(has_entry('id', str(module1.id)))) assert_that(resp_json, has_item(has_entry('id', str(module2.id))))
def test_dashboard_with_tab_slug_only_returns_tab(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-want', info=['module-info'], title='module-title', options={ 'tabs': [ { 'slug': 'tab-we-want', 'title': 'tab-title' }, { 'slug': 'tab-we-dont-want', } ] }) ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-dont-want') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/module-we-want/module-we-want-tab-we-want'} ) data = json.loads(resp.content) assert_that(data['modules'], contains( has_entries({'slug': 'tab-we-want', 'info': contains('module-info'), 'title': 'module-title - tab-title' }))) assert_that(data, has_entry('page-type', 'module'))
def test_spotlightify_with_nested_modules(self): parent = ModuleFactory(slug='a-module', order=1) child = ModuleFactory(slug='b-module', order=2, parent=parent) ModuleFactory(slug='c-module', order=3, parent=child) other_child = ModuleFactory(slug='d-module', order=4, parent=parent) spotlightify = parent.spotlightify() assert_that( spotlightify, has_entry( 'modules', contains(child.spotlightify(), other_child.spotlightify())))
def test_serialize_with_nested_modules(self): parent = ModuleFactory(slug='a-module', order=1) child = ModuleFactory(slug='b-module', order=2, parent=parent) ModuleFactory(slug='c-module', order=3, parent=child) other_child = ModuleFactory(slug='d-module', order=4, parent=parent) serialization = parent.serialize() assert_that( serialization, has_entry('modules', contains(child.serialize(), other_child.serialize()))) assert_that(serialization['modules'][0]['parent'], has_entry('id', str(parent.id)))
def test_dashboard_with_module_slug_only_returns_module(self): dashboard = DashboardFactory(slug='my-first-slug') module_type = ModuleTypeFactory() ModuleFactory(type=module_type, dashboard=dashboard, slug='module-we-want') ModuleFactory(type=module_type, dashboard=dashboard, slug='module-we-dont-want') resp = self.client.get('/public/dashboards', {'slug': 'my-first-slug/module-we-want'}) data = json.loads(resp.content) assert_that(data['modules'], contains(has_entry('slug', 'module-we-want'))) assert_that(data, has_entry('page-type', 'module'))
def test_updating_nested_module_attributes(self): dashboard = DashboardFactory(title='test dashboard') dashboard.owners.add(self.user) module = ModuleFactory( dashboard=dashboard, title='module-title', data_set=DataSetFactory() ) nested_module = ModuleFactory( parent=module, dashboard=dashboard, title='module-title', data_set=DataSetFactory() ) new_data_set = DataSetFactory( data_group__name='new-group-title', data_type__name='new-type-title' ) dashboard_data = dashboard.serialize() dashboard_data['modules'][0]['order'] = 1 dashboard_data['modules'][0]['type_id'] = module.type_id dashboard_data['modules'][0]['query_parameters'] = { 'sort_by': 'thing:desc', } nested_data = dashboard_data['modules'][0]['modules'][0] nested_data['title'] = 'new module title' nested_data['data_group'] = 'new-group-title' nested_data['data_type'] = 'new-type-title' nested_data['order'] = 2 nested_data['type_id'] = module.type_id nested_data['query_parameters'] = {'sort_by': 'thing:desc', } nested_data['modules'] = [] resp = self.client.put( '/dashboard/{}'.format(dashboard.id), json.dumps(dashboard_data, cls=JsonEncoder), content_type="application/json", HTTP_AUTHORIZATION='Bearer correct-token') module_from_db = Module.objects.get(id=nested_module.id) assert_that(resp.status_code, equal_to(200)) assert_that( module_from_db.title, equal_to('new module title') ) assert_that( module_from_db.data_set_id, equal_to(new_data_set.id) )
def test_modules_property_returns_modules(self): data_set = DataSet.objects.create(data_group=self.data_group1, data_type=self.data_type1) dashboard = DashboardFactory(status="published") module = ModuleFactory(data_set=data_set, dashboard=dashboard) assert_equal(module.slug, data_set.modules[0].slug)
def test_modules_property_returns_tabbed_modules_from_data_source(self): data_set = DataSet.objects.create(data_group=self.data_group1, data_type=self.data_type1) dashboard = DashboardFactory(status="published") options = { 'tabs': [{ 'data-source': { u'data-group': "some-thing-group", u'data-type': "some-thing-time", u'query-params': {} }, 'module-type': u'single_timeseries', 'title': "Title 1 {}".format(data_set.data_group.name), 'other_title': "Title 2 {}".format(data_set.data_type.name) }] } ModuleFactory(dashboard=dashboard, type=self.module_type, options=options) assert_equal([], data_set.modules)
def test_edit_a_module_when_not_owner(self): module1 = ModuleFactory(type=self.module_type, dashboard=self.dashboard_without_owner, slug='module-1', options={}, order=1) resp = self.client.put( '/module/{}'.format(module1.slug), data=json.dumps({ 'slug': 'a-module', 'type_id': str(self.module_type.id), 'title': 'Some module', 'description': 'Some text about the module', 'info': ['foo'], 'options': { 'thing': 'a value', }, 'objects': "some object", 'order': 1, 'modules': [], }), HTTP_AUTHORIZATION='Bearer development-oauth-access-token', content_type='application/json') assert_that(resp.status_code, is_(equal_to(405)))
def test_get_module_by_uuid(self): module1 = ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-1', options={}, order=1) resp = self.client.get('/module/{}'.format(module1.id), HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, is_(equal_to(200))) resp_json = json.loads(resp.content) module_attrs = { u'info': [], u'description': u'', u'parent': None, u'title': u'title', u'data_set': None, u'query_parameters': None, u'modules': [], u'slug': u'module-1', u'options': {}, u'dashboard': { u'id': str(module1.dashboard_id) }, u'type': { u'id': str(module1.type_id) }, u'id': str(module1.id) } assert_that(resp_json, equal_to(module_attrs))
def create_module(dashboard_model): with transaction.atomic(): ModuleFactory( slug='a-module', type=self.module_type, dashboard=dashboard_model, order=1, )
def test_list_modules_by_uuid_or_slug(self): DashboardFactory( published=True, title='A service', slug='some-slug2', ) module1 = ModuleFactory( type=self.module_type, dashboard=self.dashboard, slug='module-1', options={}, order=1) module2 = ModuleFactory( type=self.module_type, dashboard=self.dashboard, slug='module-2', options={}, order=2) resp = self.client.get( '/dashboard/{}/module'.format(self.dashboard.slug)) assert_that(resp.status_code, is_(equal_to(200))) resp_json = json.loads(resp.content) assert_that(len(resp_json), is_(equal_to(2))) assert_that( resp_json, has_item(has_entry('id', str(module1.id)))) assert_that( resp_json, has_item(has_entry('id', str(module2.id)))) self.client.get( '/dashboard/{}/module'.format(self.dashboard.id)) resp_json = json.loads(resp.content) assert_that(len(resp_json), is_(equal_to(2))) assert_that( resp_json, has_item(has_entry('id', str(module1.id)))) assert_that( resp_json, has_item(has_entry('id', str(module2.id))))
def test_spotlightify_with_a_nested_module(self): section_type = ModuleTypeFactory(name='section') graph_type = ModuleTypeFactory(name='graph') parent = ModuleFactory(type=section_type, slug='a-module', order=1, dashboard=self.dashboard) ModuleFactory(type=graph_type, slug='b-module', order=2, dashboard=self.dashboard, parent=parent) spotlightify = self.dashboard.spotlightify() assert_that(spotlightify, has_entry('modules', contains(parent.spotlightify()))) assert_that(len(spotlightify['modules']), equal_to(1))
def test_list_modules(self): ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-1', options={}, order=1) ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-2', options={}, order=2) resp = self.client.get('/modules', HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, is_(equal_to(200))) resp_json = json.loads(resp.content) assert_that(len(resp_json), is_(equal_to(2)))
def test_dashboard_with_non_existing_module_slug_returns_nothing(self): dashboard = DashboardFactory(slug='my-first-slug') module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-want') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/nonexisting-module'}) data = json.loads(resp.content) assert_that(data, has_entry('status', 404))
def test_get_module_by_uuid_404s_when_user_not_owner_of_dashboard(self): module1 = ModuleFactory(type=self.module_type, dashboard=self.dashboard_without_owner, slug='module-1', options={}, order=1) resp = self.client.get('/module/{}'.format(module1.id), HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, is_(equal_to(404)))
def test_module_doesnt_delete(self): module1 = ModuleFactory(type=self.module_type, dashboard=self.dashboard, slug='module-1', options={}, order=1) delete_resp = self.client.delete( '/module/{}'.format(module1.id), HTTP_AUTHORIZATION='Bearer development-oauth-access-token') assert_that(delete_resp.status_code, equal_to(405))
def test_serialize_with_no_nested_modules(self): module = ModuleFactory(slug='a-module', type=self.module_type, options={}, query_parameters={}, order=1, dashboard=self.dashboard_a) serialization = module.serialize() assert_that(serialization['modules'], equal_to([])) assert_that(serialization['parent'], equal_to(None))
def test_serialize_contains_nested_modules(self): module_type = ModuleTypeFactory() ModuleFactory(type=module_type, dashboard=self.dashboard, order=3, slug='slug3') parent = ModuleFactory(type=module_type, dashboard=self.dashboard, order=1, slug='slug1') ModuleFactory(parent=parent, type=module_type, order=2, slug='slug2') data = self.dashboard.serialize() assert_that( data['modules'], contains(has_entry('slug', 'slug1'), has_entry('slug', 'slug3'))) assert_that(data['modules'][0]['modules'][0], has_entry('slug', 'slug2')) assert_that(data['modules'], is_not(has_entry('slug', 'slug2')))
def test_module_delegates_to_dashboard_owners(self): module = ModuleFactory(slug='a-module', type=self.module_type, dashboard=self.dashboard_a, order=1, options={ 'foo': 'bar', }, query_parameters={'sort_by': 'foo'}) user, _ = User.objects.get_or_create(email='*****@*****.**') self.dashboard_a.owners.add(user) assert_that(module.owners.all(), self.dashboard_a.owners.all())
def build_dashboard(self, published=True, filter_by=[]): dashboard = DashboardFactory( published=published, ) module = ModuleFactory( dashboard=dashboard, data_set=self.data_set, query_parameters={ 'filter_by': filter_by, }, ) return dashboard
def test_spotlightify_with_a_module(self): module_type = ModuleTypeFactory(name='graph', schema={}) ModuleFactory( type=module_type, dashboard=self.dashboard, slug='a-module', options={}, order=1, ) spotlight_dashboard = self.dashboard.spotlightify() assert_that(len(spotlight_dashboard['modules']), equal_to(1)) assert_that(spotlight_dashboard['modules'], has_item(has_entry('slug', 'a-module')))
def test_delete_dashboard_without_permission(self): dashboard = DashboardFactory(title='test delete dashboard') dashboard.owners.add(self.user) module = ModuleFactory( title='module to remove', dashboard=dashboard) resp = self.client.delete( '/dashboard/{}'.format(dashboard.id), content_type="application/json", HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, equal_to(403)) assert_that(Dashboard.objects.count(), equal_to(1)) assert_that(Module.objects.count(), equal_to(1))
def test_modules_are_ordered_correctly(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, order=2, slug='slug2') ModuleFactory( type=module_type, dashboard=dashboard, order=1, slug='slug1') ModuleFactory( type=module_type, dashboard=dashboard, order=3, slug='slug3') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug'}) data = json.loads(resp.content) assert_that(data['modules'], contains( has_entry('slug', 'slug1'), has_entry('slug', 'slug2'), has_entry('slug', 'slug3')))
def test_dashboards_by_dataset_some_dashboards(self): data_set = DataSet.objects.get(name='group1_type1') dashboard = DashboardFactory() module_type = ModuleTypeFactory() ModuleFactory(type=module_type, dashboard=dashboard, data_set=data_set) resp = self.client.get( '/data-sets/group1_type1/dashboard', HTTP_AUTHORIZATION='Bearer development-oauth-access-token') assert_equal(resp.status_code, 200) json_resp = json.loads(resp.content.decode('utf-8')) assert_equal(len(json_resp), 1) assert_equal(json_resp[0]['id'], str(dashboard.id))
def test_removing_module(self): dashboard = DashboardFactory(title='test dashboard') module = ModuleFactory(title='module to remove', dashboard=dashboard) dashboard_data = dashboard.serialize() dashboard_data['modules'] = [] resp = self.client.put('/dashboard/{}'.format(dashboard.id), json.dumps(dashboard_data, cls=JsonEncoder), content_type="application/json", HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, equal_to(200)) assert_that(json.loads(resp.content), has_entry('modules', has_length(0))) assert_that(Module.objects.filter(id=module.id), has_length(0))