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

Add validation to catch blank ephemeral keys in CustomerConfiguration #2517

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

sfriedman-stripe
Copy link
Collaborator

Summary

Added an assertion to catch blank ephemeral keys when a user instantiates a CustomerConfiguration

Motivation

We currently don't validate that an ephemeral key is non-empty when a
user includes it in the CustomerConfiguration. This ends up giving
users a pretty obscure error in the API response, and is difficult to
debug today.

Including this validation will catch this issue early and provide users
a better debugging experience.

As part of this change, I also added a dependency on CwlPreconditionTesting.

CwlPreconditionTesting allows us to unit test traps (i.e. fatalError,
assert, precondition). We currently don't have any way to test these in
the repo, so engineers simply don't test our assertions. This will
improve the overall state of our unit tests by encouraging testing of
more code paths.

Testing

  • Added a unit test to test the assertion
  • Manually set the ephemeral key to empty in the PaymentSheet playground, and verified that the assertion triggered

Changelog

  • [Added] Improved validation for ephemeral keys

@sfriedman-stripe sfriedman-stripe force-pushed the sfriedman/validate-ephemeral-key branch 2 times, most recently from 2bd7d0d to debae23 Compare April 27, 2023 04:23
@sfriedman-stripe sfriedman-stripe marked this pull request as ready for review April 27, 2023 04:28
@sfriedman-stripe sfriedman-stripe requested review from a team as code owners April 27, 2023 04:28
yuki-stripe
yuki-stripe previously approved these changes Apr 27, 2023
Copy link
Collaborator

@yuki-stripe yuki-stripe left a comment

Choose a reason for hiding this comment

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

LG2M, @eurias-stripe can you review the Tuist changes?

Comment on lines 14 to 19
func test_customerConfigurationInit_assertsWhenEphemeralKeyIsBlank() throws {
let exception = catchBadInstruction {
_ = PaymentSheet.CustomerConfiguration(id: "foo", ephemeralKeySecret: "")
}

XCTAssertNotNil(exception)
Copy link
Collaborator

Choose a reason for hiding this comment

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

🤩

sfriedman-stripe and others added 5 commits April 27, 2023 16:10
CwlPreconditionTesting allows us to unit test traps (i.e. fatalError,
assert, precondition). We currently don't have any way to test these in
the repo, so engineers simply don't test our assertions. This will
improve the overall state of our unit tests by encouraging testing of
more code paths.

Further reading on how CwlPreconditionTesting works: https://www.cocoawithlove.com/blog/2016/02/02/partial-functions-part-two-catching-precondition-failures.html
We currently don't validate that an ephemeral key is non-empty when a
user includes it in the `CustomerConfiguration`. This ends up giving
users a pretty obscure error in the API response, and is difficult to
debug today.

Including this validation will catch this issue early and provide users
a better debugging experience.
Co-authored-by: Yuki <[email protected]>
@sfriedman-stripe
Copy link
Collaborator Author

@yuki-stripe Had to fix a couple tests that I broke. Could you PTAL and make sure I'm not breaking the expected behavior for those tests?

yuki-stripe
yuki-stripe previously approved these changes Apr 27, 2023
eurias-stripe
eurias-stripe previously approved these changes Apr 28, 2023
Copy link
Contributor

@eurias-stripe eurias-stripe left a comment

Choose a reason for hiding this comment

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

Finally a way to test assertions!

Tuist/ProjectDescriptionHelpers/Tuist+Stripe.swift Outdated Show resolved Hide resolved
CHANGELOG.md Outdated
@@ -2,6 +2,9 @@
### Payments
* [Fixed] STPPaymentHandler.handleNextAction allows payment methods that are delayed or require further customer action like like SEPA Debit or OXXO.

### PaymentSheet
* [Added] Improved validation for ephemeral keys
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need a changelog entry? This is not a user facing change.

In case we do need the entry, tiny nit:

Suggested change
* [Added] Improved validation for ephemeral keys
* [Added] Improved validation for ephemeral keys.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure what we normally do/do not include in the changelog. It does improve user-facing error messages that users have complained about in the past, so sorta-kinda user facing?

Copy link
Contributor

Choose a reason for hiding this comment

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

I can go either way with this one. If users have specifically mentioned this then it is worth calling it out.

Co-authored-by: eurias-stripe <[email protected]>
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.

None yet

3 participants