Example #1
0
    def test_validate_extra_behaviour(self):
        """Test that if arbitrary behaviors are added to the userprofile
        type, then the validation takes this into account.
        """
        view = CSVImportView(self.profiles, self.request)
        fti = getUtility(IDexterityFTI, name=USER_PORTAL_TYPE)
        behaviors = fti.behaviors + (
            'ploneintranet.userprofile.tests.test_import.IDummySchema', )
        fti.behaviors = behaviors
        invalidate_cache(fti)

        extra_fields_file_loc = self._get_fixture_location('extra_column.csv')
        with open(extra_fields_file_loc) as bf:
            self.assertTrue(
                view.validate(self._parse_file(bf.read())),
                'Validation unexpectedly failed.',
            )
Example #2
0
    def test_create_update_users(self):
        view = CSVImportView(self.profiles, self.request)
        user_fields_file_loc = self._get_fixture_location('basic_users.csv')
        with open(user_fields_file_loc) as bf:
            filedata = self._parse_file(bf.read())

        self.assertEqual(len(self.profiles.objectValues()), 0)

        count = view.create_update_users(filedata)
        self.assertEqual(count, 2)
        self.assertEqual(len(self.profiles.objectValues()), 2,
                         "Users not created")

        # validation failure on duplicate username
        with self.assertRaises(custom_exc.DuplicateUser):
            view.create_update_users(filedata)

        file_loc = self._get_fixture_location('missing_first_name.csv')
        with open(file_loc) as bf:
            filedata = self._parse_file(bf.read())

        # validation failure on missing required field
        with self.assertRaises(schema.interfaces.RequiredMissing):
            view.create_update_users(filedata)
        # Even though an exception was raised we will have an extra user
        # here as the transaction is handled by the process method
        self.assertEqual(len(self.profiles.objectValues()), 3,
                         "Users not created")

        # now update one of the users
        user_fields_file_loc = self._get_fixture_location('basic_users.csv')
        with open(user_fields_file_loc) as bf:
            filedata = self._parse_file(bf.read())
        raw = filedata.csv
        email = '*****@*****.**'
        raw = raw.replace('*****@*****.**', email)
        filedata.csv = raw

        view.create_update_users(filedata, update=True)
        self.assertEqual(
            len(self.profiles.objectValues()),
            3,
            "Users not created correctly",
        )
        user = self.membrane_tool.searchResults(getUserId='foo')
        self.assertEqual(
            user[0].getObject().email,
            email,
            'User not updated',
        )
Example #3
0
    def test_create_update_users(self):
        view = CSVImportView(self.profiles, self.request)
        user_fields_file_loc = self._get_fixture_location(
            'basic_users.csv')
        with open(user_fields_file_loc) as bf:
            filedata = self._parse_file(bf.read())

        self.assertEqual(len(self.profiles.objectValues()), 0)

        count = view.create_update_users(filedata)
        self.assertEqual(count, 2)
        self.assertEqual(
            len(self.profiles.objectValues()), 2, "Users not created")

        # validation failure on duplicate username
        with self.assertRaises(custom_exc.DuplicateUser):
            view.create_update_users(filedata)

        file_loc = self._get_fixture_location(
            'missing_first_name.csv')
        with open(file_loc) as bf:
            filedata = self._parse_file(bf.read())

        # validation failure on missing required field
        with self.assertRaises(schema.interfaces.RequiredMissing):
            view.create_update_users(filedata)
        # Even though an exception was raised we will have an extra user
        # here as the transaction is handled by the process method
        self.assertEqual(
            len(self.profiles.objectValues()), 3, "Users not created")

        # now update one of the users
        user_fields_file_loc = self._get_fixture_location(
            'basic_users.csv')
        with open(user_fields_file_loc) as bf:
            filedata = self._parse_file(bf.read())
        raw = filedata.csv
        email = '*****@*****.**'
        raw = raw.replace('*****@*****.**', email)
        filedata.csv = raw

        view.create_update_users(filedata, update=True)
        self.assertEqual(
            len(self.profiles.objectValues()), 3,
            "Users not created correctly",
        )
        user = self.membrane_tool.searchResults(
            getUserName='******')
        self.assertEqual(
            user[0].getObject().email,
            email,
            'User not updated',
        )
Example #4
0
    def test_validate_extra_behaviour(self):
        """Test that if arbitrary behaviors are added to the userprofile
        type, then the validation takes this into account.
        """
        view = CSVImportView(self.profiles, self.request)
        fti = getUtility(IDexterityFTI, name=USER_PORTAL_TYPE)
        behaviors = fti.behaviors + (
            'ploneintranet.userprofile.tests.test_import.IDummySchema', )
        fti.behaviors = behaviors
        invalidate_cache(fti)
        fti.__dict__.pop('_v_schema_behavior_schema_interfaces')

        extra_fields_file_loc = self._get_fixture_location(
            'extra_column.csv')
        with open(extra_fields_file_loc) as bf:
            self.assertTrue(
                view.validate(self._parse_file(bf.read())),
                'Validation unexpectedly failed.',
            )
