Repository: UTNkar/moore

Issue:
Check registry on account creation
The registry is currently not checked for membership status when an account is created.


Discussion:

Resolving patch:
diff --git a/website/members/cron.py b/website/members/cron.py
--- a/website/members/cron.py
+++ b/website/members/cron.py
@@ -23,7 +23,9 @@ def update_membership_status():
 
     for member in Member.objects.all():
         if member.person_number().replace('-', '') in data:
-            member.update(data='member')
+            member.update_status(data='member')
         else:
             member.update_status(data='nonmember')
-        member.save()
+        Member.objects.filter(pk=member.pk).update(
+            status=member.status, status_changed=member.status_changed
+        )
diff --git a/website/members/models.py b/website/members/models.py
--- a/website/members/models.py
+++ b/website/members/models.py
@@ -169,17 +169,21 @@ def person_number(self) -> str:
 
     def update_status(self, data=None):
         if data is None:
-            r = requests.get(
-                'https://register.utn.se/api.php',
-                auth=HTTPDigestAuth(settings.MEMBERSHIP_API_USER,
-                                    settings.MEMBERSHIP_API_PASSWORD),
-                params={
-                    'action': 'check',
-                    'person_number': self.person_number().replace('-', '')
-                },
-            )
+            if self.person_number() == '':
+                return
             try:
+                r = requests.get(
+                    'https://register.utn.se/api.php',
+                    auth=HTTPDigestAuth(settings.MEMBERSHIP_API_USER,
+                                        settings.MEMBERSHIP_API_PASSWORD),
+                    params={
+                        'action': 'check',
+                        'person_number': self.person_number().replace('-', '')
+                    },
+                )
                 data = r.json().get('status')
+            except requests.exceptions.ConnectionError:
+                data = 'unknown'
             except ValueError:
                 return
 
diff --git a/website/members/signals.py b/website/members/signals.py
--- a/website/members/signals.py
+++ b/website/members/signals.py
@@ -34,5 +34,6 @@ def send_confirmation_email(sender, email, user=None, **kwargs):
 
 @receiver(pre_save, sender=Member, dispatch_uid='member_check_membership')
 def check_membership(sender, instance, **kwargs):
-    if timezone.now() - instance.status_changed > datetime.timedelta(1):
+    if timezone.now() - instance.status_changed > datetime.timedelta(1)\
+            or instance.status == 'unknown':
         instance.update_status()


Test patch:
diff --git a/website/members/tests.py b/website/members/tests.py
--- a/website/members/tests.py
+++ b/website/members/tests.py
@@ -1,9 +1,13 @@
+import os
+import unittest
+
 from django.core import mail
 from django.test import TestCase
-from datetime import datetime
+import datetime
 
 from django.urls import reverse
 
+from members import cron
 from members.models import Member, StudyProgram
 
 
@@ -17,7 +21,7 @@ def setUp(self):
         self.assertEqual(1, Member.objects.count())
 
     def test_print_person_number(self):
-        self.member.birthday = datetime.strptime('03/01/1929', '%d/%m/%Y')
+        self.member.birthday = datetime.date(1929, 1, 3)
         self.member.person_number_ext = '1234'
         self.assertEqual(
             '19290103-1234', self.member.person_number(),
@@ -59,7 +63,7 @@ def setUp(self):
             first_name='Gordon',
             last_name='Moore',
             email='g.moore@localhost',
-            birthday=datetime.strptime('03/01/1929', '%d/%m/%Y'),
+            birthday=datetime.date(1929, 1, 3),
             person_number_ext='1234',
             phone_number='+461234567890',
             registration_year='1946',
@@ -117,6 +121,10 @@ def test_change_contact(self):
         member = Member.objects.get(username='moore')
         self.assertEqual(member.first_name, data['first_name'])
         self.assertEqual(member.last_name, data['last_name'])
+        self.assertEqual(
+            member.birthday, datetime.date(1934, 1, 7)
+        )
+        self.assertEqual(member.person_number_ext, '9876')
         self.assertEqual(member.person_number(), data['person_number'])
         self.assertEqual(member.phone_number, data['phone_number'])
 
@@ -223,3 +231,68 @@ def test_invalid_confirm(self):
             response, 'The provided confirmation token was invalid.',
         )
         self.assertNotIn(self.member.email, self.member.get_confirmed_emails())
+
+
+class MembershipAPITest(TestCase):
+    def setUp(self):
+        self.moore = Member.objects.create(
+            username='moore',
+            birthday=datetime.date(1929, 1, 3),
+            person_number_ext='1234',
+        )
+        self.flash = Member.objects.create(
+            username='flash',
+            birthday=datetime.date(1934, 1, 7),
+            person_number_ext='9876',
+        )
+
+    @unittest.skipIf(os.environ.get('TRAVIS', 'false') == 'true',
+                     'Skipping this test on Travis CI.')
+    def test_signal(self):
+        self.assertEqual(self.moore.status, 'member')
+        self.assertEqual(self.flash.status, 'nonmember')
+
+    def test_method(self):
+        self.moore.status = 'unknown'
+        self.moore.update_status('member')
+        self.assertEqual(self.moore.status, 'member')
+
+        self.moore.status = 'unknown'
+        self.moore.update_status('nonmember')
+        self.assertEqual(self.moore.status, 'nonmember')
+
+        self.moore.status = 'member'
+        self.moore.update_status('member')
+        self.assertEqual(self.moore.status, 'member')
+
+        self.moore.status = 'member'
+        self.moore.update_status('nonmember')
+        self.assertEqual(self.moore.status, 'alumnus')
+
+        self.moore.status = 'alumnus'
+        self.moore.update_status('member')
+        self.assertEqual(self.moore.status, 'member')
+
+        self.moore.status = 'alumnus'
+        self.moore.update_status('nonmember')
+        self.assertEqual(self.moore.status, 'alumnus')
+
+        self.moore.status = 'nonmember'
+        self.moore.update_status('member')
+        self.assertEqual(self.moore.status, 'member')
+
+        self.moore.status = 'nonmember'
+        self.moore.update_status('nonmember')
+        self.assertEqual(self.moore.status, 'nonmember')
+
+    @unittest.skipIf(os.environ.get('TRAVIS', 'false') == 'true',
+                     'Skipping this test on Travis CI.')
+    def test_cron(self):
+        Member.objects.filter(pk=self.moore.pk).update(status='unknown')
+        Member.objects.filter(pk=self.flash.pk).update(status='unknown')
+
+        cron.update_membership_status()
+        self.moore.refresh_from_db()
+        self.assertEqual(self.moore.status, 'member')
+        self.flash.refresh_from_db()
+        self.assertEqual(self.flash.status, 'nonmember')