Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
Also lock dev version of sftpgo. Upgrades need to be managed due to breaking changes.
  • Loading branch information
atruskie committed Aug 9, 2024
1 parent 0617d04 commit 1bca6cb
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def show
def new
do_new_resource
get_project_if_exists
do_set_attributes(site_params(for_create: true))
do_set_attributes
do_authorize_instance

# initialize lat/lng to Brisbane-ish
Expand Down
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def self.schema
#notes: { type: 'object' }, # TODO: https://github.com/QutEcoacoustics/baw-server/issues/467
notes: { type: 'string' },
**Api::Schema.all_user_stamps,
site_ids: Api::Schema.ids,
site_ids: Api::Schema.ids(read_only: true),
region_ids: Api::Schema.ids(read_only: true),
owner_ids: Api::Schema.ids(read_only: true),
image_urls: Api::Schema.image_urls,
Expand Down
2 changes: 1 addition & 1 deletion app/models/saved_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def self.schema
properties: {
id: { '$ref' => '#/components/schemas/id', readOnly: true },
analysis_job_ids: { type: 'array', items: { '$ref' => '#/components/schemas/id' } },
project_ids: { type: 'array', items: { '$ref' => '#/components/schemas/id' } },
project_ids: { type: 'array', items: { '$ref' => '#/components/schemas/id' }, readOnly: true },
name: { type: 'string' },
**Api::Schema.rendered_markdown(:description),
stored_query: { type: 'object' },
Expand Down
17 changes: 6 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ x-gh_actions:
# in all the containers. In this case /data should always be the internal path for this shared mount.
# Client side we mount to ./data.
# In each case a sub-folder is used to isolated environments.
x-data-mount-dev:
&data_mount_dev
x-data-mount-dev: &data_mount_dev
type: bind
source: ./data/development
target: /data/development
x-data-mount-test:
&data_mount_test
x-data-mount-test: &data_mount_test
type: bind
source: ./data/test
target: /data/test
Expand Down Expand Up @@ -51,12 +49,11 @@ services:
# `rails baw:truncate_sftpgo_tables`.
# Error from logs:
# Unable to initialize/update the data provider: pq: relation "sftpgo_schema_version" already exists
upload:
&upload
upload: &upload
<<: *gh_actions
# Default command is `sftpgo serve`
#command: sftpgo serve
image: drakkan/sftpgo:v2-alpine
image: drakkan/sftpgo:v2.2.3-alpine
depends_on:
- db
environment:
Expand Down Expand Up @@ -100,8 +97,7 @@ services:
target: /var/lib/sftpgo
- <<: *data_mount_test

workers:
&worker
workers: &worker
<<: *gh_actions
build:
context: .
Expand All @@ -123,8 +119,7 @@ services:
- <<: *data_mount_dev
working_dir: /home/baw_web/baw-server

workers_test:
&worker_test
workers_test: &worker_test
<<: *worker
volumes:
- type: bind
Expand Down
7 changes: 5 additions & 2 deletions spec/api/sites/site_spec_nested_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

sends_json_and_expects_json
with_authorization
for_model Site
for_model Site, factory_args: -> { { projects: [project] } }
which_has_schema ref(:site)

let(:project_id) { project.id }
Expand Down Expand Up @@ -109,7 +109,10 @@
which_has_schema ref(:site)

let!(:orphaned_site) {
create(:site, projects: [], project_ids: [])
s = build(:site, projects: [], project_ids: [])
# intentionally invalid
s.save!(validate: false)
s
}

path '/sites/orphans/filter' do
Expand Down
13 changes: 10 additions & 3 deletions spec/support/api_spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module ExampleGroup
def self.extended(base); end

attr_accessor :baw_consumes, :baw_produces, :baw_security, :baw_model_name, :baw_body_name, :baw_model,
:baw_factory, :baw_model_schema, :baw_route_params, :baw_body_params
:baw_factory, :baw_factory_args, :baw_model_schema, :baw_route_params, :baw_body_params

# these tests document an API - they're not really for testing user access
# Even if they were, the OAS specification has no concept of different
Expand All @@ -55,7 +55,7 @@ def sends_json_and_expects_json
self.baw_produces = ['application/json']
end

def for_model(given_model, factory: nil)
def for_model(given_model, factory: nil, factory_args: nil)
self.baw_model_name = baw_model_name = given_model.model_name.singular
self.baw_factory = factory || baw_model_name
# rswag expects let statements to be defined with the same name as the parameters,
Expand All @@ -69,6 +69,10 @@ def for_model(given_model, factory: nil)

self.baw_model = given_model

raise 'factory_args must be a Proc' unless factory_args.nil? || factory_args.is_a?(Proc)

self.baw_factory_args = factory_args

let(:model) { given_model }
let(:model_name) { baw_model_name }
let(:body_name) { baw_body_name }
Expand Down Expand Up @@ -148,8 +152,11 @@ def send_model(&)
# using attributes_for and filtering out readonly properties
# from the hash, according to the given schema
def auto_send_model(subset: nil)
baw_factory_args = get_parent_param :baw_factory_args

let((get_parent_param :baw_body_name)) do
body_attributes_for(model_name, factory:, subset:)
factory_args = instance_exec(&baw_factory_args) if baw_factory_args
body_attributes_for(model_name, factory:, subset:, factory_args:)
end
end

Expand Down
10 changes: 9 additions & 1 deletion spec/support/factory_bot_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ def body_attributes_for(model_name, traits: [], factory: nil, subset: nil, facto
schema = model_name.to_s.classify.constantize.schema
# was using attributes_for here but it doesn't include associations!
# full = attributes_for(model_name)
full = build(factory || model_name, *traits, **(factory_args || {})).attributes.symbolize_keys
model = build(factory || model_name, *traits, **(factory_args || {}))
full = model.attributes.symbolize_keys

# still need to fetch many to many associations
model.class.reflect_on_all_associations(:has_and_belongs_to_many).each do |assoc|
foreign_key_name = "#{assoc.plural_name.singularize}_ids".to_sym
full[foreign_key_name] = model.send(foreign_key_name)
end

writeable = schema[:properties]
.select { |_key, value| value.fetch(:readOnly, false) == false }
.keys
Expand Down

0 comments on commit 1bca6cb

Please sign in to comment.