Example #5
0
    def test_process(self):
        view = CSVImportView(self.profiles, self.request)
        file_loc = self._get_fixture_location(
            'missing_first_name.csv')
        with open(file_loc) as bf:
            filedata = bf.read()

        view.process(filedata)
        messages = IStatusMessage(self.request)
        m = messages.show()
        self.assertEqual(
            m[0].message,
            u'Missing required field first_name on row 1'
        )

        user_fields_file_loc = self._get_fixture_location(
            'basic_users.csv')
        with open(user_fields_file_loc) as bf:
            filedata = bf.read()
        view.process(filedata)
Example #6
0
    def test_process(self):
        view = CSVImportView(self.profiles, self.request)
        file_loc = self._get_fixture_location('missing_first_name.csv')
        with open(file_loc) as bf:
            filedata = bf.read()

        view.process(filedata)
        messages = IStatusMessage(self.request)
        m = messages.show()
        self.assertEqual(m[0].message,
                         u'Missing required field first_name on row 1')

        user_fields_file_loc = self._get_fixture_location('basic_users.csv')
        with open(user_fields_file_loc) as bf:
            filedata = bf.read()
        view.process(filedata)
Example #7
0
    def test_validate(self):
        view = CSVImportView(self.profiles, self.request)

        missing_fields_file_loc = self._get_fixture_location(
            'core_fields_missing.csv')
        with open(missing_fields_file_loc) as mf:
            data = self._parse_file(mf.read())
        with self.assertRaises(custom_exc.MissingCoreFields):
            view.validate(data)

        user_fields_file_loc = self._get_fixture_location('basic_users.csv')
        with open(user_fields_file_loc) as bf:
            self.assertTrue(
                view.validate(self._parse_file(bf.read())),
                'Validation unexpectedly failed.',
            )

        extra_fields_file_loc = self._get_fixture_location('extra_column.csv')
        with open(extra_fields_file_loc) as bf:
            data = self._parse_file(bf.read())
        with self.assertRaises(custom_exc.ExtraneousFields):
            view.validate(data)
Example #8
0
    def test_password_is_kept(self):
        view = CSVImportView(self.profiles, self.request)

        # if no password is provided in the import file, one get's generated
        user_fields_file_loc = self._get_fixture_location('basic_users.csv')
        with open(user_fields_file_loc) as bf:
            filedata = self._parse_file(bf.read())
        view.create_update_users(filedata)

        barry = pi_api.userprofile.get('foo')
        doug = pi_api.userprofile.get('bar')
        barry_pwd = barry.password
        doug_pwd = doug.password
        # both got encrypted passwords
        self.assertTrue(barry_pwd.startswith('{BCRYPT}'))
        self.assertTrue(doug_pwd.startswith('{BCRYPT}'))
        # doug got the password provided in the import file
        self.assertTrue(
            IMembraneUserAuth(doug).verifyCredentials(
                dict(login='******', password='******')))

        # now re-import the same users but provide a new password for barry
        user_fields_file_loc = self._get_fixture_location(
            'basic_users_password.csv')
        with open(user_fields_file_loc) as bf:
            filedata = self._parse_file(bf.read())
        view.create_update_users(filedata, update=True)

        # barry should get a new password
        self.assertNotEqual(barry_pwd, barry.password)
        self.assertTrue(
            IMembraneUserAuth(barry).verifyCredentials(
                dict(login='******', password='******')))
        # doug's password should still be the same
        self.assertEqual(doug_pwd, doug.password)
        # both got new surnames:
        self.assertEqual(barry.last_name, u'Whiter')
        self.assertEqual(doug.last_name, u'Carsbest')
Example #9
0
    def test_validate(self):
        view = CSVImportView(self.profiles, self.request)

        missing_fields_file_loc = self._get_fixture_location(
            'core_fields_missing.csv')
        with open(missing_fields_file_loc) as mf:
            data = self._parse_file(mf.read())
        with self.assertRaises(custom_exc.MissingCoreFields):
            view.validate(data)

        user_fields_file_loc = self._get_fixture_location(
            'basic_users.csv')
        with open(user_fields_file_loc) as bf:
            self.assertTrue(
                view.validate(self._parse_file(bf.read())),
                'Validation unexpectedly failed.',
            )

        extra_fields_file_loc = self._get_fixture_location(
            'extra_column.csv')
        with open(extra_fields_file_loc) as bf:
            data = self._parse_file(bf.read())
        with self.assertRaises(custom_exc.ExtraneousFields):
            view.validate(data)