Files
ubicloud/spec/routes/api/committee_spec.rb
Jeremy Evans feb1c97ff5 Mark project_id columns newly added to tables as NOT NULL
These columns are now populated and required during insert.
This causes a large amount of fallout in the specs, as there
were many places that inserted rows without having a related
project.

No changes outside the specs except for
ApiKey#create_personal_access_token, which now requires a
project keyword argument, since the method would fail without
it now (the was previously optional).
2025-01-17 12:47:51 -08:00

87 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 {
project
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
project
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