63 lines
2.0 KiB
Elixir
63 lines
2.0 KiB
Elixir
defmodule Bright.UploadsTest do
|
|
use ExUnit.Case, async: true
|
|
|
|
@test_url "https://futureporn-b2.b-cdn.net/projekt-melody.jpg"
|
|
@test_file_path "./test_download.txt"
|
|
@s3_bucket Application.compile_env(:ex_aws, [:s3, :bucket])
|
|
|
|
|
|
describe "download_file/2" do
|
|
@tag :acceptance
|
|
@tag timeout: :infinity
|
|
test "downloads a file successfully" do
|
|
# Ensure a valid file exists at the URL for testing purposes
|
|
assert {:ok, _res} = Bright.Uploads.download_file(@test_url, @test_file_path)
|
|
assert File.exists?(@test_file_path)
|
|
|
|
|
|
{:ok, stat} = File.stat(@test_file_path)
|
|
assert stat.size > 0, "File is empty"
|
|
|
|
File.rm!(@test_file_path)
|
|
end
|
|
|
|
test "returns an error for an invalid URL" do
|
|
invalid_url = "https://nonexistent.url/file.txt"
|
|
|
|
assert {:error, _reason} = Bright.Uploads.download_file(invalid_url, @test_file_path)
|
|
end
|
|
end
|
|
|
|
describe "upload_file_to_s3/3" do
|
|
setup do
|
|
File.write!(@test_file_path, "This is a test file.")
|
|
on_exit(fn -> File.rm!(@test_file_path) end)
|
|
:ok
|
|
end
|
|
|
|
@tag :acceptance
|
|
test "uploads a file to S3 successfully" do
|
|
basename = "random.txt"
|
|
random_string = for _ <- 1..12, into: "", do: <<Enum.random(~c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")>>
|
|
unique_filename = "#{random_string}-#{basename}"
|
|
|
|
|
|
assert {:ok, %{status_code: 200}} = Bright.Uploads.upload_file_to_s3(@test_file_path, @s3_bucket, unique_filename)
|
|
|
|
# Optionally verify the file exists in S3 (requires an S3 client)
|
|
{:ok, %{status_code: 200}} =
|
|
ExAws.S3.head_object(@s3_bucket, unique_filename)
|
|
|> ExAws.request()
|
|
|
|
# Cleanup: Delete the uploaded file from S3
|
|
ExAws.S3.delete_object(@s3_bucket, unique_filename)
|
|
|> ExAws.request()
|
|
end
|
|
|
|
@tag :acceptance
|
|
test "returns an error for a nonexistent file" do
|
|
assert {:error, :enoent} = Bright.Uploads.upload_file_to_s3("nonexistent.txt", @s3_bucket, "test/nonexistant.txt")
|
|
end
|
|
end
|
|
end
|