|
| 1 | +require_relative "../pact_broker/basic_auth" |
| 2 | +require "rack/test" |
| 3 | + |
| 4 | +RSpec.describe "basic auth" do |
| 5 | + |
| 6 | + include Rack::Test::Methods |
| 7 | + |
| 8 | + let(:protected_app) { ->(env) { [200, {}, []]} } |
| 9 | + |
| 10 | + let(:app) { BasicAuth.new(protected_app, 'write_username', 'write_password', 'read_username', 'read_password', allow_public_access_to_heartbeat) } |
| 11 | + let(:allow_public_access_to_heartbeat) { true } |
| 12 | + |
| 13 | + |
| 14 | + context "when requesting the heartbeat" do |
| 15 | + let(:path) { "/diagnostic/status/heartbeat" } |
| 16 | + |
| 17 | + context "when allow_public_access_to_heartbeat is true" do |
| 18 | + context "when no credentials are used" do |
| 19 | + it "allows GET" do |
| 20 | + get path |
| 21 | + expect(last_response.status).to eq 200 |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + context "when allow_public_access_to_heartbeat is false" do |
| 27 | + let(:allow_public_access_to_heartbeat) { false } |
| 28 | + |
| 29 | + context "when no credentials are used" do |
| 30 | + it "does not allow GET" do |
| 31 | + get path |
| 32 | + expect(last_response.status).to eq 401 |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context "when the correct credentials are used" do |
| 37 | + it "allows GET" do |
| 38 | + basic_authorize 'read_username', 'read_password' |
| 39 | + get path |
| 40 | + expect(last_response.status).to eq 200 |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context "when requesting a badge" do |
| 47 | + context "when no credentials are used" do |
| 48 | + it "allows GET" do |
| 49 | + get "pacts/provider/foo/consumer/bar/badge" |
| 50 | + expect(last_response.status).to eq 200 |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "with the correct username and password for the write user" do |
| 56 | + it "allows GET" do |
| 57 | + basic_authorize 'write_username', 'write_password' |
| 58 | + get "/" |
| 59 | + expect(last_response.status).to eq 200 |
| 60 | + end |
| 61 | + |
| 62 | + it "allows POST" do |
| 63 | + basic_authorize 'write_username', 'write_password' |
| 64 | + post "/" |
| 65 | + expect(last_response.status).to eq 200 |
| 66 | + end |
| 67 | + |
| 68 | + it "allows HEAD" do |
| 69 | + basic_authorize 'write_username', 'write_password' |
| 70 | + head "/" |
| 71 | + expect(last_response.status).to eq 200 |
| 72 | + end |
| 73 | + |
| 74 | + it "allows OPTIONS" do |
| 75 | + basic_authorize 'write_username', 'write_password' |
| 76 | + options "/" |
| 77 | + expect(last_response.status).to eq 200 |
| 78 | + end |
| 79 | + |
| 80 | + it "allows PUT" do |
| 81 | + basic_authorize 'write_username', 'write_password' |
| 82 | + delete "/" |
| 83 | + expect(last_response.status).to eq 200 |
| 84 | + end |
| 85 | + |
| 86 | + it "allows PATCH" do |
| 87 | + basic_authorize 'write_username', 'write_password' |
| 88 | + patch "/" |
| 89 | + expect(last_response.status).to eq 200 |
| 90 | + end |
| 91 | + |
| 92 | + it "allows DELETE" do |
| 93 | + basic_authorize 'write_username', 'write_password' |
| 94 | + delete "/" |
| 95 | + expect(last_response.status).to eq 200 |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context "with the incorrect username and password for the write user" do |
| 100 | + it "does not allow POST" do |
| 101 | + basic_authorize 'foo', 'password' |
| 102 | + post "/" |
| 103 | + expect(last_response.status).to eq 401 |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + context "with the correct username and password for the read user" do |
| 108 | + it "allows GET" do |
| 109 | + basic_authorize 'read_username', 'read_password' |
| 110 | + get "/" |
| 111 | + expect(last_response.status).to eq 200 |
| 112 | + end |
| 113 | + |
| 114 | + it "allows OPTIONS" do |
| 115 | + basic_authorize 'read_username', 'read_password' |
| 116 | + options "/" |
| 117 | + expect(last_response.status).to eq 200 |
| 118 | + end |
| 119 | + |
| 120 | + it "allows HEAD" do |
| 121 | + basic_authorize 'read_username', 'read_password' |
| 122 | + head "/" |
| 123 | + expect(last_response.status).to eq 200 |
| 124 | + end |
| 125 | + |
| 126 | + it "does not allow POST" do |
| 127 | + basic_authorize 'read_username', 'read_password' |
| 128 | + post "/" |
| 129 | + expect(last_response.status).to eq 401 |
| 130 | + end |
| 131 | + |
| 132 | + it "does not allow PUT" do |
| 133 | + basic_authorize 'read_username', 'read_password' |
| 134 | + put "/" |
| 135 | + expect(last_response.status).to eq 401 |
| 136 | + end |
| 137 | + |
| 138 | + it "does not allow PATCH" do |
| 139 | + basic_authorize 'read_username', 'read_password' |
| 140 | + patch "/" |
| 141 | + expect(last_response.status).to eq 401 |
| 142 | + end |
| 143 | + |
| 144 | + it "does not allow DELETE" do |
| 145 | + basic_authorize 'read_username', 'read_password' |
| 146 | + delete "/" |
| 147 | + expect(last_response.status).to eq 401 |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + context "with the incorrect username and password for the write user" do |
| 152 | + it "does not allow GET" do |
| 153 | + basic_authorize 'write_username', 'wrongpassword' |
| 154 | + get "/" |
| 155 | + expect(last_response.status).to eq 401 |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + context "with the incorrect username and password for the read user" do |
| 160 | + it "does not allow GET" do |
| 161 | + basic_authorize 'read_username', 'wrongpassword' |
| 162 | + get "/" |
| 163 | + expect(last_response.status).to eq 401 |
| 164 | + end |
| 165 | + end |
| 166 | + |
| 167 | + context "with a request to the badge URL" do |
| 168 | + context "with no credentials" do |
| 169 | + it "allows GET" do |
| 170 | + get "/pacts/provider/foo/consumer/bar/badge" |
| 171 | + expect(last_response.status).to eq 200 |
| 172 | + end |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + context "when there is no read only user configured" do |
| 177 | + let(:app) { BasicAuth.new(protected_app, 'write_username', 'write_password', nil, nil, allow_public_access_to_heartbeat) } |
| 178 | + |
| 179 | + context "with no credentials" do |
| 180 | + it "does not allow GET" do |
| 181 | + get "/" |
| 182 | + expect(last_response.status).to eq 401 |
| 183 | + end |
| 184 | + end |
| 185 | + |
| 186 | + context "with credentials" do |
| 187 | + it "does not allow GET" do |
| 188 | + basic_authorize "foo", "bar" |
| 189 | + get "/" |
| 190 | + expect(last_response.status).to eq 401 |
| 191 | + end |
| 192 | + end |
| 193 | + end |
| 194 | +end |
0 commit comments