def test_optimized_queries_ok(self):
        DynamicMenu.objects.create(
            name=self.faker.word(),
            menu_type=MenuTypes.MAIN_MENU
        )
        for _ in range(0, 3):
            menu = DynamicMenu.objects.create(
                name=self.faker.word(),
                menu_type=MenuTypes.MAIN_MENU
            )
            menu.add_permissions(*self.perms)
        user = baker.make(constants.USER_MODEL)
        user.user_permissions.add(
            Permission.objects.get(content_type__app_label='registration', codename='view_user')
        )
        request = RequestFactory().get('/')
        request.user = user
        queries = (
            'Menus exists',
            'User Permissions',
            'Group Permissions',
            'Select all menus',
            'First menu',
            'Second menu',
            'Third menu',
            'Fourth menu'
        )

        with self.assertNumQueries(len(queries)):
            context_processors.menus(request)
    def test_context_processor_exists_ok(self):
        count = 2
        for _ in range(0, count):
            DynamicMenu.objects.create(
                name=self.faker.word(),
                menu_type=MenuTypes.MAIN_MENU
            )
        menus = context_processors.menus(self.request)

        self.assertEqual(2, menus['menus'].count())
    def test_user_with_permissions_lists_all_menus_ok(self):
        # Random menu
        DynamicMenu.objects.create(
            name=self.faker.word(),
            menu_type=MenuTypes.MAIN_MENU
        )
        # Menu with permissions
        count = 2
        for _ in range(0, count):
            menu = DynamicMenu.objects.create(
                name=self.faker.word(),
                menu_type=MenuTypes.MAIN_MENU
            )
            menu.add_permissions(*self.perms)
        menus = context_processors.menus(self.request)

        self.assertEqual(3, menus['menus'].count())
    def test_anonymous_user_list_all_menus_ko(self):
        DynamicMenu.objects.create(
            name=self.faker.word(),
            menu_type=MenuTypes.MAIN_MENU,
            staff_required=True
        )
        count = 2
        for _ in range(0, count):
            DynamicMenu.objects.create(
                name=self.faker.word(),
                menu_type=MenuTypes.MAIN_MENU
            )
        request = RequestFactory().get('/')
        request.user = AnonymousUser()
        menus = context_processors.menus(request)

        self.assertEqual(count, menus['menus'].count())
    def test_non_existent_menu_parent_ko(self):
        menu = DynamicMenu.objects.create(
            name=self.faker.word(),
            menu_type=MenuTypes.MAIN_MENU
        )
        DynamicMenu.objects.create(
            name=self.faker.word(),
            menu_type=MenuTypes.CONTEXT_MENU,
            parent=menu
        )
        request = RequestFactory().get('/')
        request.user = self.user
        request.COOKIES['_auth_user_menu_referrer'] = self.faker.pyint(100, 1000)
        menus = context_processors.menus(request)

        self.assertEqual(0, menus['context_menus'].count())
        self.assertIsNone(request.COOKIES['_auth_user_menu_referrer'])
    def test_context_menus_ok(self):
        menu = DynamicMenu.objects.create(
            name=self.faker.word(),
            menu_type=MenuTypes.MAIN_MENU
        )
        count = 2
        for _ in range(0, count):
            DynamicMenu.objects.create(
                name=self.faker.word(),
                menu_type=MenuTypes.CONTEXT_MENU,
                parent=menu
            )
        request = RequestFactory().get('/')
        request.user = self.user
        request.COOKIES['_auth_user_menu_referrer'] = menu.id
        menus = context_processors.menus(request)

        self.assertEqual(count, menus['context_menus'].count())
    def test_user_without_permissions_lists_all_menus_ko(self):
        # Random user
        user = baker.make(constants.USER_MODEL)
        user.user_permissions.add(
            Permission.objects.get(content_type__app_label='registration', codename='view_user')
        )
        # Random menu
        DynamicMenu.objects.create(
            name=self.faker.word(),
            menu_type=MenuTypes.MAIN_MENU
        )
        # Menu with permissions
        for _ in range(0, 2):
            menu = DynamicMenu.objects.create(
                name=self.faker.word(),
                menu_type=MenuTypes.MAIN_MENU
            )
            menu.add_permissions(*self.perms)
        request = RequestFactory().get('/')
        request.user = user
        menus = context_processors.menus(request)

        self.assertEqual(1, menus['menus'].count())
    def test_no_menus_ok(self):
        menus = context_processors.menus(self.request)

        self.assertEqual(0, menus['menus'].count())