Allow empty credentials when saving settings#1048
Conversation
…credentials, allowing us to save empty values which allows us to override those credentials and still have auth work
…se credentials, allowing us to save empty values which allows us to override those credentials and still have auth work
… credentials, allowing us to save empty values which allows us to override those credentials and still have auth work
…ore attempting authentication, allowing credentials to be overridden
…those credentials, allowing us to save empty values which allows us to override those credentials and still have auth work
…ing credentials, allowing empty credentials to be saved and allowing those to be properly filtered
✅ WordPress Plugin Check Report
📊 ReportAll checks passed! No errors or warnings found. 🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check |
peterwilsoncc
left a comment
There was a problem hiding this comment.
This looks good to me.
For testing I used two approaches:
- created filters for various credentials
- Added a break point at the code point below to ensure the provider settings were saved
I've dropped a question inline as to whether we need a follow up ticket.
| * | ||
| * @return bool|mixed The filtered value of connect to service. | ||
| */ | ||
| $pre = apply_filters( 'classifai_' . self::ID . '_pre_connect_to_service', false ); |
There was a problem hiding this comment.
Should we add this filter to the other providers?
Unrelated to this ticket/PR, not a merge consideration.
There was a problem hiding this comment.
So this was added specifically to help mock API responses for AWS Polly in our E2E tests. All other requests we mock using the pre_http_request filter but because we're using the AWS SDK here, these requests don't filter through the WordPress HTTP API and thus that filter never fires.
That said, I'm not opposed to more standard hooks here though I think worth thinking through once we look to standardize on using the PHP AI Client to make our requests (where possible)
Description of the Change
We added the ability to easily filter Provider credentials in #1043. One comment on that PR flagged an issue with some Providers, where if you want to filter credentials you first need to add in dummy credentials in order for our pre-save checks to pass.
As a quick explanation, we had some checks in place where we wouldn't authenticate credentials if those credential fields were empty (no need to authenticate if we don't have an API key set, as an example). But in situations where you're overriding credentials, you shouldn't be forced to enter any credentials (dummy or not) in the admin first.
In investigating, found out that about half of our Providers worked fine but the others did not. In comparing those, the ones that work didn't check if credentials exist first before trying to authenticate and instead rely on the authentication check failing to alert the user that something is wrong. You could argue that's not the best user experience but it does more or less accomplish the same thing.
In order to keep this PR as simple as possible, I decided to follow that same approach on all the Providers that weren't working:
All Providers should now have an
authenticate_credentialsmethod (most already did but not all) and we use that method as one of the first checks when sanitizing settings. If that check fails, we show an error message but we still save all settings (including any credential fields, even if blank).How to test the Change
In particular, test the Providers listed above:
classifai_provider_credentials, overriding the credentials for the Providers you want to testChangelog Entry
Credits
Props @peterwilsoncc, @dkotter
Checklist: