|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class MissionControl::Jobs::BasicAuthenticationTest < ActionDispatch::IntegrationTest |
| 4 | + test "unconfigured basic auth is closed" do |
| 5 | + with_http_basic_auth do |
| 6 | + get mission_control_jobs.application_queues_url(@application), headers: auth_headers("dev", "secret") |
| 7 | + assert_response :unauthorized |
| 8 | + end |
| 9 | + end |
| 10 | + |
| 11 | + test "fail to authenticate without credentials" do |
| 12 | + with_http_basic_auth(user: "dev", password: "secret") do |
| 13 | + get mission_control_jobs.application_queues_url(@application) |
| 14 | + assert_response :unauthorized |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + test "fail to authenticate with wrong credentials" do |
| 19 | + with_http_basic_auth(user: "dev", password: "secret") do |
| 20 | + get mission_control_jobs.application_queues_url(@application), headers: auth_headers("dev", "wrong") |
| 21 | + assert_response :unauthorized |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + test "authenticate with correct credentials" do |
| 26 | + with_http_basic_auth(user: "dev", password: "secret") do |
| 27 | + get mission_control_jobs.application_queues_url(@application), headers: auth_headers("dev", "secret") |
| 28 | + assert_response :ok |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + def with_http_basic_auth(enabled: true, user: nil, password: nil) |
| 34 | + previous_enabled, MissionControl::Jobs.http_basic_auth_enabled = MissionControl::Jobs.http_basic_auth_enabled, enabled |
| 35 | + previous_user, MissionControl::Jobs.http_basic_auth_user = MissionControl::Jobs.http_basic_auth_user, user |
| 36 | + previous_password, MissionControl::Jobs.http_basic_auth_password = MissionControl::Jobs.http_basic_auth_password, password |
| 37 | + yield |
| 38 | + ensure |
| 39 | + MissionControl::Jobs.http_basic_auth_enabled = previous_enabled |
| 40 | + MissionControl::Jobs.http_basic_auth_user = previous_user |
| 41 | + MissionControl::Jobs.http_basic_auth_password = previous_password |
| 42 | + end |
| 43 | + |
| 44 | + def auth_headers(user, password) |
| 45 | + { Authorization: ActionController::HttpAuthentication::Basic.encode_credentials(user, password) } |
| 46 | + end |
| 47 | +end |
0 commit comments