示例#1
0
    def test_superuser_available(self):
        form = self.make_form()
        assert not hasattr(form, 'is_superuser')

        form = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': True},
                               allow_superuser=True, endpoint='auth.user:edit')
        assert hasattr(form, 'is_superuser')
示例#2
0
 def test_alternate_ident_field(self):
     with mock.patch(
             'keg_auth_ta.extensions.auth_entity_registry._user_cls',
             ents.UserNoEmail):
         form_cls = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': True},
                                    allow_superuser=False,
                                    endpoint='auth.user:edit')
         assert hasattr(form_cls, 'username')
示例#3
0
    def test_unique_alternate_ident_field(self):
        with mock.patch('keg_auth_ta.extensions.auth_entity_registry._user_cls', ents.UserNoEmail):
            form_cls = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': True},
                                       allow_superuser=False, endpoint='auth.user:edit')
            usr = ents.UserNoEmail.testing_create(username='******')

            form = self.assert_not_valid(form_cls=form_cls, username='******')
            error = PyQuery(form.username.errors[1])
            assert 'This value must be unique' in error.text()
            assert error('a').attr('href').endswith('/users/{}/edit'.format(usr.id))
            assert error('a').text() == 'foobar'

            self.assert_valid(form_cls=form_cls, obj=usr, username='******')
示例#4
0
    def test_no_email(self):
        with mock.patch('keg_auth_ta.extensions.auth_entity_registry._user_cls', ents.UserNoEmail):
            form_cls = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': False},
                                       allow_superuser=False, endpoint='auth.user:edit')
            assert hasattr(form_cls, 'username')
            assert not hasattr(form_cls, 'email')
            assert hasattr(form_cls, 'reset_password')
            assert hasattr(form_cls, 'confirm')

            form = self.assert_not_valid(form_cls=form_cls, reset_password='******', confirm='abc')
            assert form.reset_password.errors == ['Passwords must match']
            self.assert_valid(form_cls=form_cls, username='******', reset_password='******',
                              confirm='xyz')
示例#5
0
class TestUser(FormBase):
    form_cls = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': True},
                               allow_superuser=False, endpoint='auth.user:edit')

    @classmethod
    def setup_class(cls):
        ents.Permission.delete_cascaded()
        ents.Group.delete_cascaded()
        ents.Bundle.delete_cascaded()

        cls.perms = [ents.Permission.testing_create() for _ in range(3)]
        cls.groups = [ents.Group.testing_create() for _ in range(3)]
        cls.bundles = [ents.Bundle.testing_create() for _ in range(3)]

    def setup(self):
        ents.User.delete_cascaded()

    def ok_data(self, **kwargs):
        data = {
            'email': '*****@*****.**',
            'is_enabled': 'true',
            'disabled_utc': '2019-01-01',
            'permission_ids': [str(self.perms[0].id), str(self.perms[1].id)],
            'group_ids': [str(self.groups[0].id), str(self.groups[1].id)],
            'bundle_ids': [str(self.bundles[0].id), str(self.bundles[1].id)],
        }
        data.update(kwargs)
        return data

    def test_required(self):
        form = self.assert_not_valid(email='')
        assert form.email.errors == ['This field is required.']

    def test_valid_email(self):
        form = self.assert_not_valid(email='foo')
        assert form.email.errors == ['Invalid email address.']

    def test_superuser_available(self):
        form = self.make_form()
        assert not hasattr(form, 'is_superuser')

        form = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': True},
                               allow_superuser=True, endpoint='auth.user:edit')
        assert hasattr(form, 'is_superuser')

    def test_alternate_ident_field(self):
        with mock.patch('keg_auth_ta.extensions.auth_entity_registry._user_cls', ents.UserNoEmail):
            form_cls = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': True},
                                       allow_superuser=False, endpoint='auth.user:edit')
            assert hasattr(form_cls, 'username')

    def test_send_welcome_present(self):
        form = self.make_form()
        assert form.send_welcome

    def test_send_welcome_absent(self):
        user = ents.User.testing_create()
        form = self.make_form(obj=user)
        assert not form.send_welcome

    def test_no_email(self):
        with mock.patch('keg_auth_ta.extensions.auth_entity_registry._user_cls', ents.UserNoEmail):
            form_cls = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': False},
                                       allow_superuser=False, endpoint='auth.user:edit')
            assert hasattr(form_cls, 'username')
            assert not hasattr(form_cls, 'email')
            assert hasattr(form_cls, 'reset_password')
            assert hasattr(form_cls, 'confirm')

            form = self.assert_not_valid(form_cls=form_cls, reset_password='******', confirm='abc')
            assert form.reset_password.errors == ['Passwords must match']
            self.assert_valid(form_cls=form_cls, username='******', reset_password='******',
                              confirm='xyz')

    def test_multi_select(self):
        form = self.assert_valid()
        assert form.get_selected_permissions() == self.perms[:2]
        assert form.get_selected_groups() == self.groups[:2]
        assert form.get_selected_bundles() == self.bundles[:2]

        form = self.assert_valid(permission_ids=[], group_ids=[], bundle_ids=[])
        assert form.get_selected_permissions() == []
        assert form.get_selected_groups() == []
        assert form.get_selected_bundles() == []

    def test_unique(self):
        usr = ents.User.testing_create(email='*****@*****.**')

        form = self.assert_not_valid()
        error = PyQuery(form.email.errors[1])
        assert 'This value must be unique' in error.text()
        assert error('a').attr('href').endswith('/users/{}/edit'.format(usr.id))
        assert error('a').text() == '*****@*****.**'

        self.assert_valid(obj=usr)

    def test_unique_alternate_ident_field(self):
        with mock.patch('keg_auth_ta.extensions.auth_entity_registry._user_cls', ents.UserNoEmail):
            form_cls = forms.user_form({'KEGAUTH_EMAIL_OPS_ENABLED': True},
                                       allow_superuser=False, endpoint='auth.user:edit')
            usr = ents.UserNoEmail.testing_create(username='******')

            form = self.assert_not_valid(form_cls=form_cls, username='******')
            error = PyQuery(form.username.errors[1])
            assert 'This value must be unique' in error.text()
            assert error('a').attr('href').endswith('/users/{}/edit'.format(usr.id))
            assert error('a').text() == 'foobar'

            self.assert_valid(form_cls=form_cls, obj=usr, username='******')