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

Re-enabled updating encrypted custom fields via API [sc-41465] #14602

Merged
merged 15 commits into from Apr 23, 2024

Conversation

uberbrady
Copy link
Collaborator

Some recent tweaks to the API code made it so that updates to encrypted custom fields were not correctly being encrypted, and, thus, not decrypted either.

This fixes that, and adds new tests to cover those possibilities.

Additionally, we decided to add some new parts - such as when you try to update a custom field, but fail, because you don't have "admin" permissions. Now we still allow the request to succeed, but add a warning in the message. Tests now cover this possibility.

The one weird thing here is that there were TWO return statements in the API - one that, obviously, fired. And one that was being ignored. I decided to just simply delete the second one, since it was not actually being run, ever.

I think there are a lot of inefficiencies with how I've done the testing, and will probably tap @marcusmoore for some help on more Laravel-esque ways of setting those test factories up in a bit of a less-janky fashion going forward. But I'd still go with this for now, as it seems like it's more in the right direction.

Copy link

what-the-diff bot commented Apr 15, 2024

PR Summary

  • Enhancement in Asset Management

    • Modified the AssetsController.php to better manage updating custom fields, including those that are encrypted. The system now sends a response if there are difficulties updating these encrypted fields.
  • Creation of Encrypted Asset Components

    • Added new methods to AssetFactory.php, AssetModelFactory.php, and CustomFieldsetFactory.php to facilitate the creation of asset components with encrypted custom fields.
  • Inclusion of Warning Messages

    • Included a new warning message encrypted_warning in message.php that triggers during certain conditions.
  • Improved Testing

    • An additional test method testEncryptedCustomField() was included in AssetStoreTest.php to validate the updating of an encrypted custom field. The test ensures only admin users can make updates while users without sufficient permissions cannot.
  • Enhanced Assertions

    • Embedded a new assertion macro assertMessagesAre() in CustomTestMacros.php to simplify the comparison of response messages with expected values.

Copy link
Collaborator

@marcusmoore marcusmoore left a comment

Choose a reason for hiding this comment

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

I added some general comments and I'm going to dive deeper into the factories.

tests/Feature/Api/Assets/AssetStoreTest.php Outdated Show resolved Hide resolved
tests/Feature/Api/Assets/AssetStoreTest.php Outdated Show resolved Hide resolved
database/factories/AssetModelFactory.php Outdated Show resolved Hide resolved
tests/Support/CustomTestMacros.php Show resolved Hide resolved
database/factories/AssetFactory.php Outdated Show resolved Hide resolved
public function encrypted_field()
{
return $this->state(function () {
$field = CustomField::factory()->testEncrypted()->create(); // TODO - having to create and then 'find' the thing you just created is WEIRD
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm going to see if I can help make these a little clearer.

@snipe snipe changed the title This re-enables updating encrypted custom fields via API [sc-41465] Re-enabled updating encrypted custom fields via API [sc-41465] Apr 16, 2024
@uberbrady
Copy link
Collaborator Author

@marcusmoore I think I made just about all of the choices you asked for - feel free to take another look, or if you want to 'fix' the factory thing or point me the right way to do that - that's fine too.

Also, if you think we should merge this as-is and do a Shortcut writeup for the improvements, we can do that too.

@marcusmoore
Copy link
Collaborator

@uberbrady I'd rather have the factories tightened up before merging. I'll work on that.

I'll also see what's up with the MySQL test failures.

@marcusmoore
Copy link
Collaborator

@uberbrady I totally forgot I've been down this path with testing custom fields before. MySQL tests are failing because the creation of a custom field runs a migration and Laravel's tests are being run in a transaction which doesn't like that...

We could do some toggling where tests for custom fields are only run on the sqlite driver but I really don’t want to segment our tests like that.

$response = $this->actingAsForApi($normal_user)
->patchJson(route('api.assets.update', $asset->id), [
$field->db_column_name() => 'Some Other Value Entirely!'
->postJson(route('api.assets.store'), [
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This Patch -> Post thing, is that important? Or is it pretty much the same thing? Or are we talking about, maybe, two different things - updating an existing thing versus making a new thing?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or are we talking about, maybe, two different things - updating an existing thing versus making a new thing?

Yeah that's pretty much it. Our docs (and http standards I think) say POST for creating a new thing and PATCH for partially updating a thing. Side-note: we also support PUT for updating a thing fully (meaning all properties are present in the request).

@snipe
Copy link
Owner

snipe commented Apr 18, 2024

Tests seem to be failing on this @marcusmoore @uberbrady

@marcusmoore
Copy link
Collaborator

@snipe Yeah...I don't think we'll be able to get around that unfortunately (see my last comment) 😞

I hate to say it but it's probably best to remove the test cases from this PR 🙁

@uberbrady
Copy link
Collaborator Author

We talked internally about possible ways of not pulling the tests entirely, and the change that I pushed up later adds some code to mark a test as 'incomplete' if the test database driver is 'mysql' - so you can still run the tests locally against sqlite, but they won't completely explode on a GH action in a PR on the 'server side'.

While not ideal (you don't want to ever have to say "if this driver is mysql..." in a test), it's at least half-there, and maybe at some point in the future we can get all-there.

@marcusmoore was able to clean up my fixes into something a little more systemic, which was good!

@marcusmoore marcusmoore self-requested a review April 22, 2024 20:31
@marcusmoore
Copy link
Collaborator

Gonna give this another review pass. There is one todo I added that I might be able to easily handle.

@marcusmoore
Copy link
Collaborator

Note: I think it'll be a good idea to review/merge #14458 and then apply this change on top of it. Looking at the changed lines in the controllers I think the merge conflicts will be easier to handle in this PR than that one.

Copy link
Collaborator

@marcusmoore marcusmoore left a comment

Choose a reason for hiding this comment

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

I removed my todo since it wasn't holding up this PR.

This looks good but remember that it'll probably be easier to get #14458 in first and then add this PR on top of it.

@snipe snipe merged commit bdd43b7 into snipe:develop Apr 23, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants