def test_visited_sites__empty(self):
        from webidentity.views.user import visited_sites

        self.config.testing_securitypolicy(userid=u"dokai")

        session = DBSession()
        session.add(User("dokai", "secret", "*****@*****.**"))

        request = testing.DummyRequest()
        self.assertEquals(visited_sites(request), {"action_url": "form_action", "sites": []})
    def test_visited_sites__not_empty(self):
        from webidentity.views.user import visited_sites

        self.config.testing_securitypolicy(userid=u"dokai")

        session = DBSession()
        user = User("dokai", "secret", "*****@*****.**")

        user.personas.append(
            Persona(
                u"Test persönä",
                attributes=[UserAttribute(type_uri, value) for type_uri, value in DUMMY_USER_ATTRIBUTES.iteritems()],
            )
        )

        site1 = VisitedSite("http://www.rp.com", remember=False)
        site2 = VisitedSite("http://www.plone.org", remember=True)
        site2.persona = user.personas[0]

        user.visited_sites.append(site1)
        user.visited_sites.append(site2)

        user.activity.append(
            Activity(
                ipaddr="1.2.3.4",
                session="session1",
                action=Activity.AUTHORIZE_ONCE,
                url="http://www.rp.com",
                timestamp=datetime(2010, 1, 15, 12, 0),
            )
        )
        user.activity.append(
            Activity(
                ipaddr="2.3.4.5",
                session="session2",
                action=Activity.AUTHORIZE_ONCE,
                url="http://www.plone.org",
                timestamp=datetime(2010, 3, 24, 15, 23),
            )
        )
        user.activity.append(
            Activity(
                ipaddr="2.3.4.5",
                session="session3",
                action=Activity.AUTHORIZE,
                url="http://www.plone.org",
                timestamp=datetime(2010, 11, 15, 17, 9),
            )
        )

        session.add(user)

        request = testing.DummyRequest()
        self.assertEquals(
            visited_sites(request),
            {
                "action_url": "form_action",
                "sites": [
                    {
                        "url": u"http://www.plone.org",
                        "timestamp": "15.11.2010 17:09",
                        "persona": {"id": 1, "edit_url": "http://fo.bar/", "name": u"Test persönä"},
                        "id": 2,
                        "remember": "checked",
                    },
                    {
                        "url": u"http://www.rp.com",
                        "timestamp": "15.01.2010 12:00",
                        "persona": None,
                        "id": 1,
                        "remember": None,
                    },
                ],
            },
        )
    def test_visited_sites__anonymous(self):
        from webidentity.views.user import visited_sites
        from pyramid.exceptions import Forbidden

        request = testing.DummyRequest()
        self.assertRaises(Forbidden, lambda: visited_sites(request))