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

DXCDT-349: Relax url fields validation to allow to be set to empty #451

Merged
merged 2 commits into from
Jan 30, 2023

Conversation

sergiught
Copy link
Contributor

@sergiught sergiught commented Jan 30, 2023

🔧 Changes

In this PR we are changing the validation for url fields to only allow HTTPS and empty strings.

📚 References

🔬 Testing

Tests were updated to check for empty strings as well.

📝 Checklist

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

@sergiught sergiught changed the title DXCDT-349: Allow url fields to be set to empty DXCDT-349: Relax url fields validation to allow to be set to empty Jan 30, 2023
@sergiught sergiught force-pushed the issue-429/DXCDT-349-fix-empty-urls branch from a497558 to cdfeee0 Compare January 30, 2023 09:40
Optional: true,
Computed: true,
ValidateFunc: validation.All(
internalValidation.IsURLWithNoFragment,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

After some investigating in the api/v2 schemas it appears that we do allow urls with fragments. They just have to be https or empty.

Copy link
Contributor

Choose a reason for hiding this comment

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

If this type of issue is frequent, we can consider in the future checking the validations directly against the ones in API2 code.

t.Run(fmt.Sprintf("test case #%d", i), func(t *testing.T) {
var errorsAsString []string
_, actualErrors := IsURLWithHTTPSorEmptyString(testCase.inputURL, "theTestURL")
for _, actualError := range actualErrors {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need here something we can compare as all the errors returned are pointers so we're transforming everything in a slice of strings.

@sergiught sergiught marked this pull request as ready for review January 30, 2023 09:45
@sergiught sergiught requested a review from a team as a code owner January 30, 2023 09:45
@codecov-commenter
Copy link

Codecov Report

Base: 87.16% // Head: 87.31% // Increases project coverage by +0.14% 🎉

Coverage data is based on head (cdfeee0) compared to base (24917da).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #451      +/-   ##
==========================================
+ Coverage   87.16%   87.31%   +0.14%     
==========================================
  Files          42       42              
  Lines        9264     9245      -19     
==========================================
- Hits         8075     8072       -3     
+ Misses        914      902      -12     
+ Partials      275      271       -4     
Impacted Files Coverage Δ
internal/provider/resource_auth0_client.go 97.17% <100.00%> (ø)
internal/provider/resource_auth0_guardian.go 93.73% <100.00%> (ø)
internal/provider/resource_auth0_tenant.go 97.72% <100.00%> (-0.02%) ⬇️
internal/validation/validation.go 100.00% <100.00%> (+69.56%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.


if u.Fragment != "" {
errors = append(errors, fmt.Errorf("expected %q to have a url with an empty fragment. %s", k, v))
func IsURLWithHTTPSorEmptyString(i interface{}, s string) ([]string, []error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be IsURLWithHTTPSOrEmptyString?

Copy link
Contributor Author

@sergiught sergiught Jan 30, 2023

Choose a reason for hiding this comment

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

IMO no, as the "O" is not part of "HTTPS" and it also reads easier.

A similar example was set in https://github.com/hashicorp/terraform-plugin-sdk/blob/8ffdd1ea219bc0bbb64c0e7a87bf081ad40ca3ee/helper/validation/web.go#L17.

func IsURLWithHTTPSorEmptyString(i interface{}, s string) ([]string, []error) {
_, errors := validation.IsURLWithHTTPS(i, s)
for _, err := range errors {
if !strings.Contains(err.Error(), "url to not be empty") {
Copy link
Contributor

@Widcket Widcket Jan 30, 2023

Choose a reason for hiding this comment

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

Should we check this some other way, instead of looking for a hardcoded error string? Maybe checking for empty string before the IsURLWithHTTPS validation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering the same thing. Although that seems like too easy of a solution so wondering what we're missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to avoid duplicating logic from within the IsURLWithHTTPS, specifically checking for the string type and then returning nil on an empty string. But I can skip using IsURLWithHTTPS and implement our own logic that's a tiny itsy bitsy bit more performant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we keep IsURLWithHTTPS and do an empty string check before? No need to remove it. Note that it's not about performance, it's about depending on the error message string

Copy link
Contributor

Choose a reason for hiding this comment

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

Though it's not a blocker for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this in 1007111 (#451) without making use of the IsURLWithHTTPS func from the SDK. I personally didn't like duplicating the type check and returning immediately if the url is empty as there was another check inside the sdk func for empty urls.

@sergiught sergiught force-pushed the issue-429/DXCDT-349-fix-empty-urls branch from 11aacd7 to 1007111 Compare January 30, 2023 14:51
@sergiught sergiught requested review from Widcket and willvedd January 30, 2023 14:55
@sergiught sergiught merged commit e1660f4 into main Jan 30, 2023
@sergiught sergiught deleted the issue-429/DXCDT-349-fix-empty-urls branch January 30, 2023 16:55
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.

4 participants