Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rswag cop #47

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

andimrob
Copy link
Contributor

@andimrob andimrob commented Jun 7, 2024

This adds a new cop for request specs to ensure they are written using rswag to document their API contracts. This is accomplished by searching for usages of the built in http verb request methods in spec examples, helper functions, and before blocks. Specs written using RSwag should not be using these methods, they should instead be using RSwag built-ins like run_test! or submit_request!

@andimrob andimrob force-pushed the robb/add-openapi-request-spec-cop branch 2 times, most recently from e427f2f to 96412be Compare June 8, 2024 13:46
module Cop
module Betterment
class NotUsingRswag < Base
MSG = 'API tests should use documented using rswag and not the built in `get`, `post`, `put`, `patch`, `delete` methods'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it distinguish between API tests and request tests that test ERB endpoints?

Copy link
Contributor Author

@andimrob andimrob Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written it won't distinguish between the two, I hadn't considered that case. However I think if we wanted to write spec for an endpoint that produces html we'd just specify produces 'text/html and not specify anything about the structure of it. In the tests themselves whether using rswag's built in run_test! or our Retail helper submit_request! we will still have access to the response and could assert things about the html from that. So this as written might work for endpoints that respond with other content types.

This approach is perhaps a bit too naive, instead it could try to assert that a request spec is written in a particular structure using methods that come from rswag:

  path 'url_string' do
    ...
    get/post/put/patch/delete 'some_description' do
      ...
      response 'http_status_code', 'some_description' do
        ...
        run_test!
        
        # or

        it 'some_description' do
          submit_request!
        end
      end
    end
  end

@andimrob andimrob force-pushed the robb/add-openapi-request-spec-cop branch from 96412be to 6820c4e Compare October 4, 2024 17:41
@andimrob andimrob force-pushed the robb/add-openapi-request-spec-cop branch from 6820c4e to e0beafd Compare November 20, 2024 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants