def test_provide_new_id(self):
        from plone.app.content.interfaces import INameFromTitle

        self.folder.invokeFactory("Personish", "p1")
        p1 = self.folder["p1"]
        plone_user = IPloneUser(p1)
        plone_user.user_name = "dpetrovic"
        self.assertEquals(INameFromTitle(p1).title, "dpetrovic")
    def test_valid_username(self):
        class MockUser(object):
            user_name = ""

        data = MockUser()
        data.user_name = "user1"
        try:
            IPloneUser.validateInvariants(data)
        except Invalid:
            self.fail()
    def test_invalid_username(self):
        class MockUser(object):
            user_name = ""

        # Create a person
        self.folder.invokeFactory("Person", "user1")
        user1 = self.folder["user1"]
        plone_user = IPloneUser(user1)
        plone_user.user_name = "user1"
        # Now we validate the username user1
        data = MockUser()
        data.user_name = "user1"
        self.assertRaises(Invalid, IPloneUser.validateInvariants, data)
    def test_biography_with_behavior(self):
        pm = getToolByName(self.portal, 'portal_membership')
        user = pm.getAuthenticatedMember()
        properties = {"email": "*****@*****.**",
                      "description": u"Just a user"}
        user.setMemberProperties(mapping=properties)

        # We config the employee and attach a plone user for it
        self.folder.invokeFactory('Employee', 'e1')
        e1 = self.folder['e1']
        e1 = IPloneUser(e1)
        e1.user_name = TEST_USER_ID
        e1 = self.folder['e1']

        # We test the view method
        view = e1.unrestrictedTraverse('view')
        self.assertEqual(view.biography(), u"Just a user")
    def test_edit_username(self):
        class MockUser(object):
            user_name = ""
            __context__ = None

        # Create a person
        self.folder.invokeFactory("Person", "user1")
        user1 = self.folder["user1"]
        plone_user = IPloneUser(user1)
        plone_user.user_name = "user1"
        user1.reindexObject()
        # Now we validate if using the same user_name **and** in the same
        # context we have a green light
        data = MockUser()
        data.user_name = "user1"
        data.__context__ = user1
        # Now we validate the username user1
        try:
            IPloneUser.validateInvariants(data)
        except Invalid:
            self.fail()
def demo_steps(context):
    """ Run steps to prepare a demo.
    """
    if context.readDataFile('s17.employee-demo.txt') is None:
        return
    portal = context.getSite()
    portal.invokeFactory('Folder', 'employees')
    folder = portal['employees']
    folder.title = 'Employees'
    pw = getToolByName(portal, 'portal_workflow')
    list_users = [{'name':'marcelo-santos', 'password':'******',
                    'number': '1', 'birthday': (1985, 2, 17)},
                  {'name':'marcelo-alves', 'password':'******',
                   'number': '2', 'birthday': (1983, 6, 01)},
                  {'name':'julia-alvarez', 'password':'******',
                   'number': '3', 'birthday': (1988, 10, 26)},
                  {'name':'juan-perez', 'password':'******',
                   'number': '4', 'birthday': (1981, 1, 15)},
                  {'name':'gustavo-roner', 'password':'******',
                   'number': '5', 'birthday': (1986, 2, 15)}]

    for user in list_users:
        create_user(user['name'], user['password'], portal)

    # Set behaviors to employee
    fti = queryUtility(IDexterityFTI, name='Employee')
    behaviors = fti.behaviors + \
                tuple(['s17.person.behaviors.user.IPloneUser',
                's17.person.behaviors.contact.IContactInfo'])
    fti.behaviors = tuple(behaviors)

    for user in list_users:
        employee = user['name']
        fullname = employee.split('-')
        birthday = user['birthday']
        image = os.path.join(os.path.dirname(__file__), 'profiles', 'demo',
                             'images', 'picture%s.png' % user['number'])
        data = getFile(image).read()
        folder.invokeFactory('Employee', employee,
            birthday=datetime.date(datetime(birthday[0], birthday[1],
                                   birthday[2])),
            picture=NamedImage(data),
            given_name=fullname[0].capitalize(),
            surname=fullname[1].capitalize(),
            gender=u'm',
            )
        if (employee == 'julia-alvarez'):
            folder[employee].gender = 'f'
        p1_contact = IContactInfo(folder[employee])
        p1_contact.emails = [{'category': u'work',
                                          'data': u'*****@*****.**' %
                                         employee.replace('-', '.')},
                             {'category': u'home',
                                          'data': u'*****@*****.**' %
                                         employee.replace('-', '.')}]
        p1_contact.instant_messengers = [{'category': u'skype',
                                          'data': u'%s' %
                                         employee.replace('-', '_')}]
        p1_contact.telephones = [{'category': 'home', 'data': '+5511555.1213'},
                                 {'category': 'work', 'data': '+5511316.9876'}]
        p1_ploneuser = IPloneUser(folder[employee])
        p1_ploneuser.user_name = employee
        folder[employee].reindexObject()
        review_state = pw.getStatusOf(folder[employee], 'review_state')
        if not review_state == 'internally_published':
            pw.doActionFor(folder[employee], 'publish_internally')

    # publish folder. Folder must have intranet_workflow assigned.
    review_state = pw.getStatusOf(folder, 'review_state')
    folder_wchain = pw.getChainFor(folder)
    if folder_wchain == 'intranet_workflow' and \
       review_state != 'internally_published':
        pw.doActionFor(folder, 'publish_internally')

    import transaction
    transaction.commit()
