test: improve unit tests coverage for AccountService (Infection) - #8142
test: improve unit tests coverage for AccountService (Infection)#8142diogopaz wants to merge 1 commit into
Conversation
|
Thanks for opening your first pull request in this repository! ✌️ |
|
@diogopaz the problems in the unit tests aren't caused by your PR. I'll fix this in a separate PR. But have linter issues that you need to solve: https://github.com/LibreSign/libresign/actions/runs/33325571740/job/100018743935?pr=8142 |
|
Once you fix the linter issue and push the changes, the unit tests will work fine. |
Signed-off-by: Diogo Paz <diogoachiles@gmail.com>
6e439af to
e0ecb44
Compare
|
Hi @vitormattos, Sorry for the delay! I've fixed the linter issues (missing imports, multiline array trailing commas, and formatting) and updated the commit. Thanks for the review! |
vitormattos
left a comment
There was a problem hiding this comment.
I checked the PHPUnit failures.
There are two different groups of problems:
-
The
IUserFoldererrors inValidateHelperTestandSignFileServiceTestare not caused by this PR. They come from tests outside the changed file and appear in the Nextcloud master jobs. We should handle this compatibility change separately. -
The failures in
AccountServiceTestare from the tests added in this PR and need to be fixed here before merge. I added inline comments for the main cases.
So you do not need to fix the unrelated IUserFolder failures as part of this PR.
| $this->assertSame($fileToSign, $result['fileToSign']); | ||
| } | ||
|
|
||
| public function testGetFileByUuidUsesCache(): void { |
There was a problem hiding this comment.
I think we should test one more case before making this cache behavior part of the expected behavior.
AccountService caches signRequest, fileData and fileToSign, but the cache does not store which UUID the data belongs to.
This test calls getFileByUuid() twice with the same UUID, so it does not detect what happens with different UUIDs.
Could we also test something like:
$service->getFileByUuid('uuid-a');
$result = $service->getFileByUuid('uuid-b');and verify that the result belongs to uuid-b?
If this fails with the current implementation, we found a bug and should fix it instead of protecting this behavior with the mutation test. This is also tracked in #8328.
|
|
||
| $file1 = $this->createMock(File::class); | ||
| $file2 = $this->createMock(File::class); | ||
| $this->folderService->expects($this->exactly(2)) |
There was a problem hiding this comment.
This expectation does not match the current behavior.
These elements only contain elementId and starred. They do not contain file, so updateFileOfVisibleElement() returns before calling folderService->getFileByNodeId().
This is also what the failing PHPUnit job shows: the method was expected 2 times but was called 0 times.
Could you remove this filesystem setup and keep this test focused on the starred update behavior?
| $this->getService()->saveVisibleElements($elements, 'session123', $user); | ||
| } | ||
|
|
||
| public function testSaveVisibleElementUpdateFileAndStarred(): void { |
There was a problem hiding this comment.
There are a few related failures in the new visible-element tests that should be fixed before merge.
In this test, saveVisibleElement() calls both updateFileOfVisibleElement() and updateDataOfVisibleElement(). Each method calls userElementMapper->findOne(), so expecting findOne() only once makes the test fail.
There are two similar problems below:
getStarred()returns a boolean here, soassertSame(1, ...)fails withtrue. Please assert the actual type/value returned by the entity.- The invalid URL tests call
saveVisibleElement()without preparing the folder needed by the session path. The code fails withnewFile() on nullbefore it reaches the URL validation that the test wants to check.
Please adjust the setup so each test reaches the behavior it is meant to test. The invalid URL / MIME / empty body cases may also fit well in one data provider because they test the same rule with different inputs.
| $this->assertSame($fileToSign, $result['fileToSign']); | ||
| } | ||
|
|
||
| public function testCreateToSignWithSuccessAndEmailAndSignPassword(): void { |
There was a problem hiding this comment.
This test also shows why #8328 is useful: testing one account operation requires setup for account creation, identification, email and certificate handling.
I would keep the architecture refactor outside this PR, but we should avoid adding more test complexity only to improve the Infection score.
|
@diogopaz, can you rebase your branch on top of I fixed part of the failing tests in this PR: |
|
Heads-up after #8366 was merged (this morning): After rebasing, five expectations in this PR will reference a property that no longer exists. The one-to-one mapping against current
The production calls are Sorry for the extra step — happy to review the rebase if useful. |
Part of #8053
📝 Summary
This PR adds extensive PHPUnit test coverage to
AccountServiceTestto eliminate escaped mutants and improve the mutation testing score with Infection, as part of #8053.🧪 How to test
Run the focused unit test suite for AccountService:
Optionally run Infection on this specific pair to verify mutant coverage:
⚙️ API / Back‑end changes
validateCreateToSign,getFileByUuid,createToSign,saveVisibleElement(s),uploadPfx,updatePfxPassword,readPfxData, and config fallbacks.✅ Checklist
🤖 AI (if applicable)