From 2861b9337dc26dd0eb8f3daf43b787e2917548bd Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Thu, 23 Jun 2022 15:32:17 -0500 Subject: [PATCH] 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. --- spec/requests/api/badges_spec.rb | 4 ++-- spec/requests/api/schemas/json/badge_list_response.json | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/requests/api/badges_spec.rb b/spec/requests/api/badges_spec.rb index 4a891a18444..cbd8e23ff00 100644 --- a/spec/requests/api/badges_spec.rb +++ b/spec/requests/api/badges_spec.rb @@ -3,8 +3,8 @@ require 'swagger_helper' describe 'badges' do - let(:admin) { Fabricate(:admin) } - let(:badge) { Fabricate(:badge) } + fab!(:admin) { Fabricate(:admin) } + fab!(:badge) { Fabricate(:badge) } before do Jobs.run_immediately! diff --git a/spec/requests/api/schemas/json/badge_list_response.json b/spec/requests/api/schemas/json/badge_list_response.json index ba1a895d73c..9c0fa2b16bb 100644 --- a/spec/requests/api/schemas/json/badge_list_response.json +++ b/spec/requests/api/schemas/json/badge_list_response.json @@ -78,7 +78,10 @@ "type": "boolean" }, "i18n_name": { - "type": "string" + "type": [ + "string", + "null" + ] }, "badge_type_id": { "type": "integer" @@ -105,7 +108,6 @@ "target_posts", "auto_revoke", "show_posts", - "i18n_name", "badge_type_id" ] }