3scale / apisonator

Red Hat 3scale API Management Apisonator backend
https://3scale.net
Apache License 2.0
35 stars 27 forks source link

Fix bug when loading the usage limits and no_body=true #224

Closed davidor closed 4 years ago

davidor commented 4 years ago

This PR fixes a bug introduced in #221

The problem is that I assumed that in Application.load_usage_limits_affected_by all the metrics passed were already validated, but that was a mistake because the listener does not validate the metrics until it runs the Limits validator, which happens later in the process.

More specificaly, when a metric does not exist, Metric.load_id returns nil and then, the sort here crashes: https://github.com/3scale/apisonator/blob/e965a2228c331c21b159925d16888a5f473ef26c/lib/3scale/backend/application.rb#L241

We didn't caught this because we didn't have tests that used both no_body=true, and reported more than 1 metric and at least an invalid one. I have included tests for that in this PR.

In the future, maybe we should make the metrics validation more explicit as a separate test in the transactor rather than have it hidden in the Limits validator.