aboutsummaryrefslogtreecommitdiff
path: root/test/pleroma/user/backup_async_test.exs
blob: 76716d6847bbf9eb2486a139a69f664af76d0d22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Pleroma: A lightweight social networking server
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.User.BackupAsyncTest do
  use Pleroma.DataCase, async: true

  import Pleroma.Factory
  import Mox

  alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  alias Pleroma.User.Backup
  alias Pleroma.User.Backup.ProcessorMock

  setup do
    user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})

    {:ok, backup} = user |> Backup.new() |> Repo.insert()
    %{backup: backup}
  end

  @tag capture_log: true
  test "it handles unrecoverable exceptions", %{backup: backup} do
    ProcessorMock
    |> expect(:do_process, fn _, _ ->
      raise "mock exception"
    end)

    ConfigMock
    |> stub_with(Pleroma.Config)

    {:error, %{backup: backup, reason: :exit}} = Backup.process(backup, ProcessorMock)

    assert backup.state == :failed
  end

  @tag capture_log: true
  test "it handles timeouts", %{backup: backup} do
    ProcessorMock
    |> expect(:do_process, fn _, _ ->
      Process.sleep(:timer.seconds(4))
    end)

    ConfigMock
    |> expect(:get, fn [Pleroma.User.Backup, :process_wait_time] -> :timer.seconds(2) end)

    {:error, %{backup: backup, reason: :timeout}} = Backup.process(backup, ProcessorMock)

    assert backup.state == :failed
  end
end