Files
ubicloud/spec/routes/api/committee_spec.rb
Jeremy Evans daf2279a64 Add project_with_default_policy to DRY up api route specs
Also, move login_api from being defined globally to being defined
only in the spec classes.
2024-12-04 10:18:55 -08:00

85 lines
2.9 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# frozen_string_literal: true
require_relative "spec_helper"
RSpec.describe Clover, "committee infrastructure" do
let(:user) { create_account }
let(:project) { project_with_default_policy(user) }
before do
login_api(user.email)
end
it "serializes unexpected nested errors that cannot be converted" do
ex = Class.new(Committee::BadRequest) do
def self.name
"CommitteeTestNestedException"
end
def original_error
Exception.new("unaddressed error value")
end
end.new("testing conversion of unenumerated nested exceptions")
allow_any_instance_of(Committee::SchemaValidator::OpenAPI3).to receive(:request_validate) do
raise ex
end
expect {
post "/project/#{UBID.generate(UBID::TYPE_PROJECT)}/location/#{TEST_LOCATION}/vm/test-vm"
}.to raise_error ex
end
it "rejects paths that cannot be found in the schema" do
post "/not-a-prefix/"
expect(JSON.parse(last_response.body).dig("error", "message")).to eq("Sorry, we couldnt find the resource youre looking for.")
end
it "fails when the request has unparsable json" do
expect {
post "/project/#{project.ubid}/location/#{TEST_LOCATION}/vm/test-vm", "this is not json"
}.to raise_error Committee::InvalidRequest, "Request body wasn't valid JSON."
end
it "fails when response has invalid structure" do
expect(Serializers::Vm).to receive(:serialize_internal).and_return({totally_not_correct: true})
expect {
post "/project/#{project.ubid}/location/#{TEST_LOCATION}/vm/test-vm", {
public_key: "ssh key",
unix_user: "ubi",
size: "standard-2",
boot_image: "ubuntu-jammy",
storage_size: "40"
}.to_json
}.to raise_error Committee::InvalidResponse, %r{#/components/schemas/Vm missing required parameters: .*}
end
it "fails when request is missing required keys" do
expect {
post "/project/#{project.ubid}/location/#{TEST_LOCATION}/vm/test-vm", {}.to_json
}.to raise_error Committee::InvalidRequest, /missing required parameters: public_key/
end
it "fails when request has extra unexpected keys" do
expect {
post "/project/#{project.ubid}/location/#{TEST_LOCATION}/vm/test-vm", {missing_stuff: true}.to_json
}.to raise_error Committee::InvalidRequest, /schema does not define properties: missing_stuff/
end
it "reports if response body is not valid JSON" do
allow_any_instance_of(Committee::SchemaValidator::OpenAPI3).to receive(:response_validate) do
raise JSON::ParserError.new("injected parser error")
end
expect {
post "/project/#{project.ubid}/location/#{TEST_LOCATION}/vm/test-vm", {
public_key: "ssh key",
unix_user: "ubi",
size: "standard-2",
boot_image: "ubuntu-jammy",
storage_size: "40"
}.to_json
}.to raise_error Committee::InvalidResponse, "Response body wasn't valid JSON."
end
end