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
This commit is contained in:
Rio Liu 2021-11-06 21:32:15 -04:00
parent 3ba0bcbc63
commit cb90841cdd
5 changed files with 42 additions and 8 deletions

View File

@ -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')

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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']