From cb90841cdda9e2d58f36d2a54dd1d8c08fec1c66 Mon Sep 17 00:00:00 2001 From: Rio Liu Date: Sat, 6 Nov 2021 21:32:15 -0400 Subject: [PATCH] add pytest for expire member and fix issues it brought make create_user fixtures function scope add shadowExpre atttribute to IUser fix Term start month calulation --- ceo_common/interfaces/IUser.py | 1 + ceo_common/model/Term.py | 2 +- ceod/model/User.py | 10 +++++----- dev-requirements.txt | 1 + tests/ceod/api/test_members.py | 36 ++++++++++++++++++++++++++++++++-- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/ceo_common/interfaces/IUser.py b/ceo_common/interfaces/IUser.py index f288a43..7523354 100644 --- a/ceo_common/interfaces/IUser.py +++ b/ceo_common/interfaces/IUser.py @@ -22,6 +22,7 @@ class IUser(Interface): 'a club rep') mail_local_addresses = Attribute('email aliases') is_club_rep = Attribute('whether this user is a club rep or not') + shadowExpire = Attribute('whether the user is marked as expired') # Non-LDAP attributes ldap3_entry = Attribute('cached ldap3.Entry instance for this user') diff --git a/ceo_common/model/Term.py b/ceo_common/model/Term.py index 50da051..d6a5d5e 100644 --- a/ceo_common/model/Term.py +++ b/ceo_common/model/Term.py @@ -29,7 +29,7 @@ class Term: return Term(s_term) def start_month(self): - return self.seasons.index(self.s_term[0]) + return self.seasons.index(self.s_term[0]) * 4 + 1 def __add__(self, other): assert type(other) is int diff --git a/ceod/model/User.py b/ceod/model/User.py index 53e9ea0..dbc2e75 100644 --- a/ceod/model/User.py +++ b/ceod/model/User.py @@ -33,7 +33,7 @@ class User: is_club_rep: Union[bool, None] = None, is_club: bool = False, ldap3_entry: Union[ldap3.Entry, None] = None, - expired: bool = False, + shadowExpire: bool = False, ): cfg = component.getUtility(IConfig) @@ -67,7 +67,7 @@ class User: else: self.is_club_rep = is_club_rep self.ldap3_entry = ldap3_entry - self.expired = expired + self.shadowExpire = shadowExpire self.ldap_srv = component.getUtility(ILDAPService) self.krb_srv = component.getUtility(IKerberosService) @@ -84,7 +84,7 @@ class User: 'is_club': self.is_club(), 'is_club_rep': self.is_club_rep, 'program': self.program or 'Unknown', - 'expired': self.expired, + 'shadowExpire': self.shadowExpire, } if self.sn and self.given_name: data['sn'] = self.sn @@ -158,7 +158,7 @@ class User: mail_local_addresses=attrs.get('mailLocalAddress'), is_club_rep=attrs.get('isClubRep', [False])[0], is_club=('club' in attrs['objectClass']), - expired=attrs.get('shadowExpire', 0) != 0, + shadowExpire=attrs.get('shadowExpire', 0) != 0, ldap3_entry=entry, ) @@ -216,4 +216,4 @@ class User: entry.shadowExpire = 1 else: entry.shadowExpire.remove() - self.expired = expired + self.shadowExpire = expired diff --git a/dev-requirements.txt b/dev-requirements.txt index 8392fb6..f5244cd 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -4,3 +4,4 @@ wheel==0.36.2 pytest==6.2.4 aiosmtpd==1.4.2 aiohttp==3.7.4.post0 +freezegun==1.1.0 diff --git a/tests/ceod/api/test_members.py b/tests/ceod/api/test_members.py index b936d0f..3e988b7 100644 --- a/tests/ceod/api/test_members.py +++ b/tests/ceod/api/test_members.py @@ -2,8 +2,10 @@ from unittest.mock import patch import ldap3 import pytest +from freezegun import freeze_time import ceod.utils as utils +from ceo_common.model import Term def test_api_user_not_found(client): @@ -11,7 +13,7 @@ def test_api_user_not_found(client): assert status == 404 -@pytest.fixture(scope='module') +@pytest.fixture(scope='function') def create_user_resp(client, mocks_for_create_user, mock_mail_server): mock_mail_server.messages.clear() status, data = client.post('/api/members', json={ @@ -31,7 +33,7 @@ def create_user_resp(client, mocks_for_create_user, mock_mail_server): assert data[-1]['status'] == 'completed' -@pytest.fixture(scope='module') +@pytest.fixture(scope='function') def create_user_result(create_user_resp): # convenience method _, data = create_user_resp @@ -231,3 +233,33 @@ def test_authz_check(client, create_user_result): 'sn': 'One', 'terms': ['s2021'], }, principal='ctdalek', delegate=False) assert data[-1]['status'] == 'aborted' + + +@pytest.mark.parametrize('test_date, should_expire', [ + ('2021-05-15', False), + ('2021-06-01', False), + ('2021-09-15', False), + ('1966-01-20', True), + ('2021-10-15', True), + ('2022-05-20', True), + ('2050-04-01', True)]) +@pytest.mark.parametrize('term_attr', ['terms', 'non_member_terms']) +def test_expire(client, create_user_result, term_attr, test_date, should_expire): + user = create_user_result + uid = user['uid'] + term = repr(Term.current()) + + with freeze_time(test_date): + assert not user['shadowExpire'] + + status, _ = client.post('/api/members/expire') + assert status == 200 + + _, user = client.get(f'/api/members/{uid}') + assert user['shadowExpire'] == should_expire + + status, _ = client.post(f'/api/members/{uid}/renew', json={term_attr: [term]}) + assert status == 200 + + _, user = client.get(f'/api/members/{user["uid"]}') + assert not user['shadowExpire']