def demo_steps(context):
    """ Run steps to prepare a demo.
    """
    if context.readDataFile('s17.person-demo.txt') is None:
        return
    portal = context.getSite()
    portal.invokeFactory('Folder', 'Persons')
    folder = portal['Persons']
    list_users = [{'name': 'marcelo-santos', 'password': '******',
                   'number': '1', 'birthday': (1985, 2, 17)},
                  {'name': 'marcelo-alves', 'password': '******',
                   'number': '2', 'birthday': (1983, 6, 01)},
                  {'name': 'julia-alvarez', 'password': '******',
                   'number': '3', 'birthday': (1988, 10, 26)},
                  {'name': 'juan-perez', 'password': '******',
                   'number': '4', 'birthday': (1981, 1, 15)},
                  {'name': 'gustavo-roner', 'password': '******',
                   'number': '5', 'birthday': (1986, 2, 15)}]

    for user in list_users:
        create_user(user['name'], user['password'], portal)

    # Set behaviors to person
    behaviors = ['s17.person.behaviors.user.IPloneUser',
                 's17.person.behaviors.contact.IContactInfo']
    fti = queryUtility(IDexterityFTI, name='Person')
    fti.behaviors = tuple(behaviors)

    for user in list_users:
        person = user['name']
        fullname = person.split('-')
        birthday = user['birthday']
        image = os.path.join(
            os.path.dirname(__file__), 'profiles', 'demo', 'images', 'picture%s.png' % user['number'])
        data = getFile(image).read()
        folder.invokeFactory(
            'Person',
            person,
            birthday=datetime.date(datetime(birthday[0], birthday[1], birthday[2])),
            picture=NamedImage(data),
            given_name=fullname[0].capitalize(),
            surname=fullname[1].capitalize(),
            gender=u'm',
        )
        if (person == 'julia-alvarez'):
            folder[person].gender = 'f'
        p1_contact = IContactInfo(folder[person])
        p1_contact.emails = [
            {'category': u'work',
             'data': u'*****@*****.**' % person.replace('-', '.')},
            {'category': u'home',
             'data': u'*****@*****.**' % person.replace('-', '.')},
        ]
        p1_contact.instant_messengers = [
            {'category': u'skype',
             'data': u'%s' % person.replace('-', '_')},
        ]
        p1_contact.telephones = [
            {'category': 'home', 'data': '+5511555.1213'},
            {'category': 'work', 'data': '+5511316.9876'},
        ]
        p1_ploneuser = IPloneUser(folder[person])
        p1_ploneuser.user_name = person
        folder[person].reindexObject()
        review_state = folder[person].portal_workflow.getInfoFor(folder[person], 'review_state')
        if not review_state == 'published':
            folder[person].portal_workflow.doActionFor(folder[person],
                                                       'publish')
    review_state = folder.portal_workflow.getInfoFor(folder, 'review_state')
    if not review_state == 'published':
        folder.portal_workflow.doActionFor(folder, 'publish')

    import transaction
    transaction.commit()
 def test_get_user(self):
     self.folder.invokeFactory("Person", "user1")
     user1 = self.folder["user1"]
     adapter = IPloneUser(user1)
     adapter.user_name = "user1"
     self.assertNotEquals(None, adapter.getUser())