Repository: uclapi/uclapi

Issue:
[Feature Request] user data - HTTP_MAIL
**Is your feature request related to a problem? Please describe.**

At the students union we are currently using Shibboleth and when a user successfully login into the website we have access to two different emails. One their account email such as ucxxxx@ucl.ac.uk, second a nicer email with their name like john.smith.17@ucl.ac.uk. With the current UCL API we can only retrieve the first.

**Describe the solution you'd like**

We would like to be able to access this second email with the UCL API.




Discussion:

Resolving patch:
diff --git a/backend/uclapi/oauth/views.py b/backend/uclapi/oauth/views.py
--- a/backend/uclapi/oauth/views.py
+++ b/backend/uclapi/oauth/views.py
@@ -488,7 +488,6 @@ def token(request):
 )
 def userdata(request, *args, **kwargs):
     token = kwargs['token']
-    print("Checking student status")
     try:
         get_student_by_upi(
             token.user.employee_id
@@ -507,7 +506,9 @@ def userdata(request, *args, **kwargs):
         "upi": token.user.employee_id,
         "scope_number": token.scope.scope_number,
         "is_student": is_student,
-        "ucl_groups": token.user.raw_intranet_groups.split(';')
+        "ucl_groups": token.user.raw_intranet_groups.split(';'),
+        "sn": token.user.sn,
+        "mail": token.user.mail
     }
 
     return PrettyJsonResponse(


Test patch:
diff --git a/backend/uclapi/oauth/tests.py b/backend/uclapi/oauth/tests.py
--- a/backend/uclapi/oauth/tests.py
+++ b/backend/uclapi/oauth/tests.py
@@ -7,7 +7,8 @@
 from django.core.serializers.json import DjangoJSONEncoder
 from django.db import transaction
 from django.test import Client, TestCase
-from rest_framework.test import APIRequestFactory
+from django_mock_queries.query import MockModel, MockSet
+from rest_framework.test import APIRequestFactory, APITestCase
 from django.core import signing
 from parameterized import parameterized
 
@@ -1189,3 +1190,78 @@ def test_deauthorise_no_token_for_app_and_user(self):
                 "token for this user, so no action was taken."
             )
         )
+
+
+class OAuthUserDataTestCase(APITestCase):
+    """Tests the /oauth/user/data endpoint"""
+    fake_student = MockSet(
+        MockModel(
+            qtype2='UPI'
+        )
+    )
+    studenta_objects = unittest.mock.patch(
+        'timetable.models.StudentsA.objects',
+        fake_student
+    )
+    fake_locks = MockSet(
+        MockModel(
+            a=True,
+            b=False
+        )
+    )
+    lock_objects = unittest.mock.patch(
+        'timetable.models.Lock.objects',
+        fake_locks
+    )
+
+    @classmethod
+    def setUpClass(cls):
+        super().setUpClass()
+        cls.user = User.objects.create(
+            cn="cn",
+            department="Dept of UCL API",
+            email="cryptic@ucl.ac.uk",
+            full_name="First Last",
+            given_name="First",
+            employee_id="upi",
+            raw_intranet_groups="group-all",
+            sn="Last",
+            mail="fname.lname.yr.20@ucl.ac.uk"
+        )
+        cls.dev = User.objects.create(email="test@ucl.ac.uk", cn="test", given_name="Test Test")
+        cls.app = App.objects.create(user=cls.dev, name="An App")
+        cls.oauth_scope = OAuthScope.objects.create()
+        cls.oauth_token = OAuthToken.objects.create(app=cls.app, user=cls.user, scope=cls.oauth_scope)
+
+    def setUp(self):
+        self.expected_user_data = {
+            "ok": True,
+            "cn": "cn",
+            "department": "Dept of UCL API",
+            "email": "cryptic@ucl.ac.uk",
+            "full_name": "First Last",
+            "given_name": "First",
+            "upi": "upi",
+            "scope_number": 0,
+            "ucl_groups": ["group-all"],
+            "sn": "Last",
+            "mail": "fname.lname.yr.20@ucl.ac.uk"
+        }
+
+    def test_userdata_non_student(self):
+        """Tests that userdata is correctly returned for a non-student"""
+        response = self.client.get(
+            '/oauth/user/data', {'token': self.oauth_token.token, 'client_secret': self.app.client_secret})
+        self.assertEqual(response.status_code, 200)
+        self.expected_user_data['is_student'] = False
+        self.assertJSONEqual(response.content.decode('utf-8'), self.expected_user_data)
+
+    @studenta_objects
+    @lock_objects
+    def test_userdata_student(self):
+        """Tests that userdata is correctly returned for a student"""
+        response = self.client.get(
+            '/oauth/user/data', {'token': self.oauth_token.token, 'client_secret': self.app.client_secret})
+        self.assertEqual(response.status_code, 200)
+        self.expected_user_data['is_student'] = True
+        self.assertJSONEqual(response.content.decode('utf-8'), self.expected_user_data)