Repository: learningequality/kolibri

Issue:
Impossible to create a learner account on a second (imported) facility?
## Observed behavior
I managed to replicate this on 2 separate Android devices. My Windows 7 server device has 2 facilities, one created during the server setup (**Win7new**), and the second one imported from another VM (**imported-from-Win11**). This second one has several classes, and I have been using it exclusively during this past week. 

1. At first I tried to create a new learner account from the Huawei Android 8 tablet on the second facility, and got an error (_Whoops!_). I retried and the field validation was saying ``Username already exists``, which did not make sense, since I searched users on that second facility on the server VM and the username I was trying to create was not there. I moved on, imported another user and went on testing something else.
2. Some times after I was setting up another LoD (on the Motorola phone), tried again to create (not import) a new user on the second facility, and got the same message that the ``Username already exists``. After the initial confusion I had a hunch and went to check the first facility (the one I never really used for class interactions during this testing), and lo and behold, the user I was trying to create did exist there, probably from the above setup on Huawei tablet. And this is not my fat finger miss-touching the wrong radio button during the facility selection, it actually seems that the option to create a new user is somehow _bound_ to the first facility on the device, and would not _obey_ the user's selection of the second facility.

LoD | Win7 facility users | imported-from-Win11 facility users
-- | -- | --
![2024_02_02_00 48 43](https://github.com/learningequality/kolibri/assets/1457929/12ff9470-914b-4d3a-aed5-fe5720075a70) | ![2024-02-02_00-49-04](https://github.com/learningequality/kolibri/assets/1457929/3c01cd95-e57b-4ad8-a390-37cdcbab757e) | ![2024-02-02_00-49-46](https://github.com/learningequality/kolibri/assets/1457929/0c81133a-9a13-46a6-88dc-58e1a2776cdb)

## Errors and logs
[Windows 7 home folder](https://drive.google.com/file/d/1QaHEZON_yL3hnLRsW2PEURY76MVEL9JT/view?usp=drive_link) (without content)

## Expected behavior
User needs to be able to select the facility they need to create their new learner account.

## User-facing consequences
Impossible to create a new learner account on a desired facility.

## Steps to reproduce
You will need a server with 2 facilities, try setting up an LoD by creating a new account on the second one. 

## Context

 * Kolibri version: 0.16b13
 * Operating system: Windows 7

cc @pcenov to try to replicate


Discussion:
Yes I was able to replicate this issue. A learner attempting to create an account on the imported facility is getting the following error and is not able to proceed:

```
      "id": "AUTHENTICATION_FAILED",
      "metadata": {
        "field": "non_field_errors",
        "message": "401 Client Error: Unauthorized for url: http://192.168.100.50:8080/api/public/facilityuser/?facility_id=af8e235a4458affd11c92980da3d648d"
```
When I go to Facility > Users I can see that the user is actually created but in the main facility, and not in the imported facility.

https://github.com/learningequality/kolibri/assets/79847249/7b5f3da1-f716-4a63-8f74-5e7db34e7c3b

Resolving patch:
diff --git a/kolibri/plugins/setup_wizard/api.py b/kolibri/plugins/setup_wizard/api.py
--- a/kolibri/plugins/setup_wizard/api.py
+++ b/kolibri/plugins/setup_wizard/api.py
@@ -9,6 +9,7 @@
 from rest_framework.response import Response
 from rest_framework.viewsets import ViewSet
 
+from kolibri.core.auth.backends import FACILITY_CREDENTIAL_KEY
 from kolibri.core.auth.constants import user_kinds
 from kolibri.core.auth.models import Facility
 from kolibri.core.auth.models import FacilityUser
@@ -51,7 +52,9 @@ def createuseronremote(self, request):
         url = "{}{}".format(baseurl, api_url)
 
         payload = {
-            "facility_id": facility_id,
+            # N.B. facility is keyed by facility not facility_id on the signup
+            # viewset serializer.
+            FACILITY_CREDENTIAL_KEY: facility_id,
             "username": username,
             "password": password,
             "full_name": full_name,


Test patch: