Add JSON API support for user data exports (#2786)
This commit is contained in:
@@ -67,6 +67,62 @@ class Users::DataExportsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_select "h2", "Download Expired"
|
||||
end
|
||||
|
||||
test "create as JSON" do
|
||||
assert_difference -> { User::DataExport.count }, 1 do
|
||||
assert_enqueued_with(job: DataExportJob) do
|
||||
post user_data_exports_path(@user), as: :json
|
||||
end
|
||||
end
|
||||
|
||||
assert_response :created
|
||||
body = @response.parsed_body
|
||||
assert body["id"].present?
|
||||
assert_equal "pending", body["status"]
|
||||
assert_nil body["download_url"]
|
||||
end
|
||||
|
||||
test "show as JSON with completed export" do
|
||||
export = @user.data_exports.create!(account: Current.account)
|
||||
export.build
|
||||
|
||||
get user_data_export_path(@user, export), as: :json
|
||||
assert_response :success
|
||||
|
||||
body = @response.parsed_body
|
||||
assert_equal export.id, body["id"]
|
||||
assert_equal "completed", body["status"]
|
||||
assert body["download_url"].present?
|
||||
end
|
||||
|
||||
test "show as JSON with pending export" do
|
||||
export = @user.data_exports.create!(account: Current.account)
|
||||
|
||||
get user_data_export_path(@user, export), as: :json
|
||||
assert_response :success
|
||||
|
||||
body = @response.parsed_body
|
||||
assert_equal export.id, body["id"]
|
||||
assert_equal "pending", body["status"]
|
||||
assert_nil body["download_url"]
|
||||
end
|
||||
|
||||
test "show as JSON with failed export" do
|
||||
export = @user.data_exports.create!(account: Current.account, status: :failed)
|
||||
|
||||
get user_data_export_path(@user, export), as: :json
|
||||
assert_response :success
|
||||
|
||||
body = @response.parsed_body
|
||||
assert_equal export.id, body["id"]
|
||||
assert_equal "failed", body["status"]
|
||||
assert_nil body["download_url"]
|
||||
end
|
||||
|
||||
test "show as JSON with missing export" do
|
||||
get user_data_export_path(@user, "nonexistent"), as: :json
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
test "create is forbidden for other users" do
|
||||
other_user = users(:kevin)
|
||||
|
||||
@@ -75,6 +131,14 @@ class Users::DataExportsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
test "create as JSON is forbidden for other users" do
|
||||
other_user = users(:kevin)
|
||||
|
||||
post user_data_exports_path(other_user), as: :json
|
||||
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
test "show is forbidden for other users" do
|
||||
other_user = users(:kevin)
|
||||
export = other_user.data_exports.create!(account: Current.account)
|
||||
@@ -84,4 +148,26 @@ class Users::DataExportsControllerTest < ActionDispatch::IntegrationTest
|
||||
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
test "show as JSON is forbidden for other users" do
|
||||
other_user = users(:kevin)
|
||||
export = other_user.data_exports.create!(account: Current.account)
|
||||
export.build
|
||||
|
||||
get user_data_export_path(other_user, export), as: :json
|
||||
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
test "create as JSON rejects request when current export limit is reached" do
|
||||
Users::DataExportsController::CURRENT_EXPORT_LIMIT.times do
|
||||
@user.data_exports.create!(account: Current.account)
|
||||
end
|
||||
|
||||
assert_no_difference -> { User::DataExport.count } do
|
||||
post user_data_exports_path(@user), as: :json
|
||||
end
|
||||
|
||||
assert_response :too_many_requests
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user