예제 #1
0
 def testSessionAcquisition(self):
     """Users belonging to a group should be able to see all the sessions
     belonging to the group and the group children
     """
     session = Session()
     group1 = model.Group(group_id="1")
     session.add(group1)
     group2 = model.Group(group_id="2")
     session.add(group2)
     account1 = model.Account(loginname="account1")
     session.add(account1)
     account1.group = group1
     group2.parent = group1
     account2 = model.Account(loginname="account2")
     session.add(account2)
     account2.group = group2
     survey1 = model.SurveySession(
         account=account1,
         group=group1,
         zodb_path="1",
     )
     session.add(survey1)
     survey2 = model.SurveySession(
         account=account2,
         group=group2,
         zodb_path="2",
     )
     session.add(survey2)
     session.flush()
     self.assertListEqual(account1.sessions, [survey1])
     self.assertListEqual(account2.sessions, [survey2])
     self.assertListEqual(account1.acquired_sessions, [survey1, survey2])
     self.assertListEqual(account2.acquired_sessions, [survey2])
예제 #2
0
    def setUp(self):
        super(TestCloningViews, self).setUp()
        self.loginAsPortalOwner()
        addSurvey(self.portal, BASIC_SURVEY)
        self.jane = addAccount("*****@*****.**", password="******")
        self.john = addAccount("*****@*****.**", password="******")

        group = model.Group(group_id="1")
        model.Session.add(group)

        self.jane.group = group
        self.john.group = group
        model.Session.flush()

        survey_session = model.SurveySession(
            title="Dummy session",
            created=datetime(2012, 4, 22, 23, 5, 12),
            modified=datetime(2012, 4, 23, 11, 50, 30),
            zodb_path="nl/ict/software-development",
            account=self.jane,
            group=group,
            company=model.Company(country="nl",
                                  employees="1-9",
                                  referer="other"),
        )
        module = survey_session.addChild(
            model.Module(title="module 1", module_id="1", zodb_path="a"))
        risk = module.addChild(
            model.Risk(title="question 1", risk_id="1", zodb_path="a/b"))
        model.ActionPlan(action_plan="This is the plan", risk=risk)
        model.Session.add(survey_session)
예제 #3
0
 def testGroups(self):
     session = Session()
     group1 = model.Group(group_id="1")
     session.add(group1)
     session.flush()
     self.assertEqual(group1.group_id, "1")
     # Verify that a group might have one parent but many children
     group2 = model.Group(group_id="2")
     session.add(group2)
     group3 = model.Group(group_id="3")
     session.add(group3)
     group3.parent = group2.parent = group1
     session.flush()
     self.assertEqual(group2.group_id, "2")
     self.assertEqual(group2.parent, group1)
     self.assertEqual(group1.parent, None)
     self.assertListEqual(group1.children, [group2, group3])
     self.assertListEqual(group2.children, [])
예제 #4
0
    def test_get_group_filter(self):
        session = model.SurveySession()
        group = model.Group(group_id="foo")
        account = model.Account(id=1)
        # Note assertFalse will not do what you want on Binary expression
        self.assertEqual(str(session.get_group_filter()), "False")
        self.assertEqual(str(session.get_group_filter(False)), "False")
        self.assertEqual(str(session.get_group_filter(None)), "False")
        self.assertEqual(str(session.get_group_filter("")), "False")
        self.assertEqual(str(session.get_group_filter([])), "False")
        self.assertEqual(str(session.get_group_filter([""])), "False")
        self.assertEqual(
            str(session.get_group_filter("1")), "session.group_id = :group_id_1"
        )
        self.assertEqual(
            str(session.get_group_filter(1)), "session.group_id = :group_id_1"
        )
        self.assertEqual(
            str(session.get_group_filter(group)),
            "session.group_id = :group_id_1",
        )
        self.assertEqual(
            str(session.get_group_filter(["1"])), "session.group_id = :group_id_1"
        )
        self.assertEqual(
            str(session.get_group_filter([1])), "session.group_id = :group_id_1"
        )
        self.assertEqual(
            str(session.get_group_filter([group])),
            "session.group_id = :group_id_1",
        )
        self.assertEqual(
            str(session.get_group_filter([group, "2"])),
            "session.group_id IN (:group_id_1, :group_id_2)",
        )
        with mock.patch("euphorie.client.model.get_current_account", return_value=None):
            self.assertEqual(str(session.get_group_filter(True)), "False")

        # The account still does not have a group, so we should have False here
        with mock.patch(
            "euphorie.client.model.get_current_account", return_value=account
        ):
            self.assertEqual(str(session.get_group_filter(True)), "False")

        account.group_id = "foo"
        with mock.patch(
            "euphorie.client.model.get_current_account", return_value=account
        ):
            self.assertEqual(
                str(session.get_group_filter(True)),
                "session.group_id = :group_id_1",
            )

        with self.assertRaises(TypeError):
            session.get_group_filter(session)
