discourse/spec/requests/api/badges_spec.rb
Blake Erickson 2861b9337d
DEV: Fix flaky admin badges.json api docs spec (#17210)
* DEV: Fix flaky admin badges.json api docs spec

This commit is to fix this incredibly vague error message:

```
Failure/Error: expect(valid).to eq(true)

  expected: true
       got: false
```

From this test:

> Assertion: badges /admin/badges.json get success response behaves like
> a JSON endpoint response body matches the documented response schema

I was finally able to repro locally using parallel tests:

```
RAILS_ENV=test bundle exec ./bin/turbo_rspec
```

I *think* the parallel tests might be swallowing the `puts` output, but
when I also specified the individual spec file

```
RAILS_ENV=test bundle exec ./bin/turbo_rspec spec/requests/api/badges_spec.rb
```

It revealed the issue:

```
VALIDATION DETAILS: {"missing_keys"=>["i18n_name"]}
```

``` ruby
...
  def include_i18n_name?
    object.system?
  end
```

Looks like if the "system" user isn't being used the `i18n_name` won't
be returned in the json response so we shouldn't mark it as a required
attribute.

* Switch to using fab!

When using `let(:badge)` to fabricate a test badge it wouldn't be
returned from the controller, but switching to using `fab!` allows it to
be returned in the json data giving us a non-system badge to test
against.
2022-06-23 14:32:17 -06:00

110 lines
3.1 KiB
Ruby

# frozen_string_literal: true
require 'swagger_helper'
describe 'badges' do
fab!(:admin) { Fabricate(:admin) }
fab!(:badge) { Fabricate(:badge) }
before do
Jobs.run_immediately!
sign_in(admin)
end
path '/admin/badges.json' do
get 'List badges' do
tags 'Badges'
operationId 'adminListBadges'
consumes 'application/json'
expected_request_schema = nil
produces 'application/json'
response '200', 'success response' do
expected_response_schema = load_spec_schema('badge_list_response')
schema expected_response_schema
it_behaves_like "a JSON endpoint", 200 do
let(:expected_response_schema) { expected_response_schema }
let(:expected_request_schema) { expected_request_schema }
end
end
end
post 'Create badge' do
tags 'Badges'
operationId 'createBadge'
consumes 'application/json'
expected_request_schema = load_spec_schema('badge_create_request')
parameter name: :params, in: :body, schema: expected_request_schema
produces 'application/json'
response '200', 'success response' do
expected_response_schema = load_spec_schema('badge_create_response')
schema expected_response_schema
let(:params) { {
'name' => 'badge1',
'badge_type_id' => 2
} }
it_behaves_like "a JSON endpoint", 200 do
let(:expected_response_schema) { expected_response_schema }
let(:expected_request_schema) { expected_request_schema }
end
end
end
end
path '/admin/badges/{id}.json' do
put 'Update badge' do
tags 'Badges'
operationId 'updateBadge'
consumes 'application/json'
expected_request_schema = load_spec_schema('badge_update_request')
parameter name: :id, in: :path, schema: { type: :integer }
parameter name: :params, in: :body, schema: expected_request_schema
produces 'application/json'
response '200', 'success response' do
expected_response_schema = load_spec_schema('badge_update_response')
schema expected_response_schema
let(:id) { badge.id }
let(:params) { {
'name' => 'badge1',
'badge_type_id' => 2
} }
it_behaves_like "a JSON endpoint", 200 do
let(:expected_response_schema) { expected_response_schema }
let(:expected_request_schema) { expected_request_schema }
end
end
end
delete 'Delete badge' do
tags 'Badges'
operationId 'deleteBadge'
consumes 'application/json'
expected_request_schema = nil
parameter name: :id, in: :path, schema: { type: :integer }
produces 'application/json'
response '200', 'success response' do
expected_response_schema = nil
schema expected_response_schema
let(:id) { badge.id }
it_behaves_like "a JSON endpoint", 200 do
let(:expected_response_schema) { expected_response_schema }
let(:expected_request_schema) { expected_request_schema }
end
end
end
end
end