logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

aws_signed_url_test.exs (2637B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.RichMedia.TTL.AwsSignedUrlTest do
  5. use ExUnit.Case, async: true
  6. test "s3 signed url is parsed correct for expiration time" do
  7. url = "https://pleroma.social/amz"
  8. {:ok, timestamp} =
  9. Timex.now()
  10. |> DateTime.truncate(:second)
  11. |> Timex.format("{ISO:Basic:Z}")
  12. # in seconds
  13. valid_till = 30
  14. metadata = construct_metadata(timestamp, valid_till, url)
  15. expire_time =
  16. Timex.parse!(timestamp, "{ISO:Basic:Z}") |> Timex.to_unix() |> Kernel.+(valid_till)
  17. assert {:ok, expire_time} == Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl.ttl(metadata, url)
  18. end
  19. test "s3 signed url is parsed and correct ttl is set for rich media" do
  20. url = "https://pleroma.social/amz"
  21. {:ok, timestamp} =
  22. Timex.now()
  23. |> DateTime.truncate(:second)
  24. |> Timex.format("{ISO:Basic:Z}")
  25. # in seconds
  26. valid_till = 30
  27. metadata = construct_metadata(timestamp, valid_till, url)
  28. body = """
  29. <meta name="twitter:card" content="Pleroma" />
  30. <meta name="twitter:site" content="Pleroma" />
  31. <meta name="twitter:title" content="Pleroma" />
  32. <meta name="twitter:description" content="Pleroma" />
  33. <meta name="twitter:image" content="#{Map.get(metadata, :image)}" />
  34. """
  35. Tesla.Mock.mock(fn
  36. %{
  37. method: :get,
  38. url: "https://pleroma.social/amz"
  39. } ->
  40. %Tesla.Env{status: 200, body: body}
  41. end)
  42. Cachex.put(:rich_media_cache, url, metadata)
  43. Pleroma.Web.RichMedia.Parser.set_ttl_based_on_image(metadata, url)
  44. {:ok, cache_ttl} = Cachex.ttl(:rich_media_cache, url)
  45. # as there is delay in setting and pulling the data from cache we ignore 1 second
  46. # make it 2 seconds for flakyness
  47. assert_in_delta(valid_till * 1000, cache_ttl, 2000)
  48. end
  49. defp construct_s3_url(timestamp, valid_till) do
  50. "https://pleroma.s3.ap-southeast-1.amazonaws.com/sachin%20%281%29%20_a%20-%25%2Aasdasd%20BNN%20bnnn%20.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIBLWWK6RGDQXDLJQ%2F20190716%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date=#{
  51. timestamp
  52. }&X-Amz-Expires=#{valid_till}&X-Amz-Signature=04ffd6b98634f4b1bbabc62e0fac4879093cd54a6eed24fe8eb38e8369526bbf&X-Amz-SignedHeaders=host"
  53. end
  54. defp construct_metadata(timestamp, valid_till, url) do
  55. %{
  56. image: construct_s3_url(timestamp, valid_till),
  57. site: "Pleroma",
  58. title: "Pleroma",
  59. description: "Pleroma",
  60. url: url
  61. }
  62. end
  63. end