Repository: learningequality/kolibri

Issue:
Handle updated exception signature from super validate method.
## Summary
* A recent change to the handling of authentication errors in the `PeerImportSingleSyncValidator` changed the kind of error thrown by the validate function: https://github.com/learningequality/kolibri/pull/11704
* This was not accounted for in the `MergeUserValidator` which inherits from and extends this validate method
* Updates the method to handle the ValidationError instead

## References
Fixes https://github.com/learningequality/kolibri/issues/11768

## Reviewer guidance
Follow the steps outlined in the issue - attempt to merge a user into a facility, when that user does not yet exist in that facility.

----

## Testing checklist

- [ ] Contributor has fully tested the PR manually
- [ ] If there are any front-end changes, before/after screenshots are included
- [ ] Critical user journeys are covered by Gherkin stories
- [ ] Critical and brittle code paths are covered by unit tests


## PR process

- [ ] PR has the correct target branch and milestone
- [ ] PR has 'needs review' or 'work-in-progress' label
- [ ] If PR is ready for review, a reviewer has been added. (Don't use 'Assignees')
- [ ] If this is an important user-facing change, PR or related issue has a 'changelog' label
- [ ] If this includes an internal dependency change, a link to the diff is provided

## Reviewer checklist

- Automated test coverage is satisfactory
- PR is fully functional
- PR has been tested for [accessibility regressions](http://kolibri-dev.readthedocs.io/en/develop/manual_testing.html#accessibility-a11y-testing)
- External dependency files were updated if necessary (`yarn` and `pip`)
- Documentation is updated
- Contributor is in AUTHORS.md

Merging user account into account with same name gives 401 when authenticating
While regression testing the `Change learning facility` path as a user created through `On my own` I noticed that the option to change the facility while keeping the same username is not working - getting a `http://127.0.0.1:8080/api/tasks/tasks/ 400 (Bad request)` error. Merging the account with an existing one is still functioning correctly though.

https://github.com/learningequality/kolibri/assets/79847249/8a1956ff-b1ca-4c6f-8ad0-a7263b925b7f

[Logs.zip](https://github.com/learningequality/kolibri/files/14024534/Logs.zip)

_Originally posted by @pcenov in https://github.com/learningequality/kolibri/pull/11765#pullrequestreview-1838787654_
            


Discussion:
### [**Build Artifacts**](https://github.com/learningequality/kolibri/actions/runs/7629867285)
| Asset type | Download link |
|-|-|
| PEX file | [kolibri-0.16.0b11.dev0_git.9.g86cdf34d.pex](https://github.com/learningequality/kolibri/suites/20053035730/artifacts/1189546137) |
| Windows Installer (EXE) | [kolibri-0.16.0b11.dev0+git.9.g86cdf34d-unsigned.exe](https://github.com/learningequality/kolibri/suites/20053035730/artifacts/1189548435) |
| Debian Package | [kolibri_0.16.0b11.dev0+git.9.g86cdf34d-0ubuntu1_all.deb](https://github.com/learningequality/kolibri/suites/20053035730/artifacts/1189552995) |
| Mac Installer (DMG) | [kolibri-0.16.0b11.dev0+git.9.g86cdf34d-0.3.0.dmg](https://github.com/learningequality/kolibri/suites/20053035730/artifacts/1189561109) |
| Android Package (APK) | [kolibri-0.16.0b11.dev0+git.9.g86cdf34d-0.1.0-debug.apk](https://github.com/learningequality/kolibri/suites/20053035730/artifacts/1189600007) |
| TAR file | [kolibri-0.16.0b11.dev0+git.9.g86cdf34d.tar.gz](https://github.com/learningequality/kolibri/suites/20053035730/artifacts/1189542033) |
| WHL file | [kolibri-0.16.0b11.dev0+git.9.g86cdf34d-py2.py3-none-any.whl](https://github.com/learningequality/kolibri/suites/20053035730/artifacts/1189541765) |


Resolving patch:
diff --git a/kolibri/plugins/user_profile/tasks.py b/kolibri/plugins/user_profile/tasks.py
--- a/kolibri/plugins/user_profile/tasks.py
+++ b/kolibri/plugins/user_profile/tasks.py
@@ -42,10 +42,15 @@ def validate(self, data):
         try:
             job_data = super(MergeUserValidator, self).validate(data)
         except ValidationError as e:
-            if e.detail.code == error_constants.AUTHENTICATION_FAILED:
-                self.create_remote_user(data)
-                job_data = super(MergeUserValidator, self).validate(data)
+            details = e.detail if isinstance(e.detail, list) else [e.detail]
+            for detail in details:
+                # If any of the errors are authentication related, then we need to create the user
+                if detail.code == error_constants.AUTHENTICATION_FAILED:
+                    self.create_remote_user(data)
+                    job_data = super(MergeUserValidator, self).validate(data)
+                    break
             else:
+                # If we didn't break out of the loop, then we need to raise the original error
                 raise
 
         job_data["kwargs"]["local_user_id"] = data["local_user_id"].id


Test patch: