def test_update_user_name_for_new_user(user_factory, config_injector): config_injector({'user_name_regex': '^[a-z]+$'}) user = user_factory() with patch('szurubooru.func.files.has'): files.has.return_value = False users.update_user_name(user, 'dummy') assert user.name == 'dummy'
def update_user(ctx, params): user = users.get_user_by_name(params['user_name']) versions.verify_version(user, ctx) versions.bump_version(user) infix = 'self' if ctx.user.user_id == user.user_id else 'any' if ctx.has_param('name'): auth.verify_privilege(ctx.user, 'users:edit:%s:name' % infix) users.update_user_name(user, ctx.get_param_as_string('name')) if ctx.has_param('password'): auth.verify_privilege(ctx.user, 'users:edit:%s:pass' % infix) users.update_user_password( user, ctx.get_param_as_string('password')) if ctx.has_param('email'): auth.verify_privilege(ctx.user, 'users:edit:%s:email' % infix) users.update_user_email(user, ctx.get_param_as_string('email')) if ctx.has_param('rank'): auth.verify_privilege(ctx.user, 'users:edit:%s:rank' % infix) users.update_user_rank( user, ctx.get_param_as_string('rank'), ctx.user) if ctx.has_param('avatarStyle'): auth.verify_privilege(ctx.user, 'users:edit:%s:avatar' % infix) users.update_user_avatar( user, ctx.get_param_as_string('avatarStyle'), ctx.get_file('avatar')) ctx.session.commit() return _serialize(ctx, user)
def update_user(ctx: rest.Context, params: Dict[str, str]) -> rest.Response: user = users.get_user_by_name(params["user_name"]) versions.verify_version(user, ctx) versions.bump_version(user) infix = "self" if ctx.user.user_id == user.user_id else "any" if ctx.has_param("name"): auth.verify_privilege(ctx.user, "users:edit:%s:name" % infix) users.update_user_name(user, ctx.get_param_as_string("name")) if ctx.has_param("password"): auth.verify_privilege(ctx.user, "users:edit:%s:pass" % infix) users.update_user_password(user, ctx.get_param_as_string("password")) if ctx.has_param("email"): auth.verify_privilege(ctx.user, "users:edit:%s:email" % infix) users.update_user_email(user, ctx.get_param_as_string("email")) if ctx.has_param("rank"): auth.verify_privilege(ctx.user, "users:edit:%s:rank" % infix) users.update_user_rank(user, ctx.get_param_as_string("rank"), ctx.user) if ctx.has_param("avatarStyle"): auth.verify_privilege(ctx.user, "users:edit:%s:avatar" % infix) users.update_user_avatar( user, ctx.get_param_as_string("avatarStyle"), ctx.get_file("avatar", default=b""), ) ctx.session.commit() return _serialize(ctx, user)
def put(self, ctx, user_name): user = users.get_user_by_name(user_name) infix = 'self' if ctx.user.user_id == user.user_id else 'any' if ctx.has_param('name'): auth.verify_privilege(ctx.user, 'users:edit:%s:name' % infix) users.update_user_name(user, ctx.get_param_as_string('name')) if ctx.has_param('password'): auth.verify_privilege(ctx.user, 'users:edit:%s:pass' % infix) users.update_user_password( user, ctx.get_param_as_string('password')) if ctx.has_param('email'): auth.verify_privilege(ctx.user, 'users:edit:%s:email' % infix) users.update_user_email(user, ctx.get_param_as_string('email')) if ctx.has_param('rank'): auth.verify_privilege(ctx.user, 'users:edit:%s:rank' % infix) users.update_user_rank( user, ctx.get_param_as_string('rank'), ctx.user) if ctx.has_param('avatarStyle'): auth.verify_privilege(ctx.user, 'users:edit:%s:avatar' % infix) users.update_user_avatar( user, ctx.get_param_as_string('avatarStyle'), ctx.get_file('avatar')) ctx.session.commit() return users.serialize_user_with_details(user, ctx.user)
def test_update_user_name_for_new_user(user_factory, config_injector): config_injector({"user_name_regex": "^[a-z]+$"}) user = user_factory() with patch("szurubooru.func.files.has"): files.has.return_value = False users.update_user_name(user, "dummy") assert user.name == "dummy"
def test_update_user_name_with_duplicate_name(user_factory, config_injector): config_injector({"user_name_regex": "^[a-z]+$"}) user = user_factory() existing_user = user_factory(name="dummy") db.session.add(existing_user) db.session.flush() with pytest.raises(users.UserAlreadyExistsError): users.update_user_name(user, "dummy")
def test_update_user_name_with_duplicate_name(user_factory, config_injector): config_injector({'user_name_regex': '^[a-z]+$'}) user = user_factory() existing_user = user_factory(name='dummy') db.session.add(existing_user) db.session.flush() with pytest.raises(users.UserAlreadyExistsError): users.update_user_name(user, 'dummy')
def test_update_user_name_moves_avatar(user_factory, config_injector): config_injector({'user_name_regex': '^[a-z]+$'}) user = user_factory(name='old') with patch('szurubooru.func.files.has'), \ patch('szurubooru.func.files.move'): files.has.return_value = True users.update_user_name(user, 'new') files.move.assert_called_once_with( 'avatars/old.png', 'avatars/new.png')
def test_update_user_name_moves_avatar(user_factory, config_injector): config_injector({"user_name_regex": "^[a-z]+$"}) user = user_factory(name="old") with patch("szurubooru.func.files.has"), patch( "szurubooru.func.files.move"): files.has.return_value = True users.update_user_name(user, "new") files.move.assert_called_once_with("avatars/old.png", "avatars/new.png")
def test_update_user_name_reusing_own_name(user_factory, config_injector): config_injector({'user_name_regex': '^[a-z]+$'}) user = user_factory(name='dummy') db.session.add(user) db.session.flush() with patch('szurubooru.func.files.has'): files.has.return_value = False users.update_user_name(user, 'dummy') db.session.flush() assert users.try_get_user_by_name('dummy') is user
def test_update_user_name_reusing_own_name(user_factory, config_injector): config_injector({"user_name_regex": "^[a-z]+$"}) user = user_factory(name="dummy") db.session.add(user) db.session.flush() with patch("szurubooru.func.files.has"): files.has.return_value = False users.update_user_name(user, "dummy") db.session.flush() assert users.try_get_user_by_name("dummy") is user
def update_user(ctx: rest.Context, params: Dict[str, str]) -> rest.Response: user = users.get_user_by_name(params['user_name']) versions.verify_version(user, ctx) versions.bump_version(user) infix = 'self' if ctx.user.user_id == user.user_id else 'any' if ctx.has_param('name'): auth.verify_privilege(ctx.user, 'users:edit:%s:name' % infix) users.update_user_name(user, ctx.get_param_as_string('name')) if ctx.has_param('password'): auth.verify_privilege(ctx.user, 'users:edit:%s:pass' % infix) users.update_user_password(user, ctx.get_param_as_string('password')) if ctx.has_param('email'): auth.verify_privilege(ctx.user, 'users:edit:%s:email' % infix) users.update_user_email(user, ctx.get_param_as_string('email')) if ctx.has_param('rank'): auth.verify_privilege(ctx.user, 'users:edit:%s:rank' % infix) users.update_user_rank(user, ctx.get_param_as_string('rank'), ctx.user) if ctx.has_param('avatarStyle'): auth.verify_privilege(ctx.user, 'users:edit:%s:avatar' % infix) users.update_user_avatar(user, ctx.get_param_as_string('avatarStyle'), ctx.get_file('avatar', default=b'')) ctx.session.commit() return _serialize(ctx, user)
def put(self, ctx, user_name): user = users.get_user_by_name(user_name) infix = 'self' if ctx.user.user_id == user.user_id else 'any' if ctx.has_param('name'): auth.verify_privilege(ctx.user, 'users:edit:%s:name' % infix) users.update_user_name(user, ctx.get_param_as_string('name')) if ctx.has_param('password'): auth.verify_privilege(ctx.user, 'users:edit:%s:pass' % infix) users.update_user_password(user, ctx.get_param_as_string('password')) if ctx.has_param('email'): auth.verify_privilege(ctx.user, 'users:edit:%s:email' % infix) users.update_user_email(user, ctx.get_param_as_string('email')) if ctx.has_param('rank'): auth.verify_privilege(ctx.user, 'users:edit:%s:rank' % infix) users.update_user_rank(user, ctx.get_param_as_string('rank'), ctx.user) if ctx.has_param('avatarStyle'): auth.verify_privilege(ctx.user, 'users:edit:%s:avatar' % infix) users.update_user_avatar(user, ctx.get_param_as_string('avatarStyle'), ctx.get_file('avatar')) ctx.session.commit() return users.serialize_user_with_details(user, ctx.user)
def test_update_user_name_with_too_long_string(user_factory): user = user_factory() with pytest.raises(users.InvalidUserNameError): users.update_user_name(user, 'a' * 300)
def test_update_user_name_with_invalid_name(user_factory, config_injector): config_injector({"user_name_regex": "^[a-z]+$"}) user = user_factory() with pytest.raises(users.InvalidUserNameError): users.update_user_name(user, "0")
def test_update_user_name_with_too_long_string(user_factory): user = user_factory() with pytest.raises(users.InvalidUserNameError): users.update_user_name(user, "a" * 300)
def test_update_user_name_with_empty_string(user_factory): user = user_factory() with pytest.raises(users.InvalidUserNameError): users.update_user_name(user, None)
def test_update_user_name_with_invalid_name(user_factory, config_injector): config_injector({'user_name_regex': '^[a-z]+$'}) user = user_factory() with pytest.raises(users.InvalidUserNameError): users.update_user_name(user, '0')