Merge branch 'fix/user_update_and_set_cache' into 'develop'

fixed User.update_and_set_cache for stale user

See merge request pleroma/pleroma!1493
This commit is contained in:
kaniini 2019-07-28 20:29:27 +00:00
commit 0d05ec3855
2 changed files with 25 additions and 1 deletions

View File

@ -471,7 +471,7 @@ def set_cache(%User{} = user) do
end
def update_and_set_cache(changeset) do
with {:ok, user} <- Repo.update(changeset) do
with {:ok, user} <- Repo.update(changeset, stale_error_field: :id) do
set_cache(user)
else
e -> e

View File

@ -1369,4 +1369,28 @@ test "user with internal-prefixed nickname returns true" do
assert User.is_internal_user?(user)
end
end
describe "update_and_set_cache/1" do
test "returns error when user is stale instead Ecto.StaleEntryError" do
user = insert(:user)
changeset = Ecto.Changeset.change(user, bio: "test")
Repo.delete(user)
assert {:error, %Ecto.Changeset{errors: [id: {"is stale", [stale: true]}], valid?: false}} =
User.update_and_set_cache(changeset)
end
test "performs update cache if user updated" do
user = insert(:user)
assert {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
changeset = Ecto.Changeset.change(user, bio: "test-bio")
assert {:ok, %User{bio: "test-bio"} = user} = User.update_and_set_cache(changeset)
assert {:ok, user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
end
end
end