예제 #5
0
    def testSessions(self):
        session = Session()
        group1 = model.Group(group_id="1")
        session.add(group1)
        group2 = model.Group(group_id="2")
        session.add(group2)
        account1 = model.Account(loginname="account1")
        session.add(account1)
        account1.group = group1
        group2.parent = group1
        from functools import partial

        add_survey = partial(model.SurveySession, account=account1)
        survey1 = add_survey(zodb_path="1")
        session.add(survey1)
        survey2 = add_survey(zodb_path="2", group=group1)
        session.add(survey2)
        survey3 = add_survey(zodb_path="3", group=group2)
        session.add(survey3)
        session.flush()
        self.assertListEqual(account1.sessions, [survey1, survey2, survey3])
        self.assertListEqual(group1.sessions, [survey2])
        self.assertListEqual(group2.sessions, [survey3])
예제 #6
0
 def testAccountGroupsRelationship(self):
     session = Session()
     group1 = model.Group(group_id="1")
     session.add(group1)
     session.flush()
     account1 = model.Account(loginname="account1")
     session.add(account1)
     account2 = model.Account(loginname="account2")
     session.add(account2)
     session.flush()
     self.assertEqual(account1.group, None)
     account1.group = group1
     account2.group = group1
     self.assertListEqual(group1.accounts, [account1, account2])
예제 #7
0
 def testAccountGroupsHierarchy(self):
     session = Session()
     group1 = model.Group(group_id="1")
     session.add(group1)
     group2 = model.Group(group_id="2")
     group2.parent = group1
     session.add(group2)
     group3 = model.Group(group_id="3")
     session.add(group3)
     group3.parent = group1
     group4 = model.Group(group_id="4")
     session.add(group4)
     group4.parent = group2
     session.flush()
     account1 = model.Account(loginname="account1")
     session.add(account1)
     account1.group = group1
     account2 = model.Account(loginname="account2")
     session.add(account2)
     account2.group = group2
     account3 = model.Account(loginname="account3")
     session.add(account3)
     account3.group = group3
     account4 = model.Account(loginname="account4")
     session.add(account4)
     session.flush()
     self.assertListEqual(group1.descendants, [group2, group3, group4])
     self.assertListEqual(group2.descendants, [group4])
     self.assertListEqual(group3.descendants, [])
     self.assertListEqual(account1.groups, [group1, group2, group3, group4])
     self.assertListEqual(account2.groups, [group2, group4])
     self.assertListEqual(account3.groups, [group3])
     self.assertListEqual(account4.groups, [])
     self.assertListEqual(group1.parents, [])
     self.assertListEqual(group2.parents, [group1])
     self.assertListEqual(group3.parents, [group1])
     self.assertListEqual(group4.parents, [group2, group1])
예제 #8
0
    def test_get_sessions_query_authenticated(self):
        account = addAccount(password="******")
        with api.env.adopt_user("admin"):
            eu = api.content.create(type="euphorie.clientcountry",
                                    id="eu",
                                    container=self.portal.client)
            eusector = api.content.create(type="euphorie.clientsector",
                                          id="eusector",
                                          container=eu)
            api.content.create(type="euphorie.survey",
                               id="eusurvey",
                               container=eusector)
        with api.env.adopt_user(user=account):
            # Check with no parameter
            with self._get_view("webhelpers", self.portal.client) as view:
                self.assertEqual(
                    self._get_query_filters(view.get_sessions_query()),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.account_id = ? AND "
                     "(session.archived >= ? OR session.archived IS NULL) "
                     "ORDER BY session.modified DESC, session.title"),
                )

                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(include_archived=True)),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.account_id = ? "
                     "ORDER BY session.modified DESC, session.title"),
                )
                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(searchable_text="foo")),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.account_id = ? AND "
                     "(session.archived >= ? OR session.archived IS NULL) AND "
                     "lower(session.title) LIKE lower(?) "
                     "ORDER BY session.modified DESC, session.title"),
                )
                foo_group = model.Group(group_id="foo")
                account.group = foo_group
                model.Session.flush()
                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(filter_by_group=True)),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.account_id = ? AND "
                     "session.group_id = ? AND "
                     "(session.archived >= ? OR session.archived IS NULL) "
                     "ORDER BY session.modified DESC, session.title"),
                )
                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(filter_by_account=False)),
                    ("WHERE session.zodb_path IN (?) AND "
                     "(session.archived >= ? OR session.archived IS NULL) "
                     "ORDER BY session.modified DESC, session.title"),
                )
                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(filter_by_account=False,
                                                filter_by_group=True)),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.group_id = ? AND "
                     "(session.archived >= ? OR session.archived IS NULL) "
                     "ORDER BY session.modified DESC, session.title"),
                )
                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(filter_by_account=False,
                                                filter_by_group=foo_group)),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.group_id = ? AND "
                     "(session.archived >= ? OR session.archived IS NULL) "
                     "ORDER BY session.modified DESC, session.title"),
                )
                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(filter_by_account=False,
                                                filter_by_group=[foo_group])),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.group_id = ? AND "
                     "(session.archived >= ? OR session.archived IS NULL) "
                     "ORDER BY session.modified DESC, session.title"),
                )
                bar_group = model.Group(group_id="bar")
                self.assertEqual(
                    self._get_query_filters(
                        view.get_sessions_query(
                            filter_by_account=False,
                            filter_by_group=[foo_group, bar_group],
                        )),
                    ("WHERE session.zodb_path IN (?) AND "
                     "session.group_id IN (?, ?) AND "
                     "(session.archived >= ? OR session.archived IS NULL) "
                     "ORDER BY session.modified DESC, session.title"),
                )