Example #1
0
    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))
Example #2
0
    def test_updating_module_attributes(self):
        dashboard = DashboardFactory(title='test dashboard')
        module = ModuleFactory(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]['title'] = 'new module title'
        dashboard_data['modules'][0]['data_group'] = 'new-group-title'
        dashboard_data['modules'][0]['data_type'] = 'new-type-title'
        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',
        }
        dashboard_data['modules'][0]['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=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))
Example #3
0
 def setUpClass(cls):
     cls.data_group = DataGroupFactory(name='group')
     cls.data_type = DataTypeFactory(name='type')
     cls.data_set = DataSetFactory(
         data_group=cls.data_group,
         data_type=cls.data_type,
     )
     cls.module_type = ModuleTypeFactory(name='graph', schema={})
     cls.dashboard_a = DashboardFactory(slug='a-dashboard', published=False)
     cls.dashboard_b = DashboardFactory(slug='b-dashboard', published=False)
Example #4
0
    def test_list_dashboards_list_alphabetically(self):
        DashboardFactory(slug='dashboard-1', title='Alpha')
        DashboardFactory(slug='dashboard-2', title='Beta')
        resp = self.client.get(
            '/dashboards',
            HTTP_AUTHORIZATION='Bearer development-oauth-access-token')
        response_object = json.loads(resp.content)

        assert_that(response_object[0]['title'], is_('Alpha'))
        assert_that(response_object[1]['title'], is_('Beta'))
Example #5
0
    def test_putting_to_nonexistant_dashboard_returns_404(self):
        dashboard = DashboardFactory()
        dashboard_data = dashboard.serialize()

        resp = self.client.put('/dashboard/nonsense',
                               json.dumps(dashboard_data, cls=JsonEncoder),
                               content_type="application/json",
                               HTTP_AUTHORIZATION='Bearer correct-token')

        assert_that(resp.status_code, equal_to(404))
Example #6
0
    def test_get_dashboards_only_caches_when_published(self):
        DashboardFactory(slug='published_dashboard')
        DashboardFactory(slug='unpublished_dashboard', published=False)

        resp = self.client.get(
            '/public/dashboards', {'slug': 'published_dashboard'})
        assert_that(resp['Cache-Control'], equal_to('max-age=300'))

        resp = self.client.get(
            '/public/dashboards', {'slug': 'unpublished_dashboard'})
        assert_that(resp['Cache-Control'], equal_to('no-cache'))
Example #7
0
    def test_putting_to_nonexistant_dashboard_returns_404(self):
        dashboard = DashboardFactory()
        dashboard_data = dashboard.serialize()

        resp = self.client.put(
            '/dashboard/nonsense',
            json.dumps(dashboard_data, cls=JsonEncoder),
            content_type="application/json",
            HTTP_AUTHORIZATION='Bearer correct-token')

        assert_that(resp.status_code, equal_to(404))
Example #8
0
    def test_change_title_of_dashboard_changes_title_of_dashboard(self):
        dashboard = DashboardFactory()
        dashboard_data = dashboard.serialize()

        dashboard_data['title'] = 'foo'

        resp = self.client.put('/dashboard/{}'.format(dashboard.slug),
                               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(
            Dashboard.objects.get(id=dashboard.id).title, equal_to('foo'))
Example #9
0
    def test_update_non_existent_module_doesnt_work(self):
        dashboard = DashboardFactory(title='test dashboard')
        dashboard_data = dashboard.serialize()
        dashboard_data['modules'] = [{
            'id': '623b6e9c-507f-4c5c-8937-7ea81a349cfa',
            'title': 'non-existent-title'
        }]

        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(400))
Example #10
0
    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))
Example #11
0
    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)
        )
Example #12
0
def test_truncated_slug_is_replaced():

    munger = SpreadsheetMunger({
        'names_transaction_name': 11,
        'names_transaction_slug': 12,
        'names_service_name': 9,
        'names_service_slug': 10,
        'names_tx_id': 19,
        'names_other_notes': 17,
        'names_description': 8,
    })

    mock_account = Mock()
    mock_account.open_by_key() \
        .worksheet().get_all_values.return_value = tx_worksheet
    tx = munger.load_tx_worksheet(mock_account)

    mock_account = Mock()
    mock_account.open_by_key() \
        .worksheet().get_all_values.return_value = names_worksheet
    names = munger.load_names_worksheet(mock_account)

    record = munger.merge(tx, names)[0]
    truncated_slug = 'truncated-{}'.format(random.randrange(1e7))
    record['tx_truncated'] = truncated_slug

    dashboard = DashboardFactory(slug=truncated_slug)
    dashboard = dashboard_from_record(record)

    assert_that(dashboard, has_properties({'slug': record['tx_id']}))
Example #13
0
    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)
Example #14
0
    def test_get_an_existing_dashboard_returns_a_dashboard(self):
        dashboard = DashboardFactory(slug='slug1')
        dashboard.owners.add(self.user)

        resp = self.client.get(
            '/dashboard/{}'.format(dashboard.slug),
            HTTP_AUTHORIZATION='Bearer correct-token')

        assert_that(resp.status_code, equal_to(200))
        assert_that(
            json.loads(resp.content),
            has_entries(
                {
                    "description_extra": "",
                    "strapline": "Dashboard",
                    "description": "",
                    "links": [],
                    "title": "title",
                    "tagline": "",
                    "modules": [],
                    "dashboard_type": "transaction",
                    "slug": dashboard.slug,
                    "improve_dashboard_message": True,
                    "customer_type": "",
                    "costs": "",
                    "page_type": "dashboard",
                    "published": True,
                    "business_model": "",
                    "other_notes": ""
                }
            )
        )
Example #15
0
    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_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)))
Example #17
0
    def setUpClass(cls):
        cls.data_group = DataGroupFactory(name='group')
        cls.data_type = DataTypeFactory(name='type')

        cls.data_set = DataSetFactory(
            data_group=cls.data_group,
            data_type=cls.data_type,
        )

        cls.module_type = ModuleTypeFactory(
            name='a-type',
            schema={
                'type': 'object',
                'properties': {
                    'thing': {
                        'type': 'string',
                        'required': True
                    }
                },
                '$schema': "http://json-schema.org/draft-03/schema#"
            }
        )

        cls.dashboard = DashboardFactory(
            published=True,
            title='A service',
            slug='some-slug',
        )
Example #18
0
 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))
Example #19
0
 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'))
Example #20
0
 def test_get_dashboards_with_slug_query_param_returns_dashboard_json(self):
     DashboardFactory(slug='my_first_slug')
     resp = self.client.get(
         '/public/dashboards', {'slug': 'my_first_slug'})
     assert_that(json.loads(resp.content), has_entry('slug',
                                                     'my_first_slug'))
     assert_that(resp['Cache-Control'], equal_to('max-age=300'))
Example #21
0
    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'))
Example #22
0
    def test_change_title_of_dashboard_changes_title_of_dashboard(self):
        dashboard = DashboardFactory()
        dashboard.owners.add(self.user)
        dashboard_data = dashboard.serialize()

        dashboard_data['title'] = 'foo'

        resp = self.client.put(
            '/dashboard/{}'.format(dashboard.slug),
            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(
            Dashboard.objects.get(id=dashboard.id).title, equal_to('foo')
        )
Example #23
0
    def test_update_existing_link(self):
        dashboard = DashboardFactory(title='test dashboard')
        link = LinkFactory(dashboard=dashboard)
        dashboard_data = dashboard.serialize()
        dashboard_data['links'][0]['url'] = 'https://gov.uk/new-link'
        dashboard_data['links'][0]['title'] = 'new link title'

        resp = self.client.put('/dashboard/{}'.format(dashboard.id),
                               json.dumps(dashboard_data, cls=JsonEncoder),
                               content_type="application/json",
                               HTTP_AUTHORIZATION='Bearer correct-token')

        transaction_links = dashboard.link_set.filter(link_type='transaction')
        assert_that(transaction_links, has_length(1))
        link = transaction_links[0]
        assert_that(link.title, equal_to('new link title'))
        assert_that(link.url, equal_to('https://gov.uk/new-link'))
Example #24
0
    def test_update_non_existent_module_doesnt_work(self):
        dashboard = DashboardFactory(title='test dashboard')
        dashboard.owners.add(self.user)
        dashboard_data = dashboard.serialize()
        dashboard_data['modules'] = [
            {
                'id': '623b6e9c-507f-4c5c-8937-7ea81a349cfa',
                'title': 'non-existent-title'
            }
        ]

        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(400))
Example #25
0
    def test_404_when_user_not_in_ownership_array(self):
        dashboard = DashboardFactory()
        user, _ = User.objects.get_or_create(
            email='*****@*****.**')
        dashboard.owners.add(user)

        resp = self.client.get('/dashboard/{}'.format(dashboard.slug),
                               HTTP_AUTHORIZATION='Bearer correct-token')

        assert_that(resp.status_code, equal_to(404))
Example #26
0
    def test_update_existing_link(self):
        dashboard = DashboardFactory(title='test dashboard')
        dashboard.owners.add(self.user)
        link = LinkFactory(dashboard=dashboard)
        dashboard_data = dashboard.serialize()
        dashboard_data['links'][0]['url'] = 'https://gov.uk/new-link'
        dashboard_data['links'][0]['title'] = 'new link title'

        self.client.put(
            '/dashboard/{}'.format(dashboard.id),
            json.dumps(dashboard_data, cls=JsonEncoder),
            content_type="application/json",
            HTTP_AUTHORIZATION='Bearer correct-token')

        transaction_links = dashboard.link_set.filter(link_type='transaction')
        assert_that(transaction_links, has_length(1))
        link = transaction_links[0]
        assert_that(link.title, equal_to('new link title'))
        assert_that(link.url, equal_to('https://gov.uk/new-link'))
Example #27
0
 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))
Example #28
0
    def test_removing_module(self):
        dashboard = DashboardFactory(title='test dashboard')
        dashboard.owners.add(self.user)
        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))
Example #29
0
    def test_404_when_user_not_in_ownership_array_for_any_dashboards(self):
        dashboard = DashboardFactory()
        user, _ = User.objects.get_or_create(
            email='*****@*****.**')
        dashboard.owners.add(user)

        resp = self.client.get('/dashboards',
                               HTTP_AUTHORIZATION='Bearer correct-token')

        assert_that(resp.status_code, equal_to(200))
        assert_that(json.loads(resp.content), equal_to([]))
Example #30
0
 def test_class_level_list_for_spotlight_returns_minimal_json_array(self):
     dashboard_two = DashboardFactory()
     organisation = ServiceFactory()
     dashboard_two.organisation = organisation
     dashboard_two.service_cache = organisation
     agency = organisation.parents.first()
     dashboard_two.agency_cache = agency
     dashboard_two.department_cache = agency.parents.first()
     dashboard_two.validate_and_save()
     DashboardFactory(published=False)
     list_for_spotlight = Dashboard.list_for_spotlight()
     assert_that(list_for_spotlight['page-type'], equal_to('browse'))
     assert_that(len(list_for_spotlight['items']), equal_to(2))
     assert_that(
         list_for_spotlight['items'][0],
         has_entries({
             'slug': starts_with('slug'),
             'title': 'title',
             'dashboard-type': 'transaction'
         }))
     assert_that(
         list_for_spotlight['items'][1],
         has_entries({
             'slug':
             starts_with('slug'),
             'title':
             'title',
             'dashboard-type':
             'transaction',
             'department':
             has_entries({
                 'title': starts_with('department'),
                 'abbr': starts_with('abbreviation')
             }),
             'agency':
             has_entries({
                 'title': starts_with('agency'),
                 'abbr': starts_with('abbreviation')
             }),
             'service':
             has_entries({
                 'title': starts_with('service'),
                 'abbr': starts_with('abbreviation')
             })
         }))
    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
Example #32
0
    def test_updating_module_with_nonexistent_dataset(self):
        dashboard = DashboardFactory(title='test dashboard')
        module = ModuleFactory(
            dashboard=dashboard,
            title='module-title',
        )
        dashboard_data = dashboard.serialize()
        dashboard_data['modules'][0]['title'] = 'new module title'
        dashboard_data['modules'][0]['data_group'] = 'non-existent-group'
        dashboard_data['modules'][0]['data_type'] = 'non-existent-type'
        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',
        }

        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(400))
Example #33
0
    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))
Example #34
0
    def test_an_existing_dashboard_with_other_links_returns_a_dashboard(self):
        dashboard = DashboardFactory(slug='slug1')
        link = LinkFactory(dashboard=dashboard, link_type='other')
        dashboard.owners.add(self.user)

        resp = self.client.get(
            '/dashboard/{}'.format(dashboard.slug),
            HTTP_AUTHORIZATION='Bearer correct-token')

        assert_that(resp.status_code, equal_to(200))
        assert_that(
            json.loads(resp.content),
            has_entry('links', has_item(has_key('url')))
        )
Example #35
0
 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'))
Example #36
0
    def test_updating_module_with_nonexistent_dataset(self):
        dashboard = DashboardFactory(title='test dashboard')
        dashboard.owners.add(self.user)
        module = ModuleFactory(
            dashboard=dashboard,
            title='module-title',
        )
        dashboard_data = dashboard.serialize()
        dashboard_data['modules'][0]['title'] = 'new module title'
        dashboard_data['modules'][0]['data_group'] = 'non-existent-group'
        dashboard_data['modules'][0]['data_type'] = 'non-existent-type'
        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',
        }

        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(400))
    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))
Example #38
0
 def test_class_level_list_for_spotlight_returns_minimal_json_array(self):
     dashboard_two = DashboardFactory()
     organisation = ServiceFactory()
     dashboard_two.organisation = organisation
     dashboard_two.service_cache = organisation
     agency = organisation.parents.first()
     dashboard_two.agency_cache = agency
     dashboard_two.department_cache = agency.parents.first()
     dashboard_two.validate_and_save()
     DashboardFactory(published=False)
     list_for_spotlight = Dashboard.list_for_spotlight()
     assert_that(list_for_spotlight['page-type'], equal_to('browse'))
     assert_that(len(list_for_spotlight['items']), equal_to(2))
     assert_that(list_for_spotlight['items'][0],
                 has_entries({
                     'slug': starts_with('slug'),
                     'title': 'title',
                     'dashboard-type': 'transaction'
                 }))
     assert_that(list_for_spotlight['items'][1],
                 has_entries({
                     'slug': starts_with('slug'),
                     'title': 'title',
                     'dashboard-type': 'transaction',
                     'department': has_entries({
                         'title': starts_with('department'),
                         'abbr': starts_with('abbreviation')
                     }),
                     'agency': has_entries({
                         'title': starts_with('agency'),
                         'abbr': starts_with('abbreviation')
                     }),
                     'service': has_entries({
                         'title': starts_with('service'),
                         'abbr': starts_with('abbreviation')
                     })
                 }))
Example #39
0
class DashboardTestCase(TransactionTestCase):

    def setUp(self):
        self.dashboard = DashboardFactory()

    def tearDown(self):
        self.dashboard.delete()

    def test_dashboard_is_unpublished_when_status_is_unpublished(self):
        self.dashboard.status = 'unpublished'
        assert_that(self.dashboard.published, is_(False))

    def test_dashboard_is_unpublished_when_status_is_in_review(self):
        self.dashboard.status = 'in-review'
        assert_that(self.dashboard.published, is_(False))

    def test_dashboard_is_published_when_status_is_published(self):
        self.dashboard.status = 'published'
        assert_that(self.dashboard.published, is_(True))

    def test_publishing_a_dashboard_changes_its_status_to_published(self):
        dashboard = DashboardFactory(status='unpublished')
        dashboard.published = True
        assert_that(dashboard.status, equal_to('published'))

    def test_unpublishing_a_dashboard_changes_its_status_to_unpublished(self):
        self.dashboard.published = False
        assert_that(self.dashboard.status, equal_to('unpublished'))

    def test_class_level_list_for_spotlight_returns_minimal_json_array(self):
        dashboard_two = DashboardFactory()
        organisation = ServiceFactory()
        dashboard_two.organisation = organisation
        dashboard_two.service_cache = organisation
        agency = organisation.parents.first()
        dashboard_two.agency_cache = agency
        dashboard_two.department_cache = agency.parents.first()
        dashboard_two.validate_and_save()
        DashboardFactory(published=False)
        list_for_spotlight = Dashboard.list_for_spotlight()
        assert_that(list_for_spotlight['page-type'], equal_to('browse'))
        assert_that(len(list_for_spotlight['items']), equal_to(2))
        assert_that(list_for_spotlight['items'][0],
                    has_entries({
                        'slug': starts_with('slug'),
                        'title': 'title',
                        'dashboard-type': 'transaction'
                    }))
        assert_that(list_for_spotlight['items'][1],
                    has_entries({
                        'slug': starts_with('slug'),
                        'title': 'title',
                        'dashboard-type': 'transaction',
                        'department': has_entries({
                            'title': starts_with('department'),
                            'abbr': starts_with('abbreviation')
                        }),
                        'agency': has_entries({
                            'title': starts_with('agency'),
                            'abbr': starts_with('abbreviation')
                        }),
                        'service': has_entries({
                            'title': starts_with('service'),
                            'abbr': starts_with('abbreviation')
                        })
                    }))

    def test_spotlightify_no_modules(self):
        spotlight_dashboard = self.dashboard.spotlightify()
        assert_that(spotlight_dashboard, has_entry('modules', []))

    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_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_transaction_link(self):
        self.dashboard.update_transaction_link('blah', 'http://www.gov.uk')
        self.dashboard.update_transaction_link('blah2', 'http://www.gov.uk')
        self.dashboard.validate_and_save()
        assert_that(self.dashboard.link_set.all(), has_length(1))
        assert_that(self.dashboard.link_set.first().title, equal_to('blah2'))
        assert_that(
            self.dashboard.link_set.first().link_type,
            equal_to('transaction')
        )

    def test_other_link(self):
        self.dashboard.add_other_link('blah', 'http://www.gov.uk')
        self.dashboard.add_other_link('blah2', 'http://www.gov.uk')
        self.dashboard.validate_and_save()
        links = self.dashboard.link_set.all()

        assert_that(links, has_length(2))
        assert_that(
            links,
            has_items(
                has_property('title', 'blah'),
                has_property('title', 'blah2'),
            )
        )
        assert_that(
            self.dashboard.link_set.first().link_type,
            equal_to('other')
        )

    def test_spotlightify_handles_other_and_transaction_links(self):
        self.dashboard.add_other_link('other', 'http://www.gov.uk')
        self.dashboard.add_other_link('other2', 'http://www.gov.uk')
        self.dashboard.update_transaction_link(
            'transaction',
            'http://www.gov.uk'
        )
        self.dashboard.validate_and_save()
        transaction_link = self.dashboard.get_transaction_link()
        assert_that(transaction_link, instance_of(Link))
        assert_that(
            transaction_link.link_type,
            equal_to('transaction')
        )
        assert_that(
            self.dashboard.get_other_links()[0].link_type,
            equal_to('other')
        )
        assert_that(
            self.dashboard.spotlightify(),
            has_entries({
                'title': 'title',
                'page-type': 'dashboard',
                'relatedPages': has_entries({
                    'improve-dashboard-message': True,
                    'transaction':
                    has_entries({
                        'url': 'http://www.gov.uk',
                        'title': 'transaction',
                    }),
                    'other':
                    has_items(
                        has_entries({
                            'url': 'http://www.gov.uk',
                            'title': 'other',
                        }),
                        has_entries({
                            'url': 'http://www.gov.uk',
                            'title': 'other2',
                        }),
                    )
                })
            })
        )

        assert_that(self.dashboard.spotlightify(), is_not(has_key('id')))
        assert_that(self.dashboard.spotlightify(), is_not(has_key('link')))

    def test_spotlightify_handles_dashboard_without_transaction_link(self):
        assert_that(
            self.dashboard.spotlightify(), has_entries({'title': 'title'}))

    def test_spotlightify_handles_department_and_agency(self):
        agency = AgencyWithDepartmentFactory()
        self.dashboard.organisation = agency
        self.dashboard.validate_and_save()
        assert_that(
            self.dashboard.spotlightify(),
            has_entry(
                'department',
                has_entries({
                    'title': starts_with('department'),
                    'abbr': starts_with('abbreviation')
                })
            )
        )
        assert_that(
            self.dashboard.spotlightify(),
            has_entry(
                'agency',
                has_entries({
                    'title': starts_with('agency'),
                    'abbr': starts_with('abbreviation')
                })
            )
        )

    def test_serialize_contains_dashboard_properties(self):
        data = self.dashboard.serialize()

        assert_that(data['title'], is_('title'))
        assert_that(data['published'], is_(True))

    def test_serialize_serializes_dashboard_links(self):
        LinkFactory(dashboard=self.dashboard, url='https://www.gov.uk/url')
        data = self.dashboard.serialize()

        expected_link = {
            'url': u'https://www.gov.uk/url',
            'type': u'transaction',
            'title': u'Link title'
        }

        assert_that(data['links'], contains(expected_link))

    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_agency_returns_none_when_no_organisation(self):
        assert_that(self.dashboard.agency(), is_(none()))

    def test_agency_returns_none_when_organisation_is_a_department(self):
        self.dashboard.organisation = DepartmentFactory()

        assert_that(self.dashboard.agency(), is_(none()))

    def test_agency_returns_organisation_when_organisation_is_an_agency(self):
        agency = AgencyFactory()
        self.dashboard.organisation = agency
        assert_that(self.dashboard.agency(), equal_to(agency))

    def test_department_returns_organisation_when_organisation_is_a_department(self):  # noqa
        self.dashboard.organisation = DepartmentFactory()
        assert_that(
            self.dashboard.department(), equal_to(self.dashboard.organisation))

    def test_department_returns_agency_department_when_organisation_is_an_agency(self):  # noqa
        agency = AgencyWithDepartmentFactory()
        self.dashboard.organisation = agency
        assert_that(
            self.dashboard.department(), equal_to(agency.parents.first()))

    def test_department_is_none_when_agency_has_no_department(self):
        self.dashboard.organisation = AgencyFactory()
        assert_that(self.dashboard.department(), is_(none()))

    def test_department_returns_none_when_organisation_is_none(self):
        assert_that(self.dashboard.department(), is_(none()))
Example #40
0
 def test_publishing_a_dashboard_changes_its_status_to_published(self):
     dashboard = DashboardFactory(status='unpublished')
     dashboard.published = True
     assert_that(dashboard.status, equal_to('published'))
Example #41
0
 def setUp(self):
     self.dashboard = DashboardFactory()