def setUp(self):
        super(DashboardAccessTestCase, self).setUp()
        actions.login(self.ADMIN_EMAIL, is_admin=True)

        context = actions.simple_add_course(self.ACCESS_COURSE_NAME,
                                            self.ADMIN_EMAIL,
                                            'Course with access')

        self.course_with_access = courses.Course(None, context)

        with Namespace(self.course_with_access.app_context.namespace):
            role_dto = models.RoleDTO(
                None, {
                    'name': self.ROLE,
                    'users': [self.USER_EMAIL],
                    'permissions': {
                        dashboard.custom_module.name: [self.PERMISSION]
                    }
                })
            models.RoleDAO.save(role_dto)

        context = actions.simple_add_course(self.NO_ACCESS_COURSE_NAME,
                                            self.ADMIN_EMAIL,
                                            'Course with no access')

        self.course_without_access = courses.Course(None, context)

        def test_content(self):
            return self.render_page({
                'main_content': 'test',
                'page_title': 'test'
            })

        # save properties
        self.old_menu_group = DashboardHandler.root_menu_group
        # pylint: disable=W0212
        self.old_get_acitons = DashboardHandler._custom_get_actions
        # pylint: enable=W0212

        # put a dummy method in
        menu_group = menus.MenuGroup('test', 'Test Dashboard')
        DashboardHandler.root_menu_group = menu_group
        DashboardHandler.default_action = self.ACTION
        DashboardHandler.add_nav_mapping(self.ACTION, self.ACTION)
        DashboardHandler.add_sub_nav_mapping(self.ACTION,
                                             self.ACTION,
                                             self.ACTION,
                                             action=self.ACTION,
                                             contents=test_content)
        DashboardHandler.map_action_to_permission('get_%s' % self.ACTION,
                                                  self.PERMISSION)
        actions.logout()
    def test_custom_top_nav(self):
        # Add a new top level navigation action
        DashboardHandler.add_nav_mapping(self.ACTION, 'CUSTOM_MOD')

        class CustomNavHandler(object):
            @classmethod
            def show_page(cls, dashboard_handler):
                dashboard_handler.render_page({
                    'page_title':
                    dashboard_handler.format_title('CustomNav'),
                    'main_content':
                    'MainContent'
                })

        DashboardHandler.add_custom_get_action(self.ACTION,
                                               CustomNavHandler.show_page)

        dom = self.parse_html_string(self.get('dashboard').body)
        selected_nav_path = ('.//tr[@class="gcb-nav-bar-level-1"]'
                             '//a[@class="selected"]')
        self.assertEquals('Edit', dom.find(selected_nav_path).text)
        dom = self.parse_html_string(self.get(self.URL).body)

        self.assertEquals('CUSTOM_MOD', dom.find(selected_nav_path).text)
        self.assertEquals('MainContent',
                          dom.find(self.CONTENT_PATH).text.strip())

        DashboardHandler.remove_custom_get_action(self.ACTION)

        # Add a new tab under the new navigation action
        class CustomTabHandler(object):
            @classmethod
            def display_html(cls, unused_dashboard_handler):
                return 'MainTabContent'

        dashboard.DashboardHandler.add_sub_nav_mapping(
            self.ACTION,
            'cu_tab',
            'CustomTab',
            action=self.ACTION,
            contents=CustomTabHandler)
        dom = self.parse_html_string(self.get(self.URL).body)
        self.assertEquals('CUSTOM_MOD', dom.find(selected_nav_path).text)
        self.assertEquals('MainTabContent',
                          dom.find(self.CONTENT_PATH).text.strip())

        selected_tab_path = ('.//*[@class="gcb-nav-bar-level-2"]'
                             '//a[@class="selected"]')
        self.assertEquals('CustomTab', dom.find(selected_tab_path).text)
    def test_custom_top_nav(self):
        # Add a new top level navigation action
        DashboardHandler.add_nav_mapping(self.ACTION, 'CUSTOM_MOD')

        class CustomNavHandler(object):

            @classmethod
            def show_page(cls, dashboard_handler):
                dashboard_handler.render_page({
                    'page_title': dashboard_handler.format_title('CustomNav'),
                    'main_content': 'MainContent'})
        DashboardHandler.add_custom_get_action(
            self.ACTION, CustomNavHandler.show_page)

        dom = self.parse_html_string(self.get('dashboard').body)
        selected_nav_path = ('.//tr[@class="gcb-nav-bar-level-1"]'
                             '//a[@class="selected"]')
        self.assertEquals('Edit', dom.find(selected_nav_path).text)
        dom = self.parse_html_string(self.get(self.URL).body)

        self.assertEquals('CUSTOM_MOD', dom.find(selected_nav_path).text)
        self.assertEquals(
            'MainContent', dom.find(self.CONTENT_PATH).text.strip())

        DashboardHandler.remove_custom_get_action(self.ACTION)

        # Add a new tab under the new navigation action
        class CustomTabHandler(object):

            @classmethod
            def display_html(cls, unused_dashboard_handler):
                return 'MainTabContent'

        dashboard.DashboardHandler.add_sub_nav_mapping(
            self.ACTION, 'cu_tab', 'CustomTab', action=self.ACTION,
            contents=CustomTabHandler)
        dom = self.parse_html_string(self.get(self.URL).body)
        self.assertEquals('CUSTOM_MOD', dom.find(selected_nav_path).text)
        self.assertEquals(
            'MainTabContent', dom.find(self.CONTENT_PATH).text.strip())

        selected_tab_path = ('.//*[@class="gcb-nav-bar-level-2"]'
                             '//a[@class="selected"]')
        self.assertEquals('CustomTab', dom.find(selected_tab_path).text)
    def setUp(self):
        super(DashboardAccessTestCase, self).setUp()
        actions.login(self.ADMIN_EMAIL, is_admin=True)

        context = actions.simple_add_course(
            self.ACCESS_COURSE_NAME, self.ADMIN_EMAIL, 'Course with access')

        self.course_with_access = courses.Course(None, context)

        with Namespace(self.course_with_access.app_context.namespace):
            role_dto = models.RoleDTO(None, {
                'name': self.ROLE,
                'users': [self.USER_EMAIL],
                'permissions': {dashboard.custom_module.name: [self.PERMISSION]}
            })
            models.RoleDAO.save(role_dto)

        context = actions.simple_add_course(
            self.NO_ACCESS_COURSE_NAME, self.ADMIN_EMAIL,
            'Course with no access'
        )

        self.course_without_access = courses.Course(None, context)

        def test_content(self):
            return self.render_page(
                {'main_content': 'test', 'page_title': 'test'})

        # save properties
        self.old_menu_group = DashboardHandler.root_menu_group
        # pylint: disable=W0212
        self.old_get_acitons = DashboardHandler._custom_get_actions
        # pylint: enable=W0212

        # put a dummy method in
        menu_group = menus.MenuGroup('test', 'Test Dashboard')
        DashboardHandler.root_menu_group = menu_group
        DashboardHandler.default_action = self.ACTION
        DashboardHandler.add_nav_mapping(self.ACTION, self.ACTION)
        DashboardHandler.add_sub_nav_mapping(self.ACTION, self.ACTION,
            self.ACTION, action=self.ACTION, contents=test_content)
        DashboardHandler.map_action_to_permission(
            'get_%s' % self.ACTION, self.PERMISSION)
        actions.logout()