mirror of
https://github.com/System-End/hackatime.git
synced 2026-04-19 19:55:16 +00:00
110 lines
3.4 KiB
Ruby
110 lines
3.4 KiB
Ruby
require "test_helper"
|
|
|
|
class My::HeartbeatImportSourcesControllerTest < ActionDispatch::IntegrationTest
|
|
setup do
|
|
Flipper.enable(:wakatime_imports_mirrors)
|
|
end
|
|
|
|
teardown do
|
|
Flipper.disable(:wakatime_imports_mirrors)
|
|
end
|
|
|
|
test "requires auth for create" do
|
|
post my_heartbeat_import_source_path, params: {
|
|
heartbeat_import_source: {
|
|
endpoint_url: "https://wakatime.com/api/v1",
|
|
encrypted_api_key: "api-key"
|
|
}
|
|
}
|
|
|
|
assert_response :redirect
|
|
assert_redirected_to root_path
|
|
end
|
|
|
|
test "authenticated user can create source and queue sync" do
|
|
user = User.create!(timezone: "UTC")
|
|
sign_in_as(user)
|
|
GoodJob::Job.where(job_class: "HeartbeatImportSourceSyncJob").delete_all
|
|
|
|
assert_difference -> { HeartbeatImportSource.count }, 1 do
|
|
assert_difference -> { GoodJob::Job.where(job_class: "HeartbeatImportSourceSyncJob").count }, 1 do
|
|
post my_heartbeat_import_source_path, params: {
|
|
heartbeat_import_source: {
|
|
endpoint_url: "https://wakatime.com/api/v1",
|
|
encrypted_api_key: "api-key",
|
|
sync_enabled: "1"
|
|
}
|
|
}
|
|
end
|
|
end
|
|
|
|
assert_response :redirect
|
|
assert_redirected_to my_settings_data_path
|
|
end
|
|
|
|
test "show returns configured source payload" do
|
|
user = User.create!(timezone: "UTC")
|
|
source = user.create_heartbeat_import_source!(
|
|
provider: :wakatime_compatible,
|
|
endpoint_url: "https://wakatime.com/api/v1",
|
|
encrypted_api_key: "api-key"
|
|
)
|
|
sign_in_as(user)
|
|
|
|
get my_heartbeat_import_source_path
|
|
|
|
assert_response :success
|
|
payload = JSON.parse(response.body)
|
|
assert_equal source.id, payload.dig("import_source", "id")
|
|
assert_equal "wakatime_compatible", payload.dig("import_source", "provider")
|
|
end
|
|
|
|
test "sync now queues source sync" do
|
|
user = User.create!(timezone: "UTC")
|
|
source = user.create_heartbeat_import_source!(
|
|
provider: :wakatime_compatible,
|
|
endpoint_url: "https://wakatime.com/api/v1",
|
|
encrypted_api_key: "api-key",
|
|
sync_enabled: true
|
|
)
|
|
sign_in_as(user)
|
|
GoodJob::Job.where(job_class: "HeartbeatImportSourceSyncJob").delete_all
|
|
|
|
assert_difference -> { GoodJob::Job.where(job_class: "HeartbeatImportSourceSyncJob").count }, 1 do
|
|
post sync_my_heartbeat_import_source_path
|
|
end
|
|
|
|
assert_response :redirect
|
|
assert_redirected_to my_settings_data_path
|
|
assert_equal source.id, GoodJob::Job.where(job_class: "HeartbeatImportSourceSyncJob").last.serialized_params.dig("arguments", 0)
|
|
end
|
|
|
|
test "destroy removes source" do
|
|
user = User.create!(timezone: "UTC")
|
|
user.create_heartbeat_import_source!(
|
|
provider: :wakatime_compatible,
|
|
endpoint_url: "https://wakatime.com/api/v1",
|
|
encrypted_api_key: "api-key"
|
|
)
|
|
sign_in_as(user)
|
|
|
|
assert_difference -> { HeartbeatImportSource.count }, -1 do
|
|
delete my_heartbeat_import_source_path
|
|
end
|
|
|
|
assert_response :redirect
|
|
assert_redirected_to my_settings_data_path
|
|
end
|
|
|
|
test "returns not found json when imports and mirrors are disabled" do
|
|
user = User.create!(timezone: "UTC")
|
|
sign_in_as(user)
|
|
Flipper.disable(:wakatime_imports_mirrors)
|
|
|
|
get my_heartbeat_import_source_path, as: :json
|
|
|
|
assert_response :not_found
|
|
payload = JSON.parse(response.body)
|
|
assert_equal "Imports and mirrors are currently disabled.", payload["error"]
|
|
end
|
|
end
|