-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add user serializer, enable it for v3/users/current, v3/users/retrieve and v3/users/update * update tests to account for new user serializer * update tests to account for new user serializer
- Loading branch information
Showing
5 changed files
with
116 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
SEED Platform (TM), Copyright (c) Alliance for Sustainable Energy, LLC, and other contributors. | ||
See also https://github.com/SEED-platform/seed/blob/main/LICENSE.md | ||
""" | ||
|
||
from django_otp import devices_for_user | ||
from django_otp.plugins.otp_email.models import EmailDevice | ||
from django_otp.plugins.otp_totp.models import TOTPDevice | ||
from rest_framework import serializers | ||
|
||
from seed.landing.models import SEEDUser as User | ||
from seed.views.main import _get_default_org as get_default_org_for_user | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ("first_name", "last_name", "email", "username", "api_key", "is_superuser", "id", "pk") | ||
|
||
def to_representation(self, instance): | ||
ret = super().to_representation(instance) | ||
|
||
two_factor_devices = list(devices_for_user(instance)) | ||
if two_factor_devices and isinstance(two_factor_devices[0], EmailDevice): | ||
ret["two_factor_method"] = "email" | ||
elif two_factor_devices and isinstance(two_factor_devices[0], TOTPDevice): | ||
ret["two_factor_method"] = "token" | ||
else: | ||
ret["two_factor_method"] = "disabled" | ||
|
||
additional_fields = dict( | ||
list( | ||
zip( | ||
("org_id", "org_name", "org_role", "ali_name", "ali_id", "is_ali_root", "is_ali_leaf"), | ||
get_default_org_for_user(instance), | ||
) | ||
) | ||
) | ||
for k, v in additional_fields.items(): | ||
ret[k] = v | ||
|
||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
from seed.utils.organizations import create_organization | ||
from seed.utils.users import get_js_role, get_role_from_js | ||
from seed.views.main import _get_default_org | ||
from seed.views.main import _get_default_org as get_default_org_for_user | ||
from seed.views.v3.organizations import _dict_org | ||
|
||
|
||
|
@@ -575,27 +576,70 @@ def test_update_user(self): | |
json.dumps(user_data), | ||
content_type="application/json", | ||
) | ||
self.assertEqual( | ||
json.loads(resp.content), {"status": "success", "api_key": "", "email": "[email protected]", "first_name": "bob", "last_name": "d"} | ||
) | ||
( | ||
initial_org_id, | ||
initial_org_name, | ||
initial_org_user_role, | ||
access_level_instance_name, | ||
access_level_instance_id, | ||
is_ali_root, | ||
is_ali_leaf, | ||
) = get_default_org_for_user(self.user) | ||
profile = { | ||
"username": "[email protected]", | ||
"email": "[email protected]", | ||
"first_name": "bob", | ||
"last_name": "d", | ||
"ali_id": access_level_instance_id, | ||
"ali_name": access_level_instance_name, | ||
"api_key": "", | ||
"is_ali_root": is_ali_root, | ||
"is_ali_leaf": is_ali_leaf, | ||
"org_id": initial_org_id, | ||
"org_name": initial_org_name, | ||
"org_role": initial_org_user_role, | ||
"pk": self.user.pk, | ||
"id": self.user.pk, | ||
"two_factor_method": "disabled", | ||
"is_superuser": self.user.is_superuser, | ||
} | ||
self.assertEqual(json.loads(resp.content), profile) | ||
|
||
def test_get_user_profile(self): | ||
"""test for get_user_profile""" | ||
resp = self.client.get( | ||
reverse_lazy("api:v3:user-detail", args=[self.user.pk]), | ||
content_type="application/json", | ||
) | ||
self.assertEqual( | ||
json.loads(resp.content), | ||
{ | ||
"status": "success", | ||
"api_key": "", | ||
"email": "[email protected]", | ||
"first_name": "Johnny", | ||
"last_name": "Energy", | ||
"two_factor_method": "disabled", | ||
}, | ||
) | ||
( | ||
initial_org_id, | ||
initial_org_name, | ||
initial_org_user_role, | ||
access_level_instance_name, | ||
access_level_instance_id, | ||
is_ali_root, | ||
is_ali_leaf, | ||
) = get_default_org_for_user(self.user) | ||
profile = { | ||
"username": "[email protected]", | ||
"email": "[email protected]", | ||
"first_name": "Johnny", | ||
"last_name": "Energy", | ||
"ali_id": access_level_instance_id, | ||
"ali_name": access_level_instance_name, | ||
"api_key": "", | ||
"is_ali_root": is_ali_root, | ||
"is_ali_leaf": is_ali_leaf, | ||
"org_id": initial_org_id, | ||
"org_name": initial_org_name, | ||
"org_role": initial_org_user_role, | ||
"pk": self.user.pk, | ||
"id": self.user.pk, | ||
"two_factor_method": "disabled", | ||
"is_superuser": self.user.is_superuser, | ||
} | ||
|
||
self.assertEqual(json.loads(resp.content), profile) | ||
resp = self.client.post( | ||
reverse_lazy("api:v3:user-generate-api-key", args=[self.user.pk]), | ||
content_type="application/json", | ||
|
@@ -604,17 +648,8 @@ def test_get_user_profile(self): | |
reverse_lazy("api:v3:user-detail", args=[self.user.pk]), | ||
content_type="application/json", | ||
) | ||
self.assertEqual( | ||
json.loads(resp.content), | ||
{ | ||
"status": "success", | ||
"api_key": User.objects.get(pk=self.user.pk).api_key, | ||
"email": "[email protected]", | ||
"first_name": "Johnny", | ||
"last_name": "Energy", | ||
"two_factor_method": "disabled", | ||
}, | ||
) | ||
profile["api_key"] = User.objects.get(pk=self.user.pk).api_key | ||
self.assertEqual(json.loads(resp.content), profile) | ||
|
||
def test_generate_api_key(self): | ||
"""test for generate_api_key | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters