docbrown/spec/requests/admin/secrets_spec.rb
Josh Puetz 1c566e0ec4
[deploy] Move /internal to `/admin (#9639)
* First draft - all the big changes

* Changing some more references to 'internal'

* Relocate internal request tests to admin

* Relocate internal system tests to admin

* Fix trailing space

* Test fix

* Move queries from internal to admin

* Docs updates

* Rename internal stimuls controllers to admin (plus docs)

* Rename admin layout

* Fix routing after rebase

* Fixes for latest added admin interfaces

* Serviceworker ignore paths
2020-08-07 10:36:26 -04:00

64 lines
1.9 KiB
Ruby

require "rails_helper"
RSpec.describe "/admin/secrets", type: :request do
before do
allow(AppSecrets).to receive(:vault_enabled?).and_return(true)
allow(AppSecrets).to receive(:[]=)
end
context "when the user is not an admin" do
it "blocks the request" do
user = create(:user)
sign_in user
expect do
get admin_secrets_path
end.to raise_error(Pundit::NotAuthorizedError)
end
end
context "when the user is an admin" do
let(:admin) { create(:user, :admin) }
before { sign_in admin }
describe "GET /admin/secrets" do
it "renders with status 200" do
get admin_secrets_path
expect(response.status).to eq 200
end
it "displays an alert when Vault is not enabled" do
allow(AppSecrets).to receive(:vault_enabled?).and_return(false)
get admin_secrets_path
expect(response.body).to include("Vault is not currently setup for your application")
end
end
describe "PUT /admin/secrets" do
let(:valid_secret) { AppSecrets::SETTABLE_SECRETS.first }
let(:valid_params) { { valid_secret => "SECRET_VALUE" } }
it "successfully sets a secret and shows flash message" do
allow(AppSecrets).to receive(:[]=)
put admin_secrets_path, params: valid_params
expect(response.status).to eq 302
expect(AppSecrets).to have_received(:[]=).with(valid_secret, "SECRET_VALUE")
expect(flash[:success]).to include("Secret #{valid_secret} was successfully updated in Vault")
end
it "returns a bad_request with invalid params" do
put admin_secrets_path, params: {}
expect(response.status).to eq 400
end
it "creates an audit log" do
Audit::Subscribe.listen :internal
expect do
put admin_secrets_path, params: valid_params
end.to change(AuditLog, :count).by(1)
Audit::Subscribe.forget :internal
end
end
end
end