class UserLoginEditView(LoginEditView): schema = get_add_edit_schema(edit=True) def current(self): return self.context.login @property def title(self): return u"Modification des identifiants de {0}".format( format_account(self.context))
class LoginAddView(BaseFormView): """ View handling login add """ title = u"Ajouter des identifiants" schema = get_add_edit_schema() def __init__(self, *args, **kwargs): BaseFormView.__init__(self, *args, **kwargs) self.form_config = UserFormConfigState(self.session) def before(self, form): logger.debug(u"In the login form, defaults {0}".format( self.form_config.get_defaults())) form.set_appstruct({ 'login': self.context.email, 'user_id': self.context.id, 'primary_group': self.form_config.get_default('primary_group', ""), 'groups': self.form_config.get_default('groups', []), }) def submit_success(self, appstruct): password = appstruct.pop('pwd_hash', None) model = self.schema.objectify(appstruct) primary_group = appstruct.pop('primary_group', None) groups = appstruct.pop('groups', []) if groups or primary_group: groups = list(groups) groups.append(primary_group) model.groups = groups model.user_id = self.context.id model.set_password(password) self.dbsession.add(model) self.dbsession.flush() next_step = self.form_config.get_next_step() if next_step is not None: redirect = self.request.route_path( next_step, id=self.context.id, ) else: redirect = self.request.route_path( USER_ITEM_URL, id=self.context.id, ) logger.debug(u" + Login with id {0} added".format(model.id)) return HTTPFound(redirect)
def test_add_schema(dbsession, pyramid_request, login, groups): import colander from autonomie.forms.user.login import get_add_edit_schema schema = get_add_edit_schema() schema = schema.bind(request=pyramid_request) result = schema.deserialize( { 'login': '******', 'pwd_hash': 'oo', 'primary_group': 'contractor', 'groups': ['trainer'], 'user_id': 1500 } ) assert 'pwd_hash' in result with pytest.raises(colander.Invalid): schema.deserialize( { 'login': '******', 'pwd_hash': '', 'primary_group': 'contractor', 'groups': ['trainer'], 'user_id': 1500 } ) with pytest.raises(colander.Invalid): schema.deserialize( { 'login': '******', 'pwd_hash': 'ooo', 'primary_group': 'contractor', 'groups': ['trainer'], 'user_id': 1500 } ) with pytest.raises(colander.Invalid): schema.deserialize( { 'login': '******', 'pwd_hash': 'ooo', 'primary_group': '', 'groups': ['trainer'], 'user_id': 1500 } )
class LoginEditView(BaseFormView): schema = get_add_edit_schema(edit=True) def is_my_account_view(self): return self.current().user_id == self.request.user.id @property def title(self): if self.is_my_account_view(): return u"Modification de mes identifiants" else: return u"Modification des identifiants de {0}".format( format_account(self.current().user)) def before(self, form): form.set_appstruct({ 'login': self.current().login, 'primary_group': self.current().primary_group(), 'groups': self.current().groups, 'user_id': self.current().user_id, }) def current(self): return self.context def submit_success(self, appstruct): password = appstruct.pop('pwd_hash', None) model = self.schema.objectify(appstruct, self.current()) primary_group = appstruct.pop('primary_group', None) groups = appstruct.pop('groups', []) if groups or primary_group: groups = list(groups) groups.append(primary_group) model.groups = groups if password: model.set_password(password) self.dbsession.merge(model) self.dbsession.flush() redirect = self.request.route_path( USER_LOGIN_URL, id=self.current().user_id, ) logger.debug(u" + Login with id {0} modified".format(model.id)) return HTTPFound(redirect)
def test_add_schema(dbsession, pyramid_request, login, groups): import colander from autonomie.forms.user.login import get_add_edit_schema schema = get_add_edit_schema() schema = schema.bind(request=pyramid_request) result = schema.deserialize({ 'login': '******', 'pwd_hash': 'oo', 'primary_group': 'contractor', 'groups': ['trainer'], 'user_id': 1500 }) assert 'pwd_hash' in result with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': '', 'primary_group': 'contractor', 'groups': ['trainer'], 'user_id': 1500 }) with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', 'primary_group': 'contractor', 'groups': ['trainer'], 'user_id': 1500 }) with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', 'primary_group': '', 'groups': ['trainer'], 'user_id': 1500 })
class UserLoginEditView(LoginEditView): schema = get_add_edit_schema(edit=True) def current(self): return self.context.login
def test_edit_schema_login_context( dbsession, pyramid_request, login, user, groups ): import colander from autonomie.forms.user.login import get_add_edit_schema from autonomie.models.user.login import Login from autonomie.models.user.user import User user2 = User(email='*****@*****.**', lastname='lastname2', firstname='firstname2') dbsession.add(user2) dbsession.flush() item = Login(user_id=user2.id, login="******") item.set_password('pwd2') dbsession.add(item) dbsession.flush() pyramid_request.context = item schema = get_add_edit_schema(edit=True) schema = schema.bind(request=pyramid_request) result = schema.deserialize( { 'login': '******', 'pwd_hash': '', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, } ) assert 'pwd_hash' not in result result = schema.deserialize( { 'login': '******', 'pwd_hash': 'notpwd2', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, } ) assert 'pwd_hash' in result # Login already used with pytest.raises(colander.Invalid): schema.deserialize( { 'login': '******', 'pwd_hash': '', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, } ) # User already linked to Login class with pytest.raises(colander.Invalid): schema.deserialize( { 'login': '******', 'pwd_hash': 'ooo', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user.id } ) # wrong primary group with pytest.raises(colander.Invalid): schema.deserialize( { 'login': '******', 'pwd_hash': 'ooo', "primary_group": "falseone", 'groups': ['trainer'], 'user_id': user2.id, } ) # wrong group with pytest.raises(colander.Invalid): schema.deserialize( { 'login': '******', 'pwd_hash': 'ooo', "primary_group": "contractor", 'user_id': user2.id, "groups": ["falseone"], } )
def test_edit_schema_login_context(dbsession, pyramid_request, login, user, groups): import colander from autonomie.forms.user.login import get_add_edit_schema from autonomie.models.user.login import Login from autonomie.models.user.user import User user2 = User(email='*****@*****.**', lastname='lastname2', firstname='firstname2') dbsession.add(user2) dbsession.flush() item = Login(user_id=user2.id, login="******") item.set_password('pwd2') dbsession.add(item) dbsession.flush() pyramid_request.context = item schema = get_add_edit_schema(edit=True) schema = schema.bind(request=pyramid_request) result = schema.deserialize({ 'login': '******', 'pwd_hash': '', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, }) assert 'pwd_hash' not in result result = schema.deserialize({ 'login': '******', 'pwd_hash': 'notpwd2', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, }) assert 'pwd_hash' in result # Login already used with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': '', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, }) # User already linked to Login class with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user.id }) # wrong primary group with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', "primary_group": "falseone", 'groups': ['trainer'], 'user_id': user2.id, }) # wrong group with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', "primary_group": "contractor", 'user_id': user2.id, "groups": ["falseone